PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Controllers / Form / UpdateFormController.php

UpdateFormController.php in The Innovative Form Builder – IvyForms 1.1.1, at backend/src/Controllers/Form/UpdateFormController.php

90 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights reserved.
5 * @licence See COPYING.md for license details.
6 */
7
8 namespace IvyForms\Controllers\Form;
9
10 use IvyForms\Common\Exceptions\ForbiddenException;
11 use IvyForms\Common\Exceptions\InvalidArgumentException;
12 use IvyForms\Common\Exceptions\QueryExecutionException;
13 use IvyForms\Common\Exceptions\ValidationException;
14 use IvyForms\Common\Sanitizer\Sanitizer;
15 use IvyForms\Controllers\Controller;
16 use IvyForms\Services\Field\FieldService;
17 use IvyForms\Services\Field\FieldType;
18 use IvyForms\Services\Form\FormService;
19 use IvyForms\Services\Translations\BackendStrings;
20 use WP_REST_Request;
21 use WP_REST_Response;
22
23 /**
24 * Class UpdateFormController
25 */
26 class UpdateFormController extends Controller
27 {
28 private FormService $formService;
29 private FieldService $fieldService;
30
31 public function __construct(
32 FormService $formService,
33 FieldService $fieldService
34 ) {
35 $this->formService = $formService;
36 $this->fieldService = $fieldService;
37 }
38
39 /**
40 * @param WP_REST_Request<array<string,mixed>> $data
41 * @return WP_REST_Response
42 * @throws QueryExecutionException
43 * @throws InvalidArgumentException|ValidationException|ForbiddenException
44 */
45 public function handle(WP_REST_Request $data): WP_REST_Response
46 {
47 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
48
49 if (empty($data->get_params())) {
50 throw new InvalidArgumentException(
51 BackendStrings::getExceptionStrings()['invalid_request_data']
52 );
53 }
54 $params = Sanitizer::sanitizeFormData($data->get_params());
55
56 $this->fieldService->validateFieldsType($params['fields']);
57
58 // Update the form
59 [$formId, $form] = $this->formService->updateFormOrFail($params);
60 if (!$formId) {
61 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.');
62 }
63
64 // Get all existing field IDs for the form
65 $existingFieldIds = $this->fieldService->collectExistingFieldIds($formId);
66 $submittedFieldIds = [];
67
68 // Update/add all submitted fields (including options and parent/child logic)
69 if (!empty($params['fields'])) {
70 $submittedFieldIds = $this->fieldService->updateFieldsWithOptions(
71 $params['fields'],
72 $formId
73 );
74 }
75
76 // Delete any fields (and their options) that are no longer present
77 if (!empty($existingFieldIds)) {
78 $idsToDelete = array_diff($existingFieldIds, $submittedFieldIds);
79 if (!empty($idsToDelete)) {
80 $this->fieldService->deleteFields($idsToDelete);
81 }
82 }
83
84 return new WP_REST_Response([
85 'message' => BackendStrings::getCommonStrings()['ok'],
86 'data' => $form->toArray()
87 ], 200);
88 }
89 }
90