Skip to content

Commit 2f0b672

Browse files
committed
feat: add GetListAction for Providers API
- Implement provider list retrieval with filtering - Support for including/excluding disabled providers - Sort providers by type and name - Add host and username fields to response
1 parent 56ee67f commit 2f0b672

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/*
3+
* MikoPBX - free phone system for small business
4+
* Copyright © 2017-2025 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+
declare(strict_types=1);
21+
22+
namespace MikoPBX\PBXCoreREST\Lib\Providers;
23+
24+
use MikoPBX\Common\Models\Providers;
25+
use MikoPBX\Core\System\Util;
26+
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
27+
28+
/**
29+
* Class GetListAction
30+
*
31+
* Retrieves list of providers organized by categories (SIP/IAX2)
32+
*
33+
* @package MikoPBX\PBXCoreREST\Lib\Providers
34+
*/
35+
class GetListAction
36+
{
37+
/**
38+
* Get list of all providers
39+
*
40+
* @param bool $includeDisabled Include disabled providers in the list
41+
* @return PBXApiResult
42+
*/
43+
public static function main(bool $includeDisabled = false): PBXApiResult
44+
{
45+
$res = new PBXApiResult();
46+
$res->processor = __METHOD__;
47+
48+
try {
49+
// Get all providers
50+
$providers = Providers::find();
51+
52+
// Process providers
53+
foreach ($providers as $provider) {
54+
$providerData = self::processProvider($provider, $includeDisabled);
55+
if ($providerData !== null) {
56+
$res->data[] = $providerData;
57+
}
58+
}
59+
60+
// Sort providers by type and name
61+
usort($res->data, function($a, $b) {
62+
// First sort by type (SIP before IAX)
63+
$typeCompare = strcmp($a['type'], $b['type']);
64+
if ($typeCompare !== 0) {
65+
return $typeCompare;
66+
}
67+
// Then sort by name
68+
return strcmp($a['name'], $b['name']);
69+
});
70+
71+
$res->success = true;
72+
} catch (\Exception $e) {
73+
$res->messages['error'][] = $e->getMessage();
74+
}
75+
76+
return $res;
77+
}
78+
79+
/**
80+
* Process single provider record
81+
*
82+
* @param Providers $provider
83+
* @param bool $includeDisabled
84+
* @return array|null Provider data or null if should be skipped
85+
*/
86+
private static function processProvider(Providers $provider, bool $includeDisabled): ?array
87+
{
88+
$providerData = [
89+
'id' => $provider->id,
90+
'uniqid' => $provider->uniqid,
91+
'type' => $provider->type,
92+
'typeLocalized' => Util::translate("prov_dropdownCategory_{$provider->type}"),
93+
'name' => $provider->getRepresent(),
94+
'disabled' => false,
95+
'note' => $provider->note ?? ''
96+
];
97+
98+
// Check if provider is disabled based on type
99+
if ($provider->type === 'SIP' && $provider->Sip) {
100+
$providerData['disabled'] = $provider->Sip->disabled === '1';
101+
$providerData['host'] = $provider->Sip->host;
102+
$providerData['username'] = $provider->Sip->username;
103+
} elseif ($provider->type === 'IAX' && $provider->Iax) {
104+
$providerData['disabled'] = $provider->Iax->disabled === '1';
105+
$providerData['host'] = $provider->Iax->host;
106+
$providerData['username'] = $provider->Iax->username;
107+
}
108+
109+
// Skip disabled providers if not including them
110+
if (!$includeDisabled && $providerData['disabled']) {
111+
return null;
112+
}
113+
114+
return $providerData;
115+
}
116+
}

0 commit comments

Comments
 (0)