PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.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
← All changes | backend/src/Controllers/Form/AddFormController.php +46 -54 1.1.1 → 1.5.1 View file →
@@ -12,18 +12,10 @@
12 12 use IvyForms\Common\Exceptions\QueryExecutionException;
13 13 use IvyForms\Common\Exceptions\ValidationException;
14 14 use IvyForms\Common\Sanitizer\Sanitizer;
15 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;
16 +use IvyForms\Services\Form\FormCreateService;
24 17 use IvyForms\Services\Template\TemplateService;
25 -use IvyForms\ValueObjects\Form\IntegrationSettings;
26 18 use WP_REST_Request;
27 19 use WP_REST_Response;
28 20 use IvyForms\Services\Translations\BackendStrings;
29 21
@@ -33,26 +25,17 @@
33 25 * @package IvyForms\Controllers\Form
34 26 */
35 27 class AddFormController extends Controller
36 28 {
37 - private FormService $formService;
38 - private FieldService $fieldService;
39 - private NotificationService $notificationService;
40 - private ConfirmationService $confirmationService;
29 + private FormCreateService $formCreateService;
41 30 private TemplateService $templateService;
42 31
43 32 public function __construct(
44 - FormService $formService,
45 - FieldService $fieldService,
46 - NotificationService $notificationService,
47 - ConfirmationService $confirmationService,
33 + FormCreateService $formCreateService,
48 34 TemplateService $templateService
49 35 ) {
50 - $this->formService = $formService;
51 - $this->fieldService = $fieldService;
52 - $this->notificationService = $notificationService;
53 - $this->confirmationService = $confirmationService;
54 - $this->templateService = $templateService;
36 + $this->formCreateService = $formCreateService;
37 + $this->templateService = $templateService;
55 38 }
56 39
57 40 /**
58 41 * @param WP_REST_Request<array<string, mixed>> $data
@@ -67,9 +50,8 @@
67 50 public function handle(WP_REST_Request $data): WP_REST_Response
68 51 {
69 52 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
70 53
71 - // Check if the form data is provided
72 54 if (empty($data->get_params())) {
73 55 throw new InvalidArgumentException(
74 56 BackendStrings::getExceptionStrings()['invalid_request_data']
75 57 );
@@ -74,50 +56,60 @@
74 56 BackendStrings::getExceptionStrings()['invalid_request_data']
75 57 );
76 58 }
77 59
78 - // TODO Add validation for template_id with Sanitizer
79 - //$templateId = Sanitizer::sanitizeText($data->get_param('id'));
80 60 $templateId = sanitize_text_field($data->get_param('template_id'));
81 61
82 - if (empty($templateId)) {
83 - throw new InvalidArgumentException(
84 - BackendStrings::getTemplateStrings()['template_not_found']
85 - );
62 + $mergedParams = $data->get_params();
63 + $formData = [];
64 + if (!empty($templateId)) {
65 + $formData = $this->templateService->getFormDataFromTemplate($templateId);
86 66 }
87 - $formData = $this->templateService->getFormDataFromTemplate($templateId);
88 - $params = Sanitizer::sanitizeFormData($formData);
67 + /**
68 + * Keys the client may override on create. When creating from a template, field
69 + * structure and layout always come from TemplateService — not the builder store.
70 + * Merging frontend fields caused duplicate name/address rows and single-row layout.
71 + *
72 + * @param list<string> $keys
73 + */
74 + $mergeableKeys = apply_filters('ivyforms/form/add/mergeable_keys', [
75 + 'name',
76 + 'description',
77 + 'published',
78 + 'showTitle',
79 + 'showDescription',
80 + 'storeEntries',
81 + 'fields',
82 + 'integrationSettings',
83 + 'styleSettings',
84 + 'formActionButtons',
85 + 'formType',
86 + ]);
89 87
90 - $form = FormFactory::create($params);
88 + $templateLockedKeys = [
89 + 'fields',
90 + 'integrationSettings',
91 + 'styleSettings',
92 + 'formActionButtons',
93 + ];
91 94
92 - $formId = $this->formService->createForm($form);
93 - if (!$formId) {
94 - throw new QueryExecutionException(
95 - BackendStrings::getAllFormsStrings()['failed_to_create_form']
96 - );
97 - }
95 + foreach ($mergeableKeys as $key) {
96 + if (!array_key_exists($key, $mergedParams)) {
97 + continue;
98 + }
98 99
99 - $adminEmail = get_option('admin_email');
100 + if (!empty($templateId) && in_array($key, $templateLockedKeys, true)) {
101 + continue;
102 + }
100 103
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);
104 + $formData[$key] = $mergedParams[$key];
114 105 }
115 106
116 - $form->setId($formId);
107 + $params = Sanitizer::sanitizeFormData($formData);
108 + $created = $this->formCreateService->createFromSanitizedParams($params);
117 109
118 110 return new WP_REST_Response([
119 111 'message' => BackendStrings::getCommonStrings()['ok'],
120 - 'data' => array_merge($form->toArray(), ['confirmationId' => $confirmationId])
112 + 'data' => $created,
121 113 ], 200);
122 114 }
123 115 }