Passed
Push — 1.10.x ( 68dcc8...a72a1c )
by
unknown
145:06 queued 96:04
created

SubLanguageManager::make_available_language()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %
Metric Value
dl 9
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class SubLanguageManager
6
 * @package chamilo.admin.sublanguage
7
 */
8
class SubLanguageManager
9
{
10
    /**
11
     * Constructor
12
     */
13
    public function __construct()
14
    {
15
    }
16
    
17
    /**
18
     * Get all the languages
19
     * @return Array All information about sub-language
20
     */
21
    public static function getAllLanguages()
22
    {
23
        $table = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
24
        $sql = 'SELECT * FROM ' . $table;
25
        $rs = Database::query($sql);
26
        $all_languages = [];
27
        while ($row = Database::fetch_array($rs, 'ASSOC')) {
28
            $all_languages[] = $row;
29
        }
30
        
31
        return $all_languages;
32
    }
33
    
34
35
    /**
36
     * Get all files of lang folder (forum.inc.php,gradebook.inc.php,notebook.inc.php)
37
     * @param String The lang path folder  (/var/www/my_lms/main/lang/spanish)
38
     * @param bool true if we only want the "subname" trad4all instead of  trad4all.inc.php
39
     *
40
     * @return Array All file of lang folder
41
     */
42
    public static function get_lang_folder_files_list($path, $only_main_name = false)
43
    {
44
        $content_dir = array();
45
        if (is_dir($path)) {
46
            if ($dh = opendir($path)) {
47
                while (($file = readdir($dh)) !== false) {
48
                    if ($file[0] <> '.' && substr($file, -4, strlen($file)) == '.php') {
49
                        if ($only_main_name) {
50
                            if ($file != '' && strpos($file, '.inc.php'))
51
                                $content_dir[] = substr($file, 0, strpos($file, '.inc.php'));
52
                        } else {
53
                            $content_dir[] = $file;
54
                        }
55
                    }
56
                }
57
            }
58
            closedir($dh);
59
60
            return $content_dir;
61
        }
62
    }
63
64
    /**
65
     * Get all information of sub-language
66
     * @param Integer The parent id(Language father id)
67
     * @param Integer The sub language id
68
     * @return Array All information about sub-language
69
     */
70 View Code Duplication
    public static function get_all_information_of_sub_language($parent_id, $sub_language_id)
71
    {
72
        $table = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
73
        $sql = 'SELECT * FROM ' . $table . '
74
                WHERE
75
                    parent_id= ' . intval($parent_id).' AND
76
                    id= ' . intval($sub_language_id) . '';
77
        $rs = Database::query($sql);
78
        $all_information = array();
79
        while ($row = Database::fetch_array($rs, 'ASSOC')) {
80
            $all_information = $row;
81
        }
82
83
        return $all_information;
84
    }
85
86
    /**
87
     * Get all information of language
88
     * @param Integer The parent id(Language father id)
89
     * @return Array All information about language
90
     */
91 View Code Duplication
    public static function get_all_information_of_language($parent_id)
92
    {
93
        $table = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
94
        $sql = 'SELECT * FROM ' . $table . ' WHERE id = "' . intval($parent_id) . '"';
95
        $rs = Database::query($sql);
96
        $all_information = array();
97
        while ($row = Database::fetch_array($rs, 'ASSOC')) {
98
            $all_information = $row;
99
        }
100
101
        return $all_information;
102
    }
103
104
    /**
105
     * Get all information of chamilo file
106
     * @param String The chamilo path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php)
107
     * @patam Bool Whether we want to remove the '$' prefix in the results or not
108
     * @return Array Contains all information of chamilo file
109
     */
110
    public static function get_all_language_variable_in_file($system_path_file, $get_as_string_index = false)
111
    {
112
        $res_list = array();
113
        if (!is_readable($system_path_file)) {
114
            return $res_list;
115
        }
116
        $info_file = file($system_path_file);
117
        foreach ($info_file as $line) {
118
            if (substr($line, 0, 1) != '$') {
119
                continue;
120
            }
121
            list($var, $val) = split('=', $line, 2);
122
            $var = trim($var);
123
            $val = trim($val);
124
            if ($get_as_string_index) { //remove the prefix $
125
                $var = substr($var, 1);
126
            }
127
            $res_list[$var] = $val;
128
        }
129
130
        return $res_list;
131
    }
132
133
    /**
134
     * Add file in sub-language directory and add header(tag php)
135
     * @param String The chamilo path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php)
136
     * @return bool
137
     */
138
    public static function add_file_in_language_directory($system_path_file)
139
    {
140
        $return_value = @file_put_contents($system_path_file, '<?php' . PHP_EOL);
141
142
        return $return_value;
143
    }
144
145
    /**
146
     * Write in file of sub-language
147
     * @param String The path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php)
148
     * @param String The new sub-language
149
     * @param String The language variable
150
     * @return Boolean True on success, False on error
151
     */
152
    public static function write_data_in_file($path_file, $new_term, $new_variable)
153
    {
154
        $return_value = false;
155
        $new_data = $new_variable . '=' . $new_term;
156
        $resource = @fopen($path_file, "a");
157
        if (file_exists($path_file) && $resource) {
158
            if (fwrite($resource, $new_data . PHP_EOL) === false) {
159
                //not allow to write
160
                $return_value = false;
161
            } else {
162
                $return_value = true;
163
            }
164
            fclose($resource);
165
        }
166
167
        return $return_value;
168
    }
169
170
    /**
171
     * Add directory for sub-language
172
     * @param string $sub_language_dir The sub-language directory ( e.g. 'spanish_corporate' )
173
     * @return boolean  True on success, false on failure
174
     */
175
    public static function add_language_directory($sub_language_dir)
176
    {
177
        if (empty($sub_language_dir)) {
178
            return false;
179
        }
180
        $dir = api_get_path(SYS_LANG_PATH) . $sub_language_dir;
181
        if (is_dir($dir)) {
182
            return true;
183
        } //even if the dir already exists, we reach the objective of having the directory there
184
185
        return @mkdir($dir, api_get_permissions_for_new_directories());
186
    }
187
188
    /**
189
     * Delete sub-language.
190
     * In order to avoid deletion of main laguages, we check the existence of a parent
191
     * @param int The parent id
192
     * @return bool    True on success, false on error
193
     */
194
    public static function remove_sub_language($parent_id, $sub_language_id)
195
    {
196
        if (empty($parent_id) ||
197
            (intval($parent_id) != $parent_id) ||
198
            empty($sub_language_id) ||
199
            (intval($sub_language_id) != $sub_language_id)
200
        ) {
201
            return false;
202
        }
203
        $table = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
204
        $sql = 'SELECT dokeos_folder FROM ' . $table . '
205
                WHERE parent_id = ' . $parent_id . ' and id = ' . $sub_language_id;
206
        $res = Database::query($sql);
207
        if ($res === false or Database::num_rows($res) < 1) {
208
            return false;
209
        }
210
        $row = Database::fetch_assoc($res);
211
        $res = SubLanguageManager::remove_language_directory($row['dokeos_folder']);
212
        if ($res === false) {
213
            return false;
214
        } //can't delete dir, so do not delete language record
215
        $sql = 'DELETE FROM ' . $table . '
216
                WHERE id= ' . intval($sub_language_id);
217
        $res = Database::query($sql);
218
219
        return $res;
220
    }
221
222
    /**
223
     * Remove directory for sub-language
224
     * @param String The sub-language path directory ( e.g. 'spanish_corporate'' )
225
     * @return boolean  True on success, false on failure
226
     */
227
    public static function remove_language_directory($sub_language_dir)
228
    {
229
        if (empty($sub_language_dir)) {
230
            return false;
231
        }
232
        $dir = api_get_path(SYS_LANG_PATH) . $sub_language_dir;
233
        if (!is_dir($dir)) {
234
            return true;
235
        } //even if the dir does not exist, we reach the objective of not having the directory there
236
        $content = SubLanguageManager::get_lang_folder_files_list($dir);
237
238
        if (count($content) > 0) {
239
            foreach ($content as $value_content) {
0 ignored issues
show
Bug introduced by
The expression $content of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
240
                $path_file = $dir . '/' . $value_content;
241
                unlink($path_file);
242
            }
243
            return @rmdir($dir);
244
        } else {
245
            return @rmdir($dir);
246
        }
247
    }
248
249
    /**
250
     * check if language exist by id
251
     * @param int $language_id
252
     * @return bool
253
     */
254 View Code Duplication
    public static function check_if_exist_language_by_id($language_id)
255
    {
256
        $table = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
257
        $sql = 'SELECT count(*) as count
258
                FROM ' . $table . '
259
                WHERE id="' . intval($language_id) . '"';
260
        $rs = Database::query($sql);
261
        if (Database::num_rows($rs) > 0) {
262
            if (Database::result($rs, 0, 'count') == 1) {
263
                return true;
264
            } else {
265
                return false;
266
            }
267
        } else {
268
            return false;
269
        }
270
    }
271
272
    /**
273
     * Get name of language by id
274
     * @param Integer The language id
275
     * @return String The original name of language
276
     */
277
    public static function get_name_of_language_by_id($language_id)
278
    {
279
        $table = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
280
        $sql = 'SELECT original_name
281
                FROM ' . $table . '
282
                WHERE id= ' . intval($language_id) . '';
283
        $rs = Database::query($sql);
284
        if (Database::num_rows($rs) > 0) {
285
            return Database::result($rs, 0, 'original_name');
286
        } else {
287
            return '';
288
        }
289
    }
290
291
    /**
292
     * Verified if language is sub-language
293
     * @param int $language_id
294
     *
295
     * @return bool
296
     */
297 View Code Duplication
    public static function check_if_language_is_sub_language($language_id)
298
    {
299
        $table = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
300
        $sql = 'SELECT count(*) AS count FROM ' . $table . '
301
                WHERE id = ' . intval($language_id) . ' AND NOT ISNULL(parent_id)';
302
        $rs = Database::query($sql);
303
304
        if (Database::num_rows($rs) > 0 && Database::result($rs, '0', 'count') == 1) {
305
            return true;
306
        } else {
307
            return false;
308
        }
309
    }
310
311
    /**
312
     * @param int $language_id
313
     * @return bool
314
     */
315
    public static function check_if_language_is_used($language_id)
316
    {
317
        $language_info = self::get_all_information_of_language($language_id);
318
        $table = Database :: get_main_table(TABLE_MAIN_USER);
319
        $sql = 'SELECT count(*) AS count FROM ' . $table . '
320
                WHERE language ="' . Database::escape_string($language_info['english_name']).'"';
321
        $rs = Database::query($sql);
322
        if (Database::num_rows($rs) > 0 && Database::result($rs, '0', 'count') >= 1) {
323
            return true;
324
        } else {
325
            return false;
326
        }
327
    }
328
329
    /**
330
     * Verified if language is father of an sub-language
331
     * @param Integer The language id
332
     * @return Boolean
333
     */
334 View Code Duplication
    public static function check_if_language_is_father($language_id)
335
    {
336
        $table = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
337
        $sql = 'SELECT count(*) AS count FROM ' . $table . '
338
                WHERE parent_id= ' . intval($language_id) . ' AND NOT ISNULL(parent_id);';
339
        $rs = Database::query($sql);
340
341
        if (Database::num_rows($rs) > 0 && Database::result($rs, '0', 'count') == 1) {
342
            return true;
343
        } else {
344
            return false;
345
        }
346
    }
347
348
    /**
349
     * Make unavailable the language
350
     * @param Integer The language id
351
     * @return void()
0 ignored issues
show
Documentation introduced by
The doc-type void() could not be parsed: Expected "|" or "end of type", but got "(" at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
352
     */
353 View Code Duplication
    public static function make_unavailable_language($language_id)
354
    {
355
        $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
356
        $sql = "UPDATE $tbl_admin_languages SET available='0'
357
                WHERE id = " . intval($language_id) . "";
358
        $result = Database::query($sql);
359
360
        return $result !== false; //only return false on sql error
361
    }
362
363
    /**
364
     * Make available the language
365
     * @param Integer The language id
366
     * @return void
367
     */
368 View Code Duplication
    public static function make_available_language($language_id)
369
    {
370
        $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
371
        $sql = "UPDATE $tbl_admin_languages SET available='1'
372
                WHERE id = " . intval($language_id) . "";
373
        $result = Database::query($sql);
374
375
        return $result !== false; //only return false on sql error
376
    }
377
378
    /**
379
     * Set platform language
380
     * @param Integer The language id
381
     * @return bool
382
     */
383
    public static function set_platform_language($language_id)
384
    {
385
        if (empty($language_id) or (intval($language_id) != $language_id)) {
386
            return false;
387
        }
388
        $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
389
        $tbl_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
390
        $sql = "SELECT english_name FROM " . $tbl_admin_languages . "
391
                WHERE id= " . intval($language_id) . "";
392
        $result = Database::query($sql);
393
        $lang = Database::fetch_array($result);
394
        $sql_update_2 = "UPDATE " . $tbl_settings_current . " SET selected_value='" . $lang['english_name'] . "'
395
                         WHERE variable='platformLanguage'";
396
        $result_2 = Database::query($sql_update_2);
397
        Event::addEvent(
398
            LOG_PLATFORM_LANGUAGE_CHANGE,
399
            LOG_PLATFORM_LANGUAGE,
400
            $lang['english_name']
401
        );
402
403
        return $result_2 !== false;
404
    }
405
406
    /**
407
     * Get platform language ID
408
     * @return     int     The platform language ID
409
     */
410 View Code Duplication
    public static function get_platform_language_id()
411
    {
412
        $name = api_get_setting('platformLanguage');
413
        $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
414
        $sql = "SELECT id FROM " . $tbl_admin_languages . " WHERE english_name ='$name'";
415
        $res = Database::query($sql);
416
        if (Database::num_rows($res) < 1) {
417
            return false;
418
        }
419
        $row = Database::fetch_array($res);
420
421
        return $row['id'];
422
    }
423
424
    /**
425
     * Get parent language path (or null if no parent)
426
     * @param    string  Children language path
427
     * @return   string  Parent language path or null
428
     */
429 View Code Duplication
    public static function get_parent_language_path($language_path)
430
    {
431
        $tbl_admin_languages = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
432
        $sql = "SELECT dokeos_folder
433
                FROM " . $tbl_admin_languages . "
434
                WHERE id = (
435
                    SELECT parent_id FROM " . $tbl_admin_languages . "
436
                    WHERE dokeos_folder = '" . Database::escape_string($language_path) . "'
437
                )
438
                ";
439
        $result = Database::query($sql);
440
        if (Database::num_rows($result) == 0) {
441
            return null;
442
        }
443
        $row = Database::fetch_array($result);
444
        return $row['dokeos_folder'];
445
    }
446
447
    /**
448
     * Get language matching isocode
449
     * @param   string  $isocode The language isocode (en, es, fr, zh-TW, etc)
450
     * @return  mixed  English name of the matching language, or false if no active language could be found
451
     */
452 View Code Duplication
    public static function getLanguageFromIsocode($isocode)
453
    {
454
        $isocode = Database::escape_string($isocode);
455
        $adminLanguagesTable = Database :: get_main_table(TABLE_MAIN_LANGUAGE);
456
        // select language - if case several languages match, get the last (more recent) one
457
        $sql = "SELECT english_name
458
                FROM " . $adminLanguagesTable . "
459
                WHERE
460
                    isocode ='$isocode' AND
461
                    available = 1
462
                ORDER BY id
463
                DESC LIMIT 1";
464
        $res = Database::query($sql);
465
        if (Database::num_rows($res) < 1) {
466
            return false;
467
        }
468
        $row = Database::fetch_assoc($res);
469
        return $row['english_name'];
470
    }
471
472
    /**
473
     * Get best language in browser preferences
474
     * @param   string  $preferences The browser-configured language preferences (e.g. "en,es;q=0.7;en-us;q=0.3", etc)
475
     * @return  mixed  English name of the matching language, or false if no active language could be found
476
     */
477
    public static function getLanguageFromBrowserPreference($preferences)
478
    {
479
        if (empty($preferences)) {
480
            return false;
481
        }
482
483
        $preferencesArray = explode(',', $preferences);
484
485
        if (count($preferencesArray) > 0) {
486
            foreach ($preferencesArray as $pref) {
487
                $s = strpos($pref, ';');
488
                if ($s >= 2) {
489
                    $code = substr($pref, 0, $s);
490
                } else {
491
                    $code = $pref;
492
                }
493
                $name = self::getLanguageFromIsocode($code);
494
495
                if ($name !== false) {
496
                    return $name;
497
                }
498
            }
499
        }
500
501
        return false;
502
    }
503
}
504