| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Services\InstallActions\Migrations; |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 11 |
use IvyForms\Services\InstallActions\DB\Field\FieldsTable; |
| 12 |
use IvyForms\Services\Translations\BackendStrings; |
| 13 |
|
| 14 |
/** |
| 15 |
* Migration: Add row and column layout columns to fields table |
| 16 |
* |
| 17 |
* Adds rowIndex, columnIndex, and width columns to support grid-based field layout |
| 18 |
* where fields can be organized in rows with up to 5 fields per row. |
| 19 |
* |
| 20 |
* @package IvyForms\Services\InstallActions\Migrations |
| 21 |
*/ |
| 22 |
class AddRowColumnLayoutToFields |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Run the migration |
| 26 |
* |
| 27 |
* @return bool True if successful, false otherwise |
| 28 |
* @throws InvalidArgumentException |
| 29 |
*/ |
| 30 |
public static function run(): bool |
| 31 |
{ |
| 32 |
global $wpdb; |
| 33 |
|
| 34 |
$tableName = FieldsTable::getTableName(); |
| 35 |
|
| 36 |
// Check if columns already exist |
| 37 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 38 |
$rowIndexExists = $wpdb->get_results( |
| 39 |
"SHOW COLUMNS FROM `{$tableName}` LIKE 'rowIndex'" |
| 40 |
); |
| 41 |
|
| 42 |
if (!empty($rowIndexExists)) { |
| 43 |
return true; // Already migrated |
| 44 |
} |
| 45 |
|
| 46 |
// Add new columns |
| 47 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 48 |
$alterQuery = "ALTER TABLE `{$tableName}` |
| 49 |
ADD COLUMN `rowIndex` INT(11) NOT NULL DEFAULT 0 AFTER `position`, |
| 50 |
ADD COLUMN `columnIndex` INT(11) NOT NULL DEFAULT 0 AFTER `rowIndex`, |
| 51 |
ADD COLUMN `width` INT(3) NOT NULL DEFAULT 100 AFTER `columnIndex`"; |
| 52 |
|
| 53 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 54 |
$result = $wpdb->query($alterQuery); |
| 55 |
|
| 56 |
if ($result === false) { |
| 57 |
error_log(sprintf( |
| 58 |
BackendStrings::getExceptionStrings()['migration_error_add_column'], |
| 59 |
'rowIndex, columnIndex, width', |
| 60 |
$tableName |
| 61 |
)); |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
// Migrate existing data: convert position-based layout to row-based layout |
| 66 |
// Each existing field gets its own row (rowIndex = position - 1) |
| 67 |
// All fields start at columnIndex = 0, width = 100% |
| 68 |
// |
| 69 |
// We need to update ALL parent fields (parentId IS NULL or parentId = 0) |
| 70 |
// Group by formId and assign sequential row indexes based on position |
| 71 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 72 |
$updateQuery = "UPDATE `{$tableName}` |
| 73 |
SET `rowIndex` = `position` - 1, |
| 74 |
`columnIndex` = 0, |
| 75 |
`width` = 100 |
| 76 |
WHERE (`parentId` IS NULL OR `parentId` = 0)"; |
| 77 |
|
| 78 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 79 |
$wpdb->query($updateQuery); |
| 80 |
|
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Rollback the migration (for development purposes) |
| 86 |
* |
| 87 |
* @return bool True if successful, false otherwise |
| 88 |
* @throws InvalidArgumentException |
| 89 |
*/ |
| 90 |
public static function rollback(): bool |
| 91 |
{ |
| 92 |
global $wpdb; |
| 93 |
|
| 94 |
$tableName = FieldsTable::getTableName(); |
| 95 |
|
| 96 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 97 |
$alterQuery = "ALTER TABLE `{$tableName}` |
| 98 |
DROP COLUMN `rowIndex`, |
| 99 |
DROP COLUMN `columnIndex`, |
| 100 |
DROP COLUMN `width`"; |
| 101 |
|
| 102 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 103 |
$result = $wpdb->query($alterQuery); |
| 104 |
|
| 105 |
return $result !== false; |
| 106 |
} |
| 107 |
} |
| 108 |
|