PluginProbe
The Innovative Form Builder – IvyForms / 0.5
The Innovative Form Builder – IvyForms v0.5
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 / FormSubmissionController.php

FormSubmissionController.php in The Innovative Form Builder – IvyForms 0.5, at backend/src/Controllers/Form/FormSubmissionController.php

173 lines 5.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 © 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\NotFoundException;
18 use IvyForms\Common\Exceptions\ValidationException;
19 use IvyForms\Common\Helpers\EntryHelper;
20 use IvyForms\Common\Sanitizer\Sanitizer;
21 use IvyForms\Controllers\Controller;
22 use IvyForms\Factory\Entry\EntryFactory;
23 use IvyForms\Services\Confirmation\ConfirmationService;
24 use IvyForms\Services\Entry\EntryService;
25 use IvyForms\Services\Field\FieldService;
26 use IvyForms\Services\Field\FieldType;
27 use IvyForms\Services\Form\FormService;
28 use IvyForms\Services\Mailer\MailerService;
29 use IvyForms\Services\Notification\NotificationService;
30 use IvyForms\Services\Placeholder\PlaceholderService;
31 use IvyForms\Services\Security\SecurityService;
32 use IvyForms\Services\Translations\BackendStrings;
33 use WP_REST_Request;
34 use WP_REST_Response;
35
36 /**
37 * Class FormSubmissionController
38 *
39 * @package IvyForms\Controllers\Form
40 */
41 class FormSubmissionController extends Controller
42 {
43 private FormService $formService;
44 private FieldService $fieldService;
45 private NotificationService $notificationService;
46 private MailerService $mailerService;
47 private EntryService $entryService;
48 private SecurityService $securityService;
49 private ConfirmationService $confirmationService;
50
51 public function __construct(
52 FormService $formService,
53 FieldService $fieldService,
54 NotificationService $notificationService,
55 MailerService $mailerService,
56 EntryService $entryService,
57 SecurityService $securityService,
58 ConfirmationService $confirmationService
59 ) {
60 $this->formService = $formService;
61 $this->fieldService = $fieldService;
62 $this->notificationService = $notificationService;
63 $this->mailerService = $mailerService;
64 $this->entryService = $entryService;
65 $this->securityService = $securityService;
66 $this->confirmationService = $confirmationService;
67 }
68
69 /**
70 * @param WP_REST_Request $data
71 *
72 * @return WP_REST_Response
73 *
74 * @throws InvalidArgumentException|NotFoundException|ForbiddenException
75 * @throws ValidationException
76 */
77 public function handle(WP_REST_Request $data): WP_REST_Response
78 {
79 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
80
81 if (empty($data->get_params())) {
82 throw new InvalidArgumentException(
83 BackendStrings::getExceptionStrings()['invalid_request_data']
84 );
85 }
86
87 $formId = Sanitizer::sanitizeId($data->get_param('formId'));
88 Sanitizer::verifySubmissionNonce($data->get_param('nonce'), $formId);
89
90 if ($formId <= 0) {
91 throw new InvalidArgumentException(
92 BackendStrings::getExceptionStrings()['invalid_form_id']
93 );
94 }
95
96 $form = $this->formService->getFormById($formId);
97 $formFields = $this->fieldService->getAllFields($form->getId());
98
99 $this->fieldService->validateFieldsType($formFields);
100
101 // Build fieldLabels mapping for placeholders
102 $fieldLabels = PlaceholderService::buildFieldLabels($formFields);
103
104 // Validate CAPTCHA if present using SecurityService (automatically determines provider and validates)
105 $this->securityService->validateFormSubmission($data->get_params(), $formFields);
106
107 // Sanitize form submission data based on field types
108 $submissionData = Sanitizer::sanitizeFormSubmissionData($data->get_params(), $formFields);
109
110 [$isDuplicate, $duplicateErrors] = $this->fieldService->checkDuplicateFieldValues(
111 $formFields,
112 $submissionData,
113 $formId
114 );
115 if ($isDuplicate) {
116 return new WP_REST_Response([
117 'data' => [
118 'success' => null,
119 'entry' => ['stored' => false],
120 'confirmation' => null,
121 'is_duplicate' => true,
122 'duplicate_errors' => $duplicateErrors,
123 ],
124 ], 200);
125 }
126
127 $entryId = null;
128 $entry = [
129 'stored' => false
130 ];
131 if ($form->isStoreEntries()) {
132 $entryData = EntryHelper::buildEntryData($formId);
133 $entryObj = EntryFactory::create($entryData);
134 $entryId = $this->entryService->getEntryManager()->createEntry($entryObj);
135 $this->entryService->getEntryFieldManager()->addEntryFields($formFields, $entryId, $submissionData);
136 $entry = [
137 'stored' => true,
138 'id' => $entryId,
139 ];
140 }
141
142 // Build field and general entry data for placeholders
143 $fieldData = PlaceholderService::buildFieldData($formFields, $submissionData);
144 $generalData = PlaceholderService::buildGeneralData($entryId ?? 0, $submissionData);
145
146 // Use NotificationService for notifications
147 $notificationsResult = $this->notificationService->processNotifications(
148 $formId,
149 $submissionData,
150 $fieldData,
151 $generalData,
152 $this->mailerService,
153 $fieldLabels
154 );
155 // Use ConfirmationService for confirmations
156 $confirmationMessage = $this->confirmationService->processConfirmations(
157 $formId,
158 $fieldData,
159 $generalData,
160 $fieldLabels
161 );
162
163 return new WP_REST_Response([
164 'message' => BackendStrings::getCommonStrings()['ok'],
165 'data' => [
166 'success' => $notificationsResult,
167 'entry' => $entry,
168 'confirmation' => $confirmationMessage,
169 ],
170 ], 200);
171 }
172 }
173