Passed
Branch develop (9d5aa6)
by Nikolay
04:57
created

LanguageController::updateSystemLanguage()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 14
rs 9.9332
cc 4
nc 5
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\AdminCabinet\Controllers;
21
22
use MikoPBX\Common\Models\PbxSettings;
23
use MikoPBX\Common\Providers\TranslationProvider;
24
use Phalcon\Di;
25
26
/**
27
 * LanguageController
28
 *
29
 * Responsible for change language of the application
30
 */
31
class LanguageController extends BaseController
32
{
33
34
    public const WEB_ADMIN_LANGUAGE = 'WebAdminLanguage';
35
36
    /**
37
     * Updates system settings for language
38
     *
39
     */
40
    public static function updateSystemLanguage(string $newLanguage): void
41
    {
42
        if (!isset($newLanguage)) {
43
            return;
44
        }
45
        $languageSettings = PbxSettings::findFirstByKey('WebAdminLanguage');
46
        if ($languageSettings === null) {
47
            $languageSettings = new PbxSettings();
48
            $languageSettings->key = 'WebAdminLanguage';
49
            $languageSettings->value = PbxSettings::getDefaultArrayValues()['WebAdminLanguage'];
50
        }
51
        if ($newLanguage !== $languageSettings->value) {
52
            $languageSettings->value = $newLanguage;
53
            $languageSettings->save();
54
        }
55
    }
56
57
    /**
58
     * Process language change
59
     */
60
    public function changeAction(): void
61
    {
62
        $newLanguage = $this->request->getPost('newLanguage', 'string');
63
        if (array_key_exists($newLanguage, self::getAvailableWebAdminLanguages())) {
64
            $this->session->set(LanguageController::WEB_ADMIN_LANGUAGE, $newLanguage);
65
            if ($this->session->has(SessionController::SESSION_ID)) {
66
                self::updateSystemLanguage($newLanguage);
67
            }
68
            $this->view->success = true;
69
        } else {
70
            $this->view->success = false;
71
        }
72
    }
73
74
    /**
75
     * Prepares array of available WEB UI languages
76
     * @return array
77
     */
78
    public static function getAvailableWebAdminLanguages(): array
79
    {
80
        $di = Di::getDefault();
81
        $translation = $di->getShared(TranslationProvider::SERVICE_NAME);
82
83
        return [
84
            'en' => ['name' => $translation->_('ex_English'), 'flag' => 'united kingdom'],
85
            'ru' => ['name' => $translation->_('ex_Russian'), 'flag' => 'russia'],
86
            'de' => ['name' => $translation->_('ex_Deutsch'), 'flag' => 'germany'],
87
            'es' => ['name' => $translation->_('ex_Spanish'), 'flag' => 'spain'],
88
            'el' => ['name' => $translation->_('ex_Greek'), 'flag' => 'greece'],
89
            'fr' => ['name' => $translation->_('ex_French'), 'flag' => 'france'],
90
            'pt' => ['name' => $translation->_('ex_Portuguese'), 'flag' => 'portugal'],
91
            'pt_BR' => ['name' => $translation->_('ex_PortugueseBrazil'), 'flag' => 'brazil'],
92
            'uk' => ['name' => $translation->_('ex_Ukrainian'), 'flag' => 'ukraine'],
93
            'ka' => ['name' => $translation->_('ex_Georgian'), 'flag' => 'georgia'],
94
            'it' => ['name' => $translation->_('ex_Italian'), 'flag' => 'italy'],
95
            'da' => ['name' => $translation->_('ex_Danish'), 'flag' => 'netherlands'],
96
            'pl' => ['name' => $translation->_('ex_Polish'), 'flag' => 'poland'],
97
            'sv' => ['name' => $translation->_('ex_Swedish'), 'flag' => 'sweden'],
98
            'cs' => ['name' => $translation->_('ex_Czech'), 'flag' => 'czech republic'],
99
            'tr' => ['name' => $translation->_('ex_Turkish'), 'flag' => 'turkey'],
100
            'ja' => ['name' => $translation->_('ex_Japanese'), 'flag' => 'japan'],
101
            'vi' => ['name' => $translation->_('ex_Vietnamese'), 'flag' => 'vietnam'],
102
            'az' => ['name' => $translation->_('ex_Azerbaijan'), 'flag' => 'azerbaijan'],
103
            'ro' => ['name' => $translation->_('ex_Romanian'), 'flag' => 'romania'],
104
            'zh_Hans' => ['name' => $translation->_('ex_Chinese'), 'flag' => 'china'],
105
        ];
106
    }
107
}