| @@ -1,13 +1,9 @@ | ||
| 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; |
| 11 | 7 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 12 | 8 | use IvyForms\Common\Exceptions\ValidationException; |
| 13 | 9 | use IvyForms\Entity\Form\Form; |
| @@ -14,8 +10,15 @@ | ||
| 14 | 10 | use IvyForms\Factory\Form\FormFactory; |
| 15 | 11 | use IvyForms\Repository\Form\FormRepositoryInterface; |
| 16 | 12 | use IvyForms\Services\Translations\BackendStrings; |
| 17 | 13 | |
| 14 | +/** | |
| 15 | + * Class FormService | |
| 16 | + * | |
| 17 | + * @package IvyForms\Services\Form | |
| 18 | + * | |
| 19 | + * @SuppressWarnings(PHPMD) | |
| 20 | + */ | |
| 18 | 21 | class FormService |
| 19 | 22 | { |
| 20 | 23 | private FormRepositoryInterface $formRepository; |
| 21 | 24 | |
| @@ -35,8 +38,16 @@ | ||
| 35 | 38 | return $this->formRepository->getAll(); |
| 36 | 39 | } |
| 37 | 40 | |
| 38 | 41 | /** |
| 42 | + * @return list<int> | |
| 43 | + */ | |
| 44 | + public function getAllFormIds(): array | |
| 45 | + { | |
| 46 | + return $this->formRepository->getAllIds(); | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 39 | 50 | * Create a new form. |
| 40 | 51 | * |
| 41 | 52 | * @param Form $formData |
| 42 | 53 | * |
| @@ -61,9 +72,11 @@ | ||
| 61 | 72 | { |
| 62 | 73 | $form = $this->formRepository->getById($formId); |
| 63 | 74 | |
| 64 | 75 | if (!$form) { |
| 65 | - throw new NotFoundException('Form not found.'); | |
| 76 | + throw new NotFoundException( | |
| 77 | + BackendStrings::getExceptionStrings()['form_not_found'] | |
| 78 | + ); | |
| 66 | 79 | } |
| 67 | 80 | |
| 68 | 81 | return $form; |
| 69 | 82 | } |
| @@ -84,8 +97,53 @@ | ||
| 84 | 97 | return $this->formRepository->update($formId, $formData); |
| 85 | 98 | } |
| 86 | 99 | |
| 87 | 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 | + /** | |
| 88 | 146 | * Update the starred field of a form. |
| 89 | 147 | * |
| 90 | 148 | * @param int $formId |
| 91 | 149 | * @param mixed $value |
| @@ -118,12 +176,40 @@ | ||
| 118 | 176 | * @return int |
| 119 | 177 | */ |
| 120 | 178 | public function deleteForm(int $formId): int |
| 121 | 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 | + | |
| 122 | 187 | return $this->formRepository->delete($formId); |
| 123 | 188 | } |
| 124 | 189 | |
| 125 | 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 | + /** | |
| 126 | 212 | * Duplicate a form by its ID. |
| 127 | 213 | * |
| 128 | 214 | * @param int $formId |
| 129 | 215 | * |
| @@ -186,6 +272,19 @@ | ||
| 186 | 272 | if ($shouldGetCount) { |
| 187 | 273 | return $this->formRepository->getFilterCount(); |
| 188 | 274 | } |
| 189 | 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); | |
| 190 | 289 | } |
| 191 | 290 | } |