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

194 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(
66 BackendStrings::getExceptionStrings()['form_not_found']
67 );
68 }
69
70 return $form;
71 }
72
73 /**
74 * Update an existing form.
75 *
76 * @param int $formId
77 * @param Form $formData
78 *
79 * @return bool
80 *
81 */
82 public function updateForm(int $formId, Form $formData): bool
83 {
84 // Validate form data before updating
85
86 return $this->formRepository->update($formId, $formData);
87 }
88
89 /**
90 * Update the starred field of a form.
91 *
92 * @param int $formId
93 * @param mixed $value
94 *
95 * @return bool
96 */
97 public function updateFormStarred(int $formId, $value): bool
98 {
99 return $this->formRepository->updateFormStarred($formId, $value);
100 }
101
102 /**
103 * Update the published status of a form.
104 *
105 * @param int $formId
106 * @param mixed $value
107 *
108 * @return bool
109 */
110 public function updateFormStatus(int $formId, $value): bool
111 {
112 return $this->formRepository->updateFormStatus($formId, $value);
113 }
114
115 /**
116 * Delete a form by its ID.
117 *
118 * @param int $formId
119 *
120 * @return int
121 */
122 public function deleteForm(int $formId): int
123 {
124 return $this->formRepository->delete($formId);
125 }
126
127 /**
128 * Duplicate a form by its ID.
129 *
130 * @param int $formId
131 *
132 * @return int New form ID
133 *
134 * @throws NotFoundException|ValidationException
135 * @throws QueryExecutionException
136 */
137 public function duplicateForm(int $formId): int
138 {
139 // Retrieve the original form
140 $originalForm = $this->getFormById($formId);
141
142 // Duplicate the form's parameters, excluding the ID
143 $newFormData = $originalForm->toArray();
144 unset($newFormData['id']);
145 $newFormData['name'] .= ' (Copy)';
146 $newFormId = $this->createForm(FormFactory::create($newFormData));
147
148 if (!$newFormId) {
149 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_duplicate_form']);
150 }
151
152 return $newFormId;
153 }
154
155 /**
156 * Check if a form exists by its ID.
157 *
158 * @param int $formId
159 *
160 * @return bool
161 */
162 public function formExists(int $formId): bool
163 {
164 return $this->formRepository->exists($formId);
165 }
166
167 /**
168 * Search for forms based on parameters.
169 *
170 * @param array<string, mixed> $params
171 *
172 * @return array<mixed>
173 */
174 public function searchForms(array $params): array
175 {
176 return $this->formRepository->search($params);
177 }
178
179 /**
180 * Get the count of form filters.
181 *
182 * @param bool $shouldGetCount
183 *
184 * @return array<mixed>
185 */
186 public function getFilterCount(bool $shouldGetCount): array
187 {
188 if ($shouldGetCount) {
189 return $this->formRepository->getFilterCount();
190 }
191 return [];
192 }
193 }
194