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