Passed
Push — master ( 1c618d...c1c6b0 )
by Yannick
10:36 queued 02:52
created

getConfigurationSelectedValue()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 6
nop 1
dl 0
loc 22
rs 9.7333
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\SettingsCurrentFixtures;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Doctrine\DBAL\Schema\Schema;
12
13
use const JSON_UNESCAPED_SLASHES;
14
use const JSON_UNESCAPED_UNICODE;
15
16
class Version20230216122900 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Migrate configuration values to settings_current';
21
    }
22
23
    public function up(Schema $schema): void
24
    {
25
        $configurationValues = SettingsCurrentFixtures::getNewConfigurationSettings();
26
27
        foreach ($configurationValues as $category => $settings) {
28
            foreach ($settings as $setting) {
29
                $variable = $setting['name'];
30
                $category = strtolower($category);
31
                $result = $this->connection
32
                    ->executeQuery(
33
                        "SELECT COUNT(1) FROM settings_current WHERE variable = '$variable' AND category = '{$category}'"
34
                    )
35
                ;
36
                $count = $result->fetchNumeric()[0];
37
                $selectedValue = $this->getConfigurationSelectedValue($variable);
38
                error_log('Migration: Setting variable '.$variable.' category '.$category.' value '.$selectedValue);
39
40
                // To use by default courses page if this option is not empty.
41
                if ('redirect_index_to_url_for_logged_users' === $variable && !empty($selectedValue)) {
42
                    $selectedValue = 'courses';
43
                }
44
                if (empty($count)) {
45
                    $this->addSql(
46
                        "INSERT INTO settings_current (access_url, variable, category, selected_value, title, access_url_changeable, access_url_locked) VALUES (1, '{$variable}', '{$category}', '{$selectedValue}', '{$variable}', 1, 1)"
47
                    );
48
                } else {
49
                    $this->addSql(
50
                        "UPDATE settings_current SET selected_value = '{$selectedValue}', category = '{$category}' WHERE variable = '$variable' AND category = '{$category}'"
51
                    );
52
                }
53
            }
54
        }
55
56
        // Rename setting for hierarchical skill presentation.
57
        $this->addSql(
58
            "UPDATE settings_current SET variable = 'skills_hierarchical_view_in_user_tracking', title = 'skills_hierarchical_view_in_user_tracking' WHERE variable = 'table_of_hierarchical_skill_presentation'"
59
        );
60
61
        // Insert extra fields required.
62
        $result = $this->connection
63
            ->executeQuery(
64
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'session_courses_read_only_mode' AND item_type = 2 AND value_type = 13"
65
            )
66
        ;
67
        $count = $result->fetchNumeric()[0];
68
        if (empty($count)) {
69
            $this->addSql(
70
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (2, 13, 'session_courses_read_only_mode', 'Lock Course In Session', 1, 1, 1, NOW())"
71
            );
72
        }
73
74
        // Insert extra fields required.
75
        $result = $this->connection
76
            ->executeQuery(
77
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'is_mandatory' AND item_type = 12 AND value_type = 13"
78
            )
79
        ;
80
        $count = $result->fetchNumeric()[0];
81
        if (empty($count)) {
82
            $this->addSql(
83
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (12, 13, 'is_mandatory', 'IsMandatory', 1, 1, 1, NOW())"
84
            );
85
        }
86
87
        $result = $this->connection
88
            ->executeQuery(
89
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'show_in_catalogue' AND item_type = 2 AND value_type = 3"
90
            )
91
        ;
92
        $count = $result->fetchNumeric()[0];
93
        if (empty($count)) {
94
            $this->addSql(
95
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (2, 3, 'show_in_catalogue', 'Show in catalogue', 1, 1, 0, NOW())"
96
            );
97
            $this->addSql(
98
                'SET @ef_id = LAST_INSERT_ID()'
99
            );
100
            $this->addSql(
101
                "INSERT INTO extra_field_options (field_id, option_value, display_text, priority, priority_message, option_order) VALUES (@ef_id, '1', 'Yes', NULL, NULL, 1), (@ef_id, '0', 'No', NULL, NULL, 2)"
102
            );
103
        }
104
105
        $result = $this->connection
106
            ->executeQuery(
107
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'multiple_language' AND item_type = 2 AND value_type = 5"
108
            )
109
        ;
110
        $count = $result->fetchNumeric()[0];
111
        if (empty($count)) {
112
            $this->addSql(
113
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (2, 5, 'multiple_language', 'Multiple Language', 1, 1, 1, NOW())"
114
            );
115
        }
116
117
        $result = $this->connection
118
            ->executeQuery(
119
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'send_notification_at_a_specific_date' AND item_type = 21 AND value_type = 13"
120
            )
121
        ;
122
        $count = $result->fetchNumeric()[0];
123
        if (empty($count)) {
124
            $this->addSql(
125
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (21, 13, 'send_notification_at_a_specific_date', 'Send notification at a specific date', 1, 1, 1, NOW())"
126
            );
127
        }
128
129
        $result = $this->connection
130
            ->executeQuery(
131
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'date_to_send_notification' AND item_type = 21 AND value_type = 6"
132
            )
133
        ;
134
        $count = $result->fetchNumeric()[0];
135
        if (empty($count)) {
136
            $this->addSql(
137
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (21, 6, 'date_to_send_notification', 'Date to send notification', 1, 1, 1, NOW())"
138
            );
139
        }
140
141
        $result = $this->connection
142
            ->executeQuery(
143
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'send_to_users_in_session' AND item_type = 21 AND value_type = 13"
144
            )
145
        ;
146
        $count = $result->fetchNumeric()[0];
147
        if (empty($count)) {
148
            $this->addSql(
149
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (21, 13, 'send_to_users_in_session', 'Send to users in session', 1, 1, 1, NOW())"
150
            );
151
        }
152
153
        $result = $this->connection
154
            ->executeQuery(
155
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'tags' AND item_type = 22 AND value_type = 10"
156
            )
157
        ;
158
        $count = $result->fetchNumeric()[0];
159
        if (empty($count)) {
160
            $this->addSql(
161
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (22, 10, 'tags', 'Tags', 1, 1, 1, NOW())"
162
            );
163
        }
164
165
        $result = $this->connection
166
            ->executeQuery(
167
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'acquisition' AND item_type = 20 AND value_type = 3"
168
            )
169
        ;
170
        $count = $result->fetchNumeric()[0];
171
        if (empty($count)) {
172
            $this->addSql(
173
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (20, 3, 'acquisition', 'Acquisition', 1, 1, 0, NOW())"
174
            );
175
            $this->addSql(
176
                'SET @ef_id = LAST_INSERT_ID()'
177
            );
178
            $this->addSql(
179
                "INSERT INTO extra_field_options (field_id, option_value, display_text, priority, priority_message, option_order) VALUES (@ef_id, '1', 'Acquired', NULL, NULL, 1), (@ef_id, '2', 'In the process of acquisition', NULL, NULL, 2), (@ef_id, '3', 'Not acquired', NULL, NULL, 3)"
180
            );
181
        }
182
183
        $result = $this->connection
184
            ->executeQuery(
185
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'invisible' AND item_type = 20 AND value_type = 13"
186
            )
187
        ;
188
        $count = $result->fetchNumeric()[0];
189
        if (empty($count)) {
190
            $this->addSql(
191
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (20, 13, 'invisible', 'Invisible', 1, 1, 1, NOW())"
192
            );
193
        }
194
195
        $result = $this->connection
196
            ->executeQuery(
197
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'start_date' AND item_type = 7 AND value_type = 7"
198
            )
199
        ;
200
        $count = $result->fetchNumeric()[0];
201
        if (empty($count)) {
202
            $this->addSql(
203
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (7, 7, 'start_date', 'StartDate', 1, 1, 1, NOW())"
204
            );
205
        }
206
207
        $result = $this->connection
208
            ->executeQuery(
209
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'end_date' AND item_type = 7 AND value_type = 7"
210
            )
211
        ;
212
        $count = $result->fetchNumeric()[0];
213
        if (empty($count)) {
214
            $this->addSql(
215
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (7, 7, 'end_date', 'EndDate', 1, 1, 1, NOW())"
216
            );
217
        }
218
219
        $attachmentExists = $this->connection->fetchOne("SELECT COUNT(*) FROM extra_field WHERE variable = 'attachment' AND item_type = 13");
220
        if (0 == $attachmentExists) {
221
            $this->addSql(
222
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (13, 18, 'attachment', 'Attachment', 1, 1, 1, NOW())"
223
            );
224
        }
225
226
        $sendToCoachesExists = $this->connection->fetchOne("SELECT COUNT(*) FROM extra_field WHERE variable = 'send_to_coaches' AND item_type = 13");
227
        if (0 == $sendToCoachesExists) {
228
            $this->addSql(
229
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (13, 13, 'send_to_coaches', 'Send to Coaches', 1, 1, 1, NOW())"
230
            );
231
        }
232
233
        $workTimeExists = $this->connection->fetchOne("SELECT COUNT(*) FROM extra_field WHERE variable = 'work_time' AND item_type = 9");
234
        if (0 == $workTimeExists) {
235
            $this->addSql(
236
                "INSERT INTO extra_field (item_type, value_type, variable, display_text, visible_to_self, changeable, filter, created_at) VALUES (9, 15, 'work_time', 'Considered working time', 1, 1, 1, NOW())"
237
            );
238
        }
239
    }
