2026-10-03 16:49:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use App\Core\Database;
|
|
|
|
|
|
|
|
|
|
class Season {
|
2026-10-04 20:57:52 +02:00
|
|
|
|
|
|
|
|
/** Legacy-Default für seasons.max_identical_jokertipps */
|
|
|
|
|
const DEFAULT_MAX_IDENTICAL_JOKERTIPPS = 6;
|
|
|
|
|
|
|
|
|
|
/** Jede Runde besteht aus 4 Spielen (Legacy-Konvention) */
|
|
|
|
|
const GAMES_PER_ROUND = 4;
|
|
|
|
|
|
2026-10-03 16:49:52 +02:00
|
|
|
/**
|
2026-10-04 20:57:52 +02:00
|
|
|
* Holt die aktuell selektierte Saison (settings.current_season_id).
|
2026-10-03 16:49:52 +02:00
|
|
|
* Fallback: Die Saison mit der höchsten ID.
|
|
|
|
|
*/
|
|
|
|
|
public static function getCurrent() {
|
|
|
|
|
$db = Database::getConnection();
|
2026-10-04 20:57:52 +02:00
|
|
|
|
|
|
|
|
$stmt = $db->query(
|
|
|
|
|
"SELECT s.* FROM settings st
|
|
|
|
|
JOIN seasons s ON s.id = st.current_season_id
|
|
|
|
|
ORDER BY st.id
|
|
|
|
|
LIMIT 1"
|
|
|
|
|
);
|
|
|
|
|
$season = $stmt->fetch();
|
|
|
|
|
if ($season) {
|
|
|
|
|
return $season;
|
2026-10-03 16:49:52 +02:00
|
|
|
}
|
2026-10-04 20:57:52 +02:00
|
|
|
|
|
|
|
|
$stmt = $db->query("SELECT * FROM seasons ORDER BY id DESC LIMIT 1");
|
2026-10-03 16:51:44 +02:00
|
|
|
return $stmt->fetch();
|
2026-10-03 16:49:52 +02:00
|
|
|
}
|
2026-10-04 17:55:58 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-04 20:57:52 +02:00
|
|
|
* Erstellt eine neue Saison mit Platzhalter-Runden und je 4 Platzhalter-Spielen
|
|
|
|
|
* (wie im Legacy SeasonsController::store). Die Platzhalter-Spiele liegen 7 Monate
|
|
|
|
|
* in der Zukunft, damit die Runden nicht gesperrt sind, bis der Admin die echten
|
|
|
|
|
* Paarungen und Anstoßzeiten einträgt.
|
|
|
|
|
*
|
|
|
|
|
* Tipps werden NICHT vorab angelegt - sie entstehen beim ersten Tippen (siehe Tipp::saveRound).
|
2026-10-04 17:55:58 +02:00
|
|
|
*
|
2026-10-04 20:57:52 +02:00
|
|
|
* @param array $data ['name', 'rounds', optional 'max_identical_jokertipps']
|
2026-10-04 18:04:20 +02:00
|
|
|
* @return int|false Die ID der neuen Saison oder false bei einem Fehler.
|
2026-10-04 17:55:58 +02:00
|
|
|
*/
|
|
|
|
|
public static function create($data) {
|
|
|
|
|
$db = Database::getConnection();
|
2026-10-04 20:57:52 +02:00
|
|
|
$roundCount = (int)$data['rounds'];
|
|
|
|
|
$maxJoker = (int)($data['max_identical_jokertipps'] ?? self::DEFAULT_MAX_IDENTICAL_JOKERTIPPS);
|
|
|
|
|
|
2026-10-04 18:02:53 +02:00
|
|
|
$db->beginTransaction();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$stmt = $db->prepare(
|
2026-10-04 20:57:52 +02:00
|
|
|
"INSERT INTO seasons (name, rounds, max_identical_jokertipps, created_at, updated_at)
|
|
|
|
|
VALUES (:name, :rounds, :max_joker, NOW(), NOW())"
|
2026-10-04 18:02:53 +02:00
|
|
|
);
|
|
|
|
|
$stmt->execute([
|
|
|
|
|
':name' => $data['name'],
|
2026-10-04 20:57:52 +02:00
|
|
|
':rounds' => $roundCount,
|
|
|
|
|
':max_joker' => $maxJoker,
|
2026-10-04 18:02:53 +02:00
|
|
|
]);
|
2026-10-04 20:57:52 +02:00
|
|
|
$seasonId = (int)$db->lastInsertId();
|
|
|
|
|
|
|
|
|
|
$placeholderKickoff = (new \DateTime('+7 months'))->format('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
|
|
$stmtRound = $db->prepare(
|
|
|
|
|
"INSERT INTO rounds (name, season_id, created_at, updated_at) VALUES (?, ?, NOW(), NOW())"
|
|
|
|
|
);
|
|
|
|
|
$stmtGame = $db->prepare(
|
|
|
|
|
"INSERT INTO games (round_id, team1, team2, kickoff, created_at, updated_at)
|
|
|
|
|
VALUES (?, ?, ?, ?, NOW(), NOW())"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for ($i = 1; $i <= $roundCount; $i++) {
|
|
|
|
|
$stmtRound->execute([$i . ". Spieltag", $seasonId]);
|
|
|
|
|
$roundId = (int)$db->lastInsertId();
|
|
|
|
|
|
|
|
|
|
for ($g = 1; $g <= self::GAMES_PER_ROUND; $g++) {
|
|
|
|
|
$stmtGame->execute([
|
|
|
|
|
$roundId,
|
|
|
|
|
"Mannschaft " . (2 * $g - 1),
|
|
|
|
|
"Mannschaft " . (2 * $g),
|
|
|
|
|
$placeholderKickoff,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-10-04 18:02:53 +02:00
|
|
|
}
|
2026-10-04 20:57:52 +02:00
|
|
|
|
2026-10-04 18:02:53 +02:00
|
|
|
$db->commit();
|
2026-10-04 20:57:52 +02:00
|
|
|
return $seasonId;
|
2026-10-04 18:02:53 +02:00
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$db->rollBack();
|
2026-10-04 20:57:52 +02:00
|
|
|
error_log("Season::create fehlgeschlagen: " . $e->getMessage());
|
2026-10-04 18:02:53 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-10-04 17:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Aktualisiert eine Saison.
|
2026-10-04 20:57:52 +02:00
|
|
|
* Hinweis: Eine Änderung von 'rounds' legt keine Runden an oder löscht welche.
|
2026-10-04 17:55:58 +02:00
|
|
|
*
|
|
|
|
|
* @param int $id
|
2026-10-04 20:57:52 +02:00
|
|
|
* @param array $data ['name', 'rounds', optional 'max_identical_jokertipps']
|
2026-10-04 17:55:58 +02:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function update($id, $data) {
|
|
|
|
|
$db = Database::getConnection();
|
2026-10-04 20:57:52 +02:00
|
|
|
|
|
|
|
|
$sql = "UPDATE seasons SET name = :name, rounds = :rounds, updated_at = NOW()";
|
|
|
|
|
$params = [
|
2026-10-04 17:55:58 +02:00
|
|
|
':id' => $id,
|
|
|
|
|
':name' => $data['name'],
|
2026-10-04 20:57:52 +02:00
|
|
|
':rounds' => (int)$data['rounds'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (isset($data['max_identical_jokertipps'])) {
|
|
|
|
|
$sql .= ", max_identical_jokertipps = :max_joker";
|
|
|
|
|
$params[':max_joker'] = (int)$data['max_identical_jokertipps'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sql .= " WHERE id = :id";
|
|
|
|
|
$stmt = $db->prepare($sql);
|
|
|
|
|
return $stmt->execute($params);
|
2026-10-04 17:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Setzt die aktive Saison in der `settings`-Tabelle.
|
2026-10-04 20:57:52 +02:00
|
|
|
* Es wird (wie im Legacy-Code) immer die erste Zeile von settings verwendet.
|
2026-10-04 17:55:58 +02:00
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function setCurrent($id) {
|
|
|
|
|
$db = Database::getConnection();
|
2026-10-04 20:57:52 +02:00
|
|
|
|
|
|
|
|
$settingsId = $db->query("SELECT id FROM settings ORDER BY id LIMIT 1")->fetchColumn();
|
|
|
|
|
|
|
|
|
|
if ($settingsId === false) {
|
|
|
|
|
$stmt = $db->prepare(
|
|
|
|
|
"INSERT INTO settings (current_season_id, created_at, updated_at) VALUES (?, NOW(), NOW())"
|
|
|
|
|
);
|
2026-10-04 17:55:58 +02:00
|
|
|
return $stmt->execute([$id]);
|
|
|
|
|
}
|
2026-10-04 20:57:52 +02:00
|
|
|
|
|
|
|
|
$stmt = $db->prepare("UPDATE settings SET current_season_id = ?, updated_at = NOW() WHERE id = ?");
|
|
|
|
|
return $stmt->execute([$id, $settingsId]);
|
2026-10-04 17:55:58 +02:00
|
|
|
}
|
2026-10-03 16:49:52 +02:00
|
|
|
}
|