145 lines
4.4 KiB
PHP
145 lines
4.4 KiB
PHP
<?php
|
|
/**
|
|
* Copyright seit 2024 Webshop System
|
|
*
|
|
* Admin-Dashboard-Controller für das Webshop-System
|
|
*
|
|
* @author Webshop System
|
|
* @license GPL v3
|
|
*/
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use Doctrine\DBAL\DriverManager;
|
|
use Doctrine\DBAL\Exception;
|
|
|
|
class AdminDashboardController
|
|
{
|
|
public function index()
|
|
{
|
|
// Session prüfen
|
|
session_start();
|
|
if (!isset($_SESSION['admin_user_id'])) {
|
|
header('Location: /admin/login');
|
|
exit;
|
|
}
|
|
|
|
// DB-Verbindung herstellen
|
|
$connectionParams = [
|
|
'dbname' => getenv('DB_DATABASE') ?: 'freeshop',
|
|
'user' => getenv('DB_USERNAME') ?: 'freeshop_user',
|
|
'password' => getenv('DB_PASSWORD') ?: 'freeshop_password',
|
|
'host' => getenv('DB_HOST') ?: 'db',
|
|
'driver' => 'pdo_mysql',
|
|
'port' => getenv('DB_PORT') ?: 3306,
|
|
'charset' => 'utf8mb4',
|
|
];
|
|
|
|
try {
|
|
$conn = DriverManager::getConnection($connectionParams);
|
|
|
|
// Statistiken laden
|
|
$stats = $this->getStats($conn);
|
|
|
|
// Letzte Bestellungen laden
|
|
$recentOrders = $this->getRecentOrders($conn);
|
|
|
|
$this->render('admin/dashboard.html.twig', [
|
|
'title' => 'Webshop Admin - Dashboard',
|
|
'user_name' => $_SESSION['admin_user_name'],
|
|
'stats' => $stats,
|
|
'recent_orders' => $recentOrders
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
$this->render('admin/dashboard.html.twig', [
|
|
'title' => 'Webshop Admin - Dashboard',
|
|
'user_name' => $_SESSION['admin_user_name'],
|
|
'stats' => [
|
|
'orders' => 0,
|
|
'products' => 0,
|
|
'customers' => 0,
|
|
'revenue' => 0
|
|
],
|
|
'recent_orders' => []
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function getStats($conn)
|
|
{
|
|
// Bestellungen zählen
|
|
$stmt = $conn->prepare('SELECT COUNT(*) as count FROM ws_order');
|
|
$stmt->execute();
|
|
$orders = $stmt->fetchAssociative()['count'];
|
|
|
|
// Produkte zählen
|
|
$stmt = $conn->prepare('SELECT COUNT(*) as count FROM ws_product');
|
|
$stmt->execute();
|
|
$products = $stmt->fetchAssociative()['count'];
|
|
|
|
// Kunden zählen
|
|
$stmt = $conn->prepare('SELECT COUNT(*) as count FROM ws_user WHERE is_admin = 0');
|
|
$stmt->execute();
|
|
$customers = $stmt->fetchAssociative()['count'];
|
|
|
|
// Umsatz berechnen
|
|
$stmt = $conn->prepare('SELECT SUM(total) as total FROM ws_order WHERE status = "completed"');
|
|
$stmt->execute();
|
|
$revenue = $stmt->fetchAssociative()['total'] ?: 0;
|
|
|
|
return [
|
|
'orders' => $orders,
|
|
'products' => $products,
|
|
'customers' => $customers,
|
|
'revenue' => number_format($revenue, 2)
|
|
];
|
|
}
|
|
|
|
private function getRecentOrders($conn)
|
|
{
|
|
$stmt = $conn->prepare('
|
|
SELECT o.*, u.firstname, u.lastname
|
|
FROM ws_order o
|
|
LEFT JOIN ws_user u ON o.user_id = u.id
|
|
ORDER BY o.created_at DESC
|
|
LIMIT 10
|
|
');
|
|
$stmt->execute();
|
|
|
|
$orders = [];
|
|
while ($row = $stmt->fetchAssociative()) {
|
|
$orders[] = [
|
|
'id' => $row['id'],
|
|
'customer_name' => $row['firstname'] . ' ' . $row['lastname'],
|
|
'status' => $row['status'],
|
|
'status_color' => $this->getStatusColor($row['status']),
|
|
'total' => number_format($row['total'], 2),
|
|
'created_at' => date('d.m.Y H:i', strtotime($row['created_at']))
|
|
];
|
|
}
|
|
|
|
return $orders;
|
|
}
|
|
|
|
private function getStatusColor($status)
|
|
{
|
|
switch ($status) {
|
|
case 'pending':
|
|
return 'warning';
|
|
case 'completed':
|
|
return 'success';
|
|
case 'cancelled':
|
|
return 'danger';
|
|
default:
|
|
return 'secondary';
|
|
}
|
|
}
|
|
|
|
protected function render($template, $data = [])
|
|
{
|
|
// Einfache Template-Engine (später durch Twig ersetzen)
|
|
extract($data);
|
|
include __DIR__ . '/../../templates/' . $template;
|
|
}
|
|
}
|