240
241
    public function down(Schema $schema): void
242
    {
243
        $configurationValues = SettingsCurrentFixtures::getNewConfigurationSettings();
244
245
        foreach ($configurationValues as $category => $settings) {
246
            foreach ($settings as $setting) {
247
                $variable = $setting['name'];
248
                $category = strtolower($category);
249
                $result = $this->connection
250
                    ->executeQuery(
251
                        "SELECT COUNT(1) FROM settings_current WHERE variable = '$variable' AND category = '$category'"
252
                    )
253
                ;
254
                $count = $result->fetchNumeric()[0];
255
                if (!empty($count)) {
256
                    $this->addSql(
257
                        "DELETE FROM settings_current WHERE variable = '{$variable}' AND category = '$category'"
258
                    );
259
                }
260
            }
261
        }
262
263
        // Delete extra fields required.
264
        $result = $this->connection
265
            ->executeQuery(
266
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'end_date' AND item_type = 7 AND value_type = 7"
267
            )
268
        ;
269
        $count = $result->fetchNumeric()[0];
270
        if (!empty($count)) {
271
            $this->addSql(
272
                "DELETE FROM extra_field WHERE variable = 'end_date' AND item_type = 7 AND value_type = 7"
273
            );
274
        }
275
276
        $result = $this->connection
277
            ->executeQuery(
278
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'start_date' AND item_type = 7 AND value_type = 7"
279
            )
280
        ;
281
        $count = $result->fetchNumeric()[0];
282
        if (!empty($count)) {
283
            $this->addSql(
284
                "DELETE FROM extra_field WHERE variable = 'start_date' AND item_type = 7 AND value_type = 7"
285
            );
286
        }
287
288
        $result = $this->connection
289
            ->executeQuery(
290
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'invisible' AND item_type = 20 AND value_type = 13"
291
            )
292
        ;
293
        $count = $result->fetchNumeric()[0];
294
        if (!empty($count)) {
295
            $this->addSql(
296
                "DELETE FROM extra_field WHERE variable = 'invisible' AND item_type = 20 AND value_type = 13"
297
            );
298
        }
299
300
        $result = $this->connection
301
            ->executeQuery(
302
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'acquisition' AND item_type = 20 AND value_type = 3"
303
            )
304
        ;
305
        $count = $result->fetchNumeric()[0];
306
        if (!empty($count)) {
307
            $this->addSql(
308
                "DELETE FROM extra_field WHERE variable = 'acquisition' AND item_type = 20 AND value_type = 3"
309
            );
310
        }
311
312
        $result = $this->connection
313
            ->executeQuery(
314
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'tags' AND item_type = 22 AND value_type = 10"
315
            )
316
        ;
317
        $count = $result->fetchNumeric()[0];
318
        if (!empty($count)) {
319
            $this->addSql(
320
                "DELETE FROM extra_field WHERE variable = 'tags' AND item_type = 22 AND value_type = 10"
321
            );
322
        }
323
324
        $result = $this->connection
325
            ->executeQuery(
326
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'multiple_language' AND item_type = 2 AND value_type = 5"
327
            )
328
        ;
329
        $count = $result->fetchNumeric()[0];
330
        if (!empty($count)) {
331
            $this->addSql(
332
                "DELETE FROM extra_field WHERE variable = 'multiple_language' AND item_type = 2 AND value_type = 5"
333
            );
334
        }
335
336
        $result = $this->connection
337
            ->executeQuery(
338
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'show_in_catalogue' AND item_type = 2 AND value_type = 3"
339
            )
340
        ;
341
        $count = $result->fetchNumeric()[0];
342
        if (!empty($count)) {
343
            $this->addSql(
344
                "DELETE FROM extra_field WHERE variable = 'show_in_catalogue' AND item_type = 2 AND value_type = 3"
345
            );
346
        }
347
348
        $result = $this->connection
349
            ->executeQuery(
350
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'session_courses_read_only_mode' AND item_type = 2 AND value_type = 13"
351
            )
352
        ;
353
        $count = $result->fetchNumeric()[0];
354
        if (!empty($count)) {
355
            $this->addSql(
356
                "DELETE FROM extra_field WHERE variable = 'session_courses_read_only_mode' AND item_type = 2 AND value_type = 13"
357
            );
358
        }
359
360
        $result = $this->connection
361
            ->executeQuery(
362
                "SELECT COUNT(1) FROM extra_field WHERE variable = 'is_mandatory' AND item_type = 12 AND value_type = 13"
363
            )
364
        ;
365
        $count = $result->fetchNumeric()[0];
366
        if (!empty($count)) {
367
            $this->addSql(
368
                "DELETE FROM extra_field WHERE variable = 'is_mandatory' AND item_type = 12 AND value_type = 13"
369
            );
370
        }
371
    }
372
373
    public function getConfigurationSelectedValue(string $variable): string
374
    {
375
        global $_configuration;
376
        $kernel = $this->container->get('kernel');
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

376
        /** @scrutinizer ignore-call */ 
377
        $kernel = $this->container->get('kernel');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
377
        $rootPath = $kernel->getProjectDir();
378
        $oldConfigPath = $rootPath.'/app/config/configuration.php';
379
        $configFileLoaded = \in_array($oldConfigPath, get_included_files(), true);
380
        if (!$configFileLoaded) {
381
            include_once $oldConfigPath;
382
        }
383
384
        $selectedValue = '';
385
        $settingValue = $this->getConfigurationValue($variable, $_configuration);
386
        if (\is_array($settingValue)) {
387
            $selectedValue = json_encode($settingValue, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
388
        } elseif (\is_bool($settingValue)) {
389
            $selectedValue = var_export($settingValue, true);
390
        } else {
391
            $selectedValue = (string) $settingValue;
392
        }
393
394
        return $selectedValue;
395
    }
396
}
397