Passed
Push — master ( 702f24...55adc1 )
by Yannick
09:28
created

Version20250708115900::up()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 27
nc 6
nop 1
dl 0
loc 41
rs 9.1768
c 1
b 0
f 0
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
class Version20250708115900 extends AbstractMigrationChamilo
14
{
15
    public function getDescription(): string
16
    {
17
        return 'Populate settings_value_template table from SettingsValueTemplateFixtures and update settings.value_template_id accordingly.';
18
    }
19
20
    public function up(Schema $schema): void
21
    {
22
        $templates = SettingsValueTemplateFixtures::getTemplatesGrouped();
23
24
        foreach ($templates as $category => $settings) {
25
            foreach ($settings as $setting) {
26
                $name = $setting['name'];
27
                $jsonExample = json_encode(
28
                    $setting['json_example'],
29
                    JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
30
                );
31
32
                // Check if template already exists
33
                $templateId = $this->connection->fetchOne(
34
                    'SELECT id FROM settings_value_template WHERE name = ?',
35
                    [$name]
36
                );
37
38
                if ($templateId) {
39
                    $this->connection->executeStatement(
40
                        'UPDATE settings_value_template SET json_example = ?, updated_at = NOW() WHERE id = ?',
41
                        [$jsonExample, $templateId]
42
                    );
43
                } else {
44
                    $this->connection->executeStatement(
45
                        'INSERT INTO settings_value_template (name, json_example, created_at, updated_at) VALUES (?, ?, NOW(), NOW())',
46
                        [$name, $jsonExample]
47
                    );
48
49
                    $templateId = $this->connection->lastInsertId();
50
                }
51
52
                if ($templateId) {
53
                    $updatedRows = $this->connection->executeStatement(
54
                        'UPDATE settings
55
                         SET value_template_id = ?
56
                         WHERE variable = ?',
57
                        [$templateId, $name]
58
                    );
59
                } else {
60
                    echo "[DEBUG] ERROR: Template ID still NULL for name={$name}\n";
61
                }
62
            }
63
        }
64
    }
65
66
    public function down(Schema $schema): void
67
    {
68
        $templates = SettingsValueTemplateFixtures::getTemplatesGrouped();
69
70
        foreach ($templates as $category => $settings) {
71
            foreach ($settings as $setting) {
72
                $name = $setting['name'];
73
74
                $templateId = $this->connection->fetchOne(
75
                    'SELECT id FROM settings_value_template WHERE name = ?',
76
                    [$name]
77
                );
78
79
                if ($templateId) {
80
                    $this->connection->executeStatement(
81
                        'UPDATE settings
82
                         SET value_template_id = NULL
83
                         WHERE value_template_id = ?',
84
                        [$templateId]
85
                    );
86
87
                    $this->connection->executeStatement(
88
                        'DELETE FROM settings_value_template WHERE id = ?',
89
                        [$templateId]
90
                    );
91
                }
92
            }
93
        }
94
    }
95
}
96