228 lines
7.7 KiB
PHP
228 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Core\Database;
|
|
|
|
class Season {
|
|
|
|
/** Legacy-Default für seasons.max_identical_jokertipps */
|
|
const DEFAULT_MAX_IDENTICAL_JOKERTIPPS = 6;
|
|
|
|
/** Default für seasons.jackpot (in Euro) */
|
|
const DEFAULT_JACKPOT = 0;
|
|
|
|
/** Jede Runde besteht aus 4 Spielen (Legacy-Konvention) */
|
|
const GAMES_PER_ROUND = 4;
|
|
|
|
/**
|
|
* Holt die aktuell selektierte Saison (settings.current_season_id).
|
|
* Fallback: Die Saison mit der höchsten ID.
|
|
*/
|
|
public static function getCurrent() {
|
|
$db = Database::getConnection();
|
|
|
|
$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;
|
|
}
|
|
|
|
$stmt = $db->query("SELECT * FROM seasons ORDER BY id DESC LIMIT 1");
|
|
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 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).
|
|
*
|
|
* @param array $data ['name', 'rounds', optional 'max_identical_jokertipps', 'jackpot']
|
|
* @return int|false Die ID der neuen Saison oder false bei einem Fehler.
|
|
*/
|
|
public static function create($data) {
|
|
$db = Database::getConnection();
|
|
$roundCount = (int)$data['rounds'];
|
|
$maxJoker = (int)($data['max_identical_jokertipps'] ?? self::DEFAULT_MAX_IDENTICAL_JOKERTIPPS);
|
|
$jackpot = isset($data['jackpot']) && is_numeric($data['jackpot'])
|
|
? (float)$data['jackpot']
|
|
: self::DEFAULT_JACKPOT;
|
|
|
|
$db->beginTransaction();
|
|
|
|
try {
|
|
$stmt = $db->prepare(
|
|
"INSERT INTO seasons (name, rounds, max_identical_jokertipps, jackpot, created_at, updated_at)
|
|
VALUES (:name, :rounds, :max_joker, :jackpot, NOW(), NOW())"
|
|
);
|
|
$stmt->execute([
|
|
':name' => $data['name'],
|
|
':rounds' => $roundCount,
|
|
':max_joker' => $maxJoker,
|
|
':jackpot' => $jackpot,
|
|
]);
|
|
$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,
|
|
]);
|
|
}
|
|
}
|
|
|
|
$db->commit();
|
|
return $seasonId;
|
|
|
|
} catch (\Exception $e) {
|
|
$db->rollBack();
|
|
error_log("Season::create fehlgeschlagen: " . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Aktualisiert eine Saison.
|
|
* Hinweis: Eine Änderung von 'rounds' legt keine Runden an oder löscht welche.
|
|
*
|
|
* @param int $id
|
|
* @param array $data ['name', 'rounds', optional 'max_identical_jokertipps', 'jackpot']
|
|
* @return bool
|
|
*/
|
|
public static function update($id, $data) {
|
|
$db = Database::getConnection();
|
|
|
|
$sql = "UPDATE seasons SET name = :name, rounds = :rounds, updated_at = NOW()";
|
|
$params = [
|
|
':id' => $id,
|
|
':name' => $data['name'],
|
|
':rounds' => (int)$data['rounds'],
|
|
];
|
|
|
|
if (isset($data['max_identical_jokertipps'])) {
|
|
$sql .= ", max_identical_jokertipps = :max_joker";
|
|
$params[':max_joker'] = (int)$data['max_identical_jokertipps'];
|
|
}
|
|
|
|
if (isset($data['jackpot'])) {
|
|
$sql .= ", jackpot = :jackpot";
|
|
$params[':jackpot'] = (float)$data['jackpot'];
|
|
}
|
|
|
|
$sql .= " WHERE id = :id";
|
|
$stmt = $db->prepare($sql);
|
|
return $stmt->execute($params);
|
|
}
|
|
|
|
/**
|
|
* Löscht eine Saison mit allen abhängigen Daten (Tipps, Spiele, Runden,
|
|
* Quoten, Zahlungen, manuelle Ausschüttungen, Rundmails). Die aktive Saison kann nicht gelöscht werden.
|
|
*
|
|
* @param int $id
|
|
* @throws \Exception wenn die Saison aktiv ist oder nicht existiert
|
|
*/
|
|
public static function delete($id) {
|
|
$id = (int)$id;
|
|
if (!self::findById($id)) {
|
|
throw new \Exception('Saison nicht gefunden.');
|
|
}
|
|
$current = self::getCurrent();
|
|
if ($current && (int)$current['id'] === $id) {
|
|
throw new \Exception('Die aktive Saison kann nicht gelöscht werden.');
|
|
}
|
|
|
|
$db = Database::getConnection();
|
|
$db->beginTransaction();
|
|
try {
|
|
$statements = [
|
|
"DELETE t FROM tipps t JOIN games g ON g.id = t.game_id JOIN rounds r ON r.id = g.round_id WHERE r.season_id = ?",
|
|
"DELETE g FROM games g JOIN rounds r ON r.id = g.round_id WHERE r.season_id = ?",
|
|
"DELETE FROM rounds WHERE season_id = ?",
|
|
"DELETE FROM quotas WHERE season_id = ?",
|
|
"DELETE FROM payments WHERE season_id = ?",
|
|
"DELETE FROM manual_payouts WHERE season_id = ?",
|
|
"DELETE FROM roundmails WHERE season_id = ?",
|
|
"DELETE FROM seasons WHERE id = ?",
|
|
];
|
|
foreach ($statements as $sql) {
|
|
$db->prepare($sql)->execute([$id]);
|
|
}
|
|
$db->commit();
|
|
} catch (\Exception $e) {
|
|
$db->rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Setzt die aktive Saison in der `settings`-Tabelle.
|
|
* Es wird (wie im Legacy-Code) immer die erste Zeile von settings verwendet.
|
|
*
|
|
* @param int $id
|
|
* @return bool
|
|
*/
|
|
public static function setCurrent($id) {
|
|
$db = Database::getConnection();
|
|
|
|
$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())"
|
|
);
|
|
return $stmt->execute([$id]);
|
|
}
|
|
|
|
$stmt = $db->prepare("UPDATE settings SET current_season_id = ?, updated_at = NOW() WHERE id = ?");
|
|
return $stmt->execute([$id, $settingsId]);
|
|
}
|
|
}
|