|
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\PBXCoreREST\Lib\Extensions; |
|
21
|
|
|
|
|
22
|
|
|
use MikoPBX\Common\Models\Extensions; |
|
23
|
|
|
use MikoPBX\Common\Models\PbxSettings; |
|
24
|
|
|
use MikoPBX\PBXCoreREST\Lib\PBXApiResult; |
|
25
|
|
|
use Phalcon\Di\Injectable; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class Utils |
|
29
|
|
|
* Provides utility functions for handling phone numbers and availability checks. |
|
30
|
|
|
* |
|
31
|
|
|
* @package MikoPBX\PBXCoreREST\Lib\Extensions |
|
32
|
|
|
*/ |
|
33
|
|
|
class Utils extends Injectable |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Retrieves the view for the phone numbers list via AJAX request. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $numbers List of phone numbers. |
|
40
|
|
|
* @return PBXApiResult Result of the operation. |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function getPhonesRepresent(array $numbers): PBXApiResult |
|
43
|
|
|
{ |
|
44
|
|
|
$res = new PBXApiResult(); |
|
45
|
|
|
$res->processor = __METHOD__; |
|
46
|
|
|
$res->success = true; |
|
47
|
|
|
|
|
48
|
|
|
$result = []; |
|
49
|
|
|
foreach ($numbers as $number) { |
|
50
|
|
|
$result[$number] = [ |
|
51
|
|
|
'number' => $number, |
|
52
|
|
|
'represent' => self::getPhoneRepresent($number)->data, |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
$res->data = $result; |
|
56
|
|
|
|
|
57
|
|
|
return $res; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Retrieves the view for the phone number via AJAX request. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $phoneNumber The phone number. |
|
64
|
|
|
* @return PBXApiResult Result of the operation. |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function getPhoneRepresent(string $phoneNumber): PBXApiResult |
|
67
|
|
|
{ |
|
68
|
|
|
$res = new PBXApiResult(); |
|
69
|
|
|
$res->processor = __METHOD__; |
|
70
|
|
|
$res->success = true; |
|
71
|
|
|
|
|
72
|
|
|
$response = $phoneNumber; |
|
73
|
|
|
|
|
74
|
|
|
if (strlen($phoneNumber) > 10) { |
|
75
|
|
|
$seekNumber = substr($phoneNumber, -9); |
|
76
|
|
|
$parameters = [ |
|
77
|
|
|
'conditions' => 'number LIKE :SearchPhrase1:', |
|
78
|
|
|
'bind' => [ |
|
79
|
|
|
'SearchPhrase1' => "%{$seekNumber}", |
|
80
|
|
|
], |
|
81
|
|
|
]; |
|
82
|
|
|
} else { |
|
83
|
|
|
$parameters = [ |
|
84
|
|
|
'conditions' => 'number = :SearchPhrase1:', |
|
85
|
|
|
'bind' => [ |
|
86
|
|
|
'SearchPhrase1' => $phoneNumber, |
|
87
|
|
|
], |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
$result = Extensions::findFirst($parameters); |
|
91
|
|
|
if ($result !== null) { |
|
92
|
|
|
$response = $result->getRepresent(); |
|
93
|
|
|
} |
|
94
|
|
|
$res->data[] = $response; |
|
95
|
|
|
|
|
96
|
|
|
return $res; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Check the availability of a number in the extensions.js JavaScript script. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $number The internal number of the user. |
|
103
|
|
|
* @return PBXApiResult Result of the availability check. |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function ifNumberAvailable(string $number): PBXApiResult |
|
106
|
|
|
{ |
|
107
|
|
|
$res = new PBXApiResult(); |
|
108
|
|
|
$res->processor = __METHOD__; |
|
109
|
|
|
$res->success = true; |
|
110
|
|
|
|
|
111
|
|
|
// Check for overlap with internal number plan |
|
112
|
|
|
$extension = Extensions::findFirstByNumber($number); |
|
113
|
|
|
if ($extension !== null) { |
|
114
|
|
|
$res->success = false; |
|
115
|
|
|
$res->data['userId'] = $extension->userid; |
|
116
|
|
|
$res->data['represent'] = $extension->getRepresent(); |
|
117
|
|
|
return $res; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Check for overlap with parking slots |
|
121
|
|
|
$parkExt = PbxSettings::getValueByKey('PBXCallParkingExt'); |
|
122
|
|
|
$parkStartSlot = PbxSettings::getValueByKey('PBXCallParkingStartSlot'); |
|
123
|
|
|
$parkEndSlot = PbxSettings::getValueByKey('PBXCallParkingEndSlot'); |
|
124
|
|
|
if ($number === $parkExt || ($number >= $parkStartSlot && $number <= $parkEndSlot)) { |
|
125
|
|
|
$res->success = false; |
|
126
|
|
|
} |
|
127
|
|
|
return $res; |
|
128
|
|
|
} |
|
129
|
|
|
} |