Passed
Pull Request — master (#7144)
by Yannick
10:25
created

Version20251205162000   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 72
c 1
b 0
f 0
dl 0
loc 121
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 0 75 8
A getDescription() 0 3 1
A down() 0 36 2
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\DataFixtures\SettingsValueTemplateFixtures;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Doctrine\DBAL\Schema\Schema;
12
13
use const JSON_UNESCAPED_SLASHES;
14
use const JSON_UNESCAPED_UNICODE;
15
16
class Version20251205162000 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Create JSON template and link it for search_prefilter_prefix setting.';
21
    }
22
23
    public function up(Schema $schema): void
24
    {
25
        $targetVariable = 'search_prefilter_prefix';
26
27
        $templates = SettingsValueTemplateFixtures::getTemplatesGrouped();
28
29
        $foundTemplate = null;
30
31
        foreach ($templates as $category => $settings) {
32
            foreach ($settings as $setting) {
33
                if (($setting['variable'] ?? null) === $targetVariable) {
34
                    $foundTemplate = $setting;
35
                    break 2;
36
                }
37
            }
38
        }
39
40
        if (!$foundTemplate) {
41
            $this->write("No template found in SettingsValueTemplateFixtures for variable '{$targetVariable}'. Skipping.");
42
            return;
43
        }
44
45
        $jsonExample = json_encode(
46
            $foundTemplate['json_example'],
47
            JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
48
        );
49
50
        // Check if template already exists
51
        $templateId = $this->connection->fetchOne(
52
            'SELECT id FROM settings_value_template WHERE variable = ?',
53
            [$targetVariable]
54
        );
55
56
        if ($templateId) {
57
            $this->connection->executeStatement(
58
                'UPDATE settings_value_template
59
                 SET json_example = ?, updated_at = NOW()
60
                 WHERE id = ?',
61
                [$jsonExample, $templateId]
62
            );
63
            $this->write("Updated existing template (ID {$templateId}) for variable '{$targetVariable}'.");
64
        } else {
65
            $this->connection->executeStatement(
66
                'INSERT INTO settings_value_template (variable, json_example, created_at, updated_at)
67
                 VALUES (?, ?, NOW(), NOW())',
68
                [$targetVariable, $jsonExample]
69
            );
70
71
            $templateId = $this->connection->lastInsertId();
72
            $this->write("Inserted new template (ID {$templateId}) for variable '{$targetVariable}'.");
73
        }
74
75
        if ($templateId) {
76
            // Link template to existing settings rows
77
            $updatedRows = $this->connection->executeStatement(
78
                'UPDATE settings
79
                 SET value_template_id = ?
80
                 WHERE variable = ?',
81
                [$templateId, $targetVariable]
82
            );
83
            $this->write("Updated {$updatedRows} rows in settings for variable '{$targetVariable}'.");
84
85
            // Optional: clean legacy boolean values (Yes/No) to avoid invalid JSON
86
            $cleanedRows = $this->connection->executeStatement(
87
                "UPDATE settings
88
                 SET selected_value = ''
89
                 WHERE variable = ?
90
                   AND (selected_value = 'true' OR selected_value = 'false')",
91
                [$targetVariable]
92
            );
93
            if ($cleanedRows > 0) {
94
                $this->write("Cleaned legacy boolean values in {$cleanedRows} rows for variable '{$targetVariable}'.");
95
            }
96
        } else {
97
            $this->write("ERROR: Template ID is null for variable '{$targetVariable}'.");
98
        }
99
    }
100
101
    public function down(Schema $schema): void
102
    {
103
        $targetVariable = 'search_prefilter_prefix';
104
105
        $templateId = $this->connection->fetchOne(
106
            'SELECT id FROM settings_value_template WHERE variable = ?',
107
            [$targetVariable]
108
        );
109
110
        if (!$templateId) {
111
            $this->write("No template found to revert for variable '{$targetVariable}'.");
112
            return;
113
        }
114
115
        // Unlink template from settings
116
        $updatedRows = $this->connection->executeStatement(
117
            'UPDATE settings
118
             SET value_template_id = NULL
119
             WHERE value_template_id = ?',
120
            [$templateId]
121
        );
122
        $this->write("Unlinked template ID {$templateId} from {$updatedRows} settings rows.");
123
124
        // Delete template
125
        $this->connection->executeStatement(
126
            'DELETE FROM settings_value_template WHERE id = ?',
127
            [$templateId]
128
        );
129
        $this->write("Deleted template with ID {$templateId} for variable '{$targetVariable}'.");
130
131
        // Optional: reset selected_value (we do not know original Yes/No)
132
        $this->connection->executeStatement(
133
            "UPDATE settings
134
             SET selected_value = ''
135
             WHERE variable = ?",
136
            [$targetVariable]
137
        );
138
    }
139
}
140