Passed
Push — dependabot/npm_and_yarn/microm... ( e84ba6...f2f212 )
by
unknown
10:03
created

Version20240416110300::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
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\ExtraFieldFixtures;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use DateTime;
12
use Doctrine\DBAL\Schema\Schema;
13
14
class Version20240416110300 extends AbstractMigrationChamilo
15
{
16
    public function getDescription(): string
17
    {
18
        return 'Adds missing extra fields to the database based on the predefined list';
19
    }
20
21
    public function up(Schema $schema): void
22
    {
23
        $extraFields = ExtraFieldFixtures::getExtraFields();
24
25
        foreach ($extraFields as $field) {
26
            $existingField = $this->connection->executeQuery(
27
                'SELECT * FROM extra_field WHERE variable = :variable AND item_type = :item_type',
28
                [
29
                    'variable' => $field['variable'],
30
                    'item_type' => $field['item_type'],
31
                ]
32
            )->fetchAssociative();
33
34
            if (!$existingField) {
35
                // Insert new field if it does not exist
36
                $this->connection->insert('extra_field', [
37
                    'item_type' => $field['item_type'],
38
                    'value_type' => $field['value_type'],
39
                    'variable' => $field['variable'],
40
                    'display_text' => $field['display_text'],
41
                    'visible_to_self' => isset($field['visible_to_self']) ? (int) $field['visible_to_self'] : 0,
42
                    'visible_to_others' => isset($field['visible_to_others']) ? (int) $field['visible_to_others'] : 0,
43
                    'changeable' => isset($field['changeable']) ? (int) $field['changeable'] : 0,
44
                    'filter' => isset($field['filter']) ? (int) $field['filter'] : 0,
45
                    'created_at' => (new DateTime())->format('Y-m-d H:i:s'),
46
                ]);
47
            } else {
48
                // Update existing field
49
                $this->connection->update('extra_field', [
50
                    'display_text' => $field['display_text'],
51
                    'visible_to_self' => isset($field['visible_to_self']) ? (int) $field['visible_to_self'] : 0,
52
                    'visible_to_others' => isset($field['visible_to_others']) ? (int) $field['visible_to_others'] : 0,
53
                    'changeable' => isset($field['changeable']) ? (int) $field['changeable'] : 0,
54
                    'filter' => isset($field['filter']) ? (int) $field['filter'] : 0,
55
                ], [
56
                    'id' => $existingField['id'],
57
                ]);
58
            }
59
        }
60
    }
61
62
    public function down(Schema $schema): void {}
63
}
64