| @@ -1,13 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Services\Field; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -if (!defined('ABSPATH')) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 10 | 5 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 11 | 6 | use IvyForms\Common\Exceptions\ValidationException; |
| 12 | 7 | use IvyForms\Entity\Field\Field; |
| 13 | 8 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| @@ -14,9 +9,8 @@ | ||
| 14 | 9 | use IvyForms\Factory\Field\FieldFactory; |
| 15 | 10 | use IvyForms\Factory\FieldOptions\FieldOptionsFactory; |
| 16 | 11 | use IvyForms\Repository\Field\FieldRepositoryInterface; |
| 17 | 12 | use IvyForms\Repository\FieldOptions\FieldOptionsRepositoryInterface; |
| 18 | -use IvyForms\Repository\EntryField\EntryFieldRepositoryInterface; | |
| 19 | 13 | use IvyForms\Services\Translations\BackendStrings; |
| 20 | 14 | use IvyForms\Common\Helpers\FieldHelper; |
| 21 | 15 | use IvyForms\Services\Media\ImageService; |
| 22 | 16 | |
| @@ -26,9 +20,8 @@ | ||
| 26 | 20 | class FieldService |
| 27 | 21 | { |
| 28 | 22 | private FieldRepositoryInterface $fieldRepository; |
| 29 | 23 | private FieldOptionsRepositoryInterface $fieldOptionsRepository; |
| 30 | - private EntryFieldRepositoryInterface $entryFieldRepository; | |
| 31 | 24 | private ImageService $imageService; |
| 32 | 25 | |
| 33 | 26 | // Constructor injection for repositories only via PHP-DI |
| 34 | 27 | public function __construct( |
| @@ -33,14 +26,12 @@ | ||
| 33 | 26 | // Constructor injection for repositories only via PHP-DI |
| 34 | 27 | public function __construct( |
| 35 | 28 | FieldRepositoryInterface $fieldRepository, |
| 36 | 29 | FieldOptionsRepositoryInterface $fieldOptionsRepository, |
| 37 | - EntryFieldRepositoryInterface $entryFieldRepository, | |
| 38 | 30 | ImageService $imageService |
| 39 | 31 | ) { |
| 40 | 32 | $this->fieldRepository = $fieldRepository; |
| 41 | 33 | $this->fieldOptionsRepository = $fieldOptionsRepository; |
| 42 | - $this->entryFieldRepository = $entryFieldRepository; | |
| 43 | 34 | $this->imageService = $imageService; |
| 44 | 35 | } |
| 45 | 36 | |
| 46 | 37 | /** |
| @@ -199,10 +190,11 @@ | ||
| 199 | 190 | foreach ($fields as $fieldData) { |
| 200 | 191 | $fieldData['formId'] = $fieldData['formId'] ?? $formId; |
| 201 | 192 | // Resolve parentId using fieldIndex if available |
| 202 | 193 | FieldHelper::resolveParentId($fieldData, $parentMap); |
| 203 | - // Process htmlContent to upload base64 images | |
| 194 | + // Process htmlContent / rich text default values to upload base64 images | |
| 204 | 195 | FieldHelper::processFieldHtmlContent($fieldData, $this->imageService); |
| 196 | + FieldHelper::processRichTextDefaultValue($fieldData, $this->imageService); | |
| 205 | 197 | $field = FieldFactory::create($fieldData); |
| 206 | 198 | $fieldId = $this->addField($field); |
| 207 | 199 | // If this is a parent field (no parentId), store its DB id in the map |
| 208 | 200 | if ($field->getParentId() === null) { |
| @@ -232,10 +224,11 @@ | ||
| 232 | 224 | $parentMap = []; |
| 233 | 225 | foreach ($fields as $fieldData) { |
| 234 | 226 | $fieldData['formId'] = $fieldData['formId'] ?? $formId; |
| 235 | 227 | FieldHelper::resolveParentId($fieldData, $parentMap); |
| 236 | - // Process htmlContent to upload base64 images | |
| 228 | + // Process htmlContent / rich text default values to upload base64 images | |
| 237 | 229 | FieldHelper::processFieldHtmlContent($fieldData, $this->imageService); |
| 230 | + FieldHelper::processRichTextDefaultValue($fieldData, $this->imageService); | |
| 238 | 231 | $field = FieldFactory::create($fieldData); |
| 239 | 232 | $fieldId = $field->getId(); |
| 240 | 233 | |
| 241 | 234 | if ($fieldId === 0) { // new field |
| @@ -329,42 +322,8 @@ | ||
| 329 | 322 | } |
| 330 | 323 | } |
| 331 | 324 | |
| 332 | 325 | /** |
| 333 | - * Check for duplicate field values in submission. | |
| 334 | - * @param Field[] $formFields | |
| 335 | - * @param array<string,mixed> $submissionData | |
| 336 | - * @param int $formId | |
| 337 | - * @return array{0: bool, 1: array<int|string,bool>} | |
| 338 | - */ | |
| 339 | - public function checkDuplicateFieldValues( | |
| 340 | - array $formFields, | |
| 341 | - array $submissionData, | |
| 342 | - int $formId | |
| 343 | - ): array { | |
| 344 | - $duplicateErrors = []; | |
| 345 | - $isDuplicate = false; | |
| 346 | - $fieldValues = []; | |
| 347 | - | |
| 348 | - foreach ($formFields as $field) { | |
| 349 | - $fid = $field->getId(); | |
| 350 | - $fidStr = (string)$fid; | |
| 351 | - if (isset($submissionData[$fidStr])) { | |
| 352 | - $fieldValues[$fidStr] = $submissionData[$fidStr]; | |
| 353 | - } | |
| 354 | - if ($field->getFieldAdvancedSettings()->isNoDuplicates() && !empty($fieldValues[$fidStr])) { | |
| 355 | - $val = $fieldValues[$fidStr]; | |
| 356 | - if ($this->entryFieldRepository->checkDuplicateValue($formId, $fid, $val)) { | |
| 357 | - $duplicateErrors[$fidStr] = true; | |
| 358 | - $isDuplicate = true; | |
| 359 | - } | |
| 360 | - } | |
| 361 | - } | |
| 362 | - | |
| 363 | - return [$isDuplicate, $duplicateErrors]; | |
| 364 | - } | |
| 365 | - | |
| 366 | - /** | |
| 367 | 326 | * Validate that all fields have allowed types. |
| 368 | 327 | * @param Field[]|mixed[] $formFields |
| 369 | 328 | * @throws InvalidArgumentException |
| 370 | 329 | */ |
| @@ -369,24 +328,7 @@ | ||
| 369 | 328 | * @throws InvalidArgumentException |
| 370 | 329 | */ |
| 371 | 330 | public function validateFieldsType(array $formFields): void |
| 372 | 331 | { |
| 373 | - // Validate field types | |
| 374 | - $allowedTypes = FieldType::getAllowedTypes(); | |
| 375 | - foreach ($formFields as $field) { | |
| 376 | - // Check if field is an instance of Field or array | |
| 377 | - $fieldType = $field instanceof Field ? $field->getType() : ($field['type'] ?? null); | |
| 378 | - | |
| 379 | - if ($fieldType === null) { | |
| 380 | - throw new InvalidArgumentException( | |
| 381 | - BackendStrings::getExceptionStrings()['invalid_field_type'] . ' (type not found)' | |
| 382 | - ); | |
| 383 | - } | |
| 384 | - | |
| 385 | - if (!in_array($fieldType, $allowedTypes, true)) { | |
| 386 | - throw new InvalidArgumentException( | |
| 387 | - BackendStrings::getExceptionStrings()['invalid_field_type'] . ' ' . $fieldType | |
| 388 | - ); | |
| 389 | - } | |
| 390 | - } | |
| 332 | + FieldType::validateFields($formFields); | |
| 391 | 333 | } |
| 392 | 334 | } |