ivyforms
/
backend
/
src
/
Services
/
InstallActions
/
Migrations
/
AddShowEmptyFieldsToNotifications.php
AddShowEmptyFieldsToNotifications.php in The Innovative Form Builder – IvyForms 1.1, at backend/src/Services/InstallActions/Migrations/AddShowEmptyFieldsToNotifications.php
| 1 | <?php |
| 2 | |
| 3 | namespace IvyForms\Services\InstallActions\Migrations; |
| 4 | |
| 5 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 6 | use IvyForms\Services\InstallActions\DB\Notification\NotificationsTable; |
| 7 | use IvyForms\Services\Translations\BackendStrings; |
| 8 | |
| 9 | class AddShowEmptyFieldsToNotifications |
| 10 | { |
| 11 | /** |
| 12 | * Run the migration |
| 13 | * |
| 14 | * @return bool |
| 15 | * @throws InvalidArgumentException |
| 16 | */ |
| 17 | public static function run(): bool |
| 18 | { |
| 19 | global $wpdb; |
| 20 | |
| 21 | $tableName = NotificationsTable::getTableName(); |
| 22 | |
| 23 | // Check if showEmptyFields column already exists |
| 24 | $columnExists = $wpdb->get_results( |
| 25 | "SHOW COLUMNS FROM `{$tableName}` LIKE 'showEmptyFields'" |
| 26 | ); |
| 27 | |
| 28 | if (!empty($columnExists)) { |
| 29 | return true; // Already migrated |
| 30 | } |
| 31 | |
| 32 | // Add showEmptyFields column after message |
| 33 | $alterQuery = "ALTER TABLE `{$tableName}` ADD COLUMN `showEmptyFields` INT(1) NULL DEFAULT 0 AFTER `message`"; |
| 34 | |
| 35 | $result = $wpdb->query($alterQuery); |
| 36 | |
| 37 | if ($result === false) { |
| 38 | error_log(sprintf( |
| 39 | BackendStrings::getExceptionStrings()['migration_error_add_column'], |
| 40 | 'showEmptyFields', |
| 41 | $tableName |
| 42 | )); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Rollback the migration |
| 51 | * |
| 52 | * @return bool |
| 53 | */ |
| 54 | public static function rollback(): bool |
| 55 | { |
| 56 | global $wpdb; |
| 57 | |
| 58 | $tableName = NotificationsTable::getTableName(); |
| 59 | |
| 60 | $alterQuery = "ALTER TABLE `{$tableName}` DROP COLUMN `showEmptyFields`"; |
| 61 | |
| 62 | $result = $wpdb->query($alterQuery); |
| 63 | |
| 64 | return $result !== false; |
| 65 | } |
| 66 | } |
| 67 |