Passed
Push — develop ( 78234a...cd562a )
by Портнов
04:32
created

ResParkingConf::getFeatureMap()   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
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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\Core\Asterisk\Configs;
21
22
use MikoPBX\Common\Models\PbxSettings;
23
use MikoPBX\Core\System\Util;
24
25
class ResParkingConf extends CoreConfigClass
26
{
27
    protected string $ParkingExt;
28
    protected string $ParkingStartSlot;
29
    protected string $ParkingEndSlot;
30
    protected string $ParkingFeature;
31
    protected string $ParkingDuration;
32
33
    protected string $description = 'res_parking.conf';
34
35
    /**
36
     *
37
     * @return array
38
     */
39
    public function getDependenceModels(): array
40
    {
41
        return [PbxSettings::class];
42
    }
43
44
    /**
45
     * ResParkingConf constructor.
46
     *
47
     */
48
    public function __construct(){
49
        parent::__construct();
50
        // Вызов "getSettings" приемлем, так как идет работа с инициализированной переменной generalSettings.
51
        $this->getSettings();
52
    }
53
54
    protected function generateConfigProtected(): void
55
    {
56
        // Генерация конфигурационных файлов.
57
        $conf   = "[general]".PHP_EOL.
58
            "parkeddynamic = yes".PHP_EOL.PHP_EOL.
59
            "[default]".PHP_EOL.
60
            "context => parked-calls".PHP_EOL.
61
            "parkedcallreparking = caller".PHP_EOL.
62
            "parkedcalltransfers = caller".PHP_EOL.
63
            "parkext => $this->ParkingExt".PHP_EOL.
64
            "findslot => next".PHP_EOL.
65
            "comebacktoorigin=no".PHP_EOL.
66
            "comebackcontext = parked-calls-timeout".PHP_EOL.
67
            "parkpos => $this->ParkingStartSlot-$this->ParkingEndSlot".PHP_EOL.PHP_EOL;
68
        file_put_contents($this->config->path('asterisk.astetcdir') . '/res_parking.conf', $conf);
69
    }
70
71
    /**
72
     * Функция позволяет получить активные каналы.
73
     * Возвращает ассоциативный массив. Ключ - linked id, значение - массив каналов.
74
     * @param string|null $extension
75
     * @return array|null
76
     * @throws \Phalcon\Exception
77
     */
78
    public static function getParkSlotData(?string $extension = null) : ?array
79
    {
80
        $ParkeeChannel = null;
81
        $am            = Util::getAstManager('off');
82
        $res           = $am->ParkedCalls('default');
83
        if (count($res['data']) === 0) {
84
            return null;
85
        }
86
87
        foreach ($res['data']['ParkedCall'] as $park_row) {
88
            if ($park_row['ParkingSpace'] === $extension || $extension === null) {
89
                $var_data                = $am->GetVar($park_row['ParkeeChannel'], 'pt1c_is_dst');
90
                $park_row['pt1c_is_dst'] = ($var_data["Value"] === '1');
91
                $ParkeeChannel           = $park_row;
92
            }
93
        }
94
95
        return $ParkeeChannel;
96
    }
97
98
    /**
99
     * Получение настроек.
100
     */
101
    public function getSettings(): void
102
    {
103
        $this->ParkingExt       = PbxSettings::getValueByKey('PBXCallParkingExt');
104
        $this->ParkingFeature   = PbxSettings::getValueByKey('PBXCallParkingFeature');
105
        $this->ParkingDuration  = PbxSettings::getValueByKey('PBXCallParkingDuration');
106
        $this->ParkingStartSlot = (int)PbxSettings::getValueByKey('PBXCallParkingStartSlot');
107
        $this->ParkingEndSlot   = (int)PbxSettings::getValueByKey('PBXCallParkingEndSlot');
108
    }
109
110
    /**
111
     * Генерация дополнительных контекстов.
112
     *
113
     * @return string
114
     */
115
    public function extensionGenContexts(): string
116
    {
117
        // Генерация внутреннего номерного плана.
118
        $conf  = "[parked-calls]\n";
119
        $conf .= "exten => _X!,1,NoOp()\n\t";
120
        $conf .= 'same => n,AGI(cdr_connector.php,unpark_call)' . "\n\t";
121
        $conf .= 'same => n,ExecIf($["${pt1c_PARK_CHAN}x" != "x"]?Bridge(${pt1c_PARK_CHAN},kKTt))' . "\n\t";
122
        $conf .= 'same => n,ExecIf($["${pt1c_PARK_CHAN}x" == "x"]?ParkedCall(default,${EXTEN}))' . "\n\t";
123
        $conf .= 'same => n,Hangup()' . "\n\n";
124
125
        $conf .= "[parked-calls-timeout]\n";
126
        $conf .= "exten => s,1,NoOp(This is all that happens to parked calls if they time out.)\n\t";
127
        $conf .= 'same => n,Set(FROM_PEER=${EMPTYVAR})' . "\n\t";
128
        $conf .= 'same => n,AGI(cdr_connector.php,unpark_call_timeout)' . "\n\t";
129
        $conf .= 'same => n,Goto(internal,${CUT(PARKER,/,2)},1)' . "\n\t";
130
        $conf .= 'same => n,Hangup()' . "\n\n";
131
132
        return $conf;
133
    }
134
135
    /**
136
     * Возвращает номерной план для internal контекста.
137
     *
138
     * @return string
139
     */
140
    public function extensionGenInternal(): string
141
    {
142
        $conf = '';
143
        for ($ext = $this->ParkingStartSlot; $ext <= $this->ParkingEndSlot; $ext++) {
144
            $conf .= 'exten => ' . $ext . ',1,Goto(parked-calls,${EXTEN},2)' . "\n";
145
        }
146
        $conf .= "\n";
147
148
        return $conf;
149
    }
150
151
    /**
152
     * Возвращает включения в контекст internal-transfer
153
     *
154
     * @return string
155
     */
156
    public function getIncludeInternalTransfer(): string
157
    {
158
        if(empty($this->ParkingExt)){
159
            $conf = '';
160
        }else{
161
            $conf = 'exten => ' . $this->ParkingExt . ',1,Goto(parked-calls,${EXTEN},1)' . PHP_EOL;
162
        }
163
        return $conf;
164
    }
165
166
    /**
167
     * Дополнительные параметры для секции global.
168
     *
169
     * @return string
170
     */
171
    public function extensionGlobals(): string
172
    {
173
        return "PARKING_DURATION=$this->ParkingDuration".PHP_EOL;
174
    }
175
176
    /**
177
     * Дополнительные коды feature.conf
178
     *
179
     * @return string
180
     */
181
    public function getFeatureMap(): string
182
    {
183
        return "parkcall => $this->ParkingFeature".PHP_EOL;
184
    }
185
}
186