PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.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
← All changes | backend/src/Common/Helpers/EntryHelper.php +63 -26 1.1 → 1.5.1 View file →
@@ -83,15 +83,22 @@
83 83 ?int $parentId,
84 84 array $submissionData,
85 85 array $parentSubfieldKeys
86 86 ) {
87 - if ($parentId && isset($parentSubfieldKeys[$fieldId])) {
88 - $subKey = $parentSubfieldKeys[$fieldId];
89 - if (isset($submissionData[$parentId][$subKey])) {
90 - return $submissionData[$parentId][$subKey];
91 - }
87 + if (!$parentId || !isset($parentSubfieldKeys[$fieldId])) {
88 + return null;
92 89 }
93 - return null;
90 +
91 + $subKey = $parentSubfieldKeys[$fieldId];
92 + $parentValue = self::decodeSubmissionValue($submissionData[$parentId] ?? []);
93 +
94 + if (!isset($parentValue[$subKey])) {
95 + return null;
96 + }
97 +
98 + $val = $parentValue[$subKey];
99 +
100 + return is_array($val) ? implode(', ', $val) : $val;
94 101 }
95 102
96 103 /**
97 104 * Get the compound field value by combining child field values.
@@ -107,24 +114,44 @@
107 114 array $parentChildrenMap,
108 115 array $parentSubfieldKeys,
109 116 array $submissionData
110 117 ): string {
118 + if (empty($parentChildrenMap[$fieldId])) {
119 + return '';
120 + }
121 +
122 + $parentValue = self::decodeSubmissionValue($submissionData[$fieldId] ?? []);
111 123 $childValues = [];
112 - if (!empty($parentChildrenMap[$fieldId])) {
113 - foreach ($parentChildrenMap[$fieldId] as $childId) {
114 - $subKey = $parentSubfieldKeys[$childId] ?? null;
115 - if ($subKey && isset($submissionData[$fieldId][$subKey])) {
116 - $val = $submissionData[$fieldId][$subKey];
117 - if ($val !== '') {
118 - $childValues[] = $val;
119 - }
120 - }
124 +
125 + foreach ($parentChildrenMap[$fieldId] as $childId) {
126 + $subKey = $parentSubfieldKeys[$childId] ?? null;
127 + if ($subKey !== null && isset($parentValue[$subKey]) && $parentValue[$subKey] !== '') {
128 + $val = $parentValue[$subKey];
129 + $childValues[] = is_array($val) ? implode(', ', $val) : $val;
121 130 }
122 131 }
132 +
123 133 return implode(' ', $childValues);
124 134 }
125 135
126 136 /**
137 + * Decode a submission value into an array.
138 + * Handles JSON strings (e.g., Likert submissions) and ensures an array is returned.
139 + *
140 + * @param mixed $value
141 + * @return array<string, mixed>
142 + */
143 + public static function decodeSubmissionValue($value): array
144 + {
145 + if (is_string($value)) {
146 + $decoded = json_decode($value, true);
147 + return is_array($decoded) ? $decoded : [];
148 + }
149 +
150 + return is_array($value) ? $value : [];
151 + }
152 +
153 + /**
127 154 * Get the default field value from submission data.
128 155 *
129 156 * @param int $fieldId
130 157 * @param array<int|string, mixed> $submissionData
@@ -157,24 +184,34 @@
157 184 * Build subfield key map for each parent.
158 185 *
159 186 * @param array<int, array<int>> $parentChildrenMap
160 187 * @param array<mixed> $submissionData
188 + * @param array<int, object> $formFields
161 189 * @return array<int, string|null>
162 190 */
163 - public static function buildParentSubfieldKeys(array $parentChildrenMap, array $submissionData): array
164 - {
191 + public static function buildParentSubfieldKeys(
192 + array $parentChildrenMap,
193 + array $submissionData,
194 + array $formFields = []
195 + ): array {
196 + $fieldById = [];
197 + foreach ($formFields as $field) {
198 + if (method_exists($field, 'getId')) {
199 + $fieldById[$field->getId()] = $field;
200 + }
201 + }
202 +
165 203 $parentSubfieldKeys = [];
166 204 foreach ($parentChildrenMap as $parentId => $childIds) {
167 - $parentValue = $submissionData[$parentId] ?? [];
168 - // Ensure $parentValue is always an array before calling array_keys().
169 - // Scalar values (e.g. a plain string stored at the parent field index)
170 - // must be treated as an empty subfield map — no subfield keys to extract.
171 - if (!is_array($parentValue)) {
172 - $parentValue = [];
173 - }
205 + $parentValue = self::decodeSubmissionValue($submissionData[$parentId] ?? []);
174 206 $keys = array_keys($parentValue);
175 - foreach ($childIds as $i => $childId) {
176 - $parentSubfieldKeys[$childId] = $keys[$i] ?? null;
207 + $orderedChildIds = NameFieldKeyHelper::sortChildIdsByDisplayOrder($childIds, $fieldById);
208 + foreach ($orderedChildIds as $i => $childId) {
209 + $subKey = NameFieldKeyHelper::resolveStoredNameSubfieldKey($fieldById[$childId] ?? null);
210 + if ($subKey === null) {
211 + $subKey = $keys[$i] ?? null;
212 + }
213 + $parentSubfieldKeys[$childId] = $subKey;
177 214 }
178 215 }
179 216 return $parentSubfieldKeys;
180 217 }