| @@ -1,17 +1,24 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Services\Form; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -if (!defined('ABSPATH')) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 5 | +use IvyForms\Common\Exceptions\InvalidArgumentException; | |
| 10 | 6 | use IvyForms\Common\Exceptions\NotFoundException; |
| 7 | +use IvyForms\Common\Exceptions\QueryExecutionException; | |
| 8 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 11 | 9 | use IvyForms\Entity\Form\Form; |
| 10 | +use IvyForms\Factory\Form\FormFactory; | |
| 12 | 11 | use IvyForms\Repository\Form\FormRepositoryInterface; |
| 12 | +use IvyForms\Services\Translations\BackendStrings; | |
| 13 | 13 | |
| 14 | +/** | |
| 15 | + * Class FormService | |
| 16 | + * | |
| 17 | + * @package IvyForms\Services\Form | |
| 18 | + * | |
| 19 | + * @SuppressWarnings(PHPMD) | |
| 20 | + */ | |
| 14 | 21 | class FormService |
| 15 | 22 | { |
| 16 | 23 | private FormRepositoryInterface $formRepository; |
| 17 | 24 | |
| @@ -31,8 +38,16 @@ | ||
| 31 | 38 | return $this->formRepository->getAll(); |
| 32 | 39 | } |
| 33 | 40 | |
| 34 | 41 | /** |
| 42 | + * @return list<int> | |
| 43 | + */ | |
| 44 | + public function getAllFormIds(): array | |
| 45 | + { | |
| 46 | + return $this->formRepository->getAllIds(); | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 35 | 50 | * Create a new form. |
| 36 | 51 | * |
| 37 | 52 | * @param Form $formData |
| 38 | 53 | * |
| @@ -57,9 +72,11 @@ | ||
| 57 | 72 | { |
| 58 | 73 | $form = $this->formRepository->getById($formId); |
| 59 | 74 | |
| 60 | 75 | if (!$form) { |
| 61 | - throw new NotFoundException('Form not found.'); | |
| 76 | + throw new NotFoundException( | |
| 77 | + BackendStrings::getExceptionStrings()['form_not_found'] | |
| 78 | + ); | |
| 62 | 79 | } |
| 63 | 80 | |
| 64 | 81 | return $form; |
| 65 | 82 | } |
| @@ -80,8 +97,53 @@ | ||
| 80 | 97 | return $this->formRepository->update($formId, $formData); |
| 81 | 98 | } |
| 82 | 99 | |
| 83 | 100 | /** |
| 101 | + * Update form from params and return tuple [formId, form] | |
| 102 | + * | |
| 103 | + * @param array<string,mixed> $params | |
| 104 | + * @return array{0:int,1:Form} | |
| 105 | + * @throws QueryExecutionException|ValidationException | |
| 106 | + */ | |
| 107 | + public function updateFormOrFail(array $params): array | |
| 108 | + { | |
| 109 | + $form = FormFactory::create($params); | |
| 110 | + $formId = $form->getId(); | |
| 111 | + $this->updateForm($formId, $form); | |
| 112 | + if (!$form->getId()) { | |
| 113 | + throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_update_form'] . '.'); | |
| 114 | + } | |
| 115 | + return [$formId, $form]; | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Preserve the persisted formType when the caller omits it, so a partial update | |
| 120 | + * (e.g. settings/name change) cannot silently demote the form's type. | |
| 121 | + * | |
| 122 | + * @param array<string,mixed> $rawParams Request payload to be sanitized. | |
| 123 | + * @param int $formId Form id resolved from the route (URL placeholder). | |
| 124 | + * @return array<string,mixed> | |
| 125 | + * @throws NotFoundException | |
| 126 | + */ | |
| 127 | + public function withPreservedFormType(array $rawParams, int $formId): array | |
| 128 | + { | |
| 129 | + if (array_key_exists('formType', $rawParams)) { | |
| 130 | + return $rawParams; | |
| 131 | + } | |
| 132 | + | |
| 133 | + if ($formId <= 0) { | |
| 134 | + return $rawParams; | |
| 135 | + } | |
| 136 | + | |
| 137 | + $existing = $this->getFormById($formId); | |
| 138 | + if (method_exists($existing, 'getFormType')) { | |
| 139 | + $rawParams['formType'] = $existing->getFormType(); | |
| 140 | + } | |
| 141 | + | |
| 142 | + return $rawParams; | |
| 143 | + } | |
| 144 | + | |
| 145 | + /** | |
| 84 | 146 | * Update the starred field of a form. |
| 85 | 147 | * |
| 86 | 148 | * @param int $formId |
| 87 | 149 | * @param mixed $value |
| @@ -102,9 +164,9 @@ | ||
| 102 | 164 | * @return bool |
| 103 | 165 | */ |
| 104 | 166 | public function updateFormStatus(int $formId, $value): bool |
| 105 | 167 | { |
| 106 | - return $this->formRepository->updateFormStatus($formId, $value); | |
| 168 | + return $this->formRepository->updateFormStatus($formId, $value); | |
| 107 | 169 | } |
| 108 | 170 | |
| 109 | 171 | /** |
| 110 | 172 | * Delete a form by its ID. |
| @@ -114,12 +176,68 @@ | ||
| 114 | 176 | * @return int |
| 115 | 177 | */ |
| 116 | 178 | public function deleteForm(int $formId): int |
| 117 | 179 | { |
| 180 | + /** | |
| 181 | + * Fires before a form is deleted. | |
| 182 | + * | |
| 183 | + * @param int $formId The ID of the form being deleted. | |
| 184 | + */ | |
| 185 | + do_action('ivyforms/form/before_delete', $formId); | |
| 186 | + | |
| 118 | 187 | return $this->formRepository->delete($formId); |
| 119 | 188 | } |
| 120 | 189 | |
| 121 | 190 | /** |
| 191 | + * Delete multiple forms by their IDs. | |
| 192 | + * | |
| 193 | + * @param array<int> $formIds | |
| 194 | + * | |
| 195 | + * @return int Number of deleted forms | |
| 196 | + */ | |
| 197 | + public function deleteForms(array $formIds): int | |
| 198 | + { | |
| 199 | + foreach ($formIds as $formId) { | |
| 200 | + /** | |
| 201 | + * Fires before a form is deleted. | |
| 202 | + * | |
| 203 | + * @param int $formId The ID of the form being deleted. | |
| 204 | + */ | |
| 205 | + do_action('ivyforms/form/before_delete', (int) $formId); | |
| 206 | + } | |
| 207 | + | |
| 208 | + return $this->formRepository->deleteMany($formIds); | |
| 209 | + } | |
| 210 | + | |
| 211 | + /** | |
| 212 | + * Duplicate a form by its ID. | |
| 213 | + * | |
| 214 | + * @param int $formId | |
| 215 | + * | |
| 216 | + * @return int New form ID | |
| 217 | + * | |
| 218 | + * @throws NotFoundException|ValidationException | |
| 219 | + * @throws QueryExecutionException | |
| 220 | + */ | |
| 221 | + public function duplicateForm(int $formId): int | |
| 222 | + { | |
| 223 | + // Retrieve the original form | |
| 224 | + $originalForm = $this->getFormById($formId); | |
| 225 | + | |
| 226 | + // Duplicate the form's parameters, excluding the ID | |
| 227 | + $newFormData = $originalForm->toArray(); | |
| 228 | + unset($newFormData['id']); | |
| 229 | + $newFormData['name'] .= ' (Copy)'; | |
| 230 | + $newFormId = $this->createForm(FormFactory::create($newFormData)); | |
| 231 | + | |
| 232 | + if (!$newFormId) { | |
| 233 | + throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['failed_to_duplicate_form']); | |
| 234 | + } | |
| 235 | + | |
| 236 | + return $newFormId; | |
| 237 | + } | |
| 238 | + | |
| 239 | + /** | |
| 122 | 240 | * Check if a form exists by its ID. |
| 123 | 241 | * |
| 124 | 242 | * @param int $formId |
| 125 | 243 | * |
| @@ -133,11 +251,40 @@ | ||
| 133 | 251 | /** |
| 134 | 252 | * Search for forms based on parameters. |
| 135 | 253 | * |
| 136 | 254 | * @param array<string, mixed> $params |
| 255 | + * | |
| 137 | 256 | * @return array<mixed> |
| 138 | 257 | */ |
| 139 | 258 | public function searchForms(array $params): array |
| 140 | 259 | { |
| 141 | 260 | return $this->formRepository->search($params); |
| 261 | + } | |
| 262 | + | |
| 263 | + /** | |
| 264 | + * Get the count of form filters. | |
| 265 | + * | |
| 266 | + * @param bool $shouldGetCount | |
| 267 | + * | |
| 268 | + * @return array<mixed> | |
| 269 | + */ | |
| 270 | + public function getFilterCount(bool $shouldGetCount): array | |
| 271 | + { | |
| 272 | + if ($shouldGetCount) { | |
| 273 | + return $this->formRepository->getFilterCount(); | |
| 274 | + } | |
| 275 | + return []; | |
| 276 | + } | |
| 277 | + | |
| 278 | + /** | |
| 279 | + * Bulk update a column for multiple forms. | |
| 280 | + * | |
| 281 | + * @param string $column Column name to update | |
| 282 | + * @param mixed $value Value to set | |
| 283 | + * @param array<int> $ids Array of form IDs | |
| 284 | + * @return int Number of updated forms | |
| 285 | + */ | |
| 286 | + public function bulkUpdateColumn(string $column, $value, array $ids): int | |
| 287 | + { | |
| 288 | + return $this->formRepository->bulkUpdateColumn($column, $value, $ids); | |
| 142 | 289 | } |
| 143 | 290 | } |