feat: Saisons-Verwaltung im Admin-Bereich implementieren und Menü anpassen

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
torsten 2026-10-04 17:55:58 +02:00
parent 374b8fed6e
commit 55e03541ff
4 changed files with 245 additions and 1 deletions

View file

@ -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();

View file

@ -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;
}
}

View file

@ -8,7 +8,7 @@ require __DIR__ . '/partials/header.php';
<div class="admin-menu">
<ul>
<li><a href="/index.php?page=admin_users">Benutzer verwalten</a></li>
<li><a href="#">Saisons & Runden verwalten</a></li>
<li><a href="/index.php?page=admin_seasons">Saisons verwalten</a></li>
<li><a href="#">Spiele & Ergebnisse eintragen</a></li>
<li><a href="#">Zahlungen verwalten</a></li>
<li><a href="#">Systemeinstellungen</a></li>

101
src/Views/admin/seasons.php Normal file
View file

@ -0,0 +1,101 @@
<?php
$pageTitle = 'Saisons verwalten';
require __DIR__ . '/../partials/header.php';
?>
<h1>Saisons verwalten</h1>
<?php if (!empty($message)): ?>
<p style="color: green;"><?php echo htmlspecialchars($message); ?></p>
<?php endif; ?>
<?php if (!empty($error)): ?>
<p style="color: red;"><?php echo htmlspecialchars($error); ?></p>
<?php endif; ?>
<!-- Form for creating/editing a season -->
<h2><?php echo $editSeason ? 'Saison bearbeiten' : 'Neue Saison anlegen'; ?></h2>
<form action="/index.php?page=admin_seasons" method="post">
<?php if ($editSeason): ?>
<input type="hidden" name="action" value="update">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($editSeason['id']); ?>">
<?php else: ?>
<input type="hidden" name="action" value="create">
<?php endif; ?>
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo htmlspecialchars($editSeason['name'] ?? ''); ?>" required>
</div>
<div class="form-group">
<label for="rounds">Runden:</label>
<input type="number" id="rounds" name="rounds" value="<?php echo htmlspecialchars($editSeason['rounds'] ?? '34'); ?>" required>
</div>
<div class="form-group">
<button type="submit"><?php echo $editSeason ? 'Speichern' : 'Anlegen'; ?></button>
<?php if ($editSeason): ?>
<a href="/index.php?page=admin_seasons">Abbrechen</a>
<?php endif; ?>
</div>
</form>
<hr>
<!-- Season List -->
<h2>Saison-Liste</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Runden</th>
<th>Aktiv</th>
<th>Aktionen</th>
</tr>
</thead>
<tbody>
<?php foreach ($seasons as $season): ?>
<tr <?php if ($currentSeason['id'] === $season['id']) echo 'style="font-weight: bold;"'; ?>>
<td><?php echo htmlspecialchars($season['name']); ?></td>
<td><?php echo htmlspecialchars($season['rounds']); ?></td>
<td><?php echo $currentSeason['id'] === $season['id'] ? 'Ja' : 'Nein'; ?></td>
<td>
<a href="/index.php?page=admin_seasons&edit_id=<?php echo $season['id']; ?>">Bearbeiten</a>
<?php if ($currentSeason['id'] !== $season['id']): ?>
<form action="/index.php?page=admin_seasons" method="post" style="display: inline;">
<input type="hidden" name="action" value="set_current">
<input type="hidden" name="id" value="<?php echo $season['id']; ?>">
<button type="submit" class="link-button">Als aktiv setzen</button>
</form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<style>
/* From users.php, for consistency */
.form-group {
margin-bottom: 1rem;
}
.form-group label {
display: inline-block;
width: 150px;
text-align: right;
margin-right: 10px;
}
.form-group button, .form-group a {
margin-left: calc(150px + 10px);
}
.link-button {
background: none;
border: none;
color: #007bff;
cursor: pointer;
padding: 0;
font-size: inherit;
text-decoration: underline;
font-family: inherit;
}
</style>
<?php require __DIR__ . '/../partials/footer.php'; ?>