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

114 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\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\Application\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('Invalid request data.');
63 }
64
65 // Sanitize the update data
66 $params = Sanitizer::sanitizeFormData($data->get_params());
67
68 $form = FormFactory::create($params);
69
70 $formId = $form->getId();
71
72 $this->formService->updateForm($formId, $form);
73
74 if (!$form->getId()) {
75 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.');
76 }
77 // refactor by sending delete fields as array and delete it with one query
78 $existingFields = $this->fieldService->getAllFields($formId);
79 $existingFieldIds = array_map(fn($field) => $field->getId(), $existingFields);
80
81 $submittedFieldIds = [];
82
83 if (!empty($params['fields'])) {
84 foreach ($params['fields'] as $fieldData) {
85 $fieldData['formId'] = $fieldData['formId'] ?? $formId;
86 $field = FieldFactory::create($fieldData);
87 $fieldId = $field->getId();
88
89 if ($fieldId === 0) {
90 // Add new field
91 $newField = $this->fieldService->addField($field);
92 $submittedFieldIds[] = $newField;
93 continue;
94 }
95 // Update existing field
96 $this->fieldService->updateField($fieldId, $field);
97 $submittedFieldIds[] = $fieldId;
98 }
99 }
100
101 // Delete fields that are not in the submitted data
102 foreach ($existingFieldIds as $existingId) {
103 if (!in_array($existingId, $submittedFieldIds, true)) {
104 $this->fieldService->deleteField($existingId);
105 }
106 }
107
108 return new WP_REST_Response([
109 'message' => BackendStrings::getCommonStrings()['ok'],
110 'data' => $form->toArray()
111 ], 200);
112 }
113 }
114