Passed
Push — develop ( 558982...38f712 )
by Nikolay
06:07
created

DialplanApplicationsController::deleteAction()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 26
rs 9.4222
cc 5
nc 6
nop 1
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\DialplanApplicationEditForm;
23
use MikoPBX\Common\Models\{DialplanApplications, Extensions};
24
25
26
class DialplanApplicationsController extends BaseController
27
{
28
29
    /**
30
     * Builds list of dialplan applications
31
     */
32
    public function indexAction(): void
33
    {
34
        $apps = DialplanApplications::find();
35
36
        $this->view->apps = $apps;
37
    }
38
39
    /**
40
     * Creates DialplanApplications modify form
41
     *
42
     * @param string $uniqid Dialplan Application record ID
43
     */
44
    public function modifyAction(string $uniqid = ''): void
45
    {
46
        $app = DialplanApplications::findFirstByUniqid($uniqid);
47
        if ($app === null) {
48
            $app            = new DialplanApplications();
49
            $app->uniqid    = DialplanApplications::generateUniqueID('DIALPLAN-APP-');
50
            $app->type      = 'php';
51
            $app->extension = Extensions::getNextFreeApplicationNumber();
52
        }
53
54
        $form                         = new DialplanApplicationEditForm($app);
55
        $this->view->form             = $form;
56
        $this->view->represent        = $app->getRepresent();
57
        $this->view->extension        = $app->extension;
58
    }
59
60
61
    /**
62
     * Saves Dialplan Application record settings
63
     */
64
    public function saveAction(): void
65
    {
66
        if ( ! $this->request->isPost()) {
67
            return;
68
        }
69
70
        $this->db->begin();
71
72
        $data      = $this->request->getPost();
73
        $appRecord = DialplanApplications::findFirstByUniqid($data['uniqid']);
74
        if ($appRecord === null) {
75
            $appRecord = new DialplanApplications();
76
77
            $extension                    = new Extensions();
78
            $extension->type              = Extensions::TYPE_DIALPLAN_APPLICATION;
79
            $extension->number            = $data['extension'];
80
            $extension->callerid          = $this->sanitizeCallerId($data['name']);
81
            $extension->userid            = null;
82
            $extension->show_in_phonebook = '1';
83
            $extension->public_access     = '0';
84
        } else {
85
            $extension = $appRecord->Extensions;
86
        }
87
88
        // Заполним параметры внутреннего номера
89
        if ( ! $this->updateExtension($extension, $data)) {
90
            $this->view->success = false;
91
            $this->db->rollback();
92
93
            return;
94
        }
95
96
        // Заполним параметры пользователя
97
        if ( ! $this->updateDialplanApplication($appRecord, $data)) {
98
            $this->view->success = false;
99
            $this->db->rollback();
100
101
            return;
102
        }
103
104
        $this->flash->success($this->translation->_('ms_SuccessfulSaved'));
105
        $this->view->success = true;
106
        $this->db->commit();
107
108
        // Если это было создание карточки то надо перегрузить страницу с указанием ID
109
        if (empty($data['id'])) {
110
            $this->view->reload = "dialplan-applications/modify/{$data['uniqid']}";
111
        }
112
    }
113
114
    /**
115
     * Updates Extensions by POST data
116
     *
117
     * @param \MikoPBX\Common\Models\Extensions $extension
118
     * @param array                             $data POST data
119
     *
120
     * @return bool update result
121
     */
122
    private function updateExtension(Extensions $extension, array $data): bool
123
    {
124
        $extension->number   = $data['extension'];
125
        $extension->callerid = $this->sanitizeCallerId($data['name']);
126
        $extension->show_in_phonebook = '1';
127
        if ($extension->save() === false) {
128
            $errors = $extension->getMessages();
129
            $this->flash->error(implode('<br>', $errors));
130
131
            return false;
132
        }
133
134
        return true;
135
    }
136
137
    /**
138
     * Updates DialplanApplication by POST data
139
     *
140
     * @param \MikoPBX\Common\Models\DialplanApplications $application
141
     * @param array                                       $data POST data
142
     *
143
     * @return bool update result
144
     */
145
    private function updateDialplanApplication(DialplanApplications $application, array $data): bool
146
    {
147
        // Заполним параметры записи
148
        foreach ($application as $name => $value) {
149
            switch ($name) {
150
                case 'extension':
151
                case 'name':
152
                    $application->$name = $data[$name];
153
                    break;
154
                case 'applicationlogic':
155
                    $application->setApplicationlogic($data[$name]);
156
                    break;
157
                default:
158
                    if (array_key_exists($name, $data)) {
159
                        $application->$name = $data[$name];
160
                    }
161
            }
162
        }
163
164
        if ($application->save() === false) {
165
            $errors = $application->getMessages();
166
            $this->flash->error(implode('<br>', $errors));
167
168
            return false;
169
        }
170
171
        return true;
172
    }
173
174
}