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