PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
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
← All changes | backend/src/Services/Field/FieldService.php +11 -61 0.8.2 → 1.5 View file →
@@ -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,11 +9,11 @@
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;
15 +use IvyForms\Services\Media\ImageService;
21 16
22 17 /**
23 18 * @SuppressWarnings("TooManyPublicMethods")
24 19 */
@@ -25,19 +20,19 @@
25 20 class FieldService
26 21 {
27 22 private FieldRepositoryInterface $fieldRepository;
28 23 private FieldOptionsRepositoryInterface $fieldOptionsRepository;
29 - private EntryFieldRepositoryInterface $entryFieldRepository;
24 + private ImageService $imageService;
30 25
31 26 // Constructor injection for repositories only via PHP-DI
32 27 public function __construct(
33 28 FieldRepositoryInterface $fieldRepository,
34 29 FieldOptionsRepositoryInterface $fieldOptionsRepository,
35 - EntryFieldRepositoryInterface $entryFieldRepository
30 + ImageService $imageService
36 31 ) {
37 32 $this->fieldRepository = $fieldRepository;
38 33 $this->fieldOptionsRepository = $fieldOptionsRepository;
39 - $this->entryFieldRepository = $entryFieldRepository;
34 + $this->imageService = $imageService;
40 35 }
41 36
42 37 /**
43 38 * Create a new field.
@@ -195,8 +190,11 @@
195 190 foreach ($fields as $fieldData) {
196 191 $fieldData['formId'] = $fieldData['formId'] ?? $formId;
197 192 // Resolve parentId using fieldIndex if available
198 193 FieldHelper::resolveParentId($fieldData, $parentMap);
194 + // Process htmlContent / rich text default values to upload base64 images
195 + FieldHelper::processFieldHtmlContent($fieldData, $this->imageService);
196 + FieldHelper::processRichTextDefaultValue($fieldData, $this->imageService);
199 197 $field = FieldFactory::create($fieldData);
200 198 $fieldId = $this->addField($field);
201 199 // If this is a parent field (no parentId), store its DB id in the map
202 200 if ($field->getParentId() === null) {
@@ -226,8 +224,11 @@
226 224 $parentMap = [];
227 225 foreach ($fields as $fieldData) {
228 226 $fieldData['formId'] = $fieldData['formId'] ?? $formId;
229 227 FieldHelper::resolveParentId($fieldData, $parentMap);
228 + // Process htmlContent / rich text default values to upload base64 images
229 + FieldHelper::processFieldHtmlContent($fieldData, $this->imageService);
230 + FieldHelper::processRichTextDefaultValue($fieldData, $this->imageService);
230 231 $field = FieldFactory::create($fieldData);
231 232 $fieldId = $field->getId();
232 233
233 234 if ($fieldId === 0) { // new field
@@ -321,42 +322,8 @@
321 322 }
322 323 }
323 324
324 325 /**
325 - * Check for duplicate field values in submission.
326 - * @param Field[] $formFields
327 - * @param array<string,mixed> $submissionData
328 - * @param int $formId
329 - * @return array{0: bool, 1: array<int|string,bool>}
330 - */
331 - public function checkDuplicateFieldValues(
332 - array $formFields,
333 - array $submissionData,
334 - int $formId
335 - ): array {
336 - $duplicateErrors = [];
337 - $isDuplicate = false;
338 - $fieldValues = [];
339 -
340 - foreach ($formFields as $field) {
341 - $fid = $field->getId();
342 - $fidStr = (string)$fid;
343 - if (isset($submissionData[$fidStr])) {
344 - $fieldValues[$fidStr] = $submissionData[$fidStr];
345 - }
346 - if ($field->getFieldAdvancedSettings()->isNoDuplicates() && !empty($fieldValues[$fidStr])) {
347 - $val = $fieldValues[$fidStr];
348 - if ($this->entryFieldRepository->checkDuplicateValue($formId, $fid, $val)) {
349 - $duplicateErrors[$fidStr] = true;
350 - $isDuplicate = true;
351 - }
352 - }
353 - }
354 -
355 - return [$isDuplicate, $duplicateErrors];
356 - }
357 -
358 - /**
359 326 * Validate that all fields have allowed types.
360 327 * @param Field[]|mixed[] $formFields
361 328 * @throws InvalidArgumentException
362 329 */
@@ -361,24 +328,7 @@
361 328 * @throws InvalidArgumentException
362 329 */
363 330 public function validateFieldsType(array $formFields): void
364 331 {
365 - // Validate field types
366 - $allowedTypes = FieldType::getAllowedTypes();
367 - foreach ($formFields as $field) {
368 - // Check if field is an instance of Field or array
369 - $fieldType = $field instanceof Field ? $field->getType() : ($field['type'] ?? null);
370 -
371 - if ($fieldType === null) {
372 - throw new InvalidArgumentException(
373 - BackendStrings::getExceptionStrings()['invalid_field_type'] . ' (type not found)'
374 - );
375 - }
376 -
377 - if (!in_array($fieldType, $allowedTypes, true)) {
378 - throw new InvalidArgumentException(
379 - BackendStrings::getExceptionStrings()['invalid_field_type'] . ' ' . $fieldType
380 - );
381 - }
382 - }
332 + FieldType::validateFields($formFields);
383 333 }
384 334 }