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 / Services / Form / FormService.php

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

240 lines 5.8 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 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 * Create a new form.
43 *
44 * @param Form $formData
45 *
46 * @return int Form ID
47 */
48 public function createForm(Form $formData): int
49 {
50 // Create form via repository and return new form ID
51 return $this->formRepository->add($formData);
52 }
53
54 /**
55 * Get a specific form by its ID.
56 *
57 * @param int $formId
58 *
59 * @return object FormEntity
60 *
61 * @throws NotFoundException If the form is not found
62 */
63 public function getFormById(int $formId): object
64 {
65 $form = $this->formRepository->getById($formId);
66
67 if (!$form) {
68 throw new NotFoundException(
69 BackendStrings::getExceptionStrings()['form_not_found']
70 );
71 }
72
73 return $form;
74 }
75
76 /**
77 * Update an existing form.
78 *
79 * @param int $formId
80 * @param Form $formData
81 *
82 * @return bool
83 *
84 */
85 public function updateForm(int $formId, Form $formData): bool
86 {
87 // Validate form data before updating
88
89 return $this->formRepository->update($formId, $formData);
90 }
91
92 /**
93 * Update form from params and return tuple [formId, form]
94 *
95 * @param array<string,mixed> $params
96 * @return array{0:int,1:Form}
97 * @throws QueryExecutionException|ValidationException
98 */
99 public function updateFormOrFail(array $params): array
100 {
101 $form = FormFactory::create($params);
102 $formId = $form->getId();
103 $this->updateForm($formId, $form);
104 if (!$form->getId()) {
105 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.');
106 }
107 return [$formId, $form];
108 }
109
110 /**
111 * Update the starred field of a form.
112 *
113 * @param int $formId
114 * @param mixed $value
115 *
116 * @return bool
117 */
118 public function updateFormStarred(int $formId, $value): bool
119 {
120 return $this->formRepository->updateFormStarred($formId, $value);
121 }
122
123 /**
124 * Update the published status of a form.
125 *
126 * @param int $formId
127 * @param mixed $value
128 *
129 * @return bool
130 */
131 public function updateFormStatus(int $formId, $value): bool
132 {
133 return $this->formRepository->updateFormStatus($formId, $value);
134 }
135
136 /**
137 * Delete a form by its ID.
138 *
139 * @param int $formId
140 *
141 * @return int
142 */
143 public function deleteForm(int $formId): int
144 {
145 return $this->formRepository->delete($formId);
146 }
147
148 /**
149 * Delete multiple forms by their IDs.
150 *
151 * @param array<int> $formIds
152 *
153 * @return int Number of deleted forms
154 */
155 public function deleteForms(array $formIds): int
156 {
157 return $this->formRepository->deleteMany($formIds);
158 }
159
160 /**
161 * Duplicate a form by its ID.
162 *
163 * @param int $formId
164 *
165 * @return int New form ID
166 *
167 * @throws NotFoundException|ValidationException
168 * @throws QueryExecutionException
169 */
170 public function duplicateForm(int $formId): int
171 {
172 // Retrieve the original form
173 $originalForm = $this->getFormById($formId);
174
175 // Duplicate the form's parameters, excluding the ID
176 $newFormData = $originalForm->toArray();
177 unset($newFormData['id']);
178 $newFormData['name'] .= ' (Copy)';
179 $newFormId = $this->createForm(FormFactory::create($newFormData));
180
181 if (!$newFormId) {
182 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_duplicate_form']);
183 }
184
185 return $newFormId;
186 }
187
188 /**
189 * Check if a form exists by its ID.
190 *
191 * @param int $formId
192 *
193 * @return bool
194 */
195 public function formExists(int $formId): bool
196 {
197 return $this->formRepository->exists($formId);
198 }
199
200 /**
201 * Search for forms based on parameters.
202 *
203 * @param array<string, mixed> $params
204 *
205 * @return array<mixed>
206 */
207 public function searchForms(array $params): array
208 {
209 return $this->formRepository->search($params);
210 }
211
212 /**
213 * Get the count of form filters.
214 *
215 * @param bool $shouldGetCount
216 *
217 * @return array<mixed>
218 */
219 public function getFilterCount(bool $shouldGetCount): array
220 {
221 if ($shouldGetCount) {
222 return $this->formRepository->getFilterCount();
223 }
224 return [];
225 }
226
227 /**
228 * Bulk update a column for multiple forms.
229 *
230 * @param string $column Column name to update
231 * @param mixed $value Value to set
232 * @param array<int> $ids Array of form IDs
233 * @return int Number of updated forms
234 */
235 public function bulkUpdateColumn(string $column, $value, array $ids): int
236 {
237 return $this->formRepository->bulkUpdateColumn($column, $value, $ids);
238 }
239 }
240