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

PbxExtensionModulesController::sidebarIncludeAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
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\Controllers;
21
22
use MikoPBX\AdminCabinet\Forms\PbxExtensionModuleSettingsForm;
23
use MikoPBX\AdminCabinet\Providers\SecurityPluginProvider;
24
use MikoPBX\Common\Models\{PbxExtensionModules, PbxSettings};
25
use Phalcon\Text;
26
27
class PbxExtensionModulesController extends BaseController
28
{
29
    /**
30
     * Builds installed modules list
31
     */
32
    public function indexAction(): void
33
    {
34
        $licKey = PbxSettings::getValueByKey('PBXLicense');
35
        if (strlen($licKey) !== 28
36
            || ! Text::startsWith($licKey, 'MIKO-')) {
37
            $licKey = '';
38
        }
39
40
        $modules     = PbxExtensionModules::getModulesArray();
41
        $modulesList = [];
42
        foreach ($modules as $module) {
43
            $unCamelizedControllerName = Text::uncamelize($module['uniqid'], '-');
44
            $modulesList[]             = [
45
                'uniqid'      => $module['uniqid'],
46
                'name'        => $module['name'],
47
                'description' => $module['description'],
48
                'developer'   => $module['developer'],
49
                'version'     => $module['version'],
50
                'classname'   => $unCamelizedControllerName,
51
                'status'      => ($module['disabled'] === '1') ? 'disabled' : '',
52
                'permanent'   => true,
53
            ];
54
        }
55
        $this->view->modulelist = $modulesList;
56
        $this->view->licenseKey = $licKey;
57
    }
58
59
    /**
60
     * Builds page for modify how to show the module in sidebar
61
     *
62
     * @param $uniqid string of module
63
     */
64
    public function modifyAction(string $uniqid): void
65
    {
66
        $menuSettings               = "AdditionalMenuItem{$uniqid}";
67
        $unCamelizedControllerName  = Text::uncamelize($uniqid, '-');
68
        $previousMenuSettings       = PbxSettings::findFirstByKey($menuSettings);
69
        $this->view->showAtMainMenu = $previousMenuSettings !== false;
70
        if ($previousMenuSettings === null) {
71
            $previousMenuSettings        = new PbxSettings();
72
            $previousMenuSettings->key   = $menuSettings;
73
            $value                       = [
74
                'uniqid'        => $uniqid,
75
                'href'          => $this->url->get($unCamelizedControllerName),
76
                'group'         => 'modules',
77
                'iconClass'     => 'puzzle piece',
78
                'caption'       => "Breadcrumb$uniqid",
79
                'showAtSidebar' => false,
80
            ];
81
            $previousMenuSettings->value = json_encode($value);
82
        }
83
        $options = [];
84
        if ($previousMenuSettings->value!==null){
85
            $options                = json_decode($previousMenuSettings->value, true);
86
        }
87
        $this->view->form       = new PbxExtensionModuleSettingsForm($previousMenuSettings, $options);
88
        $this->view->title      = $this->translation->_('ext_SettingsForModule') . ' ' . $this->translation->_(
89
                "Breadcrumb$uniqid"
90
            );
91
        $this->view->submitMode = null;
92
        $this->view->indexUrl   = $unCamelizedControllerName;
93
    }
94
95
    /**
96
     * Saves how to show the module in sidebar settings into PbxSettings
97
     */
98
    public function saveModuleSettingsAction(): void
99
    {
100
        if ( ! $this->request->isPost()) {
101
            return;
102
        }
103
        $data = $this->request->getPost();
104
105
        $record = PbxSettings::findFirstByKey($data['key']);
106
        if ($record === null) {
107
            $record      = new PbxSettings();
108
            $record->key = $data['key'];
109
        }
110
        $value         = [
111
            'uniqid'        => $data['uniqid'],
112
            'href'          => $data['href'],
113
            'group'         => $data['menu-group'],
114
            'iconClass'     => $data['iconClass'],
115
            'caption'       => $data['caption'],
116
            'showAtSidebar' => $data['show-at-sidebar'] === 'on',
117
        ];
118
        $record->value = json_encode($value);
119
        if ($record->save() === false) {
120
            $errors = $record->getMessages();
121
            $this->flash->error(implode('<br>', $errors));
122
            $this->view->success = false;
123
124
            return;
125
        }
126
        $this->flash->success($this->translation->_('ms_SuccessfulSaved'));
127
        $this->view->success = true;
128
    }
129
130
}