Passed
Push — develop ( bef097...351c48 )
by Nikolay
12:23
created

PbxExtensionModulesController::saveAction()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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