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

ConferenceRoomsController::deleteAction()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 25
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\ConferenceRoomEditForm;
23
use MikoPBX\Common\Models\{ConferenceRooms, Extensions};
24
25
26
class ConferenceRoomsController extends BaseController
27
{
28
    /**
29
     * Build the list of conference rooms.
30
     */
31
    public function indexAction(): void
32
    {
33
        $records             = ConferenceRooms::find();
34
        $this->view->records = $records;
35
    }
36
37
    /**
38
     * Edit conference room details.
39
     *
40
     * @param string|null $uniqid The unique identifier of the conference room.
41
     */
42
    public function modifyAction(string $uniqid = null): void
43
    {
44
        $record = ConferenceRooms::findFirstByUniqid($uniqid);
45
        if ($record === null) {
46
            // Create a new conference room if not found
47
            $record            = new ConferenceRooms();
48
            $record->uniqid    = ConferenceRooms::generateUniqueID(Extensions::TYPE_CONFERENCE.'-');
49
            $record->extension = Extensions::getNextFreeApplicationNumber();
50
        }
51
        $this->view->form      = new ConferenceRoomEditForm($record);
52
        $this->view->represent = $record->getRepresent();
53
        $this->view->extension = $record->extension;
54
    }
55
56
    /**
57
     * Save the conference room.
58
     */
59
    public function saveAction(): void
60
    {
61
        if ( ! $this->request->isPost()) {
62
            return;
63
        }
64
        $this->db->begin();
65
        $data = $this->request->getPost();
66
        $room = ConferenceRooms::findFirstByUniqid($data['uniqid']);
67
        if ($room === null) {
68
            // Create new conference room and extension if not found
69
            $room                         = new ConferenceRooms();
70
            $extension                    = new Extensions();
71
            $extension->type              = Extensions::TYPE_CONFERENCE;
72
            $extension->number            = $data["extension"];
73
            $extension->callerid          = $this->sanitizeCallerId($data["name"]);
74
            $extension->userid            = null;
75
            $extension->show_in_phonebook = 1;
76
            $extension->public_access     = 1;
77
        } else {
78
            $extension = $room->Extensions;
79
        }
80
81
        // Update extension parameters
82
        if ( ! $this->updateExtension($extension, $data)) {
83
            $this->view->success = false;
84
            $this->db->rollback();
85
86
            return;
87
        }
88
89
        // Update conference room parameters
90
        if ( ! $this->updateConferenceRoom($room, $data)) {
91
            $this->view->success = false;
92
            $this->db->rollback();
93
94
            return;
95
        }
96
97
        $this->flash->success($this->translation->_('ms_SuccessfulSaved'));
98
        $this->view->success = true;
99
        $this->db->commit();
100
101
        // If it was a new entity, reload the page with the new ID
102
        if (empty($data['id'])) {
103
            $this->view->reload = "conference-rooms/modify/{$data['uniqid']}";
104
        }
105
    }
106
107
    /**
108
     * Update extension parameters.
109
     *
110
     * @param \MikoPBX\Common\Models\Extensions $extension The extension entity.
111
     * @param array                             $data      The array of fields from the POST request.
112
     *
113
     * @return bool The update result.
114
     */
115
    private function updateExtension(Extensions $extension, array $data): bool
116
    {
117
        $extension->number   = $data['extension'];
118
        $extension->callerid = $this->sanitizeCallerId($data['name']);
119
        if ($extension->save() === false) {
120
            $errors = $extension->getMessages();
121
            $this->flash->error(implode('<br>', $errors));
122
123
            return false;
124
        }
125
126
        return true;
127
    }
128
129
    /**
130
     * Update conference room properties.
131
     *
132
     * @param \MikoPBX\Common\Models\ConferenceRooms $room The conference room entity.
133
     * @param array                                  $data The POST fields.
134
     *
135
     * @return bool The update result.
136
     */
137
    private function updateConferenceRoom(ConferenceRooms $room, array $data): bool
138
    {
139
        foreach ($room as $name => $value) {
140
            switch ($name) {
141
                case "extension":
142
                case "name":
143
                    $room->$name = $data[$name];
144
                    break;
145
                default:
146
                    if ( ! array_key_exists($name, $data)) {
147
                        continue 2;
148
                    }
149
                    $room->$name = $data[$name];
150
            }
151
        }
152
        if ($room->save() === false) {
153
            $errors = $room->getMessages();
154
            $this->flash->error(implode('<br>', $errors));
155
156
            return false;
157
        }
158
159
        return true;
160
    }
161
162
}