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 +290 -0 0.6.1 → 1.5 View file →
@@ -1,0 +1,290 @@
1 +<?php
2 +
3 +namespace IvyForms\Services\Form;
4 +
5 +use IvyForms\Common\Exceptions\InvalidArgumentException;
6 +use IvyForms\Common\Exceptions\NotFoundException;
7 +use IvyForms\Common\Exceptions\QueryExecutionException;
8 +use IvyForms\Common\Exceptions\ValidationException;
9 +use IvyForms\Entity\Form\Form;
10 +use IvyForms\Factory\Form\FormFactory;
11 +use IvyForms\Repository\Form\FormRepositoryInterface;
12 +use IvyForms\Services\Translations\BackendStrings;
13 +
14 +/**
15 + * Class FormService
16 + *
17 + * @package IvyForms\Services\Form
18 + *
19 + * @SuppressWarnings(PHPMD)
20 + */
21 +class FormService
22 +{
23 + private FormRepositoryInterface $formRepository;
24 +
25 + // Constructor injection for the FormRepository via PHP-DI
26 + public function __construct(FormRepositoryInterface $formRepository)
27 + {
28 + $this->formRepository = $formRepository;
29 + }
30 +
31 + /**
32 + * Get all forms from the repository.
33 + *
34 + * @return mixed[]|null
35 + */
36 + public function getAllForms(): ?array
37 + {
38 + return $this->formRepository->getAll();
39 + }
40 +
41 + /**
42 + * @return list<int>
43 + */
44 + public function getAllFormIds(): array
45 + {
46 + return $this->formRepository->getAllIds();
47 + }
48 +
49 + /**
50 + * Create a new form.
51 + *
52 + * @param Form $formData
53 + *
54 + * @return int Form ID
55 + */
56 + public function createForm(Form $formData): int
57 + {
58 + // Create form via repository and return new form ID
59 + return $this->formRepository->add($formData);
60 + }
61 +
62 + /**
63 + * Get a specific form by its ID.
64 + *
65 + * @param int $formId
66 + *
67 + * @return object FormEntity
68 + *
69 + * @throws NotFoundException If the form is not found
70 + */
71 + public function getFormById(int $formId): object
72 + {
73 + $form = $this->formRepository->getById($formId);
74 +
75 + if (!$form) {
76 + throw new NotFoundException(
77 + BackendStrings::getExceptionStrings()['form_not_found']
78 + );
79 + }
80 +
81 + return $form;
82 + }
83 +
84 + /**
85 + * Update an existing form.
86 + *
87 + * @param int $formId
88 + * @param Form $formData
89 + *
90 + * @return bool
91 + *
92 + */
93 + public function updateForm(int $formId, Form $formData): bool
94 + {
95 + // Validate form data before updating
96 +
97 + return $this->formRepository->update($formId, $formData);
98 + }
99 +
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 + /**
146 + * Update the starred field of a form.
147 + *
148 + * @param int $formId
149 + * @param mixed $value
150 + *
151 + * @return bool
152 + */
153 + public function updateFormStarred(int $formId, $value): bool
154 + {
155 + return $this->formRepository->updateFormStarred($formId, $value);
156 + }
157 +
158 + /**
159 + * Update the published status of a form.
160 + *
161 + * @param int $formId
162 + * @param mixed $value
163 + *
164 + * @return bool
165 + */
166 + public function updateFormStatus(int $formId, $value): bool
167 + {
168 + return $this->formRepository->updateFormStatus($formId, $value);
169 + }
170 +
171 + /**
172 + * Delete a form by its ID.
173 + *
174 + * @param int $formId
175 + *
176 + * @return int
177 + */
178 + public function deleteForm(int $formId): int
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 +
187 + return $this->formRepository->delete($formId);
188 + }
189 +
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 + /**
212 + * Duplicate a form by its ID.
213 + *
214 + * @param int $formId
215 + *
216 + * @return int New form ID
217 + *
218 + * @throws NotFoundException|ValidationException
219 + * @throws QueryExecutionException
220 + */
221 + public function duplicateForm(int $formId): int
222 + {
223 + // Retrieve the original form
224 + $originalForm = $this->getFormById($formId);
225 +
226 + // Duplicate the form's parameters, excluding the ID
227 + $newFormData = $originalForm->toArray();
228 + unset($newFormData['id']);
229 + $newFormData['name'] .= ' (Copy)';
230 + $newFormId = $this->createForm(FormFactory::create($newFormData));
231 +
232 + if (!$newFormId) {
233 + throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_duplicate_form']);
234 + }
235 +
236 + return $newFormId;
237 + }
238 +
239 + /**
240 + * Check if a form exists by its ID.
241 + *
242 + * @param int $formId
243 + *
244 + * @return bool
245 + */
246 + public function formExists(int $formId): bool
247 + {
248 + return $this->formRepository->exists($formId);
249 + }
250 +
251 + /**
252 + * Search for forms based on parameters.
253 + *
254 + * @param array<string, mixed> $params
255 + *
256 + * @return array<mixed>
257 + */
258 + public function searchForms(array $params): array
259 + {
260 + return $this->formRepository->search($params);
261 + }
262 +
263 + /**
264 + * Get the count of form filters.
265 + *
266 + * @param bool $shouldGetCount
267 + *
268 + * @return array<mixed>
269 + */
270 + public function getFilterCount(bool $shouldGetCount): array
271 + {
272 + if ($shouldGetCount) {
273 + return $this->formRepository->getFilterCount();
274 + }
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);
289 + }
290 +}