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

AddFormController.php in The Innovative Form Builder – IvyForms 0.2, at backend/src/Controllers/Form/AddFormController.php

152 lines 4.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\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\Factory\Notification\NotificationFactory;
24 use IvyForms\Factory\Confirmation\ConfirmationFactory;
25 use IvyForms\Services\Field\FieldService;
26 use IvyForms\Services\Form\FormService;
27 use IvyForms\Services\Notification\NotificationService;
28 use IvyForms\Services\Confirmation\ConfirmationService;
29 use WP_REST_Request;
30 use WP_REST_Response;
31 use IvyForms\Services\Translations\BackendStrings;
32
33 /**
34 * Class AddFormController
35 *
36 * @package IvyForms\Controllers\Form
37 */
38 class AddFormController extends Controller
39 {
40 private FormService $formService;
41 private FieldService $fieldService;
42 private NotificationService $notificationService;
43 private ConfirmationService $confirmationService;
44
45 public function __construct(
46 FormService $formService,
47 FieldService $fieldService,
48 NotificationService $notificationService,
49 ConfirmationService $confirmationService
50 ) {
51 $this->formService = $formService;
52 $this->fieldService = $fieldService;
53 $this->notificationService = $notificationService;
54 $this->confirmationService = $confirmationService;
55 }
56
57 /**
58 * @param WP_REST_Request $data
59 *
60 * @return WP_REST_Response
61 *
62 * @throws QueryExecutionException
63 * @throws InvalidArgumentException
64 * @throws ValidationException
65 * @throws ForbiddenException
66 */
67 public function handle(WP_REST_Request $data): WP_REST_Response
68 {
69 // Verify the nonce
70 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
71
72 // Check if the form data is provided
73 if (empty($data->get_params())) {
74 // TODO Put all exception strings to backend strings class
75 throw new InvalidArgumentException('Invalid request data.');
76 }
77
78 // Sanitize the input data
79 $params = Sanitizer::sanitizeFormData($data->get_params());
80
81 $form = FormFactory::create($params);
82
83 $formId = $this->formService->createForm($form);
84
85 if (!$formId) {
86 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_create_form']);
87 }
88
89 // Create default notification
90 $adminEmail = get_option('admin_email');
91
92 $defaultNotification = [
93 'formId' => $formId,
94 'name' => BackendStrings::getAllFormsStrings()['admin_notification_email'],
95 'sender' => $adminEmail,
96 'receiver' => $adminEmail,
97 'enabled' => 1,
98 'subject' => $params['name'],
99 'message' => '{all_data}',
100 'smartLogic' => 0,
101 ];
102
103 $notification = NotificationFactory::create($defaultNotification);
104 $notificationId = $this->notificationService->createNotification($notification);
105
106 if (!$notificationId) {
107 throw new QueryExecutionException(
108 BackendStrings::getSettingsFormBuilderStrings()['failed_to_create_default_notification']
109 );
110 }
111
112 $notification->setId($notificationId);
113
114 $defaultConfirmation = [
115 'formId' => $formId,
116 'type' => 'successMessage',
117 'enabled' => 1,
118 'showForm' => 0,
119 'message' => BackendStrings::getAllFormsStrings()['thanks_reaching'],
120 'url' => '',
121 'page' => '',
122 ];
123
124 $confirmation = ConfirmationFactory::create($defaultConfirmation);
125 $confirmationId = $this->confirmationService->createConfirmation($confirmation);
126
127 if (!$confirmationId) {
128 throw new QueryExecutionException(
129 BackendStrings::getSettingsFormBuilderStrings()['failed_to_create_confirmation']
130 );
131 }
132
133 $confirmation->setId($confirmationId);
134
135 // Save form fields
136 if (!empty($params['fields'])) {
137 foreach ($params['fields'] as $fieldData) {
138 $fieldData['formId'] = $fieldData['formId'] ?? $formId;
139 $field = FieldFactory::create($fieldData);
140 $this->fieldService->addField($field);
141 }
142 }
143
144 $form->setId($formId);
145
146 return new WP_REST_Response([
147 'message' => BackendStrings::getCommonStrings()['ok'],
148 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmationId])
149 ], 200);
150 }
151 }
152