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 / CuratedAPIAjaxController.php

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

264 lines 7.8 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 use Averta\WordPress\Utility\JSON;
5 use Depicter\GuzzleHttp\Exception\GuzzleException;
6 use Depicter\Services\AssetsAPIService;
7 use Depicter\Utility\Http;
8 use Depicter\Utility\Sanitize;
9 use Psr\Http\Message\RequestInterface;
10 use Psr\Http\Message\ResponseInterface;
11
12
13 class CuratedAPIAjaxController
14 {
15
16 /**
17 * Search Elements
18 *
19 * @param RequestInterface $request
20 * @param string $view
21 *
22 * @return ResponseInterface
23 * @throws GuzzleException
24 */
25 public function searchElements( RequestInterface $request, $view )
26 {
27 $page = !empty( $request->query('page' ) ) ? Sanitize::int( $request->query('page') ) : 1;
28 $perpage = !empty( $request->query('perpage' ) ) ? Sanitize::int( $request->query('perpage') ) : 20;
29 $category = !empty( $request->query('category') ) ? Sanitize::textfield( $request->query('category') ) : '';
30 $search = !empty( $request->query('s' ) ) ? Sanitize::textfield( $request->query('s') ) : '';
31
32 $options = [
33 'page' => $page,
34 'perpage' => $perpage,
35 'category' => $category,
36 's' => $search
37 ];
38
39 try {
40 return \Depicter::json( AssetsAPIService::searchElements( $options ) );
41 } catch ( \Exception $exception ) {
42
43 $error = Http::getErrorExceptionResponse( $exception );
44
45 return \Depicter::json([
46 'errors' => $error
47 ])->withStatus( $error['statusCode'] );
48 }
49
50 }
51
52 /**
53 * search Document Templates
54 *
55 * @param RequestInterface $request
56 * @param string $view
57 *
58 * @return ResponseInterface
59 * @throws GuzzleException
60 */
61 public function searchDocumentTemplates( RequestInterface $request, $view )
62 {
63 $page = !empty( $request->query('page') ) ? Sanitize::int( $request->query('page') ) : 1;
64 $perpage = !empty( $request->query('perpage') ) ? Sanitize::int( $request->query('perpage') ) : 20;
65 $category = !empty( $request->query('category') ) ? Sanitize::textfield( $request->query('category') ) : '';
66 $search = !empty( $request->query('s') ) ? Sanitize::textfield( $request->query('s') ) : '';
67 $version = !empty( $request->query('v') ) ? Sanitize::textfield( $request->query('v') ) : '1';
68 $from = !empty( $request->query('from') ) ? Sanitize::textfield( $request->query('from') ) : 'website';
69
70 $options = [
71 'page' => $page,
72 'perpage' => $perpage,
73 'category' => $category,
74 's' => $search,
75 'v' => $version,
76 'from' => $from
77 ];
78
79 try {
80 return \Depicter::json( AssetsAPIService::searchDocumentTemplates( $options ) );
81
82 } catch ( \Exception $exception ) {
83 $error = Http::getErrorExceptionResponse( $exception );
84
85 return \Depicter::json([
86 'errors' => $error
87 ])->withStatus( $error['statusCode'] );
88 }
89
90 }
91
92 public function getDocumentTemplateCategories( RequestInterface $request, $view )
93 {
94 $category = !empty( $request->query('category') ) ? Sanitize::textfield( $request->query('category') ) : '';
95
96 $options = [ 'category' => $category ];
97
98 try {
99 return \Depicter::json( AssetsAPIService::getDocumentTemplateCategories( $options ) );
100 } catch ( \Exception $exception ) {
101 $error = Http::getErrorExceptionResponse( $exception );
102
103 return \Depicter::json([
104 'errors' => $error
105 ])->withStatus( $error['statusCode'] );
106 }
107 }
108
109 /**
110 * Imports a template
111 *
112 * @param RequestInterface $request
113 * @param $view
114 *
115 * @return ResponseInterface
116 * @throws GuzzleException
117 */
118 public function importDocumentTemplate( RequestInterface $request, $view ) {
119 $templateID = !empty( $request->query('ID') ) ? Sanitize::textfield( $request->query('ID') ) : '';
120 $endpointVersion = !empty( $request->query('v') ) ? Sanitize::int( $request->query('v') ) : 1;
121
122 try {
123 $result = AssetsAPIService::getDocumentTemplateData( $templateID, [ 'v' => $endpointVersion ] );
124
125 if ( !empty( $result->errors ) ) {
126 return Http::getErrorJson( $result->errors );
127
128 } elseif ( !empty( $result->hits ) ) {
129 $editorData = JSON::encode( $result->hits );
130 $editorData = preg_replace( '/"activeBreakpoint":".+?"/', '"activeBreakpoint":"default"', $editorData );
131 $document = \Depicter::documentRepository()->create();
132
133 $updateData = ['content' => $editorData ];
134 if ( !empty( $result->title ) ) {
135 $updateData['name'] = $result->title . ' ' . $document->getID();
136 }
137 if ( !empty( $result->image ) ) {
138 $previewImage = file_get_contents( $result->image );
139 \Depicter::storage()->filesystem()->write( \Depicter::documentRepository()->getPreviewImagePath( $document->getID() ) , $previewImage );
140 }
141
142 \Depicter::documentRepository()->update( $document->getID(), $updateData );
143 \Depicter::media()->importDocumentAssets( $editorData );
144
145 return \Depicter::json([
146 'hits' => [
147 'documentID' => $document->getID()
148 ]
149 ]);
150
151 } else {
152 // Return the error message received from server
153 return \Depicter::json( $result );
154 }
155
156 } catch ( \Exception $exception ) {
157
158 $error = Http::getErrorExceptionResponse( $exception );
159
160 return \Depicter::json([
161 'errors' => $error
162 ])->withStatus( $error['statusCode'] );
163 }
164 }
165
166 /**
167 * @param RequestInterface $request
168 * @param $view
169 *
170 * @return ResponseInterface
171 * @throws GuzzleException
172 */
173 public function previewDocumentTemplate( RequestInterface $request, $view ) {
174 $templateID = !empty( $request->query('ID') ) ? Sanitize::textfield( $request->query('ID') ) : '';
175 try {
176 $result = AssetsAPIService::previewDocumentTemplate( $templateID );
177 return \Depicter::output( $result );
178 } catch ( \Exception $exception ) {
179 $error = Http::getErrorExceptionResponse( $exception );
180
181 return \Depicter::json([
182 'errors' => $error
183 ])->withStatus( $error['statusCode'] );
184 }
185 }
186
187 /**
188 * Search in Animations
189 *
190 * @param RequestInterface $request
191 * @param string $view
192 *
193 * @return ResponseInterface
194 * @throws GuzzleException
195 */
196 public function searchAnimations( RequestInterface $request, $view ){
197
198 $page = !empty( $request->query('page') ) ? Sanitize::int( $request->query('page') ) : 1;
199 $perpage = !empty( $request->query('perpage') ) ? Sanitize::int( $request->query('perpage') ) : 20;
200 $phase = !empty( $request->query('phase') ) ? Sanitize::textfield( $request->query('phase') ) : '';
201 $search = !empty( $request->query('s' ) ) ? Sanitize::textfield( $request->query('s') ) : '';
202 $category = !empty( $request->query('category' ) ) ? Sanitize::textfield( $request->query('category') ) : '';
203
204 $options = [
205 'page' => $page,
206 'perpage' => $perpage,
207 'phase' => $phase,
208 's' => $search,
209 'category' => $category
210 ];
211
212
213 try {
214 return \Depicter::json( AssetsAPIService::searchAnimations( $options ) );
215
216 } catch ( \Exception $exception ) {
217 $error = Http::getErrorExceptionResponse( $exception );
218
219 return \Depicter::json([
220 'errors' => $error
221 ])->withStatus( $error['statusCode'] );
222 }
223
224 }
225
226 /**
227 * Get list of animation categories
228 *
229 * @param RequestInterface $request
230 * @param $view
231 *
232 * @return ResponseInterface
233 * @throws GuzzleException
234 */
235 public function getAnimationsCategories( RequestInterface $request, $view )
236 {
237
238 $phase = !empty( $request->query('phase') ) ? Sanitize::textfield( $request->query('phase') ) : '';
239 $options = [ 'phase' => $phase ];
240
241 if( !empty( $request->query('elementType') ) ){
242 $options['elementType'] = Sanitize::textfield( $request->query('elementType') );
243 }
244
245 try {
246 $result = AssetsAPIService::getAnimationsCategories( $options );
247
248 if ( !empty( $result['errors'] ) ) {
249 return Http::getErrorJson( $result['errors'] );
250 } else {
251 return \Depicter::json( $result );
252 }
253
254 } catch ( \Exception $exception ) {
255 $error = Http::getErrorExceptionResponse( $exception );
256
257 return \Depicter::json([
258 'errors' => $error
259 ])->withStatus( $error['statusCode'] );
260 }
261 }
262
263 }
264