PluginProbe
Depicter — Popup & Slider Builder / 1.6.2
Depicter — Popup & Slider Builder v1.6.2
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.6.2, at app/src/Controllers/Ajax/DashboardAjaxController.php

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