PHP_AdminTool_Projekt/app/Controllers/DashboardController.php
2025-11-17 06:35:41 +01:00

121 lines
4.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// Strenge Typprüfung für Parameter- und Rückgabetypen aktivieren.
declare(strict_types=1);
namespace App\Controllers;
use App\Services\Snmp\SnmpServerStatusService;
/**
* Controller für das Dashboard.
* Zuständig für:
* - Abrufen des Serverstatus (über SnmpServerStatusService)
* - Auswählen und Rendern der Dashboard-View
*/
class DashboardController
{
/** @var array<string, mixed> Vollständige Anwendungskonfiguration (aus config.php) */
private array $config;
/** @var SnmpServerStatusService Service, der den Serverstatus (später per SNMP) liefert */
private SnmpServerStatusService $snmpService;
/**
* Übergibt die Konfiguration an den Controller und initialisiert den SNMP-Statusservice.
*
* @param array<string, mixed> $config Vollständige Konfiguration aus config.php
*/
public function __construct(array $config)
{
// Komplette Config lokal speichern (falls später weitere Werte benötigt werden).
$this->config = $config;
// Teilbereich "snmp" aus der Konfiguration ziehen.
// Wenn nicht vorhanden, wird ein leeres Array übergeben (der Service prüft das selbst).
$snmpConfig = $config['snmp'] ?? [];
// SNMP-Service initialisieren, der den Serverstatus liefert.
$this->snmpService = new SnmpServerStatusService($snmpConfig);
}
/**
* Zeigt das Dashboard an.
* Holt die Serverstatus-Daten aus dem SnmpServerStatusService und übergibt sie an die View.
*/
public function show(): void
{
// Serverstatus über den SNMP-Service ermitteln.
// In der aktuellen Version liefert der Service noch Demo-Daten.
$serverStatus = $this->snmpService->getServerStatus();
// Pfad zur Dashboard-View (Template-Datei) ermitteln.
$viewPath = __DIR__ . '/../../public/views/dashboard.php';
// Falls die View-Datei (noch) nicht existiert, Fallback-HTML direkt aus dem Controller ausgeben.
if (file_exists($viewPath) === false) {
$this->renderInlineDashboard($serverStatus);
return;
}
// View-Datei einbinden. Die Variable $serverStatus steht in der View zur Verfügung.
require $viewPath;
}
/**
* Fallback-Dashboard-Ausgabe direkt aus dem Controller (ohne separate View-Datei).
* Nur als Notlösung gedacht, falls die eigentliche View noch nicht vorhanden ist.
*
* @param array<string, mixed> $serverStatus Serverstatus-Daten (Hostname, CPU%, RAM%, etc.)
*/
private function renderInlineDashboard(array $serverStatus): void
{
?>
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>AD Admin Tool Dashboard</title>
</head>
<body>
<h1>Dashboard</h1>
<h2>Serverstatus</h2>
<ul>
<li>
Hostname:
<?php echo htmlspecialchars((string)$serverStatus['hostname'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?>
</li>
<li>
Betriebssystem:
<?php echo htmlspecialchars((string)$serverStatus['os'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?>
</li>
<li>
Uptime:
<?php echo htmlspecialchars((string)$serverStatus['uptime'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?>
</li>
<li>
CPU-Auslastung:
<?php echo (int)$serverStatus['cpu_usage']; ?>%
</li>
<li>
RAM-Auslastung:
<?php echo (int)$serverStatus['memory_usage']; ?>%
</li>
<li>
Datenträger C:
<?php echo (int)$serverStatus['disk_usage_c']; ?>%
</li>
<li>
Letzte Aktualisierung:
<?php echo htmlspecialchars((string)$serverStatus['last_update'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); ?>
</li>
</ul>
<p><a href="index.php?route=logout">Logout</a></p>
</body>
</html>
<?php
}
}