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 / SeedRowIndexForFields.php

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

128 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
13 /**
14 * Seed: Initialize rowIndex for fields from AddRowColumnLayoutToFields migration
15 *
16 * This seed runs after AddRowColumnLayoutToFields migration to properly set rowIndex
17 * for existing fields. It assigns each field its own row based on position.
18 *
19 * @package IvyForms\Services\InstallActions\Migrations
20 */
21 class SeedRowIndexForFields
22 {
23 /**
24 * Run the seed
25 *
26 * @return bool True if successful, false otherwise
27 * @throws InvalidArgumentException
28 */
29 public static function run(): bool
30 {
31 global $wpdb;
32
33 $tableName = FieldsTable::getTableName();
34
35 // Get all forms that have fields
36 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
37 $forms = $wpdb->get_col(
38 "SELECT DISTINCT formId FROM `{$tableName}`
39 WHERE (`parentId` IS NULL OR `parentId` = 0)"
40 );
41
42 if (empty($forms)) {
43 return true; // No forms to seed
44 }
45
46 $seededForms = 0;
47
48 foreach ($forms as $formId) {
49 // For each form, check if all parent fields are in row 0
50 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
51 $allInRowZero = $wpdb->get_var(
52 $wpdb->prepare(
53 "SELECT COUNT(*) FROM `{$tableName}`
54 WHERE formId = %d
55 AND (`parentId` IS NULL OR `parentId` = 0)
56 AND `rowIndex` != 0",
57 $formId
58 )
59 );
60
61 // If all fields are in row 0, seed them with proper row indexes
62 if ($allInRowZero == 0) {
63 // Get all parent fields for this form ordered by position
64 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
65 $fields = $wpdb->get_results(
66 $wpdb->prepare(
67 "SELECT id, position FROM `{$tableName}`
68 WHERE formId = %d
69 AND (`parentId` IS NULL OR `parentId` = 0)
70 ORDER BY position ASC",
71 $formId
72 )
73 );
74
75 // Update each field with proper rowIndex (each field in its own row)
76 foreach ($fields as $index => $field) {
77 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
78 $wpdb->update(
79 $tableName,
80 [
81 'rowIndex' => $index,
82 'columnIndex' => 0,
83 'width' => 100,
84 ],
85 ['id' => $field->id],
86 ['%d', '%d', '%d'],
87 ['%d']
88 );
89 }
90
91 $seededForms++;
92 }
93 }
94
95 if ($seededForms > 0) {
96 error_log("IvyForms Seed: Initialized rowIndex for {$seededForms} forms");
97 }
98
99 return true;
100 }
101
102 /**
103 * Rollback the seed (for development purposes)
104 *
105 * @return bool True if successful, false otherwise
106 * @throws InvalidArgumentException
107 */
108 public static function rollback(): bool
109 {
110 global $wpdb;
111
112 $tableName = FieldsTable::getTableName();
113
114 // Reset all fields back to row 0
115 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
116 $updateQuery = "UPDATE `{$tableName}`
117 SET `rowIndex` = 0,
118 `columnIndex` = 0,
119 `width` = 100
120 WHERE (`parentId` IS NULL OR `parentId` = 0)";
121
122 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
123 $result = $wpdb->query($updateQuery);
124
125 return $result !== false;
126 }
127 }
128