From 55e03541ffdc3f8cebaeb113b408304dd9b35baf Mon Sep 17 00:00:00 2001 From: torsten Date: Sun, 4 Oct 2026 17:55:58 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Saisons-Verwaltung=20im=20Admin-Bereich?= =?UTF-8?q?=20implementieren=20und=20Men=C3=BC=20anpassen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (gemini/gemini-2.5-pro) --- public/index.php | 61 ++++++++++++++++++++++ src/Models/Season.php | 82 +++++++++++++++++++++++++++++ src/Views/admin.php | 2 +- src/Views/admin/seasons.php | 101 ++++++++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 src/Views/admin/seasons.php diff --git a/public/index.php b/public/index.php index 869aede..e201160 100644 --- a/public/index.php +++ b/public/index.php @@ -105,6 +105,67 @@ if ($page === 'login') { Auth::requireLogin(); Auth::requireAdmin(); require __DIR__ . '/../src/Views/admin.php'; +} elseif ($page === 'admin_seasons') { + Auth::requireLogin(); + Auth::requireAdmin(); + + $message = null; + $error = null; + + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $action = $_POST['action'] ?? ''; + $seasonId = $_POST['id'] ?? null; + $redirect = '/index.php?page=admin_seasons'; + + try { + if ($action === 'create' || $action === 'update') { + $data = [ + 'name' => $_POST['name'], + 'rounds' => $_POST['rounds'], + ]; + if (empty($data['name']) || !is_numeric($data['rounds'])) { + throw new \Exception('Name und Anzahl der Runden sind erforderlich.'); + } + + if ($action === 'create') { + Season::create($data); + $_SESSION['admin_message'] = "Saison erfolgreich erstellt."; + } elseif ($seasonId) { + Season::update($seasonId, $data); + $_SESSION['admin_message'] = "Saison erfolgreich aktualisiert."; + } + } elseif ($action === 'set_current' && $seasonId) { + Season::setCurrent($seasonId); + $_SESSION['admin_message'] = "Aktive Saison wurde geändert."; + } + } catch (\Throwable $e) { + $_SESSION['admin_error'] = "Fehler: " . $e->getMessage(); + if ($action === 'update' && $seasonId) { + $redirect = '/index.php?page=admin_seasons&edit_id=' . $seasonId; + } + } + + header('Location: ' . $redirect); + exit; + } + + if (isset($_SESSION['admin_message'])) { + $message = $_SESSION['admin_message']; + unset($_SESSION['admin_message']); + } + if (isset($_SESSION['admin_error'])) { + $error = $_SESSION['admin_error']; + unset($_SESSION['admin_error']); + } + + $seasons = Season::getAll(); + $currentSeason = Season::getCurrent(); + $editSeason = null; + if (isset($_GET['edit_id'])) { + $editSeason = Season::findById((int)$_GET['edit_id']); + } + + require __DIR__ . '/../src/Views/admin/seasons.php'; } elseif ($page === 'admin_users') { Auth::requireLogin(); Auth::requireAdmin(); diff --git a/src/Models/Season.php b/src/Models/Season.php index 7387cc4..87c2440 100644 --- a/src/Models/Season.php +++ b/src/Models/Season.php @@ -37,4 +37,86 @@ class Season { $stmt->execute(); return $stmt->fetch(); } + + /** + * Holt alle Saisons. + * + * @return array + */ + public static function getAll() { + $db = Database::getConnection(); + $stmt = $db->prepare("SELECT * FROM seasons ORDER BY id DESC"); + $stmt->execute(); + return $stmt->fetchAll(\PDO::FETCH_ASSOC); + } + + /** + * Holt eine Saison anhand der ID. + * + * @param int $id + * @return array|false + */ + public static function findById($id) { + $db = Database::getConnection(); + $stmt = $db->prepare("SELECT * FROM seasons WHERE id = ?"); + $stmt->execute([$id]); + return $stmt->fetch(\PDO::FETCH_ASSOC); + } + + /** + * Erstellt eine neue Saison. + * + * @param array $data + * @return bool + */ + public static function create($data) { + $db = Database::getConnection(); + $stmt = $db->prepare( + "INSERT INTO seasons (name, rounds, created_at, updated_at) VALUES (:name, :rounds, NOW(), NOW())" + ); + return $stmt->execute([ + ':name' => $data['name'], + ':rounds' => $data['rounds'], + ]); + } + + /** + * Aktualisiert eine Saison. + * + * @param int $id + * @param array $data + * @return bool + */ + public static function update($id, $data) { + $db = Database::getConnection(); + $stmt = $db->prepare( + "UPDATE seasons SET name = :name, rounds = :rounds, updated_at = NOW() WHERE id = :id" + ); + return $stmt->execute([ + ':id' => $id, + ':name' => $data['name'], + ':rounds' => $data['rounds'], + ]); + } + + /** + * Setzt die aktive Saison in der `settings`-Tabelle. + * + * @param int $id + * @return bool + */ + public static function setCurrent($id) { + $db = Database::getConnection(); + // Annahme: Es gibt immer genau einen Eintrag in settings + $stmt = $db->prepare("UPDATE settings SET current_season_id = ? WHERE id = 1"); + if (!$stmt->execute([$id])) { + return false; + } + // Wenn die Zeile nicht existiert, füge sie ein. + if ($stmt->rowCount() === 0) { + $stmt = $db->prepare("INSERT INTO settings (id, current_season_id) VALUES (1, ?)"); + return $stmt->execute([$id]); + } + return true; + } } diff --git a/src/Views/admin.php b/src/Views/admin.php index 14350a3..66b9d91 100644 --- a/src/Views/admin.php +++ b/src/Views/admin.php @@ -8,7 +8,7 @@ require __DIR__ . '/partials/header.php';
  • Benutzer verwalten
  • -
  • Saisons & Runden verwalten
  • +
  • Saisons verwalten
  • Spiele & Ergebnisse eintragen
  • Zahlungen verwalten
  • Systemeinstellungen
  • diff --git a/src/Views/admin/seasons.php b/src/Views/admin/seasons.php new file mode 100644 index 0000000..d2ede54 --- /dev/null +++ b/src/Views/admin/seasons.php @@ -0,0 +1,101 @@ + + +

    Saisons verwalten

    + + +

    + + +

    + + + +

    +
    + + + + + + + +
    + + +
    +
    + + +
    +
    + + + Abbrechen + +
    +
    + +
    + + +

    Saison-Liste

    + + + + + + + + + + + + > + + + + + + + +
    NameRundenAktivAktionen
    + Bearbeiten + +
    + + + +
    + +
    + + + +