| @@ -2,13 +2,15 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Factory\Field; |
| 4 | 4 | |
| 5 | 5 | use IvyForms\Common\Exceptions\ValidationException; |
| 6 | +use IvyForms\Common\Helpers\NameFieldKeyHelper; | |
| 6 | 7 | use IvyForms\Entity\Field\Field as FieldEntity; |
| 7 | 8 | use IvyForms\ValueObjects\Field\Field; |
| 8 | 9 | use IvyForms\ValueObjects\Field\FieldGeneralSettings; |
| 9 | 10 | use IvyForms\ValueObjects\Field\FieldOptions; |
| 10 | 11 | use IvyForms\ValueObjects\Field\FieldAdvancedSettings; |
| 12 | +use IvyForms\ValueObjects\Field\FileUploadFieldAdvancedSettings; | |
| 11 | 13 | use IvyForms\ValueObjects\Field\ConfirmationGeneralSettings; |
| 12 | 14 | use IvyForms\ValueObjects\Field\TimeFieldGeneralSettings; |
| 13 | 15 | use IvyForms\ValueObjects\Field\DateFieldGeneralSettings; |
| 14 | 16 | |
| @@ -27,9 +29,9 @@ | ||
| 27 | 29 | * @throws ValidationException |
| 28 | 30 | */ |
| 29 | 31 | public static function create(array $data): FieldEntity |
| 30 | 32 | { |
| 31 | - $settings = isset($data['settings']) ? json_decode($data['settings'], true) : []; | |
| 33 | + $settings = self::parseSettings($data['settings'] ?? null); | |
| 32 | 34 | |
| 33 | 35 | if (!empty($settings)) { |
| 34 | 36 | $data['hideLabel'] = $settings['hideLabel'] ?? false; |
| 35 | 37 | $data['readOnly'] = $settings['readOnly'] ?? false; |
| @@ -53,8 +55,9 @@ | ||
| 53 | 55 | $data['phoneAutoDetect'] = $settings['phoneAutoDetect'] ?? false; |
| 54 | 56 | $data['minValue'] = $settings['minValue'] ?? null; |
| 55 | 57 | $data['maxValue'] = $settings['maxValue'] ?? null; |
| 56 | 58 | $data['step'] = $settings['step'] ?? 1; |
| 59 | + $data['showGrid'] = $settings['showGrid'] ?? false; | |
| 57 | 60 | $data['numberFormat'] = $settings['numberFormat'] ?? ''; |
| 58 | 61 | $data['inputPrefix'] = $settings['inputPrefix'] ?? ''; |
| 59 | 62 | $data['inputSuffix'] = $settings['inputSuffix'] ?? ''; |
| 60 | 63 | $data['timeFieldType'] = $settings['timeFieldType'] ?? ''; |
| @@ -67,10 +70,18 @@ | ||
| 67 | 70 | $data['maxDateValue'] = $settings['maxDateValue'] ?? ''; |
| 68 | 71 | $data['showRatingText'] = $settings['showRatingText'] ?? false; |
| 69 | 72 | $data['ratingIcon'] = $settings['ratingIcon'] ?? 'star'; |
| 70 | 73 | $data['htmlContent'] = $settings['htmlContent'] ?? ''; |
| 74 | + $data['agreementText'] = $settings['agreementText'] ?? ''; | |
| 71 | 75 | $data['limitDateRange'] = $settings['limitDateRange'] ?? false; |
| 72 | 76 | $data['limitTimeRange'] = $settings['limitTimeRange'] ?? false; |
| 77 | + $data['maxFileSizeMb'] = isset($settings['maxFileSizeMb']) ? (int) $settings['maxFileSizeMb'] : 0; | |
| 78 | + $data['saveUploadsTo'] = isset($settings['saveUploadsTo']) | |
| 79 | + ? (string) $settings['saveUploadsTo'] | |
| 80 | + : 'ivyforms'; | |
| 81 | + if (!array_key_exists('allowedFileExtensions', $data) && isset($settings['allowedFileExtensions'])) { | |
| 82 | + $data['allowedFileExtensions'] = $settings['allowedFileExtensions']; | |
| 83 | + } | |
| 73 | 84 | // Grid layout: prioritize direct database columns over settings JSON |
| 74 | 85 | // This ensures backward compatibility while preferring the dedicated columns |
| 75 | 86 | $data['rowIndex'] = $data['rowIndex'] ?? ($settings['rowIndex'] ?? 0); |
| 76 | 87 | $data['columnIndex'] = $data['columnIndex'] ?? ($settings['columnIndex'] ?? 0); |
| @@ -80,10 +91,34 @@ | ||
| 80 | 91 | : true; |
| 81 | 92 | if ($data['visible'] === null) { |
| 82 | 93 | $data['visible'] = true; |
| 83 | 94 | } |
| 95 | + if (isset($settings['nameFieldType'])) { | |
| 96 | + $data['nameFieldType'] = (string)$settings['nameFieldType']; | |
| 97 | + } | |
| 98 | + if (array_key_exists('subFieldIndex', $settings)) { | |
| 99 | + $data['subFieldIndex'] = is_numeric($settings['subFieldIndex']) | |
| 100 | + ? (int)$settings['subFieldIndex'] | |
| 101 | + : null; | |
| 102 | + } | |
| 84 | 103 | } |
| 85 | 104 | |
| 105 | + if (isset($data['nameFieldType'])) { | |
| 106 | + $data['nameFieldType'] = (string)$data['nameFieldType']; | |
| 107 | + } | |
| 108 | + if (array_key_exists('subFieldIndex', $data)) { | |
| 109 | + $data['subFieldIndex'] = is_numeric($data['subFieldIndex']) | |
| 110 | + ? (int)$data['subFieldIndex'] | |
| 111 | + : null; | |
| 112 | + } | |
| 113 | + | |
| 114 | + if (empty($data['nameFieldType'])) { | |
| 115 | + $inferredNameFieldType = NameFieldKeyHelper::inferNameFieldKeyFromLabel($data['label'] ?? ''); | |
| 116 | + if ($inferredNameFieldType !== null) { | |
| 117 | + $data['nameFieldType'] = $inferredNameFieldType; | |
| 118 | + } | |
| 119 | + } | |
| 120 | + | |
| 86 | 121 | /** |
| 87 | 122 | * Allows modification of the settings data before creating the Field entity. |
| 88 | 123 | * This filter runs for both: |
| 89 | 124 | * - Fields loaded from database (settings from JSON string) |
| @@ -94,8 +129,9 @@ | ||
| 94 | 129 | * @param mixed[] $settings The settings array (from database or empty for API). |
| 95 | 130 | * @return mixed[] The modified field data. |
| 96 | 131 | */ |
| 97 | 132 | $data = apply_filters('ivyforms/field/factory/extract_settings', $data, $settings); |
| 133 | + | |
| 98 | 134 | $generalSettings = new FieldGeneralSettings( |
| 99 | 135 | self::validateString($data['label'] ?? '', 255, 'label'), |
| 100 | 136 | self::validateBool($data['required'] ?? false), |
| 101 | 137 | self::validateString($data['placeholder'] ?? '', 255, 'placeholder'), |
| @@ -111,8 +147,9 @@ | ||
| 111 | 147 | self::validateBool($data['phoneAutoDetect'] ?? false), |
| 112 | 148 | self::validateNumberValue($data['minValue'] ?? null, 'minValue'), |
| 113 | 149 | self::validateNumberValue($data['maxValue'] ?? null, 'maxValue'), |
| 114 | 150 | self::validateStep($data['step'] ?? 1), |
| 151 | + self::validateBool($data['showGrid'] ?? false), | |
| 115 | 152 | self::validateNumberFormat($data['numberFormat'] ?? ''), |
| 116 | 153 | new ConfirmationGeneralSettings( |
| 117 | 154 | self::validateBool($data['confirmFieldEnabled'] ?? false), |
| 118 | 155 | self::validateString( |
| @@ -147,12 +184,13 @@ | ||
| 147 | 184 | ), |
| 148 | 185 | self::validateBool($data['showRatingText'] ?? false), |
| 149 | 186 | self::validateBool($data['visible'] ?? true), |
| 150 | 187 | self::validateString($data['htmlContent'] ?? '', 4294967295, 'htmlContent'), |
| 188 | + self::validateString($data['agreementText'] ?? '', 1000, 'agreementText'), | |
| 151 | 189 | self::validateBool($data['limitRange'] ?? false), |
| 152 | 190 | ); |
| 153 | 191 | $advancedSettings = new FieldAdvancedSettings( |
| 154 | - self::validateString($data['defaultValue'] ?? '', 255, 'defaultValue'), | |
| 192 | + self::resolveDefaultValue($data), | |
| 155 | 193 | self::validateBool($data['limitMaxLength'] ?? false), |
| 156 | 194 | (int)($data['maxLength'] ?? 255), |
| 157 | 195 | self::validateString($data['labelPosition'] ?? '', 255, 'labelPosition'), |
| 158 | 196 | self::validateBool($data['noDuplicates'] ?? false), |
| @@ -157,9 +195,16 @@ | ||
| 157 | 195 | self::validateString($data['labelPosition'] ?? '', 255, 'labelPosition'), |
| 158 | 196 | self::validateBool($data['noDuplicates'] ?? false), |
| 159 | 197 | self::validateString($data['inputPrefix'] ?? '', 255, 'inputPrefix'), |
| 160 | 198 | self::validateString($data['inputSuffix'] ?? '', 255, 'inputSuffix'), |
| 161 | - self::validateString($data['ratingIcon'] ?? 'star', 50, 'ratingIcon') | |
| 199 | + self::validateString($data['ratingIcon'] ?? 'star', 50, 'ratingIcon'), | |
| 200 | + new FileUploadFieldAdvancedSettings( | |
| 201 | + (int) ($data['maxFileSizeMb'] ?? 0), | |
| 202 | + (string) ($data['saveUploadsTo'] ?? 'ivyforms'), | |
| 203 | + is_array($data['allowedFileExtensions'] ?? null) | |
| 204 | + ? $data['allowedFileExtensions'] | |
| 205 | + : [] | |
| 206 | + ) | |
| 162 | 207 | ); |
| 163 | 208 | $fieldOptions = new FieldOptions($data['options'] ?? []); |
| 164 | 209 | |
| 165 | 210 | $fieldObject = new Field( |
| @@ -174,9 +219,10 @@ | ||
| 174 | 219 | $data['parentId'] ?? null, |
| 175 | 220 | $data['rows'] ?? 0, |
| 176 | 221 | $data['rowIndex'] ?? 0, |
| 177 | 222 | $data['columnIndex'] ?? 0, |
| 178 | - $data['width'] ?? 100 | |
| 223 | + $data['width'] ?? 100, | |
| 224 | + $data['pageId'] ?? null | |
| 179 | 225 | ); |
| 180 | 226 | |
| 181 | 227 | /** |
| 182 | 228 | * Allows setting of additional properties for the Field value object. |
| @@ -213,8 +259,12 @@ | ||
| 213 | 259 | if (isset($data['parentId'])) { |
| 214 | 260 | $field->setParentId($data['parentId']); |
| 215 | 261 | } |
| 216 | 262 | |
| 263 | + if (isset($data['pageId'])) { | |
| 264 | + $field->setPageId($data['pageId']); | |
| 265 | + } | |
| 266 | + | |
| 217 | 267 | if (isset($data['rows'])) { |
| 218 | 268 | $field->setRows((int)$data['rows']); |
| 219 | 269 | } |
| 220 | 270 | |
| @@ -225,12 +275,40 @@ | ||
| 225 | 275 | * @param FieldEntity $field The current field data. |
| 226 | 276 | * @param mixed[] $data The settings array from the database. |
| 227 | 277 | * @return mixed[] The modified field data. |
| 228 | 278 | */ |
| 229 | - return apply_filters('ivyforms/field/entity/set_properties', $field, $data); | |
| 279 | + $field = apply_filters('ivyforms/field/entity/set_properties', $field, $data); | |
| 280 | + | |
| 281 | + if (!empty($data['nameFieldType'])) { | |
| 282 | + $field->setAdditionalProperty('nameFieldType', (string)$data['nameFieldType']); | |
| 283 | + } | |
| 284 | + if (array_key_exists('subFieldIndex', $data) && $data['subFieldIndex'] !== null) { | |
| 285 | + $field->setAdditionalProperty('subFieldIndex', (int)$data['subFieldIndex']); | |
| 286 | + } | |
| 287 | + | |
| 288 | + return $field; | |
| 230 | 289 | } |
| 231 | 290 | |
| 232 | 291 | /** |
| 292 | + * @param mixed $rawSettings | |
| 293 | + * @return array<string, mixed> | |
| 294 | + */ | |
| 295 | + private static function parseSettings($rawSettings): array | |
| 296 | + { | |
| 297 | + if (is_array($rawSettings)) { | |
| 298 | + return $rawSettings; | |
| 299 | + } | |
| 300 | + | |
| 301 | + if (!is_string($rawSettings) || $rawSettings === '') { | |
| 302 | + return []; | |
| 303 | + } | |
| 304 | + | |
| 305 | + $decoded = json_decode($rawSettings, true); | |
| 306 | + | |
| 307 | + return is_array($decoded) ? $decoded : []; | |
| 308 | + } | |
| 309 | + | |
| 310 | + /** | |
| 233 | 311 | * Validate and convert a value to boolean. |
| 234 | 312 | * |
| 235 | 313 | * @param mixed $value The value to validate. |
| 236 | 314 | * |
| @@ -238,8 +316,33 @@ | ||
| 238 | 316 | */ |
| 239 | 317 | private static function validateBool($value): bool |
| 240 | 318 | { |
| 241 | 319 | return (bool)$value; |
| 320 | + } | |
| 321 | + | |
| 322 | + /** | |
| 323 | + * Resolve and validate field default value (Pro may override for rich_text, etc.). | |
| 324 | + * | |
| 325 | + * @param array<string, mixed> $data | |
| 326 | + * | |
| 327 | + * @return string | |
| 328 | + * | |
| 329 | + * @throws ValidationException | |
| 330 | + */ | |
| 331 | + private static function resolveDefaultValue(array $data): string | |
| 332 | + { | |
| 333 | + $filtered = apply_filters( | |
| 334 | + 'ivyforms/field/validate_default_value', | |
| 335 | + null, | |
| 336 | + $data['defaultValue'] ?? '', | |
| 337 | + $data | |
| 338 | + ); | |
| 339 | + | |
| 340 | + if (is_string($filtered)) { | |
| 341 | + return $filtered; | |
| 342 | + } | |
| 343 | + | |
| 344 | + return self::validateString($data['defaultValue'] ?? '', 255, 'defaultValue'); | |
| 242 | 345 | } |
| 243 | 346 | |
| 244 | 347 | /** |
| 245 | 348 | * Validate a string's length. |