Passed
Push — master ( 2d7961...d66994 )
by
unknown
18:46 queued 09:32
created

Version20250905112000::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
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
final class Version20250905112000 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Insert or update platform settings title/comment for disable_gdpr and show_conditions_to_user, and link show_conditions_to_user to its value template.';
21
    }
22
23
    public function up(Schema $schema): void
24
    {
25
        // Upsert settings (title/comment/category and default selected_value if missing)
26
        $settings = [
27
            [
28
                'name' => 'disable_gdpr',
29
                'title' => 'Disable GDPR features',
30
                'comment' => 'If you already manage your personal data protection declaration to users elsewhere, you can safely disable this feature.',
31
                'category' => 'profile',
32
                'default' => 'false',
33
            ],
34
            [
35
                'name' => 'show_conditions_to_user',
36
                'title' => 'Show specific registration conditions',
37
                'comment' => "Show multiple conditions to user during sign up process. Provide an array with each element containing 'variable' (internal extra field name), 'display_text' (simple text for a checkbox), 'text_area' (long text of conditions).",
38
                'category' => 'registration',
39
                'default' => '[]',
40
            ],
41
        ];
42
43
        foreach ($settings as $setting) {
44
            $variable = addslashes($setting['name']);
45
            $title    = addslashes($setting['title']);
46
            $comment  = addslashes($setting['comment']);
47
            $category = addslashes($setting['category']);
48
            $default  = addslashes($setting['default']);
49
50
            $sqlCheck = \sprintf(
51
                "SELECT COUNT(*) AS count
52
                 FROM settings
53
                 WHERE variable = '%s'
54
                   AND subkey IS NULL
55
                   AND access_url = 1",
56
                $variable
57
            );
58
59
            $stmt   = $this->connection->executeQuery($sqlCheck);
60
            $result = $stmt->fetchAssociative();
61
62
            if ($result && (int) $result['count'] > 0) {
63
                $this->addSql(\sprintf(
64
                    "UPDATE settings
65
                     SET title = '%s',
66
                         comment = '%s',
67
                         category = '%s'
68
                     WHERE variable = '%s'
69
                       AND subkey IS NULL
70
                       AND access_url = 1",
71
                    $title,
72
                    $comment,
73
                    $category,
74
                    $variable
75
                ));
76
                $this->write(\sprintf('Updated setting: %s', $setting['name']));
77
            } else {
78
                $this->addSql(\sprintf(
79
                    "INSERT INTO settings
80
                        (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url)
81
                     VALUES
82
                        ('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)",
83
                    $variable,
84
                    $category,
85
                    $default,
86
                    $title,
87
                    $comment
88
                ));
89
                $this->write(\sprintf('Inserted setting: %s', $setting['name']));
90
            }
91
        }
92
93
        // Link show_conditions_to_user to its value template (from fixtures), only this variable
94
        $targetVariable = 'show_conditions_to_user';
95
        $templatesGrouped = SettingsValueTemplateFixtures::getTemplatesGrouped();
96
97
        foreach ($templatesGrouped as $category => $settingsList) {
98
            foreach ($settingsList as $tpl) {
99
                if (!isset($tpl['variable']) || $tpl['variable'] !== $targetVariable) {
100
                    continue;
101
                }
102
103
                $jsonExample = json_encode(
104
                    $tpl['json_example'],
105
                    JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
106
                );
107
108
                // Check if template already exists
109
                $templateId = $this->connection->fetchOne(
110
                    'SELECT id FROM settings_value_template WHERE variable = ?',
111
                    [$targetVariable]
112
                );
113
114
                if ($templateId) {
115
                    $this->connection->executeStatement(
116
                        'UPDATE settings_value_template SET json_example = ?, updated_at = NOW() WHERE id = ?',
117
                        [$jsonExample, $templateId]
118
                    );
119
                } else {
120
                    $this->connection->executeStatement(
121
                        'INSERT INTO settings_value_template (variable, json_example, created_at, updated_at)
122
                         VALUES (?, ?, NOW(), NOW())',
123
                        [$targetVariable, $jsonExample]
124
                    );
125
                    $templateId = $this->connection->lastInsertId();
126
                }
127
128
                if ($templateId) {
129
                    $updatedRows = $this->connection->executeStatement(
130
                        'UPDATE settings
131
                         SET value_template_id = ?
132
                         WHERE variable = ? AND subkey IS NULL AND access_url = 1',
133
                        [$templateId, $targetVariable]
134
                    );
135
                    $this->write("Linked settings.value_template_id for '{$targetVariable}' (updated rows: {$updatedRows})");
136
                }
137
138
                // We found and processed the target; break both loops
139
                break 2;
140
            }
141
        }
142
    }
143
144
    public function down(Schema $schema): void
145
    {
146
        // Unlink and remove the value template for show_conditions_to_user (if present)
147
        $targetVariable = 'show_conditions_to_user';
148
149
        $templateId = $this->connection->fetchOne(
150
            'SELECT id FROM settings_value_template WHERE variable = ?',
151
            [$targetVariable]
152
        );
153
154
        if ($templateId) {
155
            $this->connection->executeStatement(
156
                'UPDATE settings
157
                 SET value_template_id = NULL
158
                 WHERE value_template_id = ?',
159
                [$templateId]
160
            );
161
162
            $this->connection->executeStatement(
163
                'DELETE FROM settings_value_template WHERE id = ?',
164
                [$templateId]
165
            );
166
167
            $this->write("Deleted template with ID {$templateId} for variable {$targetVariable}");
168
        }
169
170
        // Remove both settings entries (as in previous pattern)
171
        $variables = ['disable_gdpr', 'show_conditions_to_user'];
172
173
        foreach ($variables as $variable) {
174
            $this->addSql(\sprintf(
175
                "DELETE FROM settings
176
                 WHERE variable = '%s'
177
                   AND subkey IS NULL
178
                   AND access_url = 1",
179
                addslashes($variable)
180
            ));
181
            $this->write(\sprintf('Removed setting: %s.', $variable));
182
        }
183
    }
184
}
185