244 lines
8.4 KiB
PHP
244 lines
8.4 KiB
PHP
<?php
|
|
|
|
// Lade den minimalistischen Autoloader
|
|
require_once __DIR__ . '/../src/autoload.php';
|
|
|
|
// Auth-System initialisieren
|
|
App\Core\Auth::start();
|
|
|
|
use App\Core\Database;
|
|
use App\Core\Auth;
|
|
use App\Models\Game;
|
|
use App\Models\Tipp;
|
|
use App\Models\User;
|
|
use App\Models\Round;
|
|
use App\Models\Season;
|
|
use App\Models\Ranking;
|
|
use App\Models\Payment;
|
|
|
|
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
|
$page = $_GET['page'] ?? null;
|
|
|
|
// Home-Seite erkennen
|
|
if (($uri === '/' || $uri === '/index.php') && $page === null) {
|
|
$page = 'home';
|
|
}
|
|
|
|
// Routing basierend auf Query-Parametern
|
|
if ($page === 'login') {
|
|
// Wenn bereits eingeloggt, zur Rangliste weiterleiten
|
|
if (Auth::check()) {
|
|
header('Location: /index.php?page=ranking');
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$firstname = $_POST['firstname'] ?? '';
|
|
$lastname = $_POST['lastname'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
if (Auth::login($firstname, $lastname, $password)) {
|
|
header('Location: /index.php?page=ranking');
|
|
exit;
|
|
} else {
|
|
$error = 'Ungültige Anmeldedaten.';
|
|
require __DIR__ . '/../src/Views/login.php';
|
|
}
|
|
} else {
|
|
require __DIR__ . '/../src/Views/login.php';
|
|
}
|
|
} elseif ($page === 'logout') {
|
|
Auth::logout();
|
|
header('Location: /index.php?page=login');
|
|
exit;
|
|
} elseif ($page === 'admin') {
|
|
Auth::requireLogin();
|
|
Auth::requireAdmin();
|
|
require __DIR__ . '/../src/Views/admin.php';
|
|
} elseif ($page === 'admin_users') {
|
|
Auth::requireLogin();
|
|
Auth::requireAdmin();
|
|
|
|
$currentSeason = Season::getCurrent();
|
|
$message = null;
|
|
$error = null;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$action = $_POST['action'] ?? '';
|
|
$userId = $_POST['id'] ?? null;
|
|
$redirect = '/index.php?page=admin_users';
|
|
|
|
try {
|
|
if ($action === 'create' || $action === 'update') {
|
|
$data = [
|
|
'firstname' => $_POST['firstname'],
|
|
'lastname' => $_POST['lastname'],
|
|
'email' => $_POST['email'],
|
|
'password' => $_POST['password'],
|
|
'admin' => isset($_POST['admin']) ? 1 : 0,
|
|
];
|
|
|
|
if ($action === 'create') {
|
|
if (empty($data['password'])) {
|
|
throw new \Exception('Passwort ist erforderlich.');
|
|
}
|
|
User::create($data);
|
|
$_SESSION['admin_message'] = "Benutzer erfolgreich erstellt.";
|
|
} elseif ($userId) {
|
|
User::update($userId, $data);
|
|
if ($currentSeason) {
|
|
Payment::setPaidStatus($userId, $currentSeason['id'], isset($_POST['has_paid']));
|
|
}
|
|
$_SESSION['admin_message'] = "Benutzer erfolgreich aktualisiert.";
|
|
}
|
|
} elseif ($action === 'delete' && $userId) {
|
|
User::delete($userId);
|
|
$_SESSION['admin_message'] = "Benutzer erfolgreich gelöscht.";
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$_SESSION['admin_error'] = "Fehler: " . $e->getMessage();
|
|
if ($action === 'update' && $userId) {
|
|
$redirect = '/index.php?page=admin_users&edit_id=' . $userId;
|
|
}
|
|
}
|
|
|
|
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']);
|
|
}
|
|
|
|
$users = User::getAll();
|
|
$payments = [];
|
|
if ($currentSeason) {
|
|
$payments = Payment::getPaymentsBySeason($currentSeason['id']);
|
|
}
|
|
$editUser = null;
|
|
if (isset($_GET['edit_id'])) {
|
|
$editUser = User::findById((int)$_GET['edit_id']);
|
|
}
|
|
|
|
require __DIR__ . '/../src/Views/admin/users.php';
|
|
} elseif ($page === 'round' && isset($_GET['id'])) {
|
|
Auth::requireLogin();
|
|
try {
|
|
$roundId = (int)$_GET['id'];
|
|
$selectedRound = Round::getById($roundId);
|
|
|
|
if (!$selectedRound) {
|
|
http_response_code(404);
|
|
echo "404 Not Found: Runde nicht gefunden.";
|
|
exit;
|
|
}
|
|
|
|
// Alle Runden der Saison holen
|
|
$allRoundsInSeason = Round::getBySeason($selectedRound['season_id']);
|
|
|
|
$roundsToShow = [];
|
|
$all_game_ids = [];
|
|
foreach ($allRoundsInSeason as $round) {
|
|
if ($round['id'] <= $selectedRound['id']) {
|
|
$gamesOfRound = Game::getGamesByRound($round['id']);
|
|
$round['games'] = $gamesOfRound;
|
|
$roundsToShow[] = $round;
|
|
if (!empty($gamesOfRound)) {
|
|
$all_game_ids = array_merge($all_game_ids, array_column($gamesOfRound, 'id'));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Hole alle zahlenden User der Saison
|
|
$db = Database::getConnection();
|
|
$stmt_users = $db->prepare("SELECT u.id, u.name FROM users u JOIN payments p ON u.id = p.user_id WHERE p.season_id = ? AND u.deleted IS NULL AND p.paid_date IS NOT NULL ORDER BY u.name ASC");
|
|
$stmt_users->execute([$selectedRound['season_id']]);
|
|
$users = $stmt_users->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
// Hole alle relevanten Tipps
|
|
$tipps_map = [];
|
|
if (!empty($all_game_ids) && !empty($users)) {
|
|
$user_ids = array_column($users, 'id');
|
|
$game_placeholders = implode(',', array_fill(0, count($all_game_ids), '?'));
|
|
$user_placeholders = implode(',', array_fill(0, count($user_ids), '?'));
|
|
|
|
$stmt_tipps = $db->prepare("
|
|
SELECT user_id, game_id, score1 AS tipp1, score2 AS tipp2, joker
|
|
FROM tipps
|
|
WHERE game_id IN ($game_placeholders) AND user_id IN ($user_placeholders)
|
|
");
|
|
$stmt_tipps->execute(array_merge($all_game_ids, $user_ids));
|
|
|
|
foreach ($stmt_tipps->fetchAll(\PDO::FETCH_ASSOC) as $tipp) {
|
|
$tipps_map[$tipp['user_id']][$tipp['game_id']] = $tipp;
|
|
}
|
|
}
|
|
|
|
// Lade das View-Template für die Rundenansicht
|
|
require __DIR__ . '/../src/Views/round.php';
|
|
} catch (Throwable $e) {
|
|
http_response_code(500);
|
|
echo "<p style='color:red;'>Fehler: " . htmlspecialchars($e->getMessage()) . "</p>";
|
|
}
|
|
} elseif ($page === 'ranking') {
|
|
Auth::requireLogin();
|
|
try {
|
|
$currentSeason = Season::getCurrent();
|
|
if (!$currentSeason) {
|
|
throw new \Exception("Keine aktuelle Saison gefunden.");
|
|
}
|
|
|
|
$selectedRoundId = isset($_GET['round_id']) ? (int)$_GET['round_id'] : null;
|
|
|
|
$data = Ranking::getRanking($currentSeason['id'], $selectedRoundId);
|
|
$ranking = $data['ranking'];
|
|
$games = $data['games'];
|
|
$allRounds = $data['allRounds'];
|
|
$selectedRound = $data['selectedRound'];
|
|
$dailyWinners = $data['dailyWinners'];
|
|
|
|
require __DIR__ . '/../src/Views/ranking.php';
|
|
} catch (Throwable $e) {
|
|
http_response_code(500);
|
|
echo "<p style='color:red;'>Fehler: " . htmlspecialchars($e->getMessage()) . "</p>";
|
|
}
|
|
} elseif ($page === 'home') {
|
|
Auth::requireLogin();
|
|
try {
|
|
$db = Database::getConnection();
|
|
$userCount = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();
|
|
|
|
$currentSeason = Season::getCurrent();
|
|
|
|
if ($currentSeason) {
|
|
$rounds = Round::getBySeason($currentSeason['id']);
|
|
} else {
|
|
$rounds = [];
|
|
}
|
|
|
|
// Testdaten für die Startseite
|
|
$scoreTest = Tipp::getScoreLocal(2, 1, 2, 1, true);
|
|
$lastRoundId = $db->query("SELECT round_id FROM games WHERE score1 > -1 ORDER BY id DESC LIMIT 1")->fetchColumn();
|
|
|
|
if ($lastRoundId) {
|
|
$games = Game::getGamesByRound($lastRoundId);
|
|
} else {
|
|
$games = [];
|
|
}
|
|
|
|
require __DIR__ . '/../src/Views/home.php';
|
|
|
|
} catch (Throwable $e) {
|
|
echo "<p style='color:red;'>Fehler: " . $e->getMessage() . "</p>";
|
|
}
|
|
} elseif ($uri === '/api/status') {
|
|
header('Content-Type: application/json');
|
|
echo json_encode(['status' => 'ok', 'version' => '2027.1']);
|
|
} else {
|
|
http_response_code(404);
|
|
echo "404 Not Found";
|
|
}
|