PluginProbe
The Innovative Form Builder – IvyForms / 0.8.1
The Innovative Form Builder – IvyForms v0.8.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 / Common / Helpers / FieldHelper.php

FieldHelper.php in The Innovative Form Builder – IvyForms 0.8.1, at backend/src/Common/Helpers/FieldHelper.php

122 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Common\Helpers;
4
5 // phpcs:disable PSR1.Files.SideEffects
6 if (!defined('ABSPATH')) {
7 exit; // Exit if accessed directly
8 }
9
10 use IvyForms\Common\Exceptions\ValidationException;
11 use IvyForms\Entity\FieldOptions\FieldOptions;
12 use IvyForms\Factory\FieldOptions\FieldOptionsFactory;
13 use IvyForms\Repository\FieldOptions\FieldOptionsRepositoryInterface;
14
15 /**
16 * General helper utilities related to Field processing.
17 */
18 class FieldHelper
19 {
20 /**
21 * Resolve parentId using fieldIndex -> parentId map if needed.
22 *
23 * @param array<string,mixed> $data Passed by reference; parentId modified in place.
24 * @param array<int,int> $parentMap Map of fieldIndex => newly created parent field id.
25 */
26 public static function resolveParentId(array &$data, array $parentMap): void
27 {
28 if (!array_key_exists('parentId', $data)) {
29 return;
30 }
31
32 $parentId = $data['parentId'];
33 $needsResolve = ($parentId === 0 || $parentId === '0' || $parentId === null);
34 if ($needsResolve && isset($data['fieldIndex']) && isset($parentMap[$data['fieldIndex']])) {
35 $data['parentId'] = $parentMap[$data['fieldIndex']];
36 }
37 }
38
39 /**
40 * Remap parentId for duplicated field using old-to-new field ID map.
41 *
42 * @param array<string,mixed> $fieldData Passed by reference; parentId modified in place.
43 * @param array<int,int> $fieldIdMap Map of old field ID => new field ID.
44 */
45 public static function remapParentIdForDuplication(array &$fieldData, array $fieldIdMap): void
46 {
47 if ($fieldData['parentId'] !== null && isset($fieldIdMap[$fieldData['parentId']])) {
48 $fieldData['parentId'] = $fieldIdMap[$fieldData['parentId']];
49 }
50 }
51
52 /**
53 * Partition field options into new, update, and submitted IDs.
54 *
55 * @param array<string, mixed> $fieldData
56 * @param array<int> $existingOptionIds
57 * @return array{
58 * newOptions: array<int, FieldOptions>,
59 * updateOptions: array<int, FieldOptions>,
60 * submittedOptionIds: array<int, int>
61 * }
62 * @throws ValidationException
63 */
64 public static function partitionFieldOptions(array $fieldData, array $existingOptionIds): array
65 {
66 $newOptions = [];
67 $updateOptions = [];
68 $submittedOptionIds = [];
69 foreach ($fieldData['fieldOptions'] as $optionData) {
70 $option = FieldOptionsFactory::create($optionData);
71 if (
72 isset($optionData['id']) && $optionData['id'] > 0
73 && in_array($optionData['id'], $existingOptionIds, true)
74 ) {
75 $updateOptions[] = $option;
76 $submittedOptionIds[] = $optionData['id'];
77 continue;
78 }
79 $newOptions[] = $option;
80 }
81 return [
82 'newOptions' => $newOptions,
83 'updateOptions' => $updateOptions,
84 'submittedOptionIds' => $submittedOptionIds,
85 ];
86 }
87
88 /**
89 * Check if a field type has options (radio, checkbox, select, multi-select).
90 *
91 * @param string $fieldType
92 * @return bool
93 */
94 public static function fieldTypeHasOptions(string $fieldType): bool
95 {
96 return in_array($fieldType, ['radio', 'checkbox', 'select', 'multi-select'], true);
97 }
98
99 /**
100 * Duplicate field options from one field to another.
101 *
102 * @param int $oldFieldId
103 * @param int $newFieldId
104 * @param FieldOptionsRepositoryInterface $repository
105 * @return void
106 * @throws ValidationException
107 */
108 public static function duplicateFieldOptions(
109 int $oldFieldId,
110 int $newFieldId,
111 FieldOptionsRepositoryInterface $repository
112 ): void {
113 $options = $repository->getByFieldId($oldFieldId);
114 foreach ($options as $option) {
115 $optionData = $option->toArray();
116 unset($optionData['id']);
117 $optionData['fieldId'] = $newFieldId;
118 $repository->add(FieldOptionsFactory::create($optionData));
119 }
120 }
121 }
122