2026-10-03 16:49:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use App\Core\Database;
|
|
|
|
|
|
|
|
|
|
class Season {
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-03 16:57:45 +02:00
|
|
|
* Holt die aktuell selektierte Saison aus den `settings`.
|
|
|
|
|
* Annahme: Es gibt eine Tabelle `settings` mit dem Feld `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-03 16:57:45 +02:00
|
|
|
// Lese die current_season_id aus der settings Tabelle
|
|
|
|
|
$stmt = $db->prepare("SELECT current_season_id FROM settings LIMIT 1");
|
2026-10-03 16:51:44 +02:00
|
|
|
$stmt->execute();
|
2026-10-03 16:57:45 +02:00
|
|
|
$setting = $stmt->fetch();
|
2026-10-03 16:51:44 +02:00
|
|
|
|
2026-10-03 16:57:45 +02:00
|
|
|
if ($setting && !empty($setting['current_season_id'])) {
|
|
|
|
|
$seasonId = $setting['current_season_id'];
|
|
|
|
|
|
|
|
|
|
// Hole die Saison mit der entsprechenden ID
|
|
|
|
|
$stmt = $db->prepare("SELECT * FROM seasons WHERE id = ?");
|
|
|
|
|
$stmt->execute([$seasonId]);
|
|
|
|
|
$season = $stmt->fetch();
|
|
|
|
|
|
|
|
|
|
if ($season) {
|
|
|
|
|
return $season;
|
|
|
|
|
}
|
2026-10-03 16:49:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-03 16:57:45 +02:00
|
|
|
// Fallback: Wenn keine Saison in den Settings gefunden wird oder die ID ungültig ist, nimm die mit der höchsten ID
|
2026-10-03 16:51:44 +02:00
|
|
|
$stmt = $db->prepare("SELECT * FROM seasons ORDER BY id DESC LIMIT 1");
|
|
|
|
|
$stmt->execute();
|
|
|
|
|
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 18:02:53 +02:00
|
|
|
* Erstellt eine neue Saison und die zugehörigen Runden.
|
2026-10-04 17:55:58 +02:00
|
|
|
*
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function create($data) {
|
|
|
|
|
$db = Database::getConnection();
|
2026-10-04 18:02:53 +02:00
|
|
|
$db->beginTransaction();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$stmt = $db->prepare(
|
|
|
|
|
"INSERT INTO seasons (name, rounds, created_at, updated_at) VALUES (:name, :rounds, NOW(), NOW())"
|
|
|
|
|
);
|
|
|
|
|
$stmt->execute([
|
|
|
|
|
':name' => $data['name'],
|
|
|
|
|
':rounds' => $data['rounds'],
|
|
|
|
|
]);
|
|
|
|
|
$seasonId = $db->lastInsertId();
|
|
|
|
|
|
|
|
|
|
for ($i = 1; $i <= $data['rounds']; $i++) {
|
|
|
|
|
$roundName = $i . ". Spieltag";
|
|
|
|
|
$stmt_round = $db->prepare(
|
|
|
|
|
"INSERT INTO rounds (name, season_id, created_at, updated_at) VALUES (?, ?, NOW(), NOW())"
|
|
|
|
|
);
|
|
|
|
|
$stmt_round->execute([$roundName, $seasonId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$db->commit();
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$db->rollBack();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-10-04 17:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
2026-10-03 16:49:52 +02:00
|
|
|
}
|