PluginProbe
The Innovative Form Builder – IvyForms / 0.9
The Innovative Form Builder – IvyForms v0.9
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 / Services / Form / FormService.php

FormService.php in The Innovative Form Builder – IvyForms 0.9, at backend/src/Services/Form/FormService.php

245 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Form;
4
5 // phpcs:disable PSR1.Files.SideEffects
6 if (!defined('ABSPATH')) {
7 exit; // Exit if accessed directly
8 }
9
10 use IvyForms\Common\Exceptions\InvalidArgumentException;
11 use IvyForms\Common\Exceptions\NotFoundException;
12 use IvyForms\Common\Exceptions\QueryExecutionException;
13 use IvyForms\Common\Exceptions\ValidationException;
14 use IvyForms\Entity\Form\Form;
15 use IvyForms\Factory\Form\FormFactory;
16 use IvyForms\Repository\Form\FormRepositoryInterface;
17 use IvyForms\Services\Translations\BackendStrings;
18
19 /**
20 * Class FormService
21 *
22 * @package IvyForms\Services\Form
23 *
24 * @SuppressWarnings(PHPMD)
25 */
26 class FormService
27 {
28 private FormRepositoryInterface $formRepository;
29
30 // Constructor injection for the FormRepository via PHP-DI
31 public function __construct(FormRepositoryInterface $formRepository)
32 {
33 $this->formRepository = $formRepository;
34 }
35
36 /**
37 * Get all forms from the repository.
38 *
39 * @return mixed[]|null
40 */
41 public function getAllForms(): ?array
42 {
43 return $this->formRepository->getAll();
44 }
45
46 /**
47 * Create a new form.
48 *
49 * @param Form $formData
50 *
51 * @return int Form ID
52 */
53 public function createForm(Form $formData): int
54 {
55 // Create form via repository and return new form ID
56 return $this->formRepository->add($formData);
57 }
58
59 /**
60 * Get a specific form by its ID.
61 *
62 * @param int $formId
63 *
64 * @return object FormEntity
65 *
66 * @throws NotFoundException If the form is not found
67 */
68 public function getFormById(int $formId): object
69 {
70 $form = $this->formRepository->getById($formId);
71
72 if (!$form) {
73 throw new NotFoundException(
74 BackendStrings::getExceptionStrings()['form_not_found']
75 );
76 }
77
78 return $form;
79 }
80
81 /**
82 * Update an existing form.
83 *
84 * @param int $formId
85 * @param Form $formData
86 *
87 * @return bool
88 *
89 */
90 public function updateForm(int $formId, Form $formData): bool
91 {
92 // Validate form data before updating
93
94 return $this->formRepository->update($formId, $formData);
95 }
96
97 /**
98 * Update form from params and return tuple [formId, form]
99 *
100 * @param array<string,mixed> $params
101 * @return array{0:int,1:Form}
102 * @throws QueryExecutionException|ValidationException
103 */
104 public function updateFormOrFail(array $params): array
105 {
106 $form = FormFactory::create($params);
107 $formId = $form->getId();
108 $this->updateForm($formId, $form);
109 if (!$form->getId()) {
110 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.');
111 }
112 return [$formId, $form];
113 }
114
115 /**
116 * Update the starred field of a form.
117 *
118 * @param int $formId
119 * @param mixed $value
120 *
121 * @return bool
122 */
123 public function updateFormStarred(int $formId, $value): bool
124 {
125 return $this->formRepository->updateFormStarred($formId, $value);
126 }
127
128 /**
129 * Update the published status of a form.
130 *
131 * @param int $formId
132 * @param mixed $value
133 *
134 * @return bool
135 */
136 public function updateFormStatus(int $formId, $value): bool
137 {
138 return $this->formRepository->updateFormStatus($formId, $value);
139 }
140
141 /**
142 * Delete a form by its ID.
143 *
144 * @param int $formId
145 *
146 * @return int
147 */
148 public function deleteForm(int $formId): int
149 {
150 return $this->formRepository->delete($formId);
151 }
152
153 /**
154 * Delete multiple forms by their IDs.
155 *
156 * @param array<int> $formIds
157 *
158 * @return int Number of deleted forms
159 */
160 public function deleteForms(array $formIds): int
161 {
162 return $this->formRepository->deleteMany($formIds);
163 }
164
165 /**
166 * Duplicate a form by its ID.
167 *
168 * @param int $formId
169 *
170 * @return int New form ID
171 *
172 * @throws NotFoundException|ValidationException
173 * @throws QueryExecutionException
174 */
175 public function duplicateForm(int $formId): int
176 {
177 // Retrieve the original form
178 $originalForm = $this->getFormById($formId);
179
180 // Duplicate the form's parameters, excluding the ID
181 $newFormData = $originalForm->toArray();
182 unset($newFormData['id']);
183 $newFormData['name'] .= ' (Copy)';
184 $newFormId = $this->createForm(FormFactory::create($newFormData));
185
186 if (!$newFormId) {
187 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_duplicate_form']);
188 }
189
190 return $newFormId;
191 }
192
193 /**
194 * Check if a form exists by its ID.
195 *
196 * @param int $formId
197 *
198 * @return bool
199 */
200 public function formExists(int $formId): bool
201 {
202 return $this->formRepository->exists($formId);
203 }
204
205 /**
206 * Search for forms based on parameters.
207 *
208 * @param array<string, mixed> $params
209 *
210 * @return array<mixed>
211 */
212 public function searchForms(array $params): array
213 {
214 return $this->formRepository->search($params);
215 }
216
217 /**
218 * Get the count of form filters.
219 *
220 * @param bool $shouldGetCount
221 *
222 * @return array<mixed>
223 */
224 public function getFilterCount(bool $shouldGetCount): array
225 {
226 if ($shouldGetCount) {
227 return $this->formRepository->getFilterCount();
228 }
229 return [];
230 }
231
232 /**
233 * Bulk update a column for multiple forms.
234 *
235 * @param string $column Column name to update
236 * @param mixed $value Value to set
237 * @param array<int> $ids Array of form IDs
238 * @return int Number of updated forms
239 */
240 public function bulkUpdateColumn(string $column, $value, array $ids): int
241 {
242 return $this->formRepository->bulkUpdateColumn($column, $value, $ids);
243 }
244 }
245