PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Controllers / Ajax / DashboardAjaxController.php

DashboardAjaxController.php in Depicter — Popup & Slider Builder 1.2.0, at app/src/Controllers/Ajax/DashboardAjaxController.php

252 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Controllers\Ajax;
3
4
5 use Averta\Core\Utility\Arr;
6 use Depicter\Utility\Sanitize;
7 use Psr\Http\Message\ResponseInterface;
8 use WPEmerge\Requests\RequestInterface;
9
10 class DashboardAjaxController {
11
12 /**
13 * Retrieves Lists of all entries. (GET)
14 *
15 * @param RequestInterface $request
16 * @param string $view
17 *
18 * @return ResponseInterface
19 * @throws \Exception
20 */
21 public function index(RequestInterface $request, $view)
22 {
23 return \Depicter::json([
24 'hasMore' => false,
25 'hits' => Arr::camelizeKeys( \Depicter::app()->documentRepository()->getList(), '_', [], true )
26 ])->withStatus(200);
27 }
28
29 /**
30 * Adds a new entry. (POST)
31 *
32 * @param RequestInterface $request
33 * @param string $view
34 *
35 * @return ResponseInterface
36 * @throws \Exception
37 */
38 public function create( RequestInterface $request, $view )
39 {
40 $document = \Depicter::app()->documentRepository()->create();
41
42 return \Depicter::json([
43 'hits' => $document
44 ])->withHeader('X-Message', 'New document created successfully' )
45 ->withHeader('X-Document-ID', $document->getID() )
46 ->withStatus(200);
47 }
48
49 /**
50 * Removes an entry. (DELETE)
51 *
52 * @param RequestInterface $request
53 * @param string $view
54 *
55 * @return ResponseInterface
56 * @throws \Exception
57 */
58 public function destroy(RequestInterface $request, $view)
59 {
60 // Get document ID
61 $id = Sanitize::textfield( $request->body('ID') );
62
63 if( empty( $id ) ){
64 return \Depicter::json([
65 'errors' => ['Document ID is required.']
66 ])->withStatus(400);
67 }
68
69 $ids = explode(',', $id);
70 $result = [];
71
72 foreach ( $ids as $_id ) {
73 $_id = Sanitize::int( $_id );
74 $isDeleted = \Depicter::app()->documentRepository()->delete( $_id );
75
76 if ( $isDeleted ) {
77 $result['hits'][] = $_id;
78 // \Depicter::action()->do( 'depicter/dashboard/after/delete', $id, $properties, $result );
79 do_action( 'depicter/dashboard/after/delete', $_id );
80 } else {
81 $result['errors'][] = sprintf( "Document '%s' does not exist.", $_id );
82 }
83 }
84
85 return \Depicter::json($result)->withStatus(200);
86 }
87
88 /**
89 * Duplicates an entry.
90 *
91 * @param RequestInterface $request
92 * @param string $view
93 *
94 * @return ResponseInterface
95 * @throws \Exception
96 */
97 public function duplicate(RequestInterface $request, $view)
98 {
99 // Get document ID
100 $id = Sanitize::int( $request->body('ID') );
101
102 if( empty( $id ) ){
103 return \Depicter::json([
104 'errors' => ['Document ID is required.']
105 ])->withStatus(400);
106 }
107
108 $result = \Depicter::app()->documentRepository()->duplicate( $id, true );
109
110 if( ! $result ){
111 return \Depicter::json([
112 'errors' => ['Document does not exist.']
113 ])->withHeader('X-Document-ID', $id)->withStatus(404);
114 }
115
116 return \Depicter::json([
117 'hits' => Arr::camelizeKeys( $result->getProperties(), '_', [], false )
118 ])->withStatus(200);
119 }
120
121 /**
122 * Changes a document name.
123 *
124 * @param RequestInterface $request
125 * @param string $view
126 *
127 * @return ResponseInterface
128 */
129 public function changeName(RequestInterface $request, $view)
130 {
131 // Get document ID
132 $id = Sanitize::int( $request->body('ID') );
133
134 if( empty( $id ) ){
135 return \Depicter::json([
136 'errors' => ['Document ID is required.']
137 ])->withStatus(400);
138 }
139
140 // Get new name
141 $newName = Sanitize::textfield( $request->body('name') );
142
143 if( empty( $newName ) ){
144 return \Depicter::json([
145 'errors' => ['New document name is required.']
146 ])->withStatus(400);
147 }
148
149 $result = \Depicter::app()->documentRepository()->rename( $id, $newName );
150
151 if( ! $result ){
152 return \Depicter::json([
153 'errors' => ['Document does not exist.']
154 ])->withHeader('X-Document-ID', $id)->withStatus(404);
155 }
156
157 do_action( 'depicter/dashboard/after/rename', $id, $newName );
158
159 return \Depicter::json([
160 'hits' => $result
161 ])->withStatus(200);
162 }
163
164 /**
165 * Changes a document slug.
166 *
167 * @param RequestInterface $request
168 * @param string $view
169 *
170 * @return ResponseInterface
171 */
172 public function changeSlug(RequestInterface $request, $view)
173 {
174 // Get document ID
175 $id = Sanitize::int( $request->body('ID') );
176
177 if( empty( $id ) ){
178 return \Depicter::json([
179 'errors' => ['Document ID is required.']
180 ])->withStatus(400);
181 }
182
183 // Get new name
184 $newSlug = Sanitize::textfield( $request->body('slug') );
185
186 if( empty( $newSlug ) ){
187 return \Depicter::json([
188 'errors' => ['New document slug is required.']
189 ])->withStatus(400);
190 }
191 try{
192 $result = \Depicter::app()->documentRepository()->changeSlug( $id, $newSlug );
193
194 if( ! $result ){
195 return \Depicter::json([
196 'errors' => ['Document does not exist.']
197 ])->withHeader('X-Document-ID', $id)->withStatus(404);
198 }
199
200 return \Depicter::json([
201 'hits' => $result
202 ])->withStatus(200);
203
204 } catch ( \Exception $exception ) {
205 return \Depicter::json([
206 'errors' => [ $exception->getMessage()]
207 ]);
208 }
209 }
210
211 /**
212 * Imports a document data.
213 *
214 * @param RequestInterface $request
215 * @param string $view
216 *
217 * @return ResponseInterface
218 */
219 public function import(RequestInterface $request, $view)
220 {
221 return \Depicter::json([
222 'errors' => ['Under construction!']
223 ])->withStatus(404);
224 }
225
226 /**
227 * Exports a document to file.
228 *
229 * @param RequestInterface $request
230 * @param string $view
231 *
232 * @return ResponseInterface
233 */
234 public function export(RequestInterface $request, $view)
235 {
236 // Get document ID
237 $ids = Sanitize::textfield( $request->body('ID') );
238
239 if( is_null( $ids ) ){
240 return \Depicter::json([
241 'errors' => ['Document ID is required.']
242 ])->withStatus(400);
243 }
244
245 return \Depicter::json([
246 'errors' => ['Under construction!']
247 ])->withStatus(404);
248 }
249
250
251 }
252