Passed
Push — master ( db12d9...b58c60 )
by Julito
12:19
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\ResourceLink;
8
use Chamilo\CoreBundle\Entity\SettingsCurrent;
9
use Chamilo\CoreBundle\Entity\SettingsOptions;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\Repository\Node\UserRepository;
12
use Chamilo\CoreBundle\Repository\SessionRepository;
13
use Chamilo\CourseBundle\Repository\CGroupRepository;
14
use Doctrine\DBAL\Connection;
15
use Doctrine\Migrations\AbstractMigration;
16
use Doctrine\ORM\EntityManager;
17
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Symfony\Component\HttpFoundation\File\UploadedFile;
20
21
/**
22
 * Class AbstractMigrationChamilo.
23
 */
24
abstract class AbstractMigrationChamilo extends AbstractMigration implements ContainerAwareInterface
25
{
26
    public const BATCH_SIZE = 20;
27
    private $manager;
28
    private $container;
29
30
    public function setEntityManager(EntityManager $manager)
31
    {
32
        $this->manager = $manager;
33
    }
34
35
    public function setContainer(ContainerInterface $container = null)
36
    {
37
        $this->container = $container;
38
    }
39
40
    /**
41
     * @return ContainerInterface
42
     */
43
    public function getContainer()
44
    {
45
        return $this->container;
46
    }
47
48
    public function getAdmin(): User
49
    {
50
        $container = $this->getContainer();
51
        $em = $this->getEntityManager();
52
        $connection = $em->getConnection();
53
        $userRepo = $container->get(UserRepository::class);
54
55
        $sql = 'SELECT id, user_id FROM admin ORDER BY id LIMIT 1';
56
        $result = $connection->executeQuery($sql);
57
        $adminRow = $result->fetchAssociative();
58
        $adminId = $adminRow['user_id'];
59
60
        return $userRepo->find($adminId);
61
    }
62
63
    /**
64
     * @return EntityManager
65
     */
66
    public function getEntityManager()
67
    {
68
        if (empty($this->manager)) {
69
            //$params = $this->connection->getParams();
70
            /*
71
            $dbParams = [
72
                'driver' => 'pdo_mysql',
73
                'host' => $this->connection->getHost(),
74
                'user' => $this->connection->getUsername(),
75
                'password' => $this->connection->getPassword(),
76
                'dbname' => $this->connection->getDatabase(),
77
                'port' => $this->connection->getPort(),
78
            ];*/
79
            /*$database = new \Database();
80
            $database->connect(
81
                $params,
82
                __DIR__.'/../../',
83
                __DIR__.'/../../'
84
            );
85
            $this->manager = $database->getManager();*/
86
        }
87
88
        return $this->manager;
89
    }
90
91
    /**
92
     * Speeds up SettingsCurrent creation.
93
     *
94
     * @param string $variable            The variable itself
95
     * @param string $subKey              The subkey
96
     * @param string $type                The type of setting (text, radio, select, etc)
97
     * @param string $category            The category (Platform, User, etc)
98
     * @param string $selectedValue       The default value
99
     * @param string $title               The setting title string name
100
     * @param string $comment             The setting comment string name
101
     * @param string $scope               The scope
102
     * @param string $subKeyText          Text if there is a subKey
103
     * @param int    $accessUrl           What URL it is for
104
     * @param bool   $accessUrlChangeable Whether it can be changed on each url
105
     * @param bool   $accessUrlLocked     Whether the setting for the current URL is
106
     *                                    locked to the current value
107
     * @param array  $options             Optional array in case of a radio-type field,
108
     *                                    to insert options
109
     */
110
    public function addSettingCurrent(
111
        $variable,
112
        $subKey,
113
        $type,
114
        $category,
115
        $selectedValue,
116
        $title,
117
        $comment,
118
        $scope = '',
119
        $subKeyText = '',
120
        $accessUrl = 1,
121
        $accessUrlChangeable = false,
122
        $accessUrlLocked = true,
123
        $options = []
124
    ) {
125
        $setting = new SettingsCurrent();
126
        $setting
127
            ->setVariable($variable)
128
            ->setSubkey($subKey)
129
            ->setType($type)
130
            ->setCategory($category)
131
            ->setSelectedValue($selectedValue)
132
            ->setTitle($title)
133
            ->setComment($comment)
134
            ->setScope($scope)
135
            ->setSubkeytext($subKeyText)
136
            ->setUrl($accessUrl)
137
            ->setAccessUrlChangeable($accessUrlChangeable)
138
            ->setAccessUrlLocked($accessUrlLocked);
139
140
        $this->getEntityManager()->persist($setting);
141
142
        if (count($options) > 0) {
143
            foreach ($options as $option) {
144
                if (empty($option['text'])) {
145
                    if ('true' == $option['value']) {
146
                        $option['text'] = 'Yes';
147
                    } else {
148
                        $option['text'] = 'No';
149
                    }
150
                }
151
152
                $settingOption = new SettingsOptions();
153
                $settingOption
154
                    ->setVariable($variable)
155
                    ->setValue($option['value'])
156
                    ->setDisplayText($option['text']);
157
158
                $this->getEntityManager()->persist($settingOption);
159
            }
160
        }
161
        $this->getEntityManager()->flush();
162
    }
163
164
    /**
165
     * @param string $variable
166
     */
167
    public function getConfigurationValue($variable)
168
    {
169
        global $_configuration;
170
        if (isset($_configuration[$variable])) {
171
            return $_configuration[$variable];
172
        }
173
174
        return false;
175
    }
176
177
    /**
178
     * Remove a setting completely.
179
     *
180
     * @param string $variable The setting variable name
181
     */
182
    public function removeSettingCurrent($variable)
183
    {
184
        //to be implemented
185
    }
186
187
    public function addLegacyFileToResource($filePath, $repo, $resource, $id)
188
    {
189
        if (!is_dir($filePath)) {
190
            $class = get_class($resource);
191
            $documentPath = basename($filePath);
192
            if (file_exists($filePath)) {
193
                $mimeType = mime_content_type($filePath);
194
                $file = new UploadedFile($filePath, basename($documentPath), $mimeType, null, true);
195
                if ($file) {
0 ignored issues
show
introduced by
$file is of type Symfony\Component\HttpFoundation\File\UploadedFile, thus it always evaluated to true.
Loading history...
196
                    $repo->addFile($resource, $file);
197
                } else {
198
                    $this->warnIf(true, "Cannot migrate $class #$id path: $documentPath ");
199
                }
200
            } else {
201
                $this->warnIf(true, "Cannot migrate $class #'.$id.' file not found: $documentPath");
202
            }
203
        }
204
    }
205
206
    public function fixItemProperty($repo, $course, $admin, $resource, $parent, $items)
207
    {
208
        if (empty($items)) {
209
            return;
210
        }
211
212
        $container = $this->getContainer();
213
        $doctrine = $container->get('doctrine');
214
        $em = $doctrine->getManager();
215
        $sessionRepo = $container->get(SessionRepository::class);
216
        $groupRepo = $container->get(CGroupRepository::class);
217
        $userRepo = $container->get(UserRepository::class);
218
219
        $resource->setParent($parent);
220
        $resourceNode = null;
221
        $userList = [];
222
        $groupList = [];
223
        $sessionList = [];
224
        foreach ($items as $item) {
225
            $visibility = $item['visibility'];
226
            $sessionId = $item['session_id'];
227
            $userId = $item['insert_user_id'];
228
            $groupId = $item['to_group_id'];
229
230
            $newVisibility = ResourceLink::VISIBILITY_PENDING;
231
            switch ($visibility) {
232
                case 0:
233
                    $newVisibility = ResourceLink::VISIBILITY_PENDING;
234
                    break;
235
                case 1:
236
                    $newVisibility = ResourceLink::VISIBILITY_PUBLISHED;
237
                    break;
238
                case 2:
239
                    $newVisibility = ResourceLink::VISIBILITY_DELETED;
240
                    break;
241
            }
242
243
            if (isset($userList[$userId])) {
244
                $user = $userList[$userId];
245
            } else {
246
                $user = $userList[$userId] = $userRepo->find($userId);
247
            }
248
249
            if (null === $user) {
250
                $user = $admin;
251
            }
252
253
            $session = null;
254
            if (!empty($sessionId)) {
255
                if (isset($sessionList[$sessionId])) {
256
                    $session = $sessionList[$sessionId];
257
                } else {
258
                    $session = $sessionList[$sessionId] = $sessionRepo->find($sessionId);
259
                }
260
            }
261
262
            $group = null;
263
            if (!empty($groupId)) {
264
                if (isset($groupList[$groupId])) {
265
                    $group = $groupList[$groupId];
266
                } else {
267
                    $group = $groupList[$groupId] = $groupRepo->find($groupId);
268
                }
269
            }
270
271
            if (null === $resourceNode) {
272
                $resourceNode = $repo->addResourceNode($resource, $user, $parent);
273
                $em->persist($resourceNode);
274
            }
275
            $resource->addCourseLink($course, $session, $group, $newVisibility);
276
            $em->persist($resource);
277
        }
278
    }
279
}
280