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

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

132 lines 4.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\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 IvyForms\Services\Template\TemplateService;
30 use WP_REST_Request;
31 use WP_REST_Response;
32 use IvyForms\Services\Translations\BackendStrings;
33 use IvyForms\Services\FieldOptions\FieldOptionsService;
34 use IvyForms\Factory\FieldOptions\FieldOptionsFactory;
35
36 /**
37 * Class AddFormController
38 *
39 * @package IvyForms\Controllers\Form
40 */
41 class AddFormController extends Controller
42 {
43 private FormService $formService;
44 private FieldService $fieldService;
45 private NotificationService $notificationService;
46 private ConfirmationService $confirmationService;
47 private TemplateService $templateService;
48 private FieldOptionsService $fieldOptionsService;
49
50 public function __construct(
51 FormService $formService,
52 FieldService $fieldService,
53 NotificationService $notificationService,
54 ConfirmationService $confirmationService,
55 TemplateService $templateService,
56 FieldOptionsService $fieldOptionsService
57 ) {
58 $this->formService = $formService;
59 $this->fieldService = $fieldService;
60 $this->notificationService = $notificationService;
61 $this->confirmationService = $confirmationService;
62 $this->templateService = $templateService;
63 $this->fieldOptionsService = $fieldOptionsService;
64 }
65
66 /**
67 * @param WP_REST_Request<array<string, mixed>> $data
68 *
69 * @return WP_REST_Response
70 *
71 * @throws QueryExecutionException
72 * @throws InvalidArgumentException
73 * @throws ValidationException
74 * @throws ForbiddenException
75 */
76 public function handle(WP_REST_Request $data): WP_REST_Response
77 {
78 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
79
80 // Check if the form data is provided
81 if (empty($data->get_params())) {
82 throw new InvalidArgumentException(
83 BackendStrings::getExceptionStrings()['invalid_request_data']
84 );
85 }
86
87 // TODO Add validation for template_id with Sanitizer
88 //$templateId = Sanitizer::sanitizeText($data->get_param('id'));
89 $templateId = sanitize_text_field($data->get_param('template_id'));
90
91 if (empty($templateId)) {
92 throw new InvalidArgumentException(
93 BackendStrings::getTemplateStrings()['template_not_found']
94 );
95 }
96 $formData = $this->templateService->getFormDataFromTemplate($templateId);
97 $params = Sanitizer::sanitizeFormData($formData);
98 $form = FormFactory::create($params);
99
100 $formId = $this->formService->createForm($form);
101 if (!$formId) {
102 throw new QueryExecutionException(
103 BackendStrings::getAllFormsStrings()['failed_to_create_form']
104 );
105 }
106
107 $adminEmail = get_option('admin_email');
108
109 // Create default notification
110 $this->notificationService->createDefaultNotificationForForm(
111 $formId,
112 $params['name'],
113 $adminEmail
114 );
115
116 // Create default confirmation using the service
117 $confirmationId = $this->confirmationService->createDefaultConfirmationForForm($formId);
118
119 // Save form fields and their options using the service
120 if (!empty($params['fields'])) {
121 $this->fieldService->saveFieldsWithOptions($params['fields'], $formId, $this->fieldOptionsService);
122 }
123
124 $form->setId($formId);
125
126 return new WP_REST_Response([
127 'message' => BackendStrings::getCommonStrings()['ok'],
128 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmationId])
129 ], 200);
130 }
131 }
132