PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
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/Services/Form/FormService.php +102 -5 0.3.1 → 1.5 View file →
@@ -1,13 +1,9 @@
1 1 <?php
2 2
3 3 namespace IvyForms\Services\Form;
4 4
5 -// phpcs:disable PSR1.Files.SideEffects
6 -if (!defined('ABSPATH')) {
7 - exit; // Exit if accessed directly
8 -}
9 -
5 +use IvyForms\Common\Exceptions\InvalidArgumentException;
10 6 use IvyForms\Common\Exceptions\NotFoundException;
11 7 use IvyForms\Common\Exceptions\QueryExecutionException;
12 8 use IvyForms\Common\Exceptions\ValidationException;
13 9 use IvyForms\Entity\Form\Form;
@@ -14,8 +10,15 @@
14 10 use IvyForms\Factory\Form\FormFactory;
15 11 use IvyForms\Repository\Form\FormRepositoryInterface;
16 12 use IvyForms\Services\Translations\BackendStrings;
17 13
14 +/**
15 + * Class FormService
16 + *
17 + * @package IvyForms\Services\Form
18 + *
19 + * @SuppressWarnings(PHPMD)
20 + */
18 21 class FormService
19 22 {
20 23 private FormRepositoryInterface $formRepository;
21 24
@@ -35,8 +38,16 @@
35 38 return $this->formRepository->getAll();
36 39 }
37 40
38 41 /**
42 + * @return list<int>
43 + */
44 + public function getAllFormIds(): array
45 + {
46 + return $this->formRepository->getAllIds();
47 + }
48 +
49 + /**
39 50 * Create a new form.
40 51 *
41 52 * @param Form $formData
42 53 *
@@ -86,8 +97,53 @@
86 97 return $this->formRepository->update($formId, $formData);
87 98 }
88 99
89 100 /**
101 + * Update form from params and return tuple [formId, form]
102 + *
103 + * @param array<string,mixed> $params
104 + * @return array{0:int,1:Form}
105 + * @throws QueryExecutionException|ValidationException
106 + */
107 + public function updateFormOrFail(array $params): array
108 + {
109 + $form = FormFactory::create($params);
110 + $formId = $form->getId();
111 + $this->updateForm($formId, $form);
112 + if (!$form->getId()) {
113 + throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.');
114 + }
115 + return [$formId, $form];
116 + }
117 +
118 + /**
119 + * Preserve the persisted formType when the caller omits it, so a partial update
120 + * (e.g. settings/name change) cannot silently demote the form's type.
121 + *
122 + * @param array<string,mixed> $rawParams Request payload to be sanitized.
123 + * @param int $formId Form id resolved from the route (URL placeholder).
124 + * @return array<string,mixed>
125 + * @throws NotFoundException
126 + */
127 + public function withPreservedFormType(array $rawParams, int $formId): array
128 + {
129 + if (array_key_exists('formType', $rawParams)) {
130 + return $rawParams;
131 + }
132 +
133 + if ($formId <= 0) {
134 + return $rawParams;
135 + }
136 +
137 + $existing = $this->getFormById($formId);
138 + if (method_exists($existing, 'getFormType')) {
139 + $rawParams['formType'] = $existing->getFormType();
140 + }
141 +
142 + return $rawParams;
143 + }
144 +
145 + /**
90 146 * Update the starred field of a form.
91 147 *
92 148 * @param int $formId
93 149 * @param mixed $value
@@ -120,12 +176,40 @@
120 176 * @return int
121 177 */
122 178 public function deleteForm(int $formId): int
123 179 {
180 + /**
181 + * Fires before a form is deleted.
182 + *
183 + * @param int $formId The ID of the form being deleted.
184 + */
185 + do_action('ivyforms/form/before_delete', $formId);
186 +
124 187 return $this->formRepository->delete($formId);
125 188 }
126 189
127 190 /**
191 + * Delete multiple forms by their IDs.
192 + *
193 + * @param array<int> $formIds
194 + *
195 + * @return int Number of deleted forms
196 + */
197 + public function deleteForms(array $formIds): int
198 + {
199 + foreach ($formIds as $formId) {
200 + /**
201 + * Fires before a form is deleted.
202 + *
203 + * @param int $formId The ID of the form being deleted.
204 + */
205 + do_action('ivyforms/form/before_delete', (int) $formId);
206 + }
207 +
208 + return $this->formRepository->deleteMany($formIds);
209 + }
210 +
211 + /**
128 212 * Duplicate a form by its ID.
129 213 *
130 214 * @param int $formId
131 215 *
@@ -188,6 +272,19 @@
188 272 if ($shouldGetCount) {
189 273 return $this->formRepository->getFilterCount();
190 274 }
191 275 return [];
276 + }
277 +
278 + /**
279 + * Bulk update a column for multiple forms.
280 + *
281 + * @param string $column Column name to update
282 + * @param mixed $value Value to set
283 + * @param array<int> $ids Array of form IDs
284 + * @return int Number of updated forms
285 + */
286 + public function bulkUpdateColumn(string $column, $value, array $ids): int
287 + {
288 + return $this->formRepository->bulkUpdateColumn($column, $value, $ids);
192 289 }
193 290 }