PluginProbe
The Innovative Form Builder – IvyForms / 0.7
The Innovative Form Builder – IvyForms v0.7
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 0.7, at backend/src/Services/InstallActions/Migrations/SeedRowIndexForFields.php

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