| 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 |
|
| 68 |
$options = [ |
| 69 |
'page' => $page, |
| 70 |
'perpage' => $perpage, |
| 71 |
'category' => $category, |
| 72 |
's' => $search |
| 73 |
]; |
| 74 |
|
| 75 |
try { |
| 76 |
return \Depicter::json( AssetsAPIService::searchDocumentTemplates( $options ) ); |
| 77 |
|
| 78 |
} catch ( \Exception $exception ) { |
| 79 |
$error = Http::getErrorExceptionResponse( $exception ); |
| 80 |
|
| 81 |
return \Depicter::json([ |
| 82 |
'errors' => $error |
| 83 |
])->withStatus( $error['statusCode'] ); |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
public function getDocumentTemplateCategories( RequestInterface $request, $view ) |
| 89 |
{ |
| 90 |
$category = !empty( $request->query('category') ) ? Sanitize::textfield( $request->query('category') ) : ''; |
| 91 |
|
| 92 |
$options = [ 'category' => $category ]; |
| 93 |
|
| 94 |
try { |
| 95 |
return \Depicter::json( AssetsAPIService::getDocumentTemplateCategories( $options ) ); |
| 96 |
} catch ( \Exception $exception ) { |
| 97 |
$error = Http::getErrorExceptionResponse( $exception ); |
| 98 |
|
| 99 |
return \Depicter::json([ |
| 100 |
'errors' => $error |
| 101 |
])->withStatus( $error['statusCode'] ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Imports a template |
| 107 |
* |
| 108 |
* @param RequestInterface $request |
| 109 |
* @param $view |
| 110 |
* |
| 111 |
* @return ResponseInterface |
| 112 |
* @throws GuzzleException |
| 113 |
*/ |
| 114 |
public function importDocumentTemplate( RequestInterface $request, $view ) { |
| 115 |
$templateID = !empty( $request->query('ID') ) ? Sanitize::textfield( $request->query('ID') ) : ''; |
| 116 |
try { |
| 117 |
$result = AssetsAPIService::getDocumentTemplateData( $templateID ); |
| 118 |
if ( !empty( $result->hits ) ) { |
| 119 |
$editorData = JSON::encode( $result->hits ); |
| 120 |
$document = \Depicter::documentRepository()->create(); |
| 121 |
|
| 122 |
$updateData = ['content' => $editorData ]; |
| 123 |
if ( !empty( $result->title ) ) { |
| 124 |
$updateData['name'] = $result->title . ' ' . $document->getID(); |
| 125 |
} |
| 126 |
if ( !empty( $result->image ) ) { |
| 127 |
$previewImage = file_get_contents( $result->image ); |
| 128 |
\Depicter::storage()->filesystem()->write( \Depicter::documentRepository()->getPreviewImagePath( $document->getID() ) , $previewImage ); |
| 129 |
} |
| 130 |
|
| 131 |
\Depicter::documentRepository()->update( $document->getID(), $updateData ); |
| 132 |
\Depicter::media()->importDocumentAssets( $editorData ); |
| 133 |
|
| 134 |
return \Depicter::json([ |
| 135 |
'hits' => [ |
| 136 |
'documentID' => $document->getID() |
| 137 |
] |
| 138 |
]); |
| 139 |
|
| 140 |
} else { |
| 141 |
// Return the error message received from server |
| 142 |
return \Depicter::json( $result ); |
| 143 |
} |
| 144 |
|
| 145 |
} catch ( \Exception $exception ) { |
| 146 |
|
| 147 |
$error = Http::getErrorExceptionResponse( $exception ); |
| 148 |
|
| 149 |
if( $error['group'] == 'RequestException' ){ |
| 150 |
$error['message'] = __( 'Cannot reach the server for downloading images.', 'depicter' ); |
| 151 |
$error['statusCode'] = 421; |
| 152 |
} |
| 153 |
|
| 154 |
return \Depicter::json([ |
| 155 |
'errors' => $error |
| 156 |
])->withStatus( $error['statusCode'] ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @param RequestInterface $request |
| 162 |
* @param $view |
| 163 |
* |
| 164 |
* @return ResponseInterface |
| 165 |
* @throws GuzzleException |
| 166 |
*/ |
| 167 |
public function previewDocumentTemplate( RequestInterface $request, $view ) { |
| 168 |
$templateID = !empty( $request->query('ID') ) ? Sanitize::textfield( $request->query('ID') ) : ''; |
| 169 |
try { |
| 170 |
$result = AssetsAPIService::previewDocumentTemplate( $templateID ); |
| 171 |
return \Depicter::output( $result ); |
| 172 |
} catch ( \Exception $exception ) { |
| 173 |
$error = Http::getErrorExceptionResponse( $exception ); |
| 174 |
|
| 175 |
return \Depicter::json([ |
| 176 |
'errors' => $error |
| 177 |
])->withStatus( $error['statusCode'] ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Search in Animations |
| 183 |
* |
| 184 |
* @param RequestInterface $request |
| 185 |
* @param string $view |
| 186 |
* |
| 187 |
* @return ResponseInterface |
| 188 |
* @throws GuzzleException |
| 189 |
*/ |
| 190 |
public function searchAnimations( RequestInterface $request, $view ){ |
| 191 |
|
| 192 |
$page = !empty( $request->query('page') ) ? Sanitize::int( $request->query('page') ) : 1; |
| 193 |
$perpage = !empty( $request->query('perpage') ) ? Sanitize::int( $request->query('perpage') ) : 20; |
| 194 |
$phase = !empty( $request->query('phase') ) ? Sanitize::textfield( $request->query('phase') ) : ''; |
| 195 |
$search = !empty( $request->query('s' ) ) ? Sanitize::textfield( $request->query('s') ) : ''; |
| 196 |
$category = !empty( $request->query('category' ) ) ? Sanitize::textfield( $request->query('category') ) : ''; |
| 197 |
|
| 198 |
$options = [ |
| 199 |
'page' => $page, |
| 200 |
'perpage' => $perpage, |
| 201 |
'phase' => $phase, |
| 202 |
's' => $search, |
| 203 |
'category' => $category |
| 204 |
]; |
| 205 |
|
| 206 |
|
| 207 |
try { |
| 208 |
return \Depicter::json( AssetsAPIService::searchAnimations( $options ) ); |
| 209 |
|
| 210 |
} catch ( \Exception $exception ) { |
| 211 |
$error = Http::getErrorExceptionResponse( $exception ); |
| 212 |
|
| 213 |
return \Depicter::json([ |
| 214 |
'errors' => $error |
| 215 |
])->withStatus( $error['statusCode'] ); |
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Get list of animation categories |
| 222 |
* |
| 223 |
* @param RequestInterface $request |
| 224 |
* @param $view |
| 225 |
* |
| 226 |
* @return ResponseInterface |
| 227 |
* @throws GuzzleException |
| 228 |
*/ |
| 229 |
public function getAnimationsCategories( RequestInterface $request, $view ) |
| 230 |
{ |
| 231 |
|
| 232 |
$phase = !empty( $request->query('phase') ) ? Sanitize::textfield( $request->query('phase') ) : ''; |
| 233 |
$options = [ 'phase' => $phase ]; |
| 234 |
|
| 235 |
if( !empty( $request->query('elementType') ) ){ |
| 236 |
$options['elementType'] = Sanitize::textfield( $request->query('elementType') ); |
| 237 |
} |
| 238 |
|
| 239 |
try { |
| 240 |
return \Depicter::json( AssetsAPIService::getAnimationsCategories( $options ) ); |
| 241 |
|
| 242 |
} catch ( \Exception $exception ) { |
| 243 |
$error = Http::getErrorExceptionResponse( $exception ); |
| 244 |
|
| 245 |
return \Depicter::json([ |
| 246 |
'errors' => $error |
| 247 |
])->withStatus( $error['statusCode'] ); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
|