PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Controllers / Form / UpdateFormController.php

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

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