Deploy on 2026-06-05 15:23:02
This commit is contained in:
97
app/Controllers/Admin/SettingsController.php
Normal file
97
app/Controllers/Admin/SettingsController.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?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
|
||||
{
|
||||
$lang = $this->session->get('lang', 'en');
|
||||
$langFile = __DIR__ . "/../../resources/lang/{$lang}.php";
|
||||
$t = file_exists($langFile) ? require $langFile : [];
|
||||
|
||||
// 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', [
|
||||
't' => $t,
|
||||
'lang' => $lang,
|
||||
'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');
|
||||
|
||||
$this->saveSetting('telegram_bot_token', $tgToken);
|
||||
$this->saveSetting('telegram_chat_id', $tgChatId);
|
||||
$this->saveSetting('telegram_enabled', $tgEnabled);
|
||||
|
||||
$this->session->setFlash('success', 'Settings saved successfully.');
|
||||
$response->redirect('/admin/settings');
|
||||
}
|
||||
|
||||
public function testTelegram(Request $request, Response $response): void
|
||||
{
|
||||
$tgToken = $request->post('telegram_bot_token', '');
|
||||
$tgChatId = $request->post('telegram_chat_id', '');
|
||||
|
||||
$this->notifier->configure($tgToken, $tgChatId);
|
||||
|
||||
if ($this->notifier->sendTest()) {
|
||||
$this->session->setFlash('success', 'Test notification sent to Telegram!');
|
||||
} else {
|
||||
$this->session->setFlash('error', 'Failed to send Telegram notification. Check your token and chat ID.');
|
||||
}
|
||||
$response->redirect('/admin/settings');
|
||||
}
|
||||
|
||||
public function switchLang(Request $request, Response $response): void
|
||||
{
|
||||
$lang = $request->get('lang', 'en');
|
||||
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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user