| @@ -1,18 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | -* @copyright © TMS-Plugins. All rights reserved. | |
| 4 | +* @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | 5 | * @licence See LICENCE.md for license details. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace IvyForms\ValueObjects\Field; |
| 9 | 9 | |
| 10 | -// phpcs:disable PSR1.Files.SideEffects | |
| 11 | -if (!defined('ABSPATH')) { | |
| 12 | - exit; // Exit if accessed directly | |
| 13 | -} | |
| 14 | - | |
| 15 | 10 | use IvyForms\Common\Exceptions\ValidationException; |
| 16 | 11 | use IvyForms\Services\Translations\BackendStrings; |
| 17 | 12 | |
| 18 | 13 | /** |
| @@ -43,12 +38,28 @@ | ||
| 43 | 38 | * @var int |
| 44 | 39 | */ |
| 45 | 40 | public int $position; |
| 46 | 41 | /** |
| 42 | + * @var int Row index for grid layout (0-based) | |
| 43 | + */ | |
| 44 | + public int $rowIndex; | |
| 45 | + /** | |
| 46 | + * @var int Column index within row (0-based, max 4 for 5 fields per row) | |
| 47 | + */ | |
| 48 | + public int $columnIndex; | |
| 49 | + /** | |
| 50 | + * @var int Field width percentage (20, 25, 33, 50, or 100) | |
| 51 | + */ | |
| 52 | + public int $width; | |
| 53 | + /** | |
| 47 | 54 | * @var int|null |
| 48 | 55 | */ |
| 49 | 56 | public ?int $parentId; |
| 50 | 57 | /** |
| 58 | + * @var string|null Page ID for multi-page forms | |
| 59 | + */ | |
| 60 | + public ?string $pageId; | |
| 61 | + /** | |
| 51 | 62 | * @var FieldGeneralSettings |
| 52 | 63 | */ |
| 53 | 64 | public FieldGeneralSettings $generalSettings; |
| 54 | 65 | /** |
| @@ -89,12 +100,12 @@ | ||
| 89 | 100 | * @param FieldGeneralSettings $generalSettings |
| 90 | 101 | * @param FieldOptions $fieldOptions |
| 91 | 102 | * @param FieldAdvancedSettings $advancedSettings |
| 92 | 103 | * @param int $rows |
| 93 | - * @param bool $confirmFieldEnabled | |
| 94 | - * @param string $confirmFieldLabel | |
| 95 | - * @param string $confirmFieldPlaceholder | |
| 96 | - * @param bool $confirmFieldHideLabel | |
| 104 | + * @param int $rowIndex | |
| 105 | + * @param int $columnIndex | |
| 106 | + * @param int $width | |
| 107 | + * @param string|null $pageId | |
| 97 | 108 | * |
| 98 | 109 | * @throws ValidationException |
| 99 | 110 | */ |
| 100 | 111 | public function __construct( |
| @@ -107,12 +118,12 @@ | ||
| 107 | 118 | FieldOptions $fieldOptions, |
| 108 | 119 | FieldAdvancedSettings $advancedSettings, |
| 109 | 120 | ?int $parentId = null, |
| 110 | 121 | int $rows = 0, |
| 111 | - bool $confirmFieldEnabled = false, | |
| 112 | - string $confirmFieldLabel = '', | |
| 113 | - string $confirmFieldPlaceholder = '', | |
| 114 | - bool $confirmFieldHideLabel = false | |
| 122 | + int $rowIndex = 0, | |
| 123 | + int $columnIndex = 0, | |
| 124 | + int $width = 100, | |
| 125 | + ?string $pageId = null | |
| 115 | 126 | ) { |
| 116 | 127 | $this->id = $this->validateId($id); |
| 117 | 128 | $this->formId = $this->validateId($formId); |
| 118 | 129 | $this->fieldIndex = $this->validateId($fieldIndex); |
| @@ -117,25 +128,17 @@ | ||
| 117 | 128 | $this->formId = $this->validateId($formId); |
| 118 | 129 | $this->fieldIndex = $this->validateId($fieldIndex); |
| 119 | 130 | $this->type = $this->validateString($type, 50, 'type'); |
| 120 | 131 | $this->position = $this->validatePosition($position); |
| 132 | + $this->rowIndex = $this->validateRowIndex($rowIndex); | |
| 133 | + $this->columnIndex = $this->validateColumnIndex($columnIndex); | |
| 134 | + $this->width = $this->validateWidth($width); | |
| 121 | 135 | $this->parentId = $this->validateParentId($parentId); |
| 136 | + $this->pageId = $pageId; | |
| 122 | 137 | $this->generalSettings = $generalSettings; |
| 123 | 138 | $this->fieldOptions = $fieldOptions; |
| 124 | 139 | $this->advancedSettings = $advancedSettings; |
| 125 | 140 | $this->rows = $this->validateRows($rows); |
| 126 | - $this->confirmFieldEnabled = $this->validateBool($confirmFieldEnabled); | |
| 127 | - $this->confirmFieldLabel = $this->validateString( | |
| 128 | - $confirmFieldLabel, | |
| 129 | - 255, | |
| 130 | - 'confirmFieldLabel' | |
| 131 | - ); | |
| 132 | - $this->confirmFieldPlaceholder = $this->validateString( | |
| 133 | - $confirmFieldPlaceholder, | |
| 134 | - 255, | |
| 135 | - 'confirmFieldPlaceholder' | |
| 136 | - ); | |
| 137 | - $this->confirmFieldHideLabel = $this->validateBool($confirmFieldHideLabel); | |
| 138 | 141 | |
| 139 | 142 | /** |
| 140 | 143 | * Allows initialization of additional properties for the Field value object. |
| 141 | 144 | * @since 0.1.0 |
| @@ -268,9 +271,9 @@ | ||
| 268 | 271 | private function validatePosition(int $value): int |
| 269 | 272 | { |
| 270 | 273 | if ($value < 0) { |
| 271 | 274 | throw new ValidationException( |
| 272 | - esc_html__('position must be a non-negative integer.', 'ivyforms') | |
| 275 | + BackendStrings::getExceptionStrings()['position_non_negative'] | |
| 273 | 276 | ); |
| 274 | 277 | } |
| 275 | 278 | return $value; |
| 276 | 279 | } |
| @@ -275,20 +278,65 @@ | ||
| 275 | 278 | return $value; |
| 276 | 279 | } |
| 277 | 280 | |
| 278 | 281 | /** |
| 279 | - * Validates a boolean field. | |
| 282 | + * Validates the row index. | |
| 280 | 283 | * |
| 281 | - * @param bool $value | |
| 284 | + * @param int $value | |
| 282 | 285 | * |
| 283 | - * @return bool | |
| 286 | + * @return int | |
| 287 | + * | |
| 288 | + * @throws ValidationException | |
| 284 | 289 | */ |
| 285 | - private function validateBool(bool $value): bool | |
| 290 | + private function validateRowIndex(int $value): int | |
| 286 | 291 | { |
| 292 | + if ($value < 0) { | |
| 293 | + throw new ValidationException( | |
| 294 | + BackendStrings::getExceptionStrings()['row_index_non_negative'] | |
| 295 | + ); | |
| 296 | + } | |
| 287 | 297 | return $value; |
| 288 | 298 | } |
| 289 | 299 | |
| 290 | 300 | /** |
| 301 | + * Validates the column index (0-4 for max 5 fields per row). | |
| 302 | + * | |
| 303 | + * @param int $value | |
| 304 | + * | |
| 305 | + * @return int | |
| 306 | + * | |
| 307 | + * @throws ValidationException | |
| 308 | + */ | |
| 309 | + private function validateColumnIndex(int $value): int | |
| 310 | + { | |
| 311 | + if ($value < 0 || $value > 4) { | |
| 312 | + throw new ValidationException( | |
| 313 | + BackendStrings::getExceptionStrings()['column_index_range'] | |
| 314 | + ); | |
| 315 | + } | |
| 316 | + return $value; | |
| 317 | + } | |
| 318 | + | |
| 319 | + /** | |
| 320 | + * Validates the width percentage. | |
| 321 | + * | |
| 322 | + * @param int $value | |
| 323 | + * | |
| 324 | + * @return int | |
| 325 | + * | |
| 326 | + * @throws ValidationException | |
| 327 | + */ | |
| 328 | + private function validateWidth(int $value): int | |
| 329 | + { | |
| 330 | + if ($value < 20 || $value > 100) { | |
| 331 | + throw new ValidationException( | |
| 332 | + BackendStrings::getExceptionStrings()['width_range'] | |
| 333 | + ); | |
| 334 | + } | |
| 335 | + return $value; | |
| 336 | + } | |
| 337 | + | |
| 338 | + /** | |
| 291 | 339 | * Validate rows. |
| 292 | 340 | * |
| 293 | 341 | * @param int $rows |
| 294 | 342 | * @return int |
| @@ -297,9 +345,9 @@ | ||
| 297 | 345 | private function validateRows(int $rows): int |
| 298 | 346 | { |
| 299 | 347 | if ($rows < 0) { |
| 300 | 348 | throw new ValidationException( |
| 301 | - esc_html__('rows must be at least 1.', 'ivyforms') | |
| 349 | + BackendStrings::getExceptionStrings()['rows_non_negative'] | |
| 302 | 350 | ); |
| 303 | 351 | } |
| 304 | 352 | return $rows; |
| 305 | 353 | } |
| @@ -354,8 +402,38 @@ | ||
| 354 | 402 | return $this->position; |
| 355 | 403 | } |
| 356 | 404 | |
| 357 | 405 | /** |
| 406 | + * Get the row index. | |
| 407 | + * | |
| 408 | + * @return int | |
| 409 | + */ | |
| 410 | + public function getRowIndex(): int | |
| 411 | + { | |
| 412 | + return $this->rowIndex; | |
| 413 | + } | |
| 414 | + | |
| 415 | + /** | |
| 416 | + * Get the column index. | |
| 417 | + * | |
| 418 | + * @return int | |
| 419 | + */ | |
| 420 | + public function getColumnIndex(): int | |
| 421 | + { | |
| 422 | + return $this->columnIndex; | |
| 423 | + } | |
| 424 | + | |
| 425 | + /** | |
| 426 | + * Get the field width percentage. | |
| 427 | + * | |
| 428 | + * @return int | |
| 429 | + */ | |
| 430 | + public function getWidth(): int | |
| 431 | + { | |
| 432 | + return $this->width; | |
| 433 | + } | |
| 434 | + | |
| 435 | + /** | |
| 358 | 436 | * Get the parent field id. |
| 359 | 437 | * |
| 360 | 438 | * @return int|null |
| 361 | 439 | */ |
| @@ -402,88 +480,8 @@ | ||
| 402 | 480 | $this->rows = $rows; |
| 403 | 481 | } |
| 404 | 482 | |
| 405 | 483 | /** |
| 406 | - * Check if the confirm field feature is enabled. | |
| 407 | - * | |
| 408 | - * @return bool | |
| 409 | - */ | |
| 410 | - public function isConfirmFieldEnabled(): bool | |
| 411 | - { | |
| 412 | - return $this->confirmFieldEnabled; | |
| 413 | - } | |
| 414 | - | |
| 415 | - /** | |
| 416 | - * Get the confirm field label. | |
| 417 | - * | |
| 418 | - * @return string | |
| 419 | - */ | |
| 420 | - public function getConfirmFieldLabel(): string | |
| 421 | - { | |
| 422 | - return $this->confirmFieldLabel; | |
| 423 | - } | |
| 424 | - | |
| 425 | - /** | |
| 426 | - * Get the confirm field placeholder. | |
| 427 | - * | |
| 428 | - * @return string | |
| 429 | - */ | |
| 430 | - public function getConfirmFieldPlaceholder(): string | |
| 431 | - { | |
| 432 | - return $this->confirmFieldPlaceholder; | |
| 433 | - } | |
| 434 | - | |
| 435 | - /** | |
| 436 | - * Check if the confirm field label should be hidden. | |
| 437 | - * | |
| 438 | - * @return bool | |
| 439 | - */ | |
| 440 | - public function isConfirmFieldHideLabel(): bool | |
| 441 | - { | |
| 442 | - return $this->confirmFieldHideLabel; | |
| 443 | - } | |
| 444 | - | |
| 445 | - /** | |
| 446 | - * Set the confirm field feature enabled/disabled. | |
| 447 | - * | |
| 448 | - * @param bool $enabled | |
| 449 | - */ | |
| 450 | - public function setConfirmFieldEnabled(bool $enabled): void | |
| 451 | - { | |
| 452 | - $this->confirmFieldEnabled = $enabled; | |
| 453 | - } | |
| 454 | - | |
| 455 | - /** | |
| 456 | - * Set the confirm field label. | |
| 457 | - * | |
| 458 | - * @param string $label | |
| 459 | - */ | |
| 460 | - public function setConfirmFieldLabel(string $label): void | |
| 461 | - { | |
| 462 | - $this->confirmFieldLabel = $label; | |
| 463 | - } | |
| 464 | - | |
| 465 | - /** | |
| 466 | - * Set the confirm field placeholder. | |
| 467 | - * | |
| 468 | - * @param string $ph | |
| 469 | - */ | |
| 470 | - public function setConfirmFieldPlaceholder(string $ph): void | |
| 471 | - { | |
| 472 | - $this->confirmFieldPlaceholder = $ph; | |
| 473 | - } | |
| 474 | - | |
| 475 | - /** | |
| 476 | - * Set the confirm field label visibility. | |
| 477 | - * | |
| 478 | - * @param bool $hide | |
| 479 | - */ | |
| 480 | - public function setConfirmFieldHideLabel(bool $hide): void | |
| 481 | - { | |
| 482 | - $this->confirmFieldHideLabel = $hide; | |
| 483 | - } | |
| 484 | - | |
| 485 | - /** | |
| 486 | 484 | * Get the general settings object. |
| 487 | 485 | * |
| 488 | 486 | * @return FieldAdvancedSettings |
| 489 | 487 | */ |
| @@ -504,16 +502,15 @@ | ||
| 504 | 502 | 'formId' => $this->formId, |
| 505 | 503 | 'fieldIndex' => $this->fieldIndex, |
| 506 | 504 | 'type' => $this->type, |
| 507 | 505 | 'position' => $this->position, |
| 506 | + 'rowIndex' => $this->rowIndex, | |
| 507 | + 'columnIndex' => $this->columnIndex, | |
| 508 | + 'width' => $this->width, | |
| 508 | 509 | 'parentId' => $this->parentId, |
| 509 | 510 | 'generalSettings' => $this->generalSettings->toArray(), |
| 510 | 511 | 'fieldOptions' => $this->fieldOptions->toArray(), |
| 511 | 512 | 'rows' => $this->rows, |
| 512 | - 'confirmFieldEnabled' => $this->confirmFieldEnabled, | |
| 513 | - 'confirmFieldLabel' => $this->confirmFieldLabel, | |
| 514 | - 'confirmFieldPlaceholder' => $this->confirmFieldPlaceholder, | |
| 515 | - 'confirmFieldHideLabel' => $this->confirmFieldHideLabel, | |
| 516 | 513 | // Advanced options |
| 517 | 514 | 'advancedSettings' => $this->advancedSettings->toArray() |
| 518 | 515 | ]; |
| 519 | 516 | |