4
4
namespace App \Controller ;
5
5
6
6
use App \Controller \Model \TagsController as ModelTagsController ;
7
+ use BEdita \SDK \BEditaClientException ;
7
8
use Cake \Event \EventInterface ;
8
9
use Cake \Http \Response ;
10
+ use Cake \Utility \Hash ;
9
11
10
12
/**
11
13
* Tags Controller
@@ -21,6 +23,7 @@ public function initialize(): void
21
23
{
22
24
parent ::initialize ();
23
25
$ this ->loadComponent ('ProjectConfiguration ' );
26
+ $ this ->Security ->setConfig ('unlockedActions ' , ['create ' , 'patch ' , 'delete ' ]);
24
27
}
25
28
26
29
/**
@@ -37,6 +40,110 @@ public function index(): ?Response
37
40
return null ;
38
41
}
39
42
43
+ /**
44
+ * Create new tag (ajax).
45
+ *
46
+ * @return \Cake\Http\Response|null
47
+ */
48
+ public function create (): ?Response
49
+ {
50
+ $ this ->getRequest ()->allowMethod (['post ' ]);
51
+ $ this ->viewBuilder ()->setClassName ('Json ' );
52
+ $ response = $ error = null ;
53
+ try {
54
+ $ body = (array )$ this ->getRequest ()->getData ();
55
+ $ response = $ this ->apiClient ->post ('/model/tags ' , json_encode ($ body ));
56
+ } catch (BEditaClientException $ e ) {
57
+ $ error = $ e ->getMessage ();
58
+ $ this ->log ($ error , 'error ' );
59
+ $ this ->set ('error ' , $ error );
60
+ }
61
+ $ this ->set ('response ' , $ response );
62
+ $ this ->set ('error ' , $ error );
63
+ $ this ->setSerialize (['response ' , 'error ' ]);
64
+
65
+ return null ;
66
+ }
67
+
68
+ /**
69
+ * Delete single tag (ajax).
70
+ *
71
+ * @param string $id Tag ID.
72
+ * @return \Cake\Http\Response|null
73
+ */
74
+ public function delete (string $ id ): ?Response
75
+ {
76
+ $ this ->getRequest ()->allowMethod (['post ' ]);
77
+ $ this ->viewBuilder ()->setClassName ('Json ' );
78
+ $ response = $ error = null ;
79
+ try {
80
+ $ this ->apiClient ->delete (sprintf ('/model/tags/%s ' , $ id ));
81
+ $ response = 'ok ' ;
82
+ } catch (BEditaClientException $ e ) {
83
+ $ error = $ e ->getMessage ();
84
+ $ this ->log ($ error , 'error ' );
85
+ $ this ->set ('error ' , $ error );
86
+ }
87
+ $ this ->set ('response ' , $ response );
88
+ $ this ->set ('error ' , $ error );
89
+ $ this ->setSerialize (['response ' , 'error ' ]);
90
+
91
+ return null ;
92
+ }
93
+
94
+ /**
95
+ * Save tag (ajax).
96
+ *
97
+ * @param string $id Tag ID.
98
+ * @return \Cake\Http\Response|null
99
+ */
100
+ public function patch (string $ id ): ?Response
101
+ {
102
+ $ this ->getRequest ()->allowMethod (['patch ' ]);
103
+ $ this ->viewBuilder ()->setClassName ('Json ' );
104
+ $ response = $ error = null ;
105
+ try {
106
+ $ body = (array )$ this ->getRequest ()->getData ();
107
+ $ this ->apiClient ->patch (sprintf ('/model/tags/%s ' , $ id ), json_encode ($ body ));
108
+ $ response = 'ok ' ;
109
+ } catch (BEditaClientException $ e ) {
110
+ $ error = $ e ->getMessage ();
111
+ $ this ->log ($ error , 'error ' );
112
+ $ this ->set ('error ' , $ error );
113
+ }
114
+ $ this ->set ('response ' , $ response );
115
+ $ this ->set ('error ' , $ error );
116
+ $ this ->setSerialize (['response ' , 'error ' ]);
117
+
118
+ return null ;
119
+ }
120
+
121
+ /**
122
+ * Search tags (ajax)
123
+ *
124
+ * @return \Cake\Http\Response|null
125
+ */
126
+ public function search (): ?Response
127
+ {
128
+ $ this ->getRequest ()->allowMethod (['get ' ]);
129
+ $ this ->viewBuilder ()->setClassName ('Json ' );
130
+ $ data = $ error = null ;
131
+ try {
132
+ $ query = $ this ->getRequest ()->getQueryParams ();
133
+ $ response = $ this ->apiClient ->get ('/model/tags ' , $ query );
134
+ $ data = (array )Hash::get ($ response , 'data ' );
135
+ } catch (BEditaClientException $ e ) {
136
+ $ error = $ e ->getMessage ();
137
+ $ this ->log ($ error , 'error ' );
138
+ $ this ->set ('error ' , $ error );
139
+ }
140
+ $ this ->set ('data ' , $ data );
141
+ $ this ->set ('error ' , $ error );
142
+ $ this ->setSerialize (['data ' , 'error ' ]);
143
+
144
+ return null ;
145
+ }
146
+
40
147
/**
41
148
* @inheritDoc
42
149
*/
0 commit comments