PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / InstallActions / Migrations / AddNamePositionToConfirmations.php

AddNamePositionToConfirmations.php in The Innovative Form Builder – IvyForms 1.5.1, at backend/src/Services/InstallActions/Migrations/AddNamePositionToConfirmations.php

148 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\InstallActions\Migrations;
4
5 use IvyForms\Common\Exceptions\InvalidArgumentException;
6 use IvyForms\Services\InstallActions\DB\Confirmation\ConfirmationsTable;
7 use IvyForms\Services\Translations\BackendStrings;
8
9 class AddNamePositionToConfirmations
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 = ConfirmationsTable::getTableName();
22 $runBackfill = false;
23
24 if (!self::columnExists($wpdb, $tableName, 'name')) {
25 if (!self::addColumn($wpdb, $tableName, 'name', 'VARCHAR(255) NULL AFTER `formId`')) {
26 return false;
27 }
28 $runBackfill = true;
29 }
30
31 if (!self::columnExists($wpdb, $tableName, 'position')) {
32 if (!self::addColumn($wpdb, $tableName, 'position', 'INT(11) NOT NULL DEFAULT 0 AFTER `page`')) {
33 return false;
34 }
35 $runBackfill = true;
36 }
37
38 if (!self::columnExists($wpdb, $tableName, 'isDefault')) {
39 if (
40 !self::addColumn(
41 $wpdb,
42 $tableName,
43 'isDefault',
44 'INT(1) NOT NULL DEFAULT 0 AFTER `position`'
45 )
46 ) {
47 return false;
48 }
49 $runBackfill = true;
50 }
51
52 if ($runBackfill) {
53 self::backfillNamePositionAndDefault($wpdb, $tableName);
54 }
55
56 return true;
57 }
58
59 /**
60 * @param \wpdb $wpdb
61 * @param string $tableName
62 * @param string $column
63 * @return bool
64 */
65 private static function columnExists($wpdb, string $tableName, string $column): bool
66 {
67 return !empty($wpdb->get_results("SHOW COLUMNS FROM `{$tableName}` LIKE '{$column}'"));
68 }
69
70 /**
71 * @param \wpdb $wpdb
72 * @param string $tableName
73 * @param string $column
74 * @param string $definition
75 * @return bool
76 */
77 private static function addColumn($wpdb, string $tableName, string $column, string $definition): bool
78 {
79 $result = $wpdb->query("ALTER TABLE `{$tableName}` ADD COLUMN `{$column}` {$definition}");
80 if ($result !== false) {
81 return true;
82 }
83
84 error_log(sprintf(
85 BackendStrings::getExceptionStrings()['migration_error_add_column'],
86 $column,
87 $tableName
88 ));
89
90 return false;
91 }
92
93 /**
94 * @param \wpdb $wpdb
95 * @param string $tableName
96 * @return void
97 */
98 private static function backfillNamePositionAndDefault($wpdb, string $tableName): void
99 {
100 $strings = BackendStrings::getSettingsFormBuilderStrings();
101 $defaultName = $strings['default_confirmation_name'] ?? 'Confirmation';
102
103 $rows = $wpdb->get_results(
104 "SELECT id, formId FROM `{$tableName}` ORDER BY formId ASC, id ASC",
105 ARRAY_A
106 );
107
108 if (empty($rows)) {
109 return;
110 }
111
112 $positionByForm = [];
113 foreach ($rows as $row) {
114 $formId = (int) $row['formId'];
115 $id = (int) $row['id'];
116 if (!isset($positionByForm[$formId])) {
117 $positionByForm[$formId] = 0;
118 }
119 $position = $positionByForm[$formId];
120 $wpdb->update(
121 $tableName,
122 [
123 'name' => $defaultName,
124 'position' => $position,
125 'isDefault' => $position === 0 ? 1 : 0,
126 ],
127 ['id' => $id],
128 ['%s', '%d', '%d'],
129 ['%d']
130 );
131 $positionByForm[$formId]++;
132 }
133 }
134
135 public static function rollback(): bool
136 {
137 global $wpdb;
138
139 $tableName = ConfirmationsTable::getTableName();
140
141 $wpdb->query("ALTER TABLE `{$tableName}` DROP COLUMN IF EXISTS `isDefault`");
142 $wpdb->query("ALTER TABLE `{$tableName}` DROP COLUMN IF EXISTS `position`");
143 $wpdb->query("ALTER TABLE `{$tableName}` DROP COLUMN IF EXISTS `name`");
144
145 return true;
146 }
147 }
148