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 / FormSubmissionController.php

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

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