| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Form; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 6 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 7 |
use IvyForms\Common\Helpers\FormImportValidator; |
| 8 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 9 |
use IvyForms\Controllers\Controller; |
| 10 |
use IvyForms\Services\Form\FormImportExportService; |
| 11 |
use IvyForms\Services\Translations\BackendStrings; |
| 12 |
use WP_REST_Request; |
| 13 |
use WP_REST_Response; |
| 14 |
|
| 15 |
class ImportFormController extends Controller |
| 16 |
{ |
| 17 |
private FormImportExportService $importExportService; |
| 18 |
|
| 19 |
public function __construct(FormImportExportService $importExportService) |
| 20 |
{ |
| 21 |
$this->importExportService = $importExportService; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Handle the import request |
| 26 |
* |
| 27 |
* @param WP_REST_Request<array<string, mixed>> $data |
| 28 |
* @return WP_REST_Response |
| 29 |
* @throws ForbiddenException |
| 30 |
* @throws InvalidArgumentException |
| 31 |
*/ |
| 32 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 33 |
{ |
| 34 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 35 |
$formsData = FormImportValidator::validateImportPayload($data->get_param('importData')); |
| 36 |
|
| 37 |
// Import forms using service |
| 38 |
$importedForms = $this->importExportService->importForms($formsData); |
| 39 |
if (empty($importedForms)) { |
| 40 |
throw new InvalidArgumentException( |
| 41 |
BackendStrings::getCommonStrings()['no_forms_imported'] |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
return new WP_REST_Response([ |
| 46 |
'success' => true, |
| 47 |
'data' => $importedForms, |
| 48 |
'message' => BackendStrings::getCommonStrings()['forms_imported_successfully'] |
| 49 |
], 200); |
| 50 |
} |
| 51 |
} |
| 52 |
|