Passed
Push — master ( 5e2840...d88203 )
by Julito
07:41
created

AbstractMigrationChamilo::removeSettingCurrent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Migrations;
6
7
use Chamilo\CoreBundle\Entity\SettingsCurrent;
8
use Chamilo\CoreBundle\Entity\SettingsOptions;
9
use Chamilo\CoreBundle\Entity\User;
10
use Chamilo\CoreBundle\Repository\UserRepository;
11
use Doctrine\DBAL\Connection;
12
use Doctrine\Migrations\AbstractMigration;
13
use Doctrine\ORM\EntityManager;
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
/**
18
 * Class AbstractMigrationChamilo.
19
 */
20
abstract class AbstractMigrationChamilo extends AbstractMigration implements ContainerAwareInterface
21
{
22
    private $manager;
23
    private $container;
24
25
    public function setEntityManager(EntityManager $manager)
26
    {
27
        $this->manager = $manager;
28
    }
29
30
    public function setContainer(ContainerInterface $container = null)
31
    {
32
        $this->container = $container;
33
    }
34
35
    /**
36
     * @return ContainerInterface
37
     */
38
    public function getContainer()
39
    {
40
        return $this->container;
41
    }
42
43
    public function getAdmin(): User
44
    {
45
        $container = $this->getContainer();
46
        $em = $this->getEntityManager();
47
        /** @var Connection $connection */
48
        $connection = $em->getConnection();
49
        $userRepo = $container->get(UserRepository::class);
50
51
        $sql = 'SELECT id, user_id FROM admin ORDER BY id LIMIT 1';
52
        $result = $connection->executeQuery($sql);
53
        $adminRow = $result->fetchAssociative();
54
        $adminId = $adminRow['user_id'];
55
56
        return $userRepo->find($adminId);
57
    }
58
59
    /**
60
     * @return EntityManager
61
     */
62
    public function getEntityManager()
63
    {
64
        if (empty($this->manager)) {
65
            //$params = $this->connection->getParams();
66
            /*
67
            $dbParams = [
68
                'driver' => 'pdo_mysql',
69
                'host' => $this->connection->getHost(),
70
                'user' => $this->connection->getUsername(),
71
                'password' => $this->connection->getPassword(),
72
                'dbname' => $this->connection->getDatabase(),
73
                'port' => $this->connection->getPort(),
74
            ];*/
75
            /*$database = new \Database();
76
            $database->connect(
77
                $params,
78
                __DIR__.'/../../',
79
                __DIR__.'/../../'
80
            );
81
            $this->manager = $database->getManager();*/
82
        }
83
84
        return $this->manager;
85
    }
86
87
    /**
88
     * Speeds up SettingsCurrent creation.
89
     *
90
     * @param string $variable            The variable itself
91
     * @param string $subKey              The subkey
92
     * @param string $type                The type of setting (text, radio, select, etc)
93
     * @param string $category            The category (Platform, User, etc)
94
     * @param string $selectedValue       The default value
95
     * @param string $title               The setting title string name
96
     * @param string $comment             The setting comment string name
97
     * @param string $scope               The scope
98
     * @param string $subKeyText          Text if there is a subKey
99
     * @param int    $accessUrl           What URL it is for
100
     * @param bool   $accessUrlChangeable Whether it can be changed on each url
101
     * @param bool   $accessUrlLocked     Whether the setting for the current URL is
102
     *                                    locked to the current value
103
     * @param array  $options             Optional array in case of a radio-type field,
104
     *                                    to insert options
105
     */
106
    public function addSettingCurrent(
107
        $variable,
108
        $subKey,
109
        $type,
110
        $category,
111
        $selectedValue,
112
        $title,
113
        $comment,
114
        $scope = '',
115
        $subKeyText = '',
116
        $accessUrl = 1,
117
        $accessUrlChangeable = false,
118
        $accessUrlLocked = true,
119
        $options = []
120
    ) {
121
        $setting = new SettingsCurrent();
122
        $setting
123
            ->setVariable($variable)
124
            ->setSubkey($subKey)
125
            ->setType($type)
126
            ->setCategory($category)
127
            ->setSelectedValue($selectedValue)
128
            ->setTitle($title)
129
            ->setComment($comment)
130
            ->setScope($scope)
131
            ->setSubkeytext($subKeyText)
132
            ->setUrl($accessUrl)
133
            ->setAccessUrlChangeable($accessUrlChangeable)
134
            ->setAccessUrlLocked($accessUrlLocked);
135
136
        $this->getEntityManager()->persist($setting);
137
138
        if (count($options) > 0) {
139
            foreach ($options as $option) {
140
                if (empty($option['text'])) {
141
                    if ('true' == $option['value']) {
142
                        $option['text'] = 'Yes';
143
                    } else {
144
                        $option['text'] = 'No';
145
                    }
146
                }
147
148
                $settingOption = new SettingsOptions();
149
                $settingOption
150
                    ->setVariable($variable)
151
                    ->setValue($option['value'])
152
                    ->setDisplayText($option['text']);
153
154
                $this->getEntityManager()->persist($settingOption);
155
            }
156
        }
157
        $this->getEntityManager()->flush();
158
    }
159
160
    /**
161
     * @param string $variable
162
     */
163
    public function getConfigurationValue($variable)
164
    {
165
        global $_configuration;
166
        if (isset($_configuration[$variable])) {
167
            return $_configuration[$variable];
168
        }
169
170
        return false;
171
    }
172
173
    /**
174
     * Remove a setting completely.
175
     *
176
     * @param string $variable The setting variable name
177
     */
178
    public function removeSettingCurrent($variable)
179
    {
180
        //to be implemented
181
    }
182
}
183