| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Controllers\Ajax; |
| 4 |
|
| 5 |
use Averta\Core\Utility\Data; |
| 6 |
use Averta\WordPress\Utility\JSON; |
| 7 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 8 |
use Depicter\Services\AssetsAPIService; |
| 9 |
use Depicter\Utility\Http; |
| 10 |
use Depicter\Utility\Sanitize; |
| 11 |
use Psr\Http\Message\ResponseInterface; |
| 12 |
use WPEmerge\Requests\RequestInterface; |
| 13 |
|
| 14 |
class AIWizardController { |
| 15 |
|
| 16 |
/** |
| 17 |
* Generate keywords by AI |
| 18 |
* |
| 19 |
* @param RequestInterface $request |
| 20 |
* @param string $view |
| 21 |
* |
| 22 |
* @return ResponseInterface |
| 23 |
* @throws GuzzleException |
| 24 |
*/ |
| 25 |
public function generateKeywords( RequestInterface $request, $view ) |
| 26 |
{ |
| 27 |
$args = []; |
| 28 |
|
| 29 |
if( ! Data::isNullOrEmptyStr( $request->body('description') ) ){ |
| 30 |
$args['description'] = Sanitize::textarea( $request->body('description') ); |
| 31 |
} |
| 32 |
if( ! Data::isNullOrEmptyStr( $request->body('number') ) ){ |
| 33 |
$args['number'] = Sanitize::int( $request->body('number') ); |
| 34 |
} |
| 35 |
if( ! Data::isNullOrEmptyStr( $request->body('model') ) ){ |
| 36 |
$args['model'] = Sanitize::textfield( $request->body('model') ); |
| 37 |
} |
| 38 |
if( ! Data::isNullOrEmptyStr( $request->body('debug') ) ){ |
| 39 |
$args['debug'] = Sanitize::textfield( $request->body('debug') ); |
| 40 |
} |
| 41 |
|
| 42 |
try { |
| 43 |
$response = \Depicter::remote()->post( 'v1/ai/text/wizard/keywords', [ |
| 44 |
'form_params' => $args |
| 45 |
]); |
| 46 |
$result = JSON::decode( $response->getBody(), true ); |
| 47 |
return \Depicter::json( $result )->withStatus(200); |
| 48 |
|
| 49 |
} catch ( \Exception $exception ) { |
| 50 |
$error = Http::getErrorExceptionResponse( $exception ); |
| 51 |
|
| 52 |
return \Depicter::json([ |
| 53 |
'errors' => $error |
| 54 |
])->withStatus( $error['statusCode'] ); |
| 55 |
} |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Generate content for slides |
| 61 |
* |
| 62 |
* @param RequestInterface $request |
| 63 |
* @param $view |
| 64 |
* |
| 65 |
* @return ResponseInterface |
| 66 |
* @throws GuzzleException |
| 67 |
*/ |
| 68 |
public function wizardComplete( RequestInterface $request, $view ) { |
| 69 |
|
| 70 |
$args = []; |
| 71 |
|
| 72 |
if( ! Data::isNullOrEmptyStr( $request->body('category') ) ){ |
| 73 |
$args['category'] = Sanitize::textfield( $request->body('category') ); |
| 74 |
} |
| 75 |
if( ! Data::isNullOrEmptyStr( $request->body('description') ) ){ |
| 76 |
$args['description'] = Sanitize::textarea( $request->body('description') ); |
| 77 |
} |
| 78 |
if( ! Data::isNullOrEmptyStr( $request->body('keywords') ) ){ |
| 79 |
$args['keywords'] = Sanitize::textfield( $request->body('keywords') ); |
| 80 |
} |
| 81 |
if( ! Data::isNullOrEmptyStr( $request->body('numberOfSections') ) ){ |
| 82 |
$args['numberOfSections'] = Sanitize::int( $request->body('numberOfSections') ); |
| 83 |
} |
| 84 |
if( ! Data::isNullOrEmptyStr( $request->body('headingCharLength') ) ){ |
| 85 |
$args['headingCharLength'] = Sanitize::int( $request->body('headingCharLength') ); |
| 86 |
} |
| 87 |
if( ! Data::isNullOrEmptyStr( $request->body('subheadingCharLength') ) ){ |
| 88 |
$args['subheadingCharLength'] = Sanitize::int( $request->body('subheadingCharLength') ); |
| 89 |
} |
| 90 |
if( ! Data::isNullOrEmptyStr( $request->body('descriptionCharLength') ) ){ |
| 91 |
$args['descriptionCharLength'] = Sanitize::int( $request->body('descriptionCharLength') ); |
| 92 |
} |
| 93 |
if( ! Data::isNullOrEmptyStr( $request->body('ctaCharLength') ) ){ |
| 94 |
$args['ctaCharLength'] = Sanitize::int( $request->body('ctaCharLength') ); |
| 95 |
} |
| 96 |
if( ! Data::isNullOrEmptyStr( $request->body('temperature') ) ){ |
| 97 |
$args['temperature'] = Sanitize::textfield( $request->body('temperature') ); |
| 98 |
} |
| 99 |
if( ! Data::isNullOrEmptyStr( $request->body('debug') ) ){ |
| 100 |
$args['debug'] = Sanitize::textfield( $request->body('debug') ); |
| 101 |
} |
| 102 |
|
| 103 |
try { |
| 104 |
$response = \Depicter::remote()->post( 'v1/ai/text/wizard/complete', [ |
| 105 |
'form_params' => $args |
| 106 |
]); |
| 107 |
$result = JSON::decode( $response->getBody(), true ); |
| 108 |
return \Depicter::json( $result )->withStatus(200); |
| 109 |
|
| 110 |
} catch ( \Exception $exception ) { |
| 111 |
$error = Http::getErrorExceptionResponse( $exception ); |
| 112 |
|
| 113 |
return \Depicter::json([ |
| 114 |
'errors' => $error |
| 115 |
])->withStatus( $error['statusCode'] ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @param RequestInterface $request |
| 121 |
* @param $view |
| 122 |
* |
| 123 |
* @return ResponseInterface|void |
| 124 |
* @throws GuzzleException |
| 125 |
*/ |
| 126 |
public function importAIDocumentTemplate( RequestInterface $request, $view ) { |
| 127 |
|
| 128 |
$data = $request->body('data'); |
| 129 |
$templateId = $request->body('ID', ''); |
| 130 |
|
| 131 |
if ( empty( $templateId ) ) { |
| 132 |
return \Depicter::json([ |
| 133 |
'errors' => ['Template ID is required.'] |
| 134 |
])->withStatus(400); |
| 135 |
} |
| 136 |
|
| 137 |
if ( !JSON::isJson( $data ) ) { |
| 138 |
return \Depicter::json([ |
| 139 |
'errors' => ['Invalid JSON format.'] |
| 140 |
])->withStatus(400); |
| 141 |
} |
| 142 |
|
| 143 |
$data = JSON::decode( $data, true ); |
| 144 |
|
| 145 |
if ( empty( $data['colorPalette'] ) || count( $data['colorPalette'] ) < 5 ) { |
| 146 |
return \Depicter::json([ |
| 147 |
'errors' => ['colorPalette should have 5 colors'] |
| 148 |
])->withStatus(400); |
| 149 |
} |
| 150 |
|
| 151 |
$result = AssetsAPIService::getDocumentTemplateData( $templateId, [ 'directory' => 4 ] ); |
| 152 |
|
| 153 |
if ( !empty( $result->errors ) ) { |
| 154 |
return Http::getErrorJson( $result->errors ); |
| 155 |
|
| 156 |
} elseif ( !empty( $result->hits ) ) { |
| 157 |
$editorData = JSON::encode( $result->hits ); |
| 158 |
$editorData = JSON::decode( $editorData, true ); |
| 159 |
$editorData = JSON::encode( \Depicter::AIWizard()->updateEditorDataByAiContent( $editorData, $data ) ); |
| 160 |
|
| 161 |
$editorData = preg_replace( '/"activeBreakpoint":".+?"/', '"activeBreakpoint":"default"', $editorData ); |
| 162 |
$document = \Depicter::documentRepository()->create(); |
| 163 |
|
| 164 |
$updateData = []; |
| 165 |
if ( !empty( $result->title ) ) { |
| 166 |
$updateData['name'] = __("AI-Aided Slider") . ' ' . $document->getID(); |
| 167 |
} |
| 168 |
|
| 169 |
if ( !empty( $result->image ) ) { |
| 170 |
$previewImage = file_get_contents( $result->image ); |
| 171 |
\Depicter::storage()->filesystem()->write( \Depicter::documentRepository()->getPreviewImagePath( $document->getID() ) , $previewImage ); |
| 172 |
} |
| 173 |
|
| 174 |
\Depicter::media()->importDocumentAssets( $editorData ); |
| 175 |
$updateData['content'] = \Depicter::AIWizard()->fixMediaSizes( $editorData ); |
| 176 |
\Depicter::documentRepository()->update( $document->getID(), $updateData ); |
| 177 |
|
| 178 |
return \Depicter::json([ |
| 179 |
'hits' => [ |
| 180 |
'documentID' => $document->getID() |
| 181 |
] |
| 182 |
]); |
| 183 |
} else { |
| 184 |
// Return the error message received from server |
| 185 |
return \Depicter::json( $result ); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|