Passed
Push — develop ( 1b53f1...8547d9 )
by Nikolay
13:03 queued 03:03
created

AsteriskManagersController::availableAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Copyright (C) MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Nikolay Beketov, 5 2018
7
 *
8
 */
9
10
namespace MikoPBX\AdminCabinet\Controllers;
11
12
use MikoPBX\AdminCabinet\Forms\AsteriskManagerEditForm;
13
use MikoPBX\Common\Models\{AsteriskManagerUsers, NetworkFilters};
14
use Phalcon\Mvc\Model\Resultset;
15
16
class AsteriskManagersController extends BaseController
17
{
18
19
    private array $arrCheckBoxes;
20
21
    /**
22
     * Base class initialization
23
     */
24
    public function initialize(): void
25
    {
26
        $this->arrCheckBoxes = [
27
            'call',
28
            'cdr',
29
            'originate',
30
            'reporting',
31
            'agent',
32
            'config',
33
            'dialplan',
34
            'dtmf',
35
            'log',
36
            'system',
37
            'user',
38
            'verbose',
39
        ];
40
        parent::initialize();
41
    }
42
43
    /**
44
     * Generates Asterisk Managers index page
45
     */
46
    public function indexAction()
47
    {
48
        $amiUsers = AsteriskManagerUsers::find();
49
        $amiUsers->setHydrateMode(
50
            Resultset::HYDRATE_ARRAYS
51
        );
52
53
        $arrNetworkFilters = [];
54
        $networkFilters    = NetworkFilters::find();
55
        foreach ($networkFilters as $filter) {
56
            $arrNetworkFilters[$filter->id] = $filter->getRepresent();
57
        }
58
        $this->view->networkFilters = $arrNetworkFilters;
59
        $this->view->amiUsers       = $amiUsers;
60
    }
61
62
63
    /**
64
     * Modifies Asterisk Managers by Webinterface
65
     *
66
     * @param string $id AsteriskManagerUsers record ID
67
     */
68
    public function modifyAction($id = '')
69
    {
70
        $manager = AsteriskManagerUsers::findFirstById($id);
71
        if ($manager === null) {
72
            $manager = new AsteriskManagerUsers();
73
        }
74
75
        $arrNetworkFilters = [];
76
        $networkFilters    = NetworkFilters::getAllowedFiltersForType(
77
            [
78
                'AJAM',
79
                'AMI',
80
            ]
81
        );
82
        $arrNetworkFilters['none']
83
                           = $this->translation->_('ex_NoNetworkFilter');
84
        foreach ($networkFilters as $filter) {
85
            $arrNetworkFilters[$filter->id] = $filter->getRepresent();
86
        }
87
88
89
        $this->view->form = new AsteriskManagerEditForm(
90
            $manager,
91
            [
92
                'network_filters'     => $arrNetworkFilters,
93
                'array_of_checkboxes' => $this->arrCheckBoxes,
94
            ]
95
        );
96
97
        $this->view->arrCheckBoxes = $this->arrCheckBoxes;
98
        $this->view->represent     = $manager->getRepresent();
99
    }
100
101
102
    /**
103
     * Save Asterisk Manager User settings
104
     */
105
    public function saveAction(): void
106
    {
107
        if ( ! $this->request->isPost()) {
108
            return;
109
        }
110
111
        $data    = $this->request->getPost();
112
        $manager = null;
113
        if (isset($data['id'])) {
114
            $manager = AsteriskManagerUsers::findFirst($data['id']);
115
        }
116
        if ($manager === null) {
117
            $manager = new AsteriskManagerUsers();
118
        }
119
120
        foreach ($manager as $name => $value) {
121
            if (in_array($name, $this->arrCheckBoxes, true)) {
122
                $manager->$name = '';
123
                $manager->$name .= ($data[$name . '_read'] === 'on') ? 'read' : '';
124
                $manager->$name .= ($data[$name . '_write'] === 'on') ? 'write' : '';
125
                continue;
126
            }
127
128
            if ( ! array_key_exists($name, $data)) {
129
                continue;
130
            }
131
            $manager->$name = $data[$name];
132
        }
133
        $errors = false;
134
        if ( ! $manager->save()) {
135
            $errors = $manager->getMessages();
136
        }
137
138
        if ($errors) {
139
            $this->flash->error(implode('<br>', $errors));
140
            $this->view->success = false;
141
        } else {
142
            $this->flash->success($this->translation->_('ms_SuccessfulSaved'));
143
            $this->view->success = true;
144
            $this->view->reload  = "asterisk-managers/modify/{$manager->id}";
145
        }
146
    }
147
148
    /**
149
     * Deletes Asterisk Manager
150
     *
151
     * @param string $amiId
152
     *
153
     * @return void
154
     */
155
    public function deleteAction(string $amiId = ''): void
156
    {
157
        $manager = AsteriskManagerUsers::findFirstByid($amiId);
158
        if ($manager !== null) {
159
            $manager->delete();
160
        }
161
        $this->forward('asterisk-managers/index');
162
    }
163
164
    /**
165
     * Checks uniqueness for AMI username from JS.
166
     *
167
     * @param string $username
168
     *
169
     * @return void  result send by ControllerBase::afterExecuteRoute()
170
     */
171
    public function availableAction(string $username): void
172
    {
173
        $result = true;
174
        $amiUser = AsteriskManagerUsers::findFirst("username = '{$username}'");
175
        if ($amiUser !== null) {
176
            $result             = false;
177
            $this->view->userId = $amiUser->id;
178
        }
179
        $this->view->nameAvailable = $result;
180
        $this->view->success = true;
181
    }
182
}