← All changes
|
backend/src/Services/Entry/Managers/EntryFieldValueManager.php
+103
-10
1.0
→
1.5
View file →
| @@ -1,9 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Services\Entry\Managers; |
| 4 | 4 | |
| 5 | -use IvyForms\Common\Helpers\DateTimeHelper\DateTimeHelper; | |
| 5 | +// phpcs:disable PSR1.Files.SideEffects | |
| 6 | +if (!defined('ABSPATH')) { | |
| 7 | + exit; // Exit if accessed directly | |
| 8 | +} | |
| 9 | + | |
| 10 | +use IvyForms\Common\Helpers\DateTimeHelper\DateHelper; | |
| 11 | +use IvyForms\Common\Helpers\DateTimeHelper\TimeHelper; | |
| 6 | 12 | use IvyForms\Common\Helpers\EntryHelper; |
| 7 | 13 | use IvyForms\Services\Field\FieldService; |
| 8 | 14 | |
| 9 | 15 | /** |
| @@ -42,9 +48,9 @@ | ||
| 42 | 48 | $field = $fieldsById[$fieldId]; |
| 43 | 49 | $fieldType = strtolower($field->getType()); |
| 44 | 50 | $value = $entryField['fieldValue']; |
| 45 | 51 | |
| 46 | - $entryField['fieldValue'] = $this->formatFieldValue($fieldType, $value, $skipFormatting); | |
| 52 | + $entryField['fieldValue'] = $this->formatFieldValue($fieldType, $value, $skipFormatting, $field); | |
| 47 | 53 | } |
| 48 | 54 | |
| 49 | 55 | return $entryFields; |
| 50 | 56 | } |
| @@ -120,20 +126,87 @@ | ||
| 120 | 126 | if ($customValue !== null) { |
| 121 | 127 | return $customValue; |
| 122 | 128 | } |
| 123 | 129 | |
| 124 | - if (in_array($fieldType, ['name', 'address'], true) && ($parentId === null || $parentId === 0)) { | |
| 125 | - return $this->getCompoundFieldValue($fieldId, $parentChildrenMap, $parentSubfieldKeys, $submissionData); | |
| 130 | + return $this->resolveByType( | |
| 131 | + $fieldType, | |
| 132 | + $fieldId, | |
| 133 | + $parentId, | |
| 134 | + $submissionData, | |
| 135 | + $parentChildrenMap, | |
| 136 | + $parentSubfieldKeys | |
| 137 | + ); | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * Resolve value based on field type. | |
| 142 | + * | |
| 143 | + * @param string $fieldType | |
| 144 | + * @param int $fieldId | |
| 145 | + * @param int|null $parentId | |
| 146 | + * @param array<mixed> $submissionData | |
| 147 | + * @param array<int, array<int>> $parentChildrenMap | |
| 148 | + * @param array<int, string|null> $parentSubfieldKeys | |
| 149 | + * @return mixed | |
| 150 | + */ | |
| 151 | + private function resolveByType( | |
| 152 | + string $fieldType, | |
| 153 | + int $fieldId, | |
| 154 | + ?int $parentId, | |
| 155 | + array $submissionData, | |
| 156 | + array $parentChildrenMap, | |
| 157 | + array $parentSubfieldKeys | |
| 158 | + ) { | |
| 159 | + $isParent = $parentId === null || $parentId === 0; | |
| 160 | + | |
| 161 | + if ($isParent) { | |
| 162 | + /** | |
| 163 | + * Filters the resolved value for a parent field before compound resolution. | |
| 164 | + * Allows Pro field types (e.g. likert) to resolve parent values without | |
| 165 | + * hardcoding field types in Lite. | |
| 166 | + * | |
| 167 | + * @since 0.1.0 | |
| 168 | + * | |
| 169 | + * @param mixed|null $value The resolved value (null to continue with default processing). | |
| 170 | + * @param string $fieldType The field type. | |
| 171 | + * @param int $fieldId The parent field ID. | |
| 172 | + * @param mixed[] $parentChildrenMap Map of parent to children field relationships. | |
| 173 | + * @param mixed[] $parentSubfieldKeys Map of parent subfield keys. | |
| 174 | + * @param mixed[] $submissionData The submission data array. | |
| 175 | + * @return mixed The resolved parent field value or null to continue default processing. | |
| 176 | + */ | |
| 177 | + $parentValue = apply_filters( | |
| 178 | + 'ivyforms/entry/resolve_parent_field_value', | |
| 179 | + null, | |
| 180 | + $fieldType, | |
| 181 | + $fieldId, | |
| 182 | + $parentChildrenMap, | |
| 183 | + $parentSubfieldKeys, | |
| 184 | + $submissionData | |
| 185 | + ); | |
| 186 | + | |
| 187 | + if ($parentValue !== null) { | |
| 188 | + return $parentValue; | |
| 189 | + } | |
| 126 | 190 | } |
| 127 | 191 | |
| 192 | + if (in_array($fieldType, ['name', 'address'], true) && $isParent) { | |
| 193 | + return $this->resolveParentFieldValue( | |
| 194 | + $fieldId, | |
| 195 | + $parentChildrenMap, | |
| 196 | + $parentSubfieldKeys, | |
| 197 | + $submissionData | |
| 198 | + ); | |
| 199 | + } | |
| 200 | + | |
| 128 | 201 | if ($fieldType === 'time') { |
| 129 | 202 | $value = EntryHelper::getDefaultFieldValue($fieldId, $submissionData); |
| 130 | - return DateTimeHelper::normalizeTimeValue($value); | |
| 203 | + return TimeHelper::normalizeTimeValue($value); | |
| 131 | 204 | } |
| 132 | 205 | |
| 133 | 206 | if ($fieldType === 'date') { |
| 134 | 207 | $value = EntryHelper::getDefaultFieldValue($fieldId, $submissionData); |
| 135 | - return DateTimeHelper::normalizeDateValue($value); | |
| 208 | + return DateHelper::normalizeDateValue($value); | |
| 136 | 209 | } |
| 137 | 210 | |
| 138 | 211 | return EntryHelper::getDefaultFieldValue($fieldId, $submissionData); |
| 139 | 212 | } |
| @@ -138,8 +211,26 @@ | ||
| 138 | 211 | return EntryHelper::getDefaultFieldValue($fieldId, $submissionData); |
| 139 | 212 | } |
| 140 | 213 | |
| 141 | 214 | /** |
| 215 | + * Resolve value for a parent field (name, address). | |
| 216 | + * | |
| 217 | + * @param int $fieldId | |
| 218 | + * @param array<int, array<int>> $parentChildrenMap | |
| 219 | + * @param array<int, string|null> $parentSubfieldKeys | |
| 220 | + * @param array<int|string, mixed> $submissionData | |
| 221 | + * @return string | |
| 222 | + */ | |
| 223 | + private function resolveParentFieldValue( | |
| 224 | + int $fieldId, | |
| 225 | + array $parentChildrenMap, | |
| 226 | + array $parentSubfieldKeys, | |
| 227 | + array $submissionData | |
| 228 | + ): string { | |
| 229 | + return $this->getCompoundFieldValue($fieldId, $parentChildrenMap, $parentSubfieldKeys, $submissionData); | |
| 230 | + } | |
| 231 | + | |
| 232 | + /** | |
| 142 | 233 | * Build a map of field entities indexed by field ID. |
| 143 | 234 | * |
| 144 | 235 | * @param array<int, array<string, mixed>> $entryFields |
| 145 | 236 | * @return array<int, object> |
| @@ -161,21 +252,22 @@ | ||
| 161 | 252 | * |
| 162 | 253 | * @param string $fieldType |
| 163 | 254 | * @param mixed $value |
| 164 | 255 | * @param bool $skipFormatting |
| 256 | + * @param object|null $field | |
| 165 | 257 | * @return mixed |
| 166 | 258 | * @SuppressWarnings(PHPMD) |
| 167 | 259 | */ |
| 168 | - private function formatFieldValue(string $fieldType, $value, bool $skipFormatting = false) | |
| 260 | + private function formatFieldValue(string $fieldType, $value, bool $skipFormatting = false, ?object $field = null) | |
| 169 | 261 | { |
| 170 | 262 | // Convert timestamp to HH:MM format for time fields |
| 171 | 263 | if ($fieldType === 'time' && is_numeric($value)) { |
| 172 | - $value = DateTimeHelper::timestampToTime((int)$value); | |
| 264 | + $value = TimeHelper::timestampToTime((int)$value); | |
| 173 | 265 | } |
| 174 | 266 | |
| 175 | 267 | // Convert timestamp to date format for date fields |
| 176 | 268 | if ($fieldType === 'date' && is_numeric($value)) { |
| 177 | - $value = DateTimeHelper::timestampToDate((int)$value); | |
| 269 | + $value = DateHelper::timestampToDate((int)$value); | |
| 178 | 270 | } |
| 179 | 271 | |
| 180 | 272 | /** |
| 181 | 273 | * Filters the formatted field value for frontend display. |
| @@ -196,11 +288,12 @@ | ||
| 196 | 288 | * @since 0.1.0 |
| 197 | 289 | * |
| 198 | 290 | * @param mixed $value The field value to format. |
| 199 | 291 | * @param bool $skipFormatting Whether the field value should be formatted. |
| 292 | + * @param object|null $field The field entity object. | |
| 200 | 293 | * @return mixed The formatted field value. |
| 201 | 294 | */ |
| 202 | - return apply_filters("ivyforms/entry/field/format_value/{$fieldType}", $value, $skipFormatting); | |
| 295 | + return apply_filters("ivyforms/entry/field/format_value/{$fieldType}", $value, $skipFormatting, $field); | |
| 203 | 296 | } |
| 204 | 297 | |
| 205 | 298 | /** |
| 206 | 299 | * Get the value of a parent subfield. |