PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.1
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 1.1.1, at backend/src/Controllers/Form/AddFormController.php

124 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights reserved.
5 * @licence See COPYING.md for license details.
6 */
7
8 namespace IvyForms\Controllers\Form;
9
10 use IvyForms\Common\Exceptions\ForbiddenException;
11 use IvyForms\Common\Exceptions\InvalidArgumentException;
12 use IvyForms\Common\Exceptions\QueryExecutionException;
13 use IvyForms\Common\Exceptions\ValidationException;
14 use IvyForms\Common\Sanitizer\Sanitizer;
15 use IvyForms\Controllers\Controller;
16 use IvyForms\Factory\Field\FieldFactory;
17 use IvyForms\Factory\Form\FormFactory;
18 use IvyForms\Factory\Notification\NotificationFactory;
19 use IvyForms\Factory\Confirmation\ConfirmationFactory;
20 use IvyForms\Services\Field\FieldService;
21 use IvyForms\Services\Form\FormService;
22 use IvyForms\Services\Notification\NotificationService;
23 use IvyForms\Services\Confirmation\ConfirmationService;
24 use IvyForms\Services\Template\TemplateService;
25 use IvyForms\ValueObjects\Form\IntegrationSettings;
26 use WP_REST_Request;
27 use WP_REST_Response;
28 use IvyForms\Services\Translations\BackendStrings;
29
30 /**
31 * Class AddFormController
32 *
33 * @package IvyForms\Controllers\Form
34 */
35 class AddFormController extends Controller
36 {
37 private FormService $formService;
38 private FieldService $fieldService;
39 private NotificationService $notificationService;
40 private ConfirmationService $confirmationService;
41 private TemplateService $templateService;
42
43 public function __construct(
44 FormService $formService,
45 FieldService $fieldService,
46 NotificationService $notificationService,
47 ConfirmationService $confirmationService,
48 TemplateService $templateService
49 ) {
50 $this->formService = $formService;
51 $this->fieldService = $fieldService;
52 $this->notificationService = $notificationService;
53 $this->confirmationService = $confirmationService;
54 $this->templateService = $templateService;
55 }
56
57 /**
58 * @param WP_REST_Request<array<string, mixed>> $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 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
70
71 // Check if the form data is provided
72 if (empty($data->get_params())) {
73 throw new InvalidArgumentException(
74 BackendStrings::getExceptionStrings()['invalid_request_data']
75 );
76 }
77
78 // TODO Add validation for template_id with Sanitizer
79 //$templateId = Sanitizer::sanitizeText($data->get_param('id'));
80 $templateId = sanitize_text_field($data->get_param('template_id'));
81
82 if (empty($templateId)) {
83 throw new InvalidArgumentException(
84 BackendStrings::getTemplateStrings()['template_not_found']
85 );
86 }
87 $formData = $this->templateService->getFormDataFromTemplate($templateId);
88 $params = Sanitizer::sanitizeFormData($formData);
89
90 $form = FormFactory::create($params);
91
92 $formId = $this->formService->createForm($form);
93 if (!$formId) {
94 throw new QueryExecutionException(
95 BackendStrings::getAllFormsStrings()['failed_to_create_form']
96 );
97 }
98
99 $adminEmail = get_option('admin_email');
100
101 // Create default notification
102 $this->notificationService->createDefaultNotificationForForm(
103 $formId,
104 $params['name'],
105 $adminEmail
106 );
107
108 // Create default confirmation using the service
109 $confirmationId = $this->confirmationService->createDefaultConfirmationForForm($formId);
110
111 // Save form fields and their options using the service
112 if (!empty($params['fields'])) {
113 $this->fieldService->saveFieldsWithOptions($params['fields'], $formId);
114 }
115
116 $form->setId($formId);
117
118 return new WP_REST_Response([
119 'message' => BackendStrings::getCommonStrings()['ok'],
120 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmationId])
121 ], 200);
122 }
123 }
124