Passed
Pull Request — master (#6772)
by
unknown
08:39
created

Version20250918152000::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 12
rs 9.8666
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\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
final class Version20250918152000 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return "Mail settings: fix titles/comments and upsert defaults for mailer_from_email & mailer_from_name in settings.";
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $pairs = [
22
            [
23
                'variable' => 'mailer_from_email',
24
                'title' => 'Send all e-mails from this e-mail address',
25
                'comment' => 'Sets the default email address used in the "from" field of emails.',
26
                'category' => 'mail',
27
                'default' => '',
28
            ],
29
            [
30
                'variable' => 'mailer_from_name',
31
                'title' => 'Send all e-mails as originating from this (organizational) name',
32
                'comment' => 'Sets the default display name used for sending platform emails. e.g. "Support team".',
33
                'category' => 'mail',
34
                'default' => '',
35
            ],
36
        ];
37
38
        foreach ($pairs as $p) {
39
            $exists = (int) $this->connection->fetchOne(
40
                "SELECT COUNT(*) FROM settings
41
                 WHERE variable = ? AND subkey IS NULL AND access_url = 1",
42
                [$p['variable']]
43
            );
44
45
            if ($exists === 0) {
46
                $this->connection->executeStatement(
47
                    "INSERT INTO settings
48
                        (variable, subkey, type, category, selected_value, title, comment,
49
                         access_url_changeable, access_url_locked, access_url)
50
                     VALUES
51
                        (?, NULL, NULL, ?, ?, ?, ?, 1, 0, 1)",
52
                    [$p['variable'], $p['category'], $p['default'], $p['title'], $p['comment']]
53
                );
54
                $this->write(sprintf("Inserted missing setting: %s", $p['variable']));
55
            } else {
56
                $this->connection->executeStatement(
57
                    "UPDATE settings
58
                     SET title = ?, comment = ?, category = ?
59
                     WHERE variable = ? AND subkey IS NULL AND access_url = 1",
60
                    [$p['title'], $p['comment'], $p['category'], $p['variable']]
61
                );
62
                $this->write(sprintf("Updated setting metadata: %s", $p['variable']));
63
            }
64
        }
65
    }
66
67
    public function down(Schema $schema): void
68
    {
69
        $this->connection->executeStatement(
70
            "UPDATE settings
71
             SET title = 'Send all e-mails as originating from this (organizational) name',
72
                 comment = 'Sets the default display name used for sending platform emails. e.g. \"Support team\".'
73
             WHERE variable = 'mailer_from_email'
74
               AND subkey IS NULL AND access_url = 1"
75
        );
76
77
        $this->connection->executeStatement(
78
            "UPDATE settings
79
             SET title = 'Send all e-mails from this e-mail address',
80
                 comment = 'Sets the default email address used in the \"from\" field of emails.'
81
             WHERE variable = 'mailer_from_name'
82
               AND subkey IS NULL AND access_url = 1"
83
        );
84
    }
85
}
86