/
mikopbx
/
ModuleBeelinePbx
Обзор
Документация
Войти
/
mikopbx
/
ModuleBeelinePbx
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
master
App/Controllers/ModuleBeelinePbxController.php
140 строк
6 KB
boffart
Инициализация модуля ModuleBeelinePbx
08 июл 2026, 17:11
08 июл 2026, 17:11
1cc4e1a
Код
Авторство
О чём код?
<?php /* * MikoPBX - free phone system for small business * Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program. * If not, see <https://www.gnu.org/licenses/>. */ namespace Modules\ModuleBeelinePbx\App\Controllers; use MikoPBX\AdminCabinet\Controllers\BaseController; use MikoPBX\Core\System\Util; use MikoPBX\Modules\PbxExtensionUtils; use Modules\ModuleBeelinePbx\App\Forms\ModuleBeelinePbxForm; use Modules\ModuleBeelinePbx\Models\ModuleBeelinePbx; class ModuleBeelinePbxController extends BaseController { private $moduleUniqueID = 'ModuleBeelinePbx'; private $moduleDir; /** * Инициализация контроллера. */ public function initialize(): void { $this->moduleDir = PbxExtensionUtils::getModuleDir($this->moduleUniqueID); $this->view->logoImagePath = "{$this->url->get()}assets/img/cache/{$this->moduleUniqueID}/logo.svg"; $this->view->submitMode = null; parent::initialize(); } /** * Страница настроек модуля. */ public function indexAction(): void { $footerCollection = $this->assets->collection('footerJS'); $footerCollection->addJs('js/pbx/main/form.js', true); $footerCollection->addJs("js/cache/{$this->moduleUniqueID}/module-beeline-pbx-index.js", true); $headerCollectionCSS = $this->assets->collection('headerCSS'); $headerCollectionCSS->addCss("css/cache/{$this->moduleUniqueID}/module-beeline-pbx.css", true); $settings = ModuleBeelinePbx::findFirst(); if ($settings === null) { $settings = new ModuleBeelinePbx(); $settings->save(); } // Готовый callback-URL приёмника событий: показываем, если задан публичный адрес АТС. $callbackUrl = ''; if (!empty($settings->pbxPublicUrl)) { $callbackUrl = rtrim((string)$settings->pbxPublicUrl, '/') . '/pbxcore/beeline-pbx/event'; } $this->view->callbackUrl = $callbackUrl; $this->view->downloadRecordings = (int)$settings->downloadRecordings === 1; $this->view->pushSubscriptionEnabled = (int)$settings->pushSubscriptionEnabled === 1; // Человекочитаемый статус подписки для индикатора в UI. $subscriptionStatus = ''; $subscriptionStatusColor = 'grey'; if (!empty($settings->subscriptionError)) { $subscriptionStatusColor = 'red'; $subscriptionStatus = Util::translate('module_beeline_pbx_subscriptionError') . ': ' . $settings->subscriptionError; } elseif (!empty($settings->subscriptionId) && (int)$settings->subscriptionExpiresAt > time()) { $subscriptionStatusColor = 'green'; $until = date('Y-m-d H:i', (int)$settings->subscriptionExpiresAt); $subscriptionStatus = Util::translate('module_beeline_pbx_subscriptionActive') . ' ' . $until; } elseif ((int)$settings->pushSubscriptionEnabled === 1) { $subscriptionStatus = Util::translate('module_beeline_pbx_subscriptionPending'); } $this->view->subscriptionStatus = $subscriptionStatus; $this->view->subscriptionStatusColor = $subscriptionStatusColor; $this->view->form = new ModuleBeelinePbxForm($settings); $this->view->pick("{$this->moduleDir}/App/Views/index"); } /** * Сохранение настроек (AJAX). */ public function saveAction(): void { $data = $this->request->getPost(); $record = ModuleBeelinePbx::findFirst(); if ($record === null) { $record = new ModuleBeelinePbx(); } $this->db->begin(); foreach ($record as $key => $value) { switch ($key) { case 'id': break; case 'downloadRecordings': case 'pushSubscriptionEnabled': // Fomantic 'get values' шлёт для чекбокса "1" (включён) либо false → в POST // приходит строка "false" (выключен). НЕЛЬЗЯ использовать !empty(): строка // "false" непустая → выключение чекбокса никогда бы не сохранялось. // Считаем включением только явные truthy-значения. $raw = $data[$key] ?? '0'; $record->$key = in_array($raw, ['1', 'on', 'true', true, 1], true) ? '1' : '0'; break; case 'subscriptionId': case 'subscriptionExpiresAt': case 'subscriptionError': // Служебные поля подписки формой не редактируются — ими владеет cron-скрипт. break; default: if (array_key_exists($key, $data)) { $record->$key = $data[$key]; } } } if ($record->save() === false) { $errors = $record->getMessages(); $this->flash->error(implode('<br>', $errors)); $this->view->success = false; $this->db->rollback(); return; } $this->flash->success($this->translation->_('ms_SuccessfulSaved')); $this->view->success = true; $this->db->commit(); } }