87 lines
3.1 KiB
PHP
87 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Controllers\Controller;
|
|
use App\Core\Request;
|
|
use App\Core\Response;
|
|
use App\Services\Database\Connection;
|
|
use App\Services\Notification\TelegramNotifier;
|
|
use PDO;
|
|
use Throwable;
|
|
|
|
class SettingsController extends Controller
|
|
{
|
|
private PDO $pdo;
|
|
private TelegramNotifier $notifier;
|
|
|
|
public function __construct(Connection $connection, TelegramNotifier $notifier)
|
|
{
|
|
parent::__construct();
|
|
$this->pdo = $connection->getPdo();
|
|
$this->notifier = $notifier;
|
|
}
|
|
|
|
public function index(Request $request, Response $response): string
|
|
{
|
|
// Get telegram settings from database
|
|
$tgToken = $this->getSetting('telegram_bot_token', '');
|
|
$tgChatId = $this->getSetting('telegram_chat_id', '');
|
|
$tgEnabled = $this->getSetting('telegram_enabled', '0');
|
|
|
|
return $this->render('admin/settings/index', [
|
|
'tg_token' => $tgToken,
|
|
'tg_chat_id' => $tgChatId,
|
|
'tg_enabled' => $tgEnabled,
|
|
], 'admin');
|
|
}
|
|
|
|
public function save(Request $request, Response $response): void
|
|
{
|
|
$tgToken = $request->post('telegram_bot_token', '');
|
|
$tgChatId = $request->post('telegram_chat_id', '');
|
|
$tgEnabled = $request->post('telegram_enabled', '0');
|
|
$action = $request->post('action', 'save');
|
|
|
|
$this->saveSetting('telegram_bot_token', $tgToken);
|
|
$this->saveSetting('telegram_chat_id', $tgChatId);
|
|
$this->saveSetting('telegram_enabled', $tgEnabled);
|
|
|
|
if ($action === 'test') {
|
|
$this->notifier->configure($tgToken, $tgChatId);
|
|
if ($this->notifier->sendTest()) {
|
|
$this->session->setFlash('success', 'Settings saved and test notification sent to Telegram!');
|
|
} else {
|
|
$err = $this->notifier->getLastError() ?: 'Check your token and chat ID.';
|
|
$this->session->setFlash('error', 'Settings saved, but failed to send Telegram notification: ' . $err);
|
|
}
|
|
} else {
|
|
$this->session->setFlash('success', 'Settings saved successfully.');
|
|
}
|
|
$response->redirect('/admin/settings');
|
|
}
|
|
|
|
public function switchLang(Request $request, Response $response, string $lang = 'en'): void
|
|
{
|
|
$lang = $request->routeParam('lang', $lang);
|
|
if (in_array($lang, ['ar', 'en'])) {
|
|
$this->session->set('lang', $lang);
|
|
}
|
|
$ref = $request->getHeader('Referer') ?? '/admin/dashboard';
|
|
$response->redirect($ref);
|
|
}
|
|
|
|
private function getSetting(string $key, string $default = ''): string
|
|
{
|
|
$stmt = $this->pdo->prepare("SELECT `value` FROM settings WHERE `key` = ?");
|
|
$stmt->execute([$key]);
|
|
$val = $stmt->fetchColumn();
|
|
return $val !== false ? $val : $default;
|
|
}
|
|
|
|
private function saveSetting(string $key, string $value): void
|
|
{
|
|
$stmt = $this->pdo->prepare("INSERT INTO settings (`key`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = ?");
|
|
$stmt->execute([$key, $value, $value]);
|
|
}
|
|
} |