Passed
Push — develop ( 439163...06f922 )
by Nikolay
13:40
created

SoundFilesController::modifyAction()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 18
rs 9.8333
cc 4
nc 8
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 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\SoundFilesEditForm;
23
use MikoPBX\Common\Models\SoundFiles;
24
25
class SoundFilesController extends BaseController
26
{
27
28
    /**
29
     * Build sounds list
30
     */
31
    public function indexAction(): void
32
    {
33
        $this->view->mohFiles    = SoundFiles::find('category="' . SoundFiles::CATEGORY_MOH . '"');
34
        $this->view->customFiles = SoundFiles::find('category="' . SoundFiles::CATEGORY_CUSTOM . '"');
35
    }
36
37
38
    /**
39
     * Open and edit record
40
     *
41
     * @param string $id редактируемой записи
42
     */
43
    public function modifyAction(string $id = ''): void
44
    {
45
        if (in_array($id, [SoundFiles::CATEGORY_CUSTOM, SoundFiles::CATEGORY_MOH], true)) {
46
            $file           = new SoundFiles();
47
            $file->category = $id;
48
        } else {
49
            $file = SoundFiles::findFirstById($id);
50
        }
51
        if ($file === null) {
52
            $file           = new SoundFiles();
53
            $file->category = SoundFiles::CATEGORY_CUSTOM;
54
        }
55
56
        $form                  = new SoundFilesEditForm($file);
57
        $this->view->form      = $form;
58
        $this->view->category  = $file->category;
59
        $this->view->audioPath = empty($file->path) ? '' : "/pbxcore/api/cdr/playback?view={$file->path}";
60
        $this->view->represent = $file->getRepresent();
61
    }
62
63
64
    /**
65
     * Save sound file to storage
66
     *
67
     * @return void
68
     */
69
    public function saveAction(): void
70
    {
71
        if ( ! $this->request->isPost()) {
72
            return;
73
        }
74
        $data = $this->request->getPost();
75
76
        $soundFile = SoundFiles::findFirstById($data['id']);
77
        if ($soundFile === null) {
78
            $soundFile = new SoundFiles();
79
        }
80
81
        foreach ($soundFile as $name => $value) {
82
            switch ($name) {
83
                case "id":
84
                    break;
85
                default:
86
                    if ( ! array_key_exists($name, $data)) {
87
                        continue 2;
88
                    }
89
                    $soundFile->$name = $data[$name];
90
            }
91
        }
92
93
        if ($soundFile->save() === false) {
94
            $errors = $soundFile->getMessages();
95
            $this->flash->error(implode('<br>', $errors));
96
            $this->view->success = false;
97
        } else {
98
            $this->flash->success($this->translation->_('ms_SuccessfulSaved'));
99
            $this->view->success = true;
100
            // If it was create new one, we will reload page
101
            if (empty($data['id'])) {
102
                $this->view->reload = "sound-files/modify/{$soundFile->id}";
103
            }
104
        }
105
    }
106
107
    /**
108
     * Delete sound file by ID
109
     *
110
     * @param string $id
111
     */
112
    public function deleteAction(string $id = ''): void
113
    {
114
        if ($id === '') {
115
            return;
116
        }
117
        $soundFile = SoundFiles::findFirstById($id);
118
        if ($soundFile === null){
119
            return;
120
        }
121
        $errors    = false;
122
        if (! $soundFile->delete()) {
123
            $errors = $soundFile->getMessages();
124
        }
125
        if ($errors) {
126
            $this->flash->error(implode('<br>', $errors));
127
            $this->view->success = false;
128
        } else {
129
            $this->view->success = true;
130
        }
131
    }
132
133
    /**
134
     * Get full path to the file by ID
135
     *
136
     * @param string $id
137
     */
138
    public function getPathByIdAction(string $id = ''): void
139
    {
140
        $soundFile = SoundFiles::findFirstById($id);
141
        if ($soundFile !== null) {
142
            $this->view->message = $soundFile->path;
143
            $this->view->success = true;
144
        } else {
145
            $this->view->success = false;
146
        }
147
    }
148
149
    /**
150
     * Returns array of sound files for dropdown lists
151
     *
152
     * @param string $category
153
     */
154
    public function getSoundFilesAction(string $category='custom'):void
155
    {
156
        $soundFiles = SoundFiles::find("category='{$category}'");
157
        $soundFilesList = [];
158
        foreach ($soundFiles as $soundFile) {
159
            $soundFilesList[] =
160
                [
161
                    'name'=>$soundFile->getRepresent(),
162
                    'value'=>$soundFile->id
163
                ];
164
        }
165
        $this->view->results = $soundFilesList;
166
        $this->view->success = true;
167
    }
168
169
}