Passed
Push — develop ( 73b673...d6f831 )
by Nikolay
05:32
created

Elements::getAvailableWebAdminLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 23
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 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\Library;
21
22
use MikoPBX\AdminCabinet\Providers\SecurityPluginProvider;
23
use MikoPBX\Common\Models\PbxExtensionModules;
24
use MikoPBX\Common\Models\PbxSettings;
25
use MikoPBX\Common\Providers\PBXConfModulesProvider;
26
use MikoPBX\Modules\Config\WebUIConfigInterface;
27
use Phalcon\Di\Injectable;
28
use Phalcon\Text;
29
30
/**
31
 * Elements
32
 * Helps to build UI elements for the application
33
 *
34
 * @property \MikoPBX\Common\Providers\TranslationProvider translation
35
 *
36
 */
37
class Elements extends Injectable
38
{
39
40
    private array $_headerMenu
41
        = [
42
            'setup' => [
43
                'caption' => 'mm_Setup',
44
                'iconclass' => '',
45
                'submenu' => [
46
                    'extensions' => [
47
                        'caption' => 'mm_Extensions',
48
                        'iconclass' => 'user outline',
49
                        'action' => 'index',
50
                        'param' => '',
51
                        'style' => '',
52
                    ],
53
                    'call-queues' => [
54
                        'caption' => 'mm_CallQueues',
55
                        'iconclass' => 'users',
56
                        'action' => 'index',
57
                        'param' => '',
58
                        'style' => '',
59
                    ],
60
                    'ivr-menu' => [
61
                        'caption' => 'mm_IvrMenu',
62
                        'iconclass' => 'sitemap',
63
                        'action' => 'index',
64
                        'param' => '',
65
                        'style' => '',
66
                    ],
67
                    'conference-rooms' => [
68
                        'caption' => 'mm_Conferences',
69
                        'iconclass' => 'phone volume',
70
                        'action' => 'index',
71
                        'param' => '',
72
                        'style' => '',
73
                    ],
74
                    'sound-files' => [
75
                        'caption' => 'mm_SoundFiles',
76
                        'iconclass' => 'sound',
77
                        'action' => 'index',
78
                        'param' => '',
79
                        'style' => '',
80
                    ],
81
                    'call-detail-records' => [
82
                        'caption' => 'mm_CallDetailRecords',
83
                        'iconclass' => 'list ul',
84
                        'action' => 'index',
85
                        'param' => '',
86
                        'style' => '',
87
                    ],
88
                ],
89
            ],
90
            'routing' => [
91
                'caption' => 'mm_Routing',
92
                'iconclass' => '',
93
                'submenu' => [
94
                    'providers' => [
95
                        'caption' => 'mm_Providers',
96
                        'iconclass' => 'server',
97
                        'action' => 'index',
98
                        'param' => '',
99
                        'style' => '',
100
                    ],
101
                    'incoming-routes' => [
102
                        'caption' => 'mm_IncomingRoutes',
103
                        'iconclass' => 'map signs',
104
                        'action' => 'index',
105
                        'param' => '',
106
                        'style' => '',
107
                    ],
108
                    'outbound-routes' => [
109
                        'caption' => 'mm_OutboundRoutes',
110
                        'iconclass' => 'random',
111
                        'action' => 'index',
112
                        'param' => '',
113
                        'style' => '',
114
                    ],
115
                    'out-off-work-time' => [
116
                        'caption' => 'mm_OutOffWorkTime',
117
                        'iconclass' => 'calendar times outline',
118
                        'action' => 'index',
119
                        'param' => '',
120
                        'style' => '',
121
                    ],
122
                ],
123
            ],
124
125
            'modules' => [
126
                'caption' => 'mm_Modules',
127
                'iconclass' => '',
128
                'submenu' => [
129
                    'dialplan-applications' => [
130
                        'caption' => 'mm_DialplanApplication',
131
                        'iconclass' => 'php',
132
                        'action' => 'index',
133
                        'param' => '',
134
                        'style' => '',
135
                    ],
136
                    'pbx-extension-modules' => [
137
                        'caption' => 'mm_ModuleManager',
138
                        'iconclass' => 'puzzle piece',
139
                        'action' => 'index',
140
                        'param' => '',
141
                        'style' => '',
142
                    ],
143
                    'licensing' => [
144
                        'caption' => 'mm_PaidModulesAccessNew',
145
                        'iconclass' => 'key',
146
                        'action' => 'modify',
147
                        'param' => '',
148
                        'style' => '',
149
                    ],
150
                ],
151
            ],
152
153
            'maintenance' => [
154
                'caption' => 'mm_Maintenance',
155
                'iconclass' => '',
156
                'submenu' => [
157
                    'update' => [
158
                        'caption' => 'mm_UpdateSystem',
159
                        'iconclass' => 'sync',
160
                        'action' => 'index',
161
                        'param' => '',
162
                        'style' => '',
163
                    ],
164
                    'system-diagnostic' => [
165
                        'caption' => 'mm_SystemDiagnostic',
166
                        'iconclass' => 'stethoscope',
167
                        'action' => 'index',
168
                        'param' => '',
169
                        'style' => '',
170
                    ],
171
                    'console' => [
172
                        'caption' => 'mm_SSHConsole',
173
                        'iconclass' => 'terminal',
174
                        'action' => 'index',
175
                        'param' => '',
176
                        'style' => '',
177
                    ],
178
                    'restart' => [
179
                        'caption' => 'mm_Restart',
180
                        'iconclass' => 'power off',
181
                        'action' => 'index',
182
                        'param' => '',
183
                        'style' => '',
184
                    ],
185
                ],
186
            ],
187
            'networkSettings' => [
188
                'caption' => 'mm_NetworkSettings',
189
                'iconclass' => '',
190
                'submenu' => [
191
                    'network' => [
192
                        'caption' => 'mm_Network',
193
                        'iconclass' => 'globe',
194
                        'action' => 'modify',
195
                        'param' => '',
196
                        'style' => '',
197
                    ],
198
                    'firewall' => [
199
                        'caption' => 'mm_Firewall',
200
                        'iconclass' => 'shield alternate',
201
                        'action' => 'index',
202
                        'param' => '',
203
                        'style' => '',
204
                    ],
205
                    'fail2-ban' => [
206
                        'caption' => 'mm_BruteForceProtection',
207
                        'iconclass' => 'user secret',
208
                        'action' => 'index',
209
                        'param' => '',
210
                        'style' => '',
211
                    ],
212
                ],
213
            ],
214
            'server' => [
215
                'caption' => 'mm_System',
216
                'iconclass' => '',
217
                'submenu' => [
218
                    'general-settings' => [
219
                        'caption' => 'mm_GeneralSettings',
220
                        'iconclass' => 'cogs',
221
                        'action' => 'modify',
222
                        'param' => '',
223
                        'style' => '',
224
                    ],
225
                    'time-settings' => [
226
                        'caption' => 'mm_SystemClock',
227
                        'iconclass' => 'time',
228
                        'action' => 'modify',
229
                        'param' => '',
230
                        'style' => '',
231
                    ],
232
                    'mail-settings' => [
233
                        'caption' => 'mm_MailSettings',
234
                        'iconclass' => 'envelope outline',
235
                        'action' => 'modify',
236
                        'param' => '',
237
                        'style' => '',
238
                    ],
239
                    'asterisk-managers' => [
240
                        'caption' => 'mm_AsteriskManagerInterface',
241
                        'iconclass' => 'asterisk',
242
                        'action' => 'index',
243
                        'param' => '',
244
                        'style' => '',
245
                    ],
246
                    'custom-files' => [
247
                        'caption' => 'mm_CustomFiles',
248
                        'iconclass' => 'linux',
249
                        'action' => 'index',
250
                        'param' => '',
251
                        'style' => '',
252
                    ],
253
                ],
254
            ],
255
256
        ];
257
258
    /**
259
     * Generates the HTML code for the header menu by iterating through the items and checking if they are allowed
260
     * to be displayed by the current user based on their role.
261
     *
262
     * @return void
263
     */
264
    public function getMenu(): void
265
    {
266
        $resultHtml = '';
267
268
        $this->addMenuItemsFromExternalModules();
269
270
        foreach ($this->_headerMenu as $group => $groupparams) {
271
            $addToHTML = false;
272
            $groupHtml = '';
273
            if (array_key_exists('submenu', $groupparams)) {
274
                $groupHtml .= '<div class="item">';
275
                $groupHtml .= '<div class="header">';
276
                if (array_key_exists('iconclass', $groupparams) && !empty($groupparams['iconclass'])) {
277
                    $groupHtml .= "<i class='{$groupparams['iconclass']} icon'></i>";
278
                }
279
                $groupHtml .= $this->translation->_($groupparams['caption']) . '</div>';
280
                $groupHtml .= "<div class='menu' data-group='{$group}'>";
281
                foreach ($groupparams['submenu'] as $controller => $option) {
282
                    $isAllowed = $this->di->get(SecurityPluginProvider::SERVICE_NAME, [$controller, $option['action']]);
283
                    if ($isAllowed) {
284
                        $link = $this->url->get($controller . '/' . $option['action'] . '/' . $option['param']);
285
                        $caption = $this->translation->_($option['caption']);
286
                        $groupHtml .= "<a class='item {$option['style']}' href='{$link}'>
287
                    		<i class='{$option['iconclass']} icon'></i>{$caption}
288
                    	 </a>";
289
                        $addToHTML = true;
290
                    }
291
                }
292
                $groupHtml .= '</div>';
293
                $groupHtml .= '</div>';
294
            } else {
295
                $isAllowedGroup = $this->di->get(SecurityPluginProvider::SERVICE_NAME, [$group, $groupparams['action'] ?? 'index']);
296
                if ($isAllowedGroup) {
297
                    $link = $this->url->get($group . '/' . $groupparams['action'] . '/' . $groupparams['param']);
298
                    $caption = $this->translation->_($groupparams['caption']);
299
                    $groupHtml .= "<a class='item {$groupparams['style']}' href='{$link}'>
300
                    	    <i class='{$groupparams['iconclass']} icon'></i>{$caption}
301
                        </a>";
302
                    $addToHTML = true;
303
                }
304
            }
305
            if ($addToHTML) {
306
                $resultHtml .= $groupHtml;
307
            }
308
        }
309
        echo $resultHtml;
310
    }
311
312
    /**
313
     * Returns icon html by controller name
314
     *
315
     * @param $controllerName
316
     *
317
     * @return string
318
     */
319
    public function getIconByController($controllerName): string
320
    {
321
        $uncamelizeControllerName = Text::uncamelize($controllerName, '-');
322
        $result = '';
323
        foreach ($this->_headerMenu as $index => $group) {
324
            if ($index === $uncamelizeControllerName
325
                && array_key_exists('iconclass', $group[$uncamelizeControllerName])
326
                && !empty($group[$uncamelizeControllerName]['iconclass'])
327
            ) {
328
                $result = "<i class='{$group[$uncamelizeControllerName]['iconclass']} icon'></i>";
329
                break;
330
            }
331
            if (array_key_exists('submenu', $group)) {
332
                foreach ($group['submenu'] as $index2 => $submenu) {
333
                    if ($index2 === $uncamelizeControllerName
334
                        && !empty($submenu['iconclass'])) {
335
                        $result = "<i class='{$submenu['iconclass']} icon'></i>";
336
                        break;
337
                    }
338
                }
339
            }
340
        }
341
342
        return $result;
343
    }
344
345
    /**
346
     * Returns an array with the allowed menu groups based on the current user's permissions.
347
     *
348
     * @return array An array of the allowed menu groups where the key is the group name and the value is its caption.
349
     */
350
    public function getMenuGroups(): array
351
    {
352
        $result = [];
353
        foreach ($this->_headerMenu as $group => $groupparams) {
354
            if (array_key_exists('submenu', $groupparams)) {
355
                $result[$group] = $this->translation->_($groupparams['caption']);
356
            }
357
        }
358
359
        return $result;
360
    }
361
362
363
    /**
364
     * Prepares array of available WEB UI languages
365
     * @return array
366
     */
367
    public function getAvailableWebAdminLanguages(): array
368
    {
369
        return [
370
            'en' => $this->translation->_('ex_English'),
371
            'ru' => $this->translation->_('ex_Russian'),
372
            'de' => $this->translation->_('ex_Deutsch'),
373
            'es' => $this->translation->_('ex_Spanish'),
374
            'el' => $this->translation->_('ex_Greek'),
375
            'fr' => $this->translation->_('ex_French'),
376
            'pt' => $this->translation->_('ex_Portuguese'),
377
            'pt_BR' => $this->translation->_('ex_PortugueseBrazil'),
378
            'uk' => $this->translation->_('ex_Ukrainian'),
379
            'ka' => $this->translation->_('ex_Georgian'),
380
            'it' => $this->translation->_('ex_Italian'),
381
            'da' => $this->translation->_('ex_Danish'),
382
            'pl' => $this->translation->_('ex_Polish'),
383
            'sv' => $this->translation->_('ex_Swedish'),
384
            'cs' => $this->translation->_('ex_Czech'),
385
            'tr' => $this->translation->_('ex_Turkish'),
386
            'ja' => $this->translation->_('ex_Japanese'),
387
            'vi' => $this->translation->_('ex_Vietnamese'),
388
            'az' => $this->translation->_('ex_Azərbaycan'),
389
            'zh_Hans' => $this->translation->_('ex_Chinese'),
390
        ];
391
    }
392
393
    /**
394
     * Adds menu items from enabled external modules to the header menu.
395
     *
396
     * @return void
397
     */
398
    public function addMenuItemsFromExternalModules(): void
399
    {
400
        $modules = PbxExtensionModules::getEnabledModulesArray();
401
        foreach ($modules as $module) {
402
            $unCamelizedControllerName = Text::uncamelize($module['uniqid'], '-');
403
            $isAllowed = $this->di->get(SecurityPluginProvider::SERVICE_NAME, [$unCamelizedControllerName]);
404
            if ($isAllowed) {
405
                $menuSettings = "AdditionalMenuItem{$module['uniqid']}";
406
                $previousMenuSettings = PbxSettings::findFirstByKey($menuSettings);
407
                if ($previousMenuSettings !== null) {
408
                    $menuItem = json_decode($previousMenuSettings->value, true);
409
                    if ($menuItem['showAtSidebar']) {
410
                        $controllerUrl = "$unCamelizedControllerName\\$unCamelizedControllerName";
411
                        $this->_headerMenu[$menuItem['group']]['submenu'][$controllerUrl] = [
412
                            'caption' => $menuItem['caption'],
413
                            'iconclass' => $menuItem['iconClass'],
414
                            'action' => 'index',
415
                            'param' => '',
416
                            'style' => '',
417
                        ];
418
                    }
419
                }
420
            }
421
        }
422
423
        PBXConfModulesProvider::hookModulesProcedure(WebUIConfigInterface::ON_BEFORE_HEADER_MENU_SHOW, [&$this->_headerMenu]);
424
    }
425
426
}
427