| @@ -1,23 +1,15 @@ | ||
| 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 | -use IvyForms\ValueObjects\Field\FieldGeneralSettings; | |
| 18 | -use IvyForms\ValueObjects\Field\FieldOptions; | |
| 19 | -use IvyForms\ValueObjects\Field\FieldAdvancedSettings; | |
| 20 | 12 | |
| 21 | 13 | /** |
| 22 | 14 | * Class Field |
| 23 | 15 | * |
| @@ -46,12 +38,28 @@ | ||
| 46 | 38 | * @var int |
| 47 | 39 | */ |
| 48 | 40 | public int $position; |
| 49 | 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 | + /** | |
| 50 | 54 | * @var int|null |
| 51 | 55 | */ |
| 52 | 56 | public ?int $parentId; |
| 53 | 57 | /** |
| 58 | + * @var string|null Page ID for multi-page forms | |
| 59 | + */ | |
| 60 | + public ?string $pageId; | |
| 61 | + /** | |
| 54 | 62 | * @var FieldGeneralSettings |
| 55 | 63 | */ |
| 56 | 64 | public FieldGeneralSettings $generalSettings; |
| 57 | 65 | /** |
| @@ -73,8 +81,12 @@ | ||
| 73 | 81 | /** |
| 74 | 82 | * @var FieldAdvancedSettings |
| 75 | 83 | */ |
| 76 | 84 | public FieldAdvancedSettings $advancedSettings; |
| 85 | + /** | |
| 86 | + * @var array<string, mixed> Additional properties from Pro plugin | |
| 87 | + */ | |
| 88 | + private array $additionalProperties = []; | |
| 77 | 89 | |
| 78 | 90 | /** |
| 79 | 91 | * Field constructor. |
| 80 | 92 | * @SuppressWarnings(PHPMD) |
| @@ -86,14 +98,14 @@ | ||
| 86 | 98 | * @param int $position |
| 87 | 99 | * @param int|null $parentId |
| 88 | 100 | * @param FieldGeneralSettings $generalSettings |
| 89 | 101 | * @param FieldOptions $fieldOptions |
| 102 | + * @param FieldAdvancedSettings $advancedSettings | |
| 90 | 103 | * @param int $rows |
| 91 | - * @param bool $confirmFieldEnabled | |
| 92 | - * @param string $confirmFieldLabel | |
| 93 | - * @param string $confirmFieldPlaceholder | |
| 94 | - * @param bool $confirmFieldHideLabel | |
| 95 | - * @param FieldAdvancedSettings $advancedSettings | |
| 104 | + * @param int $rowIndex | |
| 105 | + * @param int $columnIndex | |
| 106 | + * @param int $width | |
| 107 | + * @param string|null $pageId | |
| 96 | 108 | * |
| 97 | 109 | * @throws ValidationException |
| 98 | 110 | */ |
| 99 | 111 | public function __construct( |
| @@ -101,17 +113,17 @@ | ||
| 101 | 113 | int $formId, |
| 102 | 114 | int $fieldIndex, |
| 103 | 115 | string $type, |
| 104 | 116 | int $position, |
| 105 | - ?int $parentId = null, | |
| 106 | 117 | FieldGeneralSettings $generalSettings, |
| 107 | 118 | FieldOptions $fieldOptions, |
| 108 | 119 | FieldAdvancedSettings $advancedSettings, |
| 120 | + ?int $parentId = null, | |
| 109 | 121 | int $rows = 0, |
| 110 | - bool $confirmFieldEnabled = false, | |
| 111 | - string $confirmFieldLabel = '', | |
| 112 | - string $confirmFieldPlaceholder = '', | |
| 113 | - bool $confirmFieldHideLabel = false | |
| 122 | + int $rowIndex = 0, | |
| 123 | + int $columnIndex = 0, | |
| 124 | + int $width = 100, | |
| 125 | + ?string $pageId = null | |
| 114 | 126 | ) { |
| 115 | 127 | $this->id = $this->validateId($id); |
| 116 | 128 | $this->formId = $this->validateId($formId); |
| 117 | 129 | $this->fieldIndex = $this->validateId($fieldIndex); |
| @@ -116,24 +128,78 @@ | ||
| 116 | 128 | $this->formId = $this->validateId($formId); |
| 117 | 129 | $this->fieldIndex = $this->validateId($fieldIndex); |
| 118 | 130 | $this->type = $this->validateString($type, 50, 'type'); |
| 119 | 131 | $this->position = $this->validatePosition($position); |
| 132 | + $this->rowIndex = $this->validateRowIndex($rowIndex); | |
| 133 | + $this->columnIndex = $this->validateColumnIndex($columnIndex); | |
| 134 | + $this->width = $this->validateWidth($width); | |
| 120 | 135 | $this->parentId = $this->validateParentId($parentId); |
| 136 | + $this->pageId = $pageId; | |
| 121 | 137 | $this->generalSettings = $generalSettings; |
| 122 | 138 | $this->fieldOptions = $fieldOptions; |
| 123 | 139 | $this->advancedSettings = $advancedSettings; |
| 124 | 140 | $this->rows = $this->validateRows($rows); |
| 125 | - $this->confirmFieldEnabled = $this->validateBool($confirmFieldEnabled); | |
| 126 | - $this->confirmFieldLabel = $this->validateString($confirmFieldLabel, 255, 'confirmFieldLabel'); | |
| 127 | - $this->confirmFieldPlaceholder = $this->validateString( | |
| 128 | - $confirmFieldPlaceholder, | |
| 129 | - 255, | |
| 130 | - 'confirmFieldPlaceholder' | |
| 131 | - ); | |
| 132 | - $this->confirmFieldHideLabel = $this->validateBool($confirmFieldHideLabel); | |
| 141 | + | |
| 142 | + /** | |
| 143 | + * Allows initialization of additional properties for the Field value object. | |
| 144 | + * @since 0.1.0 | |
| 145 | + * | |
| 146 | + * @param mixed[] $additionalProperties The current list of additional properties. | |
| 147 | + * @param Field $field The Field value object instance. | |
| 148 | + * @return mixed[] The modified list of additional properties. | |
| 149 | + */ | |
| 150 | + $this->additionalProperties = | |
| 151 | + apply_filters('ivyforms/field/value_object/init_additional_properties', [], $this); | |
| 133 | 152 | } |
| 134 | 153 | |
| 135 | 154 | /** |
| 155 | + * Set an additional property | |
| 156 | + * | |
| 157 | + * @param string $key | |
| 158 | + * @param mixed $value | |
| 159 | + * | |
| 160 | + * @return void | |
| 161 | + */ | |
| 162 | + public function setAdditionalProperty(string $key, $value): void | |
| 163 | + { | |
| 164 | + foreach ($this->additionalProperties as &$property) { | |
| 165 | + if (array_key_exists($key, $property)) { | |
| 166 | + $property[$key] = $value; // update existing | |
| 167 | + return; | |
| 168 | + } | |
| 169 | + } | |
| 170 | + | |
| 171 | + // if not found, append new key-value pair | |
| 172 | + $this->additionalProperties[] = [$key => $value]; | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * Get an additional property | |
| 177 | + * | |
| 178 | + * @param string $key | |
| 179 | + * @return mixed|null The value of the additional property, or null if not found | |
| 180 | + */ | |
| 181 | + public function getAdditionalProperty(string $key) | |
| 182 | + { | |
| 183 | + foreach ($this->additionalProperties as $property) { | |
| 184 | + if (array_key_exists($key, $property)) { | |
| 185 | + return $property[$key]; | |
| 186 | + } | |
| 187 | + } | |
| 188 | + return null; | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Get all additional properties | |
| 193 | + * | |
| 194 | + * @return array<string, mixed> The additional properties | |
| 195 | + */ | |
| 196 | + public function getAllAdditionalProperties(): array | |
| 197 | + { | |
| 198 | + return $this->additionalProperties; | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 136 | 202 | * Validates the ID. |
| 137 | 203 | * |
| 138 | 204 | * @param int $id |
| 139 | 205 | * |
| @@ -205,9 +271,9 @@ | ||
| 205 | 271 | private function validatePosition(int $value): int |
| 206 | 272 | { |
| 207 | 273 | if ($value < 0) { |
| 208 | 274 | throw new ValidationException( |
| 209 | - esc_html__('position must be a non-negative integer.', 'ivyforms') | |
| 275 | + BackendStrings::getExceptionStrings()['position_non_negative'] | |
| 210 | 276 | ); |
| 211 | 277 | } |
| 212 | 278 | return $value; |
| 213 | 279 | } |
| @@ -212,20 +278,65 @@ | ||
| 212 | 278 | return $value; |
| 213 | 279 | } |
| 214 | 280 | |
| 215 | 281 | /** |
| 216 | - * Validates a boolean field. | |
| 282 | + * Validates the row index. | |
| 217 | 283 | * |
| 218 | - * @param bool $value | |
| 284 | + * @param int $value | |
| 219 | 285 | * |
| 220 | - * @return bool | |
| 286 | + * @return int | |
| 287 | + * | |
| 288 | + * @throws ValidationException | |
| 221 | 289 | */ |
| 222 | - private function validateBool(bool $value): bool | |
| 290 | + private function validateRowIndex(int $value): int | |
| 223 | 291 | { |
| 292 | + if ($value < 0) { | |
| 293 | + throw new ValidationException( | |
| 294 | + BackendStrings::getExceptionStrings()['row_index_non_negative'] | |
| 295 | + ); | |
| 296 | + } | |
| 224 | 297 | return $value; |
| 225 | 298 | } |
| 226 | 299 | |
| 227 | 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 | + /** | |
| 228 | 339 | * Validate rows. |
| 229 | 340 | * |
| 230 | 341 | * @param int $rows |
| 231 | 342 | * @return int |
| @@ -234,9 +345,9 @@ | ||
| 234 | 345 | private function validateRows(int $rows): int |
| 235 | 346 | { |
| 236 | 347 | if ($rows < 0) { |
| 237 | 348 | throw new ValidationException( |
| 238 | - esc_html__('rows must be at least 1.', 'ivyforms') | |
| 349 | + BackendStrings::getExceptionStrings()['rows_non_negative'] | |
| 239 | 350 | ); |
| 240 | 351 | } |
| 241 | 352 | return $rows; |
| 242 | 353 | } |
| @@ -291,8 +402,38 @@ | ||
| 291 | 402 | return $this->position; |
| 292 | 403 | } |
| 293 | 404 | |
| 294 | 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 | + /** | |
| 295 | 436 | * Get the parent field id. |
| 296 | 437 | * |
| 297 | 438 | * @return int|null |
| 298 | 439 | */ |
| @@ -339,88 +480,8 @@ | ||
| 339 | 480 | $this->rows = $rows; |
| 340 | 481 | } |
| 341 | 482 | |
| 342 | 483 | /** |
| 343 | - * Check if the confirm field feature is enabled. | |
| 344 | - * | |
| 345 | - * @return bool | |
| 346 | - */ | |
| 347 | - public function isConfirmFieldEnabled(): bool | |
| 348 | - { | |
| 349 | - return $this->confirmFieldEnabled; | |
| 350 | - } | |
| 351 | - | |
| 352 | - /** | |
| 353 | - * Get the confirm field label. | |
| 354 | - * | |
| 355 | - * @return string | |
| 356 | - */ | |
| 357 | - public function getConfirmFieldLabel(): string | |
| 358 | - { | |
| 359 | - return $this->confirmFieldLabel; | |
| 360 | - } | |
| 361 | - | |
| 362 | - /** | |
| 363 | - * Get the confirm field placeholder. | |
| 364 | - * | |
| 365 | - * @return string | |
| 366 | - */ | |
| 367 | - public function getConfirmFieldPlaceholder(): string | |
| 368 | - { | |
| 369 | - return $this->confirmFieldPlaceholder; | |
| 370 | - } | |
| 371 | - | |
| 372 | - /** | |
| 373 | - * Check if the confirm field label should be hidden. | |
| 374 | - * | |
| 375 | - * @return bool | |
| 376 | - */ | |
| 377 | - public function isConfirmFieldHideLabel(): bool | |
| 378 | - { | |
| 379 | - return $this->confirmFieldHideLabel; | |
| 380 | - } | |
| 381 | - | |
| 382 | - /** | |
| 383 | - * Set the confirm field feature enabled/disabled. | |
| 384 | - * | |
| 385 | - * @param bool $enabled | |
| 386 | - */ | |
| 387 | - public function setConfirmFieldEnabled(bool $enabled): void | |
| 388 | - { | |
| 389 | - $this->confirmFieldEnabled = $enabled; | |
| 390 | - } | |
| 391 | - | |
| 392 | - /** | |
| 393 | - * Set the confirm field label. | |
| 394 | - * | |
| 395 | - * @param string $label | |
| 396 | - */ | |
| 397 | - public function setConfirmFieldLabel(string $label): void | |
| 398 | - { | |
| 399 | - $this->confirmFieldLabel = $label; | |
| 400 | - } | |
| 401 | - | |
| 402 | - /** | |
| 403 | - * Set the confirm field placeholder. | |
| 404 | - * | |
| 405 | - * @param string $ph | |
| 406 | - */ | |
| 407 | - public function setConfirmFieldPlaceholder(string $ph): void | |
| 408 | - { | |
| 409 | - $this->confirmFieldPlaceholder = $ph; | |
| 410 | - } | |
| 411 | - | |
| 412 | - /** | |
| 413 | - * Set the confirm field label visibility. | |
| 414 | - * | |
| 415 | - * @param bool $hide | |
| 416 | - */ | |
| 417 | - public function setConfirmFieldHideLabel(bool $hide): void | |
| 418 | - { | |
| 419 | - $this->confirmFieldHideLabel = $hide; | |
| 420 | - } | |
| 421 | - | |
| 422 | - /** | |
| 423 | 484 | * Get the general settings object. |
| 424 | 485 | * |
| 425 | 486 | * @return FieldAdvancedSettings |
| 426 | 487 | */ |
| @@ -435,23 +496,32 @@ | ||
| 435 | 496 | * @return array<string, mixed> |
| 436 | 497 | */ |
| 437 | 498 | public function toArray(): array |
| 438 | 499 | { |
| 439 | - return [ | |
| 500 | + $data = [ | |
| 440 | 501 | 'id' => $this->id, |
| 441 | 502 | 'formId' => $this->formId, |
| 442 | 503 | 'fieldIndex' => $this->fieldIndex, |
| 443 | 504 | 'type' => $this->type, |
| 444 | 505 | 'position' => $this->position, |
| 506 | + 'rowIndex' => $this->rowIndex, | |
| 507 | + 'columnIndex' => $this->columnIndex, | |
| 508 | + 'width' => $this->width, | |
| 445 | 509 | 'parentId' => $this->parentId, |
| 446 | 510 | 'generalSettings' => $this->generalSettings->toArray(), |
| 447 | 511 | 'fieldOptions' => $this->fieldOptions->toArray(), |
| 448 | 512 | 'rows' => $this->rows, |
| 449 | - 'confirmFieldEnabled' => $this->confirmFieldEnabled, | |
| 450 | - 'confirmFieldLabel' => $this->confirmFieldLabel, | |
| 451 | - 'confirmFieldPlaceholder' => $this->confirmFieldPlaceholder, | |
| 452 | - 'confirmFieldHideLabel' => $this->confirmFieldHideLabel, | |
| 453 | 513 | // Advanced options |
| 454 | 514 | 'advancedSettings' => $this->advancedSettings->toArray() |
| 455 | 515 | ]; |
| 516 | + | |
| 517 | + /** | |
| 518 | + * Allows modification of the array representation of the Field value object. | |
| 519 | + * @since 0.1.0 | |
| 520 | + * | |
| 521 | + * @param mixed[] $data The current array representation of the Field value object. | |
| 522 | + * @param Field $field The Field value object instance. | |
| 523 | + * @return mixed[] The modified array representation of the Field value object. | |
| 524 | + */ | |
| 525 | + return apply_filters('ivyforms/field/value_object/add_to_array_additional_properties', $data, $this); | |
| 456 | 526 | } |
| 457 | 527 | } |