PluginProbe
The Innovative Form Builder – IvyForms / 0.4
The Innovative Form Builder – IvyForms v0.4
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 0.4, at backend/src/Controllers/Form/UpdateFormController.php

103 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © TMS-Plugins. 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\Factory\Form\FormFactory;
22 use IvyForms\Services\Field\FieldService;
23 use IvyForms\Services\Form\FormService;
24 use IvyForms\Services\Translations\BackendStrings;
25 use IvyForms\Services\FieldOptions\FieldOptionsService;
26 use IvyForms\Factory\FieldOptions\FieldOptionsFactory;
27 use WP_REST_Request;
28 use WP_REST_Response;
29
30 /**
31 * Class UpdateFormController
32 */
33 class UpdateFormController extends Controller
34 {
35 private FormService $formService;
36 private FieldService $fieldService;
37 private FieldOptionsService $fieldOptionsService;
38
39 public function __construct(
40 FormService $formService,
41 FieldService $fieldService,
42 FieldOptionsService $fieldOptionsService
43 ) {
44 $this->formService = $formService;
45 $this->fieldService = $fieldService;
46 $this->fieldOptionsService = $fieldOptionsService;
47 }
48
49 /**
50 * @param WP_REST_Request<array<string,mixed>> $data
51 * @return WP_REST_Response
52 * @throws QueryExecutionException
53 * @throws InvalidArgumentException|ValidationException|ForbiddenException
54 */
55 public function handle(WP_REST_Request $data): WP_REST_Response
56 {
57 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
58
59 if (empty($data->get_params())) {
60 throw new InvalidArgumentException(
61 BackendStrings::getExceptionStrings()['invalid_request_data']
62 );
63 }
64 $params = Sanitizer::sanitizeFormData($data->get_params());
65
66 // Update the form
67 [$formId, $form] = $this->formService->updateFormOrFail($params);
68 if (!$formId) {
69 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.');
70 }
71
72 // Get all existing field IDs for the form
73 $existingFieldIds = $this->fieldService->collectExistingFieldIds($formId);
74 $submittedFieldIds = [];
75
76 // Update/add all submitted fields (including options and parent/child logic)
77 if (!empty($params['fields'])) {
78 // Handle fields with parent/child fields and fieldOptions
79 $submittedFieldIds = $this->fieldService->updateFieldsWithOptions(
80 $params['fields'],
81 $formId,
82 $this->fieldOptionsService
83 );
84 }
85
86 // Delete any fields (and their options) that are no longer present
87 if (!empty($existingFieldIds)) {
88 $idsToDelete = array_diff($existingFieldIds, $submittedFieldIds);
89 if (!empty($idsToDelete)) {
90 // Bulk delete all field options
91 $this->fieldOptionsService->deleteOptionsByFieldIds($idsToDelete);
92 // Bulk delete all fields
93 $this->fieldService->deleteFields($idsToDelete);
94 }
95 }
96
97 return new WP_REST_Response([
98 'message' => BackendStrings::getCommonStrings()['ok'],
99 'data' => $form->toArray()
100 ], 200);
101 }
102 }
103