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