← All changes
|
backend/src/Controllers/Form/UpdateFormController.php
+15
-6
1.1.1
→
1.5
View file →
| @@ -13,9 +13,8 @@ | ||
| 13 | 13 | use IvyForms\Common\Exceptions\ValidationException; |
| 14 | 14 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 15 | 15 | use IvyForms\Controllers\Controller; |
| 16 | 16 | use IvyForms\Services\Field\FieldService; |
| 17 | -use IvyForms\Services\Field\FieldType; | |
| 18 | 17 | use IvyForms\Services\Form\FormService; |
| 19 | 18 | use IvyForms\Services\Translations\BackendStrings; |
| 20 | 19 | use WP_REST_Request; |
| 21 | 20 | use WP_REST_Response; |
| @@ -50,23 +49,25 @@ | ||
| 50 | 49 | throw new InvalidArgumentException( |
| 51 | 50 | BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 52 | 51 | ); |
| 53 | 52 | } |
| 54 | - $params = Sanitizer::sanitizeFormData($data->get_params()); | |
| 55 | 53 | |
| 54 | + $formId = (int) $data->get_param('id'); | |
| 55 | + | |
| 56 | + $params = Sanitizer::sanitizeFormData( | |
| 57 | + $this->formService->withPreservedFormType($data->get_params(), $formId) | |
| 58 | + ); | |
| 59 | + | |
| 56 | 60 | $this->fieldService->validateFieldsType($params['fields']); |
| 57 | 61 | |
| 58 | - // Update the form | |
| 59 | 62 | [$formId, $form] = $this->formService->updateFormOrFail($params); |
| 60 | 63 | if (!$formId) { |
| 61 | 64 | throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.'); |
| 62 | 65 | } |
| 63 | 66 | |
| 64 | - // Get all existing field IDs for the form | |
| 65 | 67 | $existingFieldIds = $this->fieldService->collectExistingFieldIds($formId); |
| 66 | 68 | $submittedFieldIds = []; |
| 67 | 69 | |
| 68 | - // Update/add all submitted fields (including options and parent/child logic) | |
| 69 | 70 | if (!empty($params['fields'])) { |
| 70 | 71 | $submittedFieldIds = $this->fieldService->updateFieldsWithOptions( |
| 71 | 72 | $params['fields'], |
| 72 | 73 | $formId |
| @@ -72,9 +73,8 @@ | ||
| 72 | 73 | $formId |
| 73 | 74 | ); |
| 74 | 75 | } |
| 75 | 76 | |
| 76 | - // Delete any fields (and their options) that are no longer present | |
| 77 | 77 | if (!empty($existingFieldIds)) { |
| 78 | 78 | $idsToDelete = array_diff($existingFieldIds, $submittedFieldIds); |
| 79 | 79 | if (!empty($idsToDelete)) { |
| 80 | 80 | $this->fieldService->deleteFields($idsToDelete); |
| @@ -79,8 +79,17 @@ | ||
| 79 | 79 | if (!empty($idsToDelete)) { |
| 80 | 80 | $this->fieldService->deleteFields($idsToDelete); |
| 81 | 81 | } |
| 82 | 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'); | |
| 83 | 92 | |
| 84 | 93 | return new WP_REST_Response([ |
| 85 | 94 | 'message' => BackendStrings::getCommonStrings()['ok'], |
| 86 | 95 | 'data' => $form->toArray() |