Passed
Push — master ( 54aa45...2d7961 )
by
unknown
17:08 queued 08:38
created

Version20250905112000   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 51
c 1
b 0
f 1
dl 0
loc 79
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A up() 0 55 4
A down() 0 13 2
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\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
final class Version20250905112000 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Insert or update platform setting title/comment for disable_gdpr.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $settings = [
22
            [
23
                'name' => 'disable_gdpr',
24
                'title' => 'Disable GDPR features',
25
                'comment' => 'If you already manage your personal data protection declaration to users elsewhere, you can safely disable this feature.',
26
            ],
27
        ];
28
29
        foreach ($settings as $setting) {
30
            $variable = addslashes($setting['name']);
31
            $title    = addslashes($setting['title']);
32
            $comment  = addslashes($setting['comment']);
33
34
            // Check if the setting exists for the main access_url (1) and no subkey
35
            $sqlCheck = \sprintf(
36
                "SELECT COUNT(*) AS count
37
                 FROM settings
38
                 WHERE variable = '%s'
39
                   AND subkey IS NULL
40
                   AND access_url = 1",
41
                $variable
42
            );
43
44
            $stmt   = $this->connection->executeQuery($sqlCheck);
45
            $result = $stmt->fetchAssociative();
46
47
            if ($result && (int) $result['count'] > 0) {
48
                // Update only title/comment/category; keep selected_value as is
49
                $this->addSql(\sprintf(
50
                    "UPDATE settings
51
                     SET title = '%s',
52
                         comment = '%s',
53
                         category = 'privacy'
54
                     WHERE variable = '%s'
55
                       AND subkey IS NULL
56
                       AND access_url = 1",
57
                    $title,
58
                    $comment,
59
                    $variable
60
                ));
61
                $this->write(\sprintf('Updated setting: %s', $setting['name']));
62
            } else {
63
                // Insert with sensible defaults (selected_value = 'false')
64
                $this->addSql(\sprintf(
65
                    "INSERT INTO settings
66
                        (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url)
67
                     VALUES
68
                        ('%s', NULL, NULL, 'privacy', 'false', '%s', '%s', 1, 0, 1)",
69
                    $variable,
70
                    $title,
71
                    $comment
72
                ));
73
                $this->write(\sprintf('Inserted setting: %s', $setting['name']));
74
            }
75
        }
76
    }
77
78
    public function down(Schema $schema): void
79
    {
80
        $variables = ['disable_gdpr'];
81
82
        foreach ($variables as $variable) {
83
            $this->addSql(\sprintf(
84
                "DELETE FROM settings
85
                 WHERE variable = '%s'
86
                   AND subkey IS NULL
87
                   AND access_url = 1",
88
                addslashes($variable)
89
            ));
90
            $this->write(\sprintf('Removed setting: %s.', $variable));
91
        }
92
    }
93
}
94