| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © TMS-Plugins. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Controllers\Form; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 17 |
use IvyForms\Common\Exceptions\QueryExecutionException; |
| 18 |
use IvyForms\Common\Exceptions\ValidationException; |
| 19 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 20 |
use IvyForms\Controllers\Controller; |
| 21 |
use IvyForms\Factory\Field\FieldFactory; |
| 22 |
use IvyForms\Factory\Form\FormFactory; |
| 23 |
use IvyForms\Services\Field\FieldService; |
| 24 |
use IvyForms\Services\Form\FormService; |
| 25 |
use WP_REST_Request; |
| 26 |
use WP_REST_Response; |
| 27 |
|
| 28 |
/** |
| 29 |
* Class UpdateFormController |
| 30 |
* |
| 31 |
* @package IvyForms\Application\Controllers\Form |
| 32 |
*/ |
| 33 |
class UpdateFormController extends Controller |
| 34 |
{ |
| 35 |
private FormService $formService; |
| 36 |
private FieldService $fieldService; |
| 37 |
|
| 38 |
public function __construct( |
| 39 |
FormService $formService, |
| 40 |
FieldService $fieldService |
| 41 |
) { |
| 42 |
$this->formService = $formService; |
| 43 |
$this->fieldService = $fieldService; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @param WP_REST_Request $data |
| 48 |
* |
| 49 |
* @return WP_REST_Response |
| 50 |
* |
| 51 |
* @throws QueryExecutionException |
| 52 |
* @throws InvalidArgumentException|ValidationException|ForbiddenException |
| 53 |
*/ |
| 54 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 55 |
{ |
| 56 |
// Verify the nonce |
| 57 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 58 |
|
| 59 |
// Check if the form data is provided |
| 60 |
if (empty($data->get_params())) { |
| 61 |
throw new InvalidArgumentException('Invalid request data.'); |
| 62 |
} |
| 63 |
|
| 64 |
// Sanitize the update data |
| 65 |
$params = Sanitizer::sanitizeFormData($data->get_params()); |
| 66 |
|
| 67 |
$form = FormFactory::create($params); |
| 68 |
|
| 69 |
$formId = $form->getId(); |
| 70 |
|
| 71 |
$this->formService->updateForm($formId, $form); |
| 72 |
|
| 73 |
if (!$form->getId()) { |
| 74 |
throw new QueryExecutionException('Failed to update form.'); |
| 75 |
} |
| 76 |
// refactor by sending delete fields as array and delete it with one query |
| 77 |
$existingFields = $this->fieldService->getAllFields($formId); |
| 78 |
$existingFieldIds = array_map(fn($field) => $field->getId(), $existingFields); |
| 79 |
|
| 80 |
$submittedFieldIds = []; |
| 81 |
|
| 82 |
if (!empty($params['fields'])) { |
| 83 |
foreach ($params['fields'] as $fieldData) { |
| 84 |
$fieldData['formId'] = $fieldData['formId'] ?? $formId; |
| 85 |
$field = FieldFactory::create($fieldData); |
| 86 |
$fieldId = $field->getId(); |
| 87 |
|
| 88 |
if ($fieldId === 0) { |
| 89 |
// Add new field |
| 90 |
$newField = $this->fieldService->addField($field); |
| 91 |
$submittedFieldIds[] = $newField; |
| 92 |
continue; |
| 93 |
} |
| 94 |
// Update existing field |
| 95 |
$this->fieldService->updateField($fieldId, $field); |
| 96 |
$submittedFieldIds[] = $fieldId; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// Delete fields that are not in the submitted data |
| 101 |
foreach ($existingFieldIds as $existingId) { |
| 102 |
if (!in_array($existingId, $submittedFieldIds, true)) { |
| 103 |
$this->fieldService->deleteField($existingId); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return new WP_REST_Response([ |
| 108 |
'message' => 'OK', |
| 109 |
'data' => $form->toArray() |
| 110 |
], 200); |
| 111 |
} |
| 112 |
} |
| 113 |
|