Passed
Pull Request — master (#6840)
by
unknown
09:49
created

Version20240416110300   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
C up() 0 58 15
A down() 0 1 1
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
        $protectedFlagsVars = ['my_terms', 'terms_ville', 'terms_villedustage'];
26
27
        foreach ($extraFields as $field) {
28
            $existingField = $this->connection->executeQuery(
29
                'SELECT * FROM extra_field WHERE variable = :variable AND item_type = :item_type',
30
                [
31
                    'variable' => $field['variable'],
32
                    'item_type' => $field['item_type'],
33
                ]
34
            )->fetchAssociative();
35
36
            if (!$existingField) {
37
                // Insert new field if it does not exist
38
                $this->connection->insert('extra_field', [
39
                    'item_type'        => $field['item_type'],
40
                    'value_type'       => $field['value_type'],
41
                    'variable'         => $field['variable'],
42
                    'display_text'     => $field['display_text'],
43
                    'visible_to_self'  => isset($field['visible_to_self'])  ? (int) $field['visible_to_self']  : 0,
44
                    'visible_to_others'=> isset($field['visible_to_others'])? (int) $field['visible_to_others']: 0,
45
                    'changeable'       => isset($field['changeable'])       ? (int) $field['changeable']       : 0,
46
                    'filter'           => isset($field['filter'])           ? (int) $field['filter']           : 0,
47
                    'created_at'       => (new \DateTime())->format('Y-m-d H:i:s'),
48
                ]);
49
                continue;
50
            }
51
52
            $updateData = [
53
                'display_text' => $field['display_text'],
54
            ];
55
56
            if (isset($field['filter'])) {
57
                $updateData['filter'] = (int) $field['filter'];
58
            }
59
60
            if (isset($field['value_type']) && (int)$existingField['value_type'] !== (int)$field['value_type']) {
61
                $updateData['value_type'] = (int)$field['value_type'];
62
            }
63
64
            if (!in_array($field['variable'], $protectedFlagsVars, true)) {
65
                if (array_key_exists('visible_to_self', $field)) {
66
                    $updateData['visible_to_self'] = (int) $field['visible_to_self'];
67
                }
68
                if (array_key_exists('visible_to_others', $field)) {
69
                    $updateData['visible_to_others'] = (int) $field['visible_to_others'];
70
                }
71
                if (array_key_exists('changeable', $field)) {
72
                    $updateData['changeable'] = (int) $field['changeable'];
73
                }
74
            }
75
76
            if (!empty($updateData)) {
77
                $this->connection->update('extra_field', $updateData, [
78
                    'id' => $existingField['id'],
79
                ]);
80
            }
81
        }
82
    }
83
84
    public function down(Schema $schema): void {}
85
}
86