Passed
Push — develop ( a4a90f...fc5c50 )
by Nikolay
05:38
created

TopMenuSearchController::sortItemsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\{AsteriskManagerUsers,
23
    CallQueues,
24
    ConferenceRooms,
25
    CustomFiles,
26
    DialplanApplications,
27
    IvrMenu,
28
    NetworkFilters,
29
    OutWorkTimes,
30
    PbxExtensionModules,
31
    Providers,
32
    Users
33
};
34
use MikoPBX\AdminCabinet\Providers\SecurityPluginProvider;
35
use Modules\ModuleUsersUI\App\Controllers\UsersCredentialsController;
36
use Phalcon\Text;
37
38
class TopMenuSearchController extends BaseController
39
{
40
    /**
41
     * Makes top search menu over AJAX request
42
     *
43
     * @return void The parameters are placed in the view and processed through ControllerBase::afterExecuteRoute().
44
     */
45
    public function getForSelectAction(): void
46
    {
47
        $arrClasses = [
48
            Users::class,
49
            Providers::class,
50
            CallQueues::class,
51
            IvrMenu::class,
52
            PbxExtensionModules::class,
53
            ConferenceRooms::class,
54
            DialplanApplications::class,
55
            NetworkFilters::class,
56
            OutWorkTimes::class,
57
            AsteriskManagerUsers::class,
58
            CustomFiles::class,
59
        ];
60
        $results = [[]];
61
        foreach ($arrClasses as $itemClass) {
62
            $records = call_user_func([$itemClass, 'find']);
63
            $categoryItems = [];
64
            foreach ($records as $record) {
65
                if ($itemClass === Users::class && $record->id === '1') {
66
                    continue; // Admin
67
                }
68
                $this->addMenuItem($categoryItems, $record, $itemClass);
69
            }
70
            usort($categoryItems, [__CLASS__, 'sortItemsArray']);
71
            $results[] = $categoryItems;
72
        }
73
74
        $results = array_merge(...$results);
75
        $this->addOtherMenuItems($results);
76
77
78
        $this->view->success = true;
79
        $this->view->results = $results;
80
    }
81
82
    /**
83
     * Add items to menu
84
     *
85
     * @param $items
86
     * @param $record
87
     * @param $itemClass
88
     */
89
    private function addMenuItem(&$items, $record, $itemClass): void
90
    {
91
        $link = $record->getWebInterfaceLink();
92
        if ($link === '#') {
93
            return;
94
        }
95
96
        $category = explode('\\', $itemClass)[3];
97
        $type = Text::underscore(strtoupper($category));
98
        $represent = $record->getRepresent();
99
100
        $clearedRepresent = strip_tags($represent);
101
        $items[] = [
102
            'name' => $represent,
103
            'value' => $link,
104
            'type' => $type,
105
            'typeLocalized' => $this->translation->_("ex_dropdownCategory_{$type}"),
106
            'sorter' => $clearedRepresent,
107
        ];
108
    }
109
110
    /**
111
     * Add non object menu items
112
     *
113
     * @param $items
114
     */
115
    private function addOtherMenuItems(&$items): void
116
    {
117
118
        $additionalMenuItems = [
119
            IncomingRoutesController::class => 'index',
120
            OutboundRoutesController::class => 'index',
121
            GeneralSettingsController::class => 'modify',
122
            TimeSettingsController::class => 'modify',
123
            NetworkController::class => 'modify',
124
            MailSettingsController::class => 'modify',
125
            CallDetailRecordsController::class => 'index',
126
            SoundFilesController::class => 'index',
127
            PbxExtensionModulesController::class => 'index',
128
            SystemDiagnosticController::class => 'index',
129
            Fail2BanController::class => 'index',
130
            UpdateController::class => 'index',
131
        ];
132
133
        foreach ($additionalMenuItems as $controllerClass => $action) {
134
            $this->addAdditionalMenuItem($controllerClass, $action, $items);
135
        }
136
137
    }
138
139
    /**
140
     * Sorts an array of items based on the 'sorter' value in each item.
141
     *
142
     * @param $a
143
     * @param $b
144
     *
145
     * @return int
146
     */
147
    private function sortItemsArray($a, $b): int
148
    {
149
        return strcmp($a['sorter'], $b['sorter']);
150
    }
151
152
153
    /**
154
     * Add a menu item if it allowed to be added.
155
     * @param string $controllerClass The controller class name.
156
     * @param string $action The action name.
157
     * @param array $items The array of menu items.
158
     * @return void
159
     */
160
    private function addAdditionalMenuItem(string $controllerClass, string $action, array &$items): void
161
    {
162
        if ($this->di->get(SecurityPluginProvider::SERVICE_NAME, [$controllerClass, $action])) {
163
            $controllerParts = explode('\\', $controllerClass);
164
            $controllerName = end($controllerParts);
165
            // Remove the "Controller" suffix if present
166
            $controllerName = str_replace("Controller", "", $controllerName);
167
            $unCamelizedControllerName = Text::uncamelize($controllerName, '-');
168
            $translatedControllerName = $this->translation->_('mm_' . $controllerName);
169
170
            $items[] = [
171
                'name' => $this->elements->getIconByController($controllerClass) . ' ' . $translatedControllerName,
172
                'value' => $this->url->get($unCamelizedControllerName . '/' . $action),
173
                'type' => 'MENUITEMS',
174
                'typeLocalized' => $this->translation->_('ex_dropdownCategory_MENUITEMS'),
175
                'sorter' => strip_tags($translatedControllerName),
176
            ];
177
        }
178
    }
179
}