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,109 @@ 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
+ $ response = $ this ->apiClient ->delete (sprintf ('/model/tags/%s ' , $ id ));
81
+ } catch (BEditaClientException $ e ) {
82
+ $ error = $ e ->getMessage ();
83
+ $ this ->log ($ error , 'error ' );
84
+ $ this ->set ('error ' , $ error );
85
+ }
86
+ $ this ->set ('response ' , $ response );
87
+ $ this ->set ('error ' , $ error );
88
+ $ this ->setSerialize (['response ' , 'error ' ]);
89
+
90
+ return null ;
91
+ }
92
+
93
+ /**
94
+ * Save tag (ajax).
95
+ *
96
+ * @param string $id Tag ID.
97
+ * @return \Cake\Http\Response|null
98
+ */
99
+ public function patch (string $ id ): ?Response
100
+ {
101
+ $ this ->getRequest ()->allowMethod (['patch ' ]);
102
+ $ this ->viewBuilder ()->setClassName ('Json ' );
103
+ $ response = $ error = null ;
104
+ try {
105
+ $ body = (array )$ this ->getRequest ()->getData ();
106
+ $ this ->apiClient ->patch (sprintf ('/model/tags/%s ' , $ id ), json_encode ($ body ));
107
+ $ response = 'ok ' ;
108
+ } catch (BEditaClientException $ e ) {
109
+ $ error = $ e ->getMessage ();
110
+ $ this ->log ($ error , 'error ' );
111
+ $ this ->set ('error ' , $ error );
112
+ }
113
+ $ this ->set ('response ' , $ response );
114
+ $ this ->set ('error ' , $ error );
115
+ $ this ->setSerialize (['response ' , 'error ' ]);
116
+
117
+ return null ;
118
+ }
119
+
120
+ /**
121
+ * Search tags (ajax)
122
+ *
123
+ * @return \Cake\Http\Response|null
124
+ */
125
+ public function search (): ?Response
126
+ {
127
+ $ this ->getRequest ()->allowMethod (['get ' ]);
128
+ $ this ->viewBuilder ()->setClassName ('Json ' );
129
+ $ data = $ error = null ;
130
+ try {
131
+ $ query = $ this ->getRequest ()->getQueryParams ();
132
+ $ response = $ this ->apiClient ->get ('/model/tags ' , $ query );
133
+ $ data = (array )Hash::get ($ response , 'data ' );
134
+ } catch (BEditaClientException $ e ) {
135
+ $ error = $ e ->getMessage ();
136
+ $ this ->log ($ error , 'error ' );
137
+ $ this ->set ('error ' , $ error );
138
+ }
139
+ $ this ->set ('data ' , $ data );
140
+ $ this ->set ('error ' , $ error );
141
+ $ this ->setSerialize (['data ' , 'error ' ]);
142
+
143
+ return null ;
144
+ }
145
+
40
146
/**
41
147
* @inheritDoc
42
148
*/
0 commit comments