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

116 lines 3.6 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\Field\FieldFactory;
22 use IvyForms\Factory\Form\FormFactory;
23 use IvyForms\Services\Field\FieldService;
24 use IvyForms\Services\Form\FormService;
25 use IvyForms\Services\Translations\BackendStrings;
26 use WP_REST_Request;
27 use WP_REST_Response;
28
29 /**
30 * Class UpdateFormController
31 *
32 * @package IvyForms\Controllers\Form
33 */
34 class UpdateFormController extends Controller
35 {
36 private FormService $formService;
37 private FieldService $fieldService;
38
39 public function __construct(
40 FormService $formService,
41 FieldService $fieldService
42 ) {
43 $this->formService = $formService;
44 $this->fieldService = $fieldService;
45 }
46
47 /**
48 * @param WP_REST_Request $data
49 *
50 * @return WP_REST_Response
51 *
52 * @throws QueryExecutionException
53 * @throws InvalidArgumentException|ValidationException|ForbiddenException
54 */
55 public function handle(WP_REST_Request $data): WP_REST_Response
56 {
57 // Verify the nonce
58 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
59
60 // Check if the form data is provided
61 if (empty($data->get_params())) {
62 throw new InvalidArgumentException(
63 BackendStrings::getExceptionStrings()['invalid_request_data']
64 );
65 }
66
67 // Sanitize the update data
68 $params = Sanitizer::sanitizeFormData($data->get_params());
69
70 $form = FormFactory::create($params);
71
72 $formId = $form->getId();
73
74 $this->formService->updateForm($formId, $form);
75
76 if (!$form->getId()) {
77 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.');
78 }
79 // refactor by sending delete fields as array and delete it with one query
80 $existingFields = $this->fieldService->getAllFields($formId);
81 $existingFieldIds = array_map(fn($field) => $field->getId(), $existingFields);
82
83 $submittedFieldIds = [];
84
85 if (!empty($params['fields'])) {
86 foreach ($params['fields'] as $fieldData) {
87 $fieldData['formId'] = $fieldData['formId'] ?? $formId;
88 $field = FieldFactory::create($fieldData);
89 $fieldId = $field->getId();
90
91 if ($fieldId === 0) {
92 // Add new field
93 $newField = $this->fieldService->addField($field);
94 $submittedFieldIds[] = $newField;
95 continue;
96 }
97 // Update existing field
98 $this->fieldService->updateField($fieldId, $field);
99 $submittedFieldIds[] = $fieldId;
100 }
101 }
102
103 // Delete fields that are not in the submitted data
104 foreach ($existingFieldIds as $existingId) {
105 if (!in_array($existingId, $submittedFieldIds, true)) {
106 $this->fieldService->deleteField($existingId);
107 }
108 }
109
110 return new WP_REST_Response([
111 'message' => BackendStrings::getCommonStrings()['ok'],
112 'data' => $form->toArray()
113 ], 200);
114 }
115 }
116