| @@ -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,14 +12,11 @@ | ||
| 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\Form\FormFactory; | |
| 22 | 16 | use IvyForms\Services\Field\FieldService; |
| 23 | 17 | use IvyForms\Services\Form\FormService; |
| 24 | 18 | use IvyForms\Services\Translations\BackendStrings; |
| 25 | -use IvyForms\Services\FieldOptions\FieldOptionsService; | |
| 26 | -use IvyForms\Factory\FieldOptions\FieldOptionsFactory; | |
| 27 | 19 | use WP_REST_Request; |
| 28 | 20 | use WP_REST_Response; |
| 29 | 21 | |
| 30 | 22 | /** |
| @@ -33,18 +25,15 @@ | ||
| 33 | 25 | class UpdateFormController extends Controller |
| 34 | 26 | { |
| 35 | 27 | private FormService $formService; |
| 36 | 28 | private FieldService $fieldService; |
| 37 | - private FieldOptionsService $fieldOptionsService; | |
| 38 | 29 | |
| 39 | 30 | public function __construct( |
| 40 | 31 | FormService $formService, |
| 41 | - FieldService $fieldService, | |
| 42 | - FieldOptionsService $fieldOptionsService | |
| 32 | + FieldService $fieldService | |
| 43 | 33 | ) { |
| 44 | 34 | $this->formService = $formService; |
| 45 | 35 | $this->fieldService = $fieldService; |
| 46 | - $this->fieldOptionsService = $fieldOptionsService; | |
| 47 | 36 | } |
| 48 | 37 | |
| 49 | 38 | /** |
| 50 | 39 | * @param WP_REST_Request<array<string,mixed>> $data |
| @@ -60,40 +49,47 @@ | ||
| 60 | 49 | throw new InvalidArgumentException( |
| 61 | 50 | BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 62 | 51 | ); |
| 63 | 52 | } |
| 64 | - $params = Sanitizer::sanitizeFormData($data->get_params()); | |
| 65 | 53 | |
| 66 | - // Update the form | |
| 54 | + $formId = (int) $data->get_param('id'); | |
| 55 | + | |
| 56 | + $params = Sanitizer::sanitizeFormData( | |
| 57 | + $this->formService->withPreservedFormType($data->get_params(), $formId) | |
| 58 | + ); | |
| 59 | + | |
| 60 | + $this->fieldService->validateFieldsType($params['fields']); | |
| 61 | + | |
| 67 | 62 | [$formId, $form] = $this->formService->updateFormOrFail($params); |
| 68 | 63 | if (!$formId) { |
| 69 | 64 | throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.'); |
| 70 | 65 | } |
| 71 | 66 | |
| 72 | - // Get all existing field IDs for the form | |
| 73 | 67 | $existingFieldIds = $this->fieldService->collectExistingFieldIds($formId); |
| 74 | 68 | $submittedFieldIds = []; |
| 75 | 69 | |
| 76 | - // Update/add all submitted fields (including options and parent/child logic) | |
| 77 | 70 | if (!empty($params['fields'])) { |
| 78 | - // Handle fields with parent/child fields and fieldOptions | |
| 79 | 71 | $submittedFieldIds = $this->fieldService->updateFieldsWithOptions( |
| 80 | 72 | $params['fields'], |
| 81 | - $formId, | |
| 82 | - $this->fieldOptionsService | |
| 73 | + $formId | |
| 83 | 74 | ); |
| 84 | 75 | } |
| 85 | 76 | |
| 86 | - // Delete any fields (and their options) that are no longer present | |
| 87 | 77 | if (!empty($existingFieldIds)) { |
| 88 | 78 | $idsToDelete = array_diff($existingFieldIds, $submittedFieldIds); |
| 89 | 79 | if (!empty($idsToDelete)) { |
| 90 | - // Bulk delete all field options | |
| 91 | - $this->fieldOptionsService->deleteOptionsByFieldIds($idsToDelete); | |
| 92 | - // Bulk delete all fields | |
| 93 | 80 | $this->fieldService->deleteFields($idsToDelete); |
| 94 | 81 | } |
| 95 | 82 | } |
| 83 | + | |
| 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'); | |
| 96 | 92 | |
| 97 | 93 | return new WP_REST_Response([ |
| 98 | 94 | 'message' => BackendStrings::getCommonStrings()['ok'], |
| 99 | 95 | 'data' => $form->toArray() |