PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Services / Form / FormService.php

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

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