Passed
Push — master ( d1b872...ced008 )
by
unknown
17:53 queued 09:01
created

Version20251021200200::dbg()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
declare(strict_types=1);
5
6
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
7
8
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
9
use Doctrine\DBAL\Schema\Schema;
10
11
final class Version20251021200200 extends AbstractMigrationChamilo
12
{
13
    private const DEBUG = true;
14
15
    public function getDescription(): string
16
    {
17
        return 'Consolidate show_tabs and show_tabs_per_role to category "display" and remove duplicates from "platform" or other categories.';
18
    }
19
20
    public function up(Schema $schema): void
21
    {
22
        $conn = $this->connection;
23
        $vars = ['show_tabs', 'show_tabs_per_role'];
24
25
        foreach ($vars as $var) {
26
            $rows = $conn->fetchAllAssociative(
27
                'SELECT id, access_url, category, selected_value
28
                   FROM settings
29
                  WHERE variable = ?',
30
                [$var]
31
            );
32
33
            if (empty($rows)) {
34
                $this->dbg("No '{$var}' entries found, skipping.");
35
                continue;
36
            }
37
38
            $byUrl = [];
39
            foreach ($rows as $r) {
40
                $urlKey = $r['access_url'] === null ? 'NULL' : (string)$r['access_url'];
41
                $byUrl[$urlKey][] = $r;
42
            }
43
44
            foreach ($byUrl as $urlKey => $group) {
45
                $main = null;
46
                foreach ($group as $r) {
47
                    if ($r['category'] === 'display') {
48
                        $main = $r;
49
                        break;
50
                    }
51
                }
52
53
                if (!$main) {
54
                    $main = $group[0];
55
                    $conn->update('settings', ['category' => 'display'], ['id' => $main['id']]);
56
                    $this->dbg("Moved '{$var}' id={$main['id']} to category 'display'.");
57
                }
58
59
                foreach ($group as $r) {
60
                    if ($r['id'] === $main['id']) {
61
                        continue;
62
                    }
63
64
                    if (empty($main['selected_value']) && !empty($r['selected_value'])) {
65
                        $conn->update('settings', ['selected_value' => $r['selected_value']], ['id' => $main['id']]);
66
                        $this->dbg("Updated '{$var}' id={$main['id']} with non-empty value from id={$r['id']}.");
67
                    }
68
69
                    $conn->delete('settings', ['id' => $r['id']]);
70
                    $this->dbg("Deleted duplicate '{$var}' id={$r['id']} (category={$r['category']}).");
71
                }
72
            }
73
        }
74
75
        $this->dbg('--- [END] show_tabs consolidation done ---');
76
    }
77
78
    public function down(Schema $schema): void
79
    {
80
        $this->dbg('No down migration: duplicates will not be restored.');
81
    }
82
83
    private function dbg(string $msg): void
84
    {
85
        if (self::DEBUG) {
86
            error_log('[MIG][show_tabs] ' . $msg);
87
        }
88
    }
89
}
90