Files
server-manager/src/Controllers/DashboardController.php

150 lines
4.8 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace ServerManager\Controllers;
use ServerManager\Core\App;
use ServerManager\Core\Database;
use ServerManager\Core\Session;
use ServerManager\Models\Server;
use ServerManager\Models\Notification;
use ServerManager\Services\MonitoringService;
use ServerManager\Services\AuditService;
class DashboardController
{
private \ServerManager\Core\View $view;
private MonitoringService $monitoringService;
private AuditService $auditService;
public function __construct()
{
$this->view = App::getInstance()->getView();
$this->monitoringService = new MonitoringService();
$this->auditService = new AuditService();
}
public function landing(): void
{
if (Session::has('user_id')) {
$this->view->redirect('/dashboard');
}
$stats = [];
try {
$db = Database::getInstance();
$stats['servers'] = $db->fetch('SELECT COUNT(*) as count FROM servers')['count'] ?? 0;
$stats['users'] = $db->fetch('SELECT COUNT(*) as count FROM users')['count'] ?? 0;
$stats['online_servers'] = $db->fetch("SELECT COUNT(*) as count FROM servers WHERE current_status = 'online'")['count'] ?? 0;
} catch (\Throwable) {
$stats = ['servers' => 0, 'users' => 0, 'online_servers' => 0];
}
$this->view->setLayout('auth')->display('pages.landing', [
'title' => 'ServerManager - Linux Server Management Platform',
'stats' => $stats,
]);
}
public function index(): void
{
$serverModel = new Server();
$userId = (int) Session::get('user_id');
$accessibleIds = $serverModel->getAccessibleServerIds($userId);
$stats = $this->monitoringService->getDashboardStats($accessibleIds);
$servers = !empty($accessibleIds) ? array_values(array_filter($serverModel->findAll(true), function ($s) use ($accessibleIds) {
return in_array((int) $s['id'], $accessibleIds, true);
})) : [];
$aggregatedMetrics = $this->monitoringService->getAggregatedHistoricalMetrics($accessibleIds ?? []);
$recentActivity = $this->auditService->getRecentActivity(10, $userId);
$this->view->display('dashboard.index', [
'title' => 'Dashboard - ServerManager',
'stats' => $stats,
'servers' => $servers,
'aggregatedMetrics' => $aggregatedMetrics,
'recentActivity' => $recentActivity,
]);
}
public function stats(): void
{
$serverModel = new Server();
$accessibleIds = $serverModel->getAccessibleServerIds((int) Session::get('user_id'));
$stats = $this->monitoringService->getDashboardStats($accessibleIds);
$this->view->json([
'success' => true,
'data' => $stats,
]);
}
public function refreshMetrics(): void
{
$this->monitoringService->collectAllMetrics();
$this->view->json([
'success' => true,
'message' => 'Metrics refreshed',
]);
}
public function chartData(): void
{
$serverModel = new Server();
$accessibleIds = $serverModel->getAccessibleServerIds((int) Session::get('user_id'));
$data = $this->monitoringService->getAggregatedHistoricalMetrics($accessibleIds ?? []);
$this->view->json([
'success' => true,
'data' => $data,
]);
}
public function privacy(): void
{
$db = Database::getInstance();
$row = $db->fetch('SELECT `value` FROM app_config WHERE `key` = ?', ['privacy_policy']);
$content = $row ? $row['value'] : '';
$this->view->setLayout('auth')->display('pages.privacy', [
'title' => 'Privacy Policy - ServerManager',
'content' => $content,
]);
}
public function terms(): void
{
$db = Database::getInstance();
$row = $db->fetch('SELECT `value` FROM app_config WHERE `key` = ?', ['terms_conditions']);
$content = $row ? $row['value'] : '';
$this->view->setLayout('auth')->display('pages.terms', [
'title' => 'Terms & Conditions - ServerManager',
'content' => $content,
]);
}
public function notifications(): void
{
$userId = (int) Session::get('user_id');
$page = (int) ($_GET['page'] ?? 1);
$perPage = 20;
$notificationModel = new Notification();
$result = $notificationModel->getForUser($userId, $page, $perPage);
$unreadCount = $notificationModel->getUnreadCount($userId);
$this->view->display('notifications.index', [
'title' => 'Notifications - ServerManager',
'notifications' => $result['data'],
'pagination' => $result,
'unreadCount' => $unreadCount,
]);
}
}