PluginProbe
The Innovative Form Builder – IvyForms / 0.2.1
The Innovative Form Builder – IvyForms v0.2.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 0.2.1, at backend/src/Services/Form/FormService.php

192 lines 4.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 class FormService
19 {
20 private FormRepositoryInterface $formRepository;
21
22 // Constructor injection for the FormRepository via PHP-DI
23 public function __construct(FormRepositoryInterface $formRepository)
24 {
25 $this->formRepository = $formRepository;
26 }
27
28 /**
29 * Get all forms from the repository.
30 *
31 * @return mixed[]|null
32 */
33 public function getAllForms(): ?array
34 {
35 return $this->formRepository->getAll();
36 }
37
38 /**
39 * Create a new form.
40 *
41 * @param Form $formData
42 *
43 * @return int Form ID
44 */
45 public function createForm(Form $formData): int
46 {
47 // Create form via repository and return new form ID
48 return $this->formRepository->add($formData);
49 }
50
51 /**
52 * Get a specific form by its ID.
53 *
54 * @param int $formId
55 *
56 * @return object FormEntity
57 *
58 * @throws NotFoundException If the form is not found
59 */
60 public function getFormById(int $formId): object
61 {
62 $form = $this->formRepository->getById($formId);
63
64 if (!$form) {
65 throw new NotFoundException('Form not found.');
66 }
67
68 return $form;
69 }
70
71 /**
72 * Update an existing form.
73 *
74 * @param int $formId
75 * @param Form $formData
76 *
77 * @return bool
78 *
79 */
80 public function updateForm(int $formId, Form $formData): bool
81 {
82 // Validate form data before updating
83
84 return $this->formRepository->update($formId, $formData);
85 }
86
87 /**
88 * Update the starred field of a form.
89 *
90 * @param int $formId
91 * @param mixed $value
92 *
93 * @return bool
94 */
95 public function updateFormStarred(int $formId, $value): bool
96 {
97 return $this->formRepository->updateFormStarred($formId, $value);
98 }
99
100 /**
101 * Update the published status of a form.
102 *
103 * @param int $formId
104 * @param mixed $value
105 *
106 * @return bool
107 */
108 public function updateFormStatus(int $formId, $value): bool
109 {
110 return $this->formRepository->updateFormStatus($formId, $value);
111 }
112
113 /**
114 * Delete a form by its ID.
115 *
116 * @param int $formId
117 *
118 * @return int
119 */
120 public function deleteForm(int $formId): int
121 {
122 return $this->formRepository->delete($formId);
123 }
124
125 /**
126 * Duplicate a form by its ID.
127 *
128 * @param int $formId
129 *
130 * @return int New form ID
131 *
132 * @throws NotFoundException|ValidationException
133 * @throws QueryExecutionException
134 */
135 public function duplicateForm(int $formId): int
136 {
137 // Retrieve the original form
138 $originalForm = $this->getFormById($formId);
139
140 // Duplicate the form's parameters, excluding the ID
141 $newFormData = $originalForm->toArray();
142 unset($newFormData['id']);
143 $newFormData['name'] .= ' (Copy)';
144 $newFormId = $this->createForm(FormFactory::create($newFormData));
145
146 if (!$newFormId) {
147 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_duplicate_form']);
148 }
149
150 return $newFormId;
151 }
152
153 /**
154 * Check if a form exists by its ID.
155 *
156 * @param int $formId
157 *
158 * @return bool
159 */
160 public function formExists(int $formId): bool
161 {
162 return $this->formRepository->exists($formId);
163 }
164
165 /**
166 * Search for forms based on parameters.
167 *
168 * @param array<string, mixed> $params
169 *
170 * @return array<mixed>
171 */
172 public function searchForms(array $params): array
173 {
174 return $this->formRepository->search($params);
175 }
176
177 /**
178 * Get the count of form filters.
179 *
180 * @param bool $shouldGetCount
181 *
182 * @return array<mixed>
183 */
184 public function getFilterCount(bool $shouldGetCount): array
185 {
186 if ($shouldGetCount) {
187 return $this->formRepository->getFilterCount();
188 }
189 return [];
190 }
191 }
192