Matchcenter: Saison-Tabs, Vorsaison-Archiv & gehärteter Cache-Write

- Sync schreibt Cache jetzt fehlersicher: json_encode mit JSON_THROW_ON_ERROR
  + Rückgabeprüfung von file_put_contents/rename → ein defektes UTF-8-Zeichen
  aus der BFV-API kann den guten Cache nicht mehr leerschreiben (alter Cache
  bleibt via $fail unangetastet)
- Vorsaison wird bei erkanntem compoundId-Wechsel einmalig archiviert (previous),
  pro Team compound_id + abgeleitetes season-Label + stale-Flag persistiert
- Aktuelle Saison ist immer sichtbar (auch leer), Wechsel zur Vorsaison über
  wiederverwendete Stripe-Tabbar (group-tabs); neue Komponente matchcenter-season
- Leere aktuelle Saison zeigt Hinweis statt leerer Fläche

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-21 00:51:14 +02:00
parent 9722d8eccc
commit 3a591fe8d0
5 changed files with 206 additions and 43 deletions

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
/**
* Eine Saison einer Matchcenter-Mannschaft: Tabelle (2/3 links) + anstehende Spiele
* und letzte Ergebnisse (1/3 rechts). Wiederverwendet von matchcenter-section.php
* für die aktuelle Saison und (per Tab) für die Vorsaison.
* Props:
* $next array anstehende Spiele (bei Vorsaison leer/ignoriert)
* $results array letzte Ergebnisse
* $table array Liga-Tabelle (teams[].table[])
* $caption string Tabellen-Beschriftung für Screenreader
* $is_previous bool true = abgelaufene Saison (keine „Nächsten Spiele")
* Leere Blöcke verstecken sich selbst; ist die aktuelle Saison komplett leer
* (Saisonstart/-pause), erscheint ein freundlicher Hinweis statt einer leeren Fläche.
*/
$next = $is_previous ?? false ? [] : ($next ?? []);
$results = $results ?? [];
$table = $table ?? [];
$caption = (string) ($caption ?? 'Tabelle');
$isPrevious = (bool) ($is_previous ?? false);
?>
<div class="matchcenter-team__grid">
<?php if ($table !== []): ?>
<div class="matchcenter-team__main">
<?php /* Sichtbare Überschrift bewusst weggelassen; <caption> labelt die Tabelle für Screenreader. */ ?>
<?php component('league-table', ['table' => $table, 'caption' => $caption]); ?>
</div>
<?php endif; ?>
<?php if ($next !== [] || $results !== []): ?>
<aside class="matchcenter-team__aside">
<?php if ($next !== []): ?>
<h3 class="visually-hidden">Nächste Spiele</h3>
<ul class="match-list">
<?php foreach ($next as $m): ?>
<?php component('match-row', ['match' => $m]); ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if ($results !== []): ?>
<h3 class="visually-hidden">Letzte Ergebnisse</h3>
<ul class="match-list">
<?php foreach ($results as $m): ?>
<?php component('match-row', ['match' => $m]); ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</aside>
<?php endif; ?>
<?php if (!$isPrevious && $table === [] && $next === [] && $results === []): ?>
<p class="matchcenter-team__empty">Spiele folgen, sobald die Saison startet.</p>
<?php endif; ?>
</div>

View File

@@ -4,22 +4,31 @@ declare(strict_types=1);
/** /**
* Matchcenter-Sektion einer Mannschaft (kicker-Stil): Kopf (Name + Liga + Link zur * Matchcenter-Sektion einer Mannschaft (kicker-Stil): Kopf (Name + Liga + Link zur
* Mannschaftsseite), darunter zweispaltig — Tabelle 2/3 links, anstehende Spiele + * Mannschaftsseite), darunter die aktuelle Saison (Tabelle + Spiele) via
* letzte Ergebnisse als kompakte Liste 1/3 rechts. Auf schmalen Screens gestapelt. * matchcenter-season. Liegt eine archivierte Vorsaison vor, sind beide Saisons über
* Props: $team (data/matchcenter.json → teams[]). Leere Blöcke verstecken sich selbst. * eine Stripe-Tabbar umschaltbar (wiederverwendetes group-tabs-Pattern, ohne JS
* gestapelt sichtbar). Props: $team (data/matchcenter.json → teams[]).
*/ */
$team = $team ?? []; $team = $team ?? [];
$next = $team['next_matches'] ?? []; $next = $team['next_matches'] ?? [];
$results = $team['last_results'] ?? []; $results = $team['last_results'] ?? [];
$table = $team['table'] ?? []; $table = $team['table'] ?? [];
$league = $team['league'] ?? null; $league = $team['league'] ?? null;
$name = (string) ($team['name'] ?? '');
$season = (string) ($team['season'] ?? '');
$previous = $team['previous'] ?? null;
$hasPrevious = is_array($previous)
&& (($previous['last_results'] ?? []) !== [] || ($previous['table'] ?? []) !== []);
$key = preg_replace('/[^a-z0-9-]+/', '', strtolower((string) ($team['key'] ?? ''))); $key = preg_replace('/[^a-z0-9-]+/', '', strtolower((string) ($team['key'] ?? '')));
$curCaption = 'Tabelle ' . $name . ($season !== '' ? ' · ' . $season : ($league ? ' · ' . $league : ''));
$tabCur = $season !== '' ? $season : 'Aktuelle Saison';
?> ?>
<section class="section matchcenter-team" id="team-<?= e($key) ?>"> <section class="section matchcenter-team" id="team-<?= e($key) ?>">
<div class="container"> <div class="container">
<div class="matchcenter-team__head"> <div class="matchcenter-team__head">
<div class="matchcenter-team__heading"> <div class="matchcenter-team__heading">
<h2><?= e($team['name'] ?? '') ?></h2> <h2><?= e($name) ?></h2>
<?php if ($league): ?> <?php if ($league): ?>
<p class="matchcenter-team__league"><?= icon('trophy', 'matchcenter-team__league-icon') ?><?= e($league) ?></p> <p class="matchcenter-team__league"><?= icon('trophy', 'matchcenter-team__league-icon') ?><?= e($league) ?></p>
<?php endif; ?> <?php endif; ?>
@@ -28,33 +37,40 @@ $key = preg_replace('/[^a-z0-9-]+/', '', strtolower((string) ($team['key'] ?? ''
<a class="btn btn--outline" href="<?= e(url($team['slug'])) ?>">Zur Mannschaft</a> <a class="btn btn--outline" href="<?= e(url($team['slug'])) ?>">Zur Mannschaft</a>
<?php endif; ?> <?php endif; ?>
</div> </div>
<div class="matchcenter-team__grid"> <?php if (!$hasPrevious): ?>
<?php if ($table !== []): ?> <?php component('matchcenter-season', [
<div class="matchcenter-team__main"> 'next' => $next, 'results' => $results, 'table' => $table, 'caption' => $curCaption,
<?php /* Sichtbare Überschrift bewusst weggelassen; <caption> labelt die Tabelle für Screenreader. */ ?> ]); ?>
<?php component('league-table', ['table' => $table, 'caption' => 'Tabelle ' . ($team['name'] ?? '') . ($league ? ' · ' . $league : '')]); ?> <?php else: ?>
<?php
$prevSeason = (string) ($previous['season'] ?? '');
$prevLeague = $previous['league'] ?? $league;
$tabPrev = $prevSeason !== '' ? $prevSeason : 'Vorsaison';
$prevCaption = 'Tabelle ' . $name . ($prevSeason !== '' ? ' · ' . $prevSeason : '');
?>
<div class="matchcenter-team__seasons group-tabs" data-group-tabs>
<div class="group-tabs__bar matchcenter-team__seasons-bar" role="tablist" aria-label="Saison wählen <?= e($name) ?>">
<button class="group-tabs__tab" role="tab" type="button"
id="mc-tab-<?= e($key) ?>-cur" aria-controls="mc-panel-<?= e($key) ?>-cur" aria-selected="true"
><?= e($tabCur) ?></button>
<button class="group-tabs__tab" role="tab" type="button"
id="mc-tab-<?= e($key) ?>-prev" aria-controls="mc-panel-<?= e($key) ?>-prev" aria-selected="false"
><?= e($tabPrev) ?></button>
</div>
<div class="matchcenter-team__season-panel" role="tabpanel"
id="mc-panel-<?= e($key) ?>-cur" aria-labelledby="mc-tab-<?= e($key) ?>-cur" tabindex="0">
<?php component('matchcenter-season', [
'next' => $next, 'results' => $results, 'table' => $table, 'caption' => $curCaption,
]); ?>
</div>
<div class="matchcenter-team__season-panel" role="tabpanel"
id="mc-panel-<?= e($key) ?>-prev" aria-labelledby="mc-tab-<?= e($key) ?>-prev" tabindex="0">
<?php component('matchcenter-season', [
'results' => $previous['last_results'] ?? [], 'table' => $previous['table'] ?? [],
'caption' => $prevCaption, 'is_previous' => true,
]); ?>
</div> </div>
<?php endif; ?>
<?php if ($next !== [] || $results !== []): ?>
<aside class="matchcenter-team__aside">
<?php if ($next !== []): ?>
<h3 class="visually-hidden">Nächste Spiele</h3>
<ul class="match-list">
<?php foreach ($next as $m): ?>
<?php component('match-row', ['match' => $m]); ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if ($results !== []): ?>
<h3 class="visually-hidden">Letzte Ergebnisse</h3>
<ul class="match-list">
<?php foreach ($results as $m): ?>
<?php component('match-row', ['match' => $m]); ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</aside>
<?php endif; ?>
</div> </div>
<?php endif; ?>
</div> </div>
</section> </section>

View File

@@ -17,7 +17,7 @@ $meta = [
'title' => 'Matchcenter Spiele, Ergebnisse & Tabellen', 'title' => 'Matchcenter Spiele, Ergebnisse & Tabellen',
'description' => 'Spielplan, Ergebnisse und Tabellen der Mannschaften des TSV 08 Kulmbach 1. und 2. Mannschaft sowie Damen, automatisch aktuell.', 'description' => 'Spielplan, Ergebnisse und Tabellen der Mannschaften des TSV 08 Kulmbach 1. und 2. Mannschaft sowie Damen, automatisch aktuell.',
'og_image' => 'img/pages/fussball-hero-1600.jpg', 'og_image' => 'img/pages/fussball-hero-1600.jpg',
'scripts' => ['carousel.js', 'crest.js'], 'scripts' => ['carousel.js', 'crest.js', 'group-tabs.js'],
'schema' => array_merge( 'schema' => array_merge(
page_schema([ page_schema([
['name' => 'Startseite', 'slug' => ''], ['name' => 'Startseite', 'slug' => ''],

View File

@@ -178,6 +178,29 @@ $dateDisplay = static function (?DateTimeImmutable $dt, string $raw) use ($weekd
return $weekdaysDe[(int) $dt->format('w')] . ', ' . $dt->format('d.m.'); return $weekdaysDe[(int) $dt->format('w')] . ', ' . $dt->format('d.m.');
}; };
// --- Saison-Label (Fußball-Saison läuft JulJun): aus dem jüngsten Spiel ableiten,
// ersatzweise (leere Saison) aus dem heutigen Datum → liefert immer ein Label. ---
$seasonLabel = static function (DateTimeImmutable $d): string {
$y = (int) $d->format('Y');
$start = (int) $d->format('n') >= 7 ? $y : $y - 1;
return sprintf('Saison %d/%02d', $start, ($start + 1) % 100);
};
$seasonFromRows = static function (array ...$rowSets) use ($seasonLabel, $tz, $today): string {
$latest = null;
foreach ($rowSets as $rows) {
foreach ($rows as $r) {
if (empty($r['kickoff'])) {
continue;
}
$dt = DateTimeImmutable::createFromFormat('Y-m-d\TH:i', (string) $r['kickoff'], $tz);
if ($dt && ($latest === null || $dt > $latest)) {
$latest = $dt;
}
}
}
return $seasonLabel($latest ?? $today);
};
/** /**
* Ein Match aus der API in unser Anzeige-Format überführen. * Ein Match aus der API in unser Anzeige-Format überführen.
* @param array<string,mixed> $m Rohes Match aus data.matches[] * @param array<string,mixed> $m Rohes Match aus data.matches[]
@@ -260,12 +283,32 @@ foreach ($teamsCfg as $cfg) {
$prevTeam = $prevByKey[$key] ?? null; $prevTeam = $prevByKey[$key] ?? null;
try { try {
$prevCompound = (string) ($prevTeam['compound_id'] ?? '');
$previous = $prevTeam['previous'] ?? null;
// Spiele zuerst — daraus leiten wir die aktuelle Wettbewerbs-ID (Tabelle) ab, // Spiele zuerst — daraus leiten wir die aktuelle Wettbewerbs-ID (Tabelle) ab,
// damit eine neue Saison automatisch übernommen wird (die team_id ist stabil, // damit eine neue Saison automatisch übernommen wird (die team_id ist stabil,
// die compound_id der Tabelle wechselt pro Saison). // die compound_id der Tabelle wechselt pro Saison).
$matchesData = $fetchJson("{$apiHost}/api/service/widget/v1/team/{$teamCfgId}/matches"); $matchesData = $fetchJson("{$apiHost}/api/service/widget/v1/team/{$teamCfgId}/matches");
$apiCompound = (string) ($matchesData['data']['team']['compoundId'] ?? ''); $apiCompound = (string) ($matchesData['data']['team']['compoundId'] ?? '');
$compoundId = $apiCompound !== '' ? $apiCompound : $cfgCompoundId;
// Saisonwechsel: liefert die API eine NEUE compoundId, archivieren wir die eben
// abgelaufene Saison einmalig (verschachteltes previous des Vorgängers wird nicht
// mitkopiert → keine unbegrenzte Schachtelung).
if ($apiCompound !== '' && $prevCompound !== '' && $apiCompound !== $prevCompound && $prevTeam !== null) {
$previous = [
'season' => $prevTeam['season'] ?? null,
'league' => $prevTeam['league'] ?? null,
'last_results' => $prevTeam['last_results'] ?? [],
'table' => $prevTeam['table'] ?? [],
];
}
// Beste bekannte compoundId: API > letzter Cache > Config-Fallback. So triggert ein
// vorübergehender Fetch-Ausfall (apiCompound leer) keinen falschen Saisonwechsel.
$compoundId = $apiCompound !== ''
? $apiCompound
: ($prevCompound !== '' ? $prevCompound : $cfgCompoundId);
$tableData = $compoundId !== '' ? $fetchJson("{$apiHost}/api/service/widget/v1/competition/{$compoundId}/table") : null; $tableData = $compoundId !== '' ? $fetchJson("{$apiHost}/api/service/widget/v1/competition/{$compoundId}/table") : null;
// Name/Liga bevorzugt aus teams.json (Single Source), sonst API/alter Cache. // Name/Liga bevorzugt aus teams.json (Single Source), sonst API/alter Cache.
@@ -366,26 +409,43 @@ foreach ($teamsCfg as $cfg) {
$log("Team {$key}: Tabellen-Fetch fehlgeschlagen — alte Tabelle übernommen"); $log("Team {$key}: Tabellen-Fetch fehlgeschlagen — alte Tabelle übernommen");
} }
if ($upcoming === [] && $results === [] && $table === []) { // Aktuelle Saison immer zeigen, solange der Spiele-Fetch erfolgreich war (auch wenn
// sie noch leer ist) oder Daten/eine Vorsaison vorliegen. Nur überspringen, wenn der
// Fetch fehlschlug UND weder Cache noch Vorsaison existieren.
$hasCurrent = $upcoming !== [] || $results !== [] || $table !== [];
$hasPrevious = $previous !== null
&& (($previous['last_results'] ?? []) !== [] || ($previous['table'] ?? []) !== []);
if (!$hasCurrent && $matchesData === null && !$hasPrevious) {
$log("Team {$key}: keine Daten (API leer, kein Cache) — übersprungen"); $log("Team {$key}: keine Daten (API leer, kein Cache) — übersprungen");
continue; continue;
} }
$resultTeams[] = [ $season = $seasonFromRows($upcoming, $results);
$teamBlock = [
'key' => $key, 'key' => $key,
'name' => $name, 'name' => $name,
'league' => $league, 'league' => $league,
'slug' => $slugMap[$key] ?? '', 'slug' => $slugMap[$key] ?? '',
'compound_id' => $compoundId,
'season' => $season,
'next_matches' => $upcoming, 'next_matches' => $upcoming,
'last_results' => $results, 'last_results' => $results,
'table' => $table, 'table' => $table,
]; ];
if ($hasPrevious) {
$teamBlock['previous'] = $previous;
$keepCrests($previous['last_results'] ?? []);
$keepCrests($previous['table'] ?? []);
}
$resultTeams[] = $teamBlock;
foreach ($upcoming as $u) { foreach ($upcoming as $u) {
$upcomingAll[] = array_merge($u, ['team_key' => $key, 'team_name' => $name]); $upcomingAll[] = array_merge($u, ['team_key' => $key, 'team_name' => $name]);
} }
$log("Team {$key}: " . count($upcoming) . ' anstehend, ' . count($results) . ' Ergebnisse, ' . count($table) . ' Tabellenplätze'); $log("Team {$key}: " . count($upcoming) . ' anstehend, ' . count($results) . ' Ergebnisse, ' . count($table) . ' Tabellenplätze'
. ($hasPrevious ? ' (+ Vorsaison archiviert)' : ''));
} catch (\Throwable $e) { } catch (\Throwable $e) {
// Ein fehlschlagendes Team darf die anderen nicht killen — alten Block behalten. // Ein fehlschlagendes Team darf die anderen nicht killen — alten Block behalten.
$log("Team {$key} übersprungen: " . $e->getMessage()); $log("Team {$key} übersprungen: " . $e->getMessage());
@@ -395,6 +455,8 @@ foreach ($teamsCfg as $cfg) {
$keepCrests($prevTeam['next_matches'] ?? []); $keepCrests($prevTeam['next_matches'] ?? []);
$keepCrests($prevTeam['last_results'] ?? []); $keepCrests($prevTeam['last_results'] ?? []);
$keepCrests($prevTeam['table'] ?? []); $keepCrests($prevTeam['table'] ?? []);
$keepCrests($prevTeam['previous']['last_results'] ?? []);
$keepCrests($prevTeam['previous']['table'] ?? []);
foreach ($prevTeam['next_matches'] ?? [] as $u) { foreach ($prevTeam['next_matches'] ?? [] as $u) {
$upcomingAll[] = array_merge($u, ['team_key' => $key, 'team_name' => $prevTeam['name'] ?? $key]); $upcomingAll[] = array_merge($u, ['team_key' => $key, 'team_name' => $prevTeam['name'] ?? $key]);
} }
@@ -421,15 +483,27 @@ if (!$cacheFallbackUsed && $crestKeep !== []) {
} }
} }
// --- Cache atomar schreiben (tmp + rename) --- // --- Cache atomar schreiben (tmp + rename). Jeder Schritt ist abgesichert: ein
$payload = json_encode([ // defektes UTF-8-Zeichen aus der API (json_encode → Exception) oder ein
'fetched_at' => time(), // Schreib-/Rename-Fehler darf den bestehenden guten Cache NICHT leerschreiben. ---
'teams' => $resultTeams, try {
'upcoming' => $upcomingAll, $payload = json_encode([
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); 'fetched_at' => time(),
'stale' => $cacheFallbackUsed, // true = mind. ein Teil kam aus dem alten Cache
'teams' => $resultTeams,
'upcoming' => $upcomingAll,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
} catch (\Throwable $e) {
$fail('Cache-Encode fehlgeschlagen: ' . $e->getMessage());
}
$tmp = $cacheFile . '.tmp'; $tmp = $cacheFile . '.tmp';
file_put_contents($tmp, $payload); if (file_put_contents($tmp, $payload) === false) {
rename($tmp, $cacheFile); $fail("Cache-Schreiben fehlgeschlagen (tmp: {$tmp})");
}
if (!rename($tmp, $cacheFile)) {
@unlink($tmp);
$fail("Cache-Rename fehlgeschlagen ({$tmp}{$cacheFile})");
}
$log('OK: ' . count($resultTeams) . ' Teams gecached, ' . count($crestKeep) . ' Wappen lokal in assets/img/crests/'); $log('OK: ' . count($resultTeams) . ' Teams gecached, ' . count($crestKeep) . ' Wappen lokal in assets/img/crests/');

View File

@@ -3905,6 +3905,25 @@
color: var(--clr-text-muted); color: var(--clr-text-muted);
} }
/* Saison-Umschalter: wiederverwendete Stripe-Tabbar (group-tabs__bar/__tab), aber
linksbündig und etwas kompakter als bei den Gruppen-Tabs. Ohne JS sind beide
Panels gestapelt sichtbar (group-tabs.js versteckt erst on init das zweite). */
.matchcenter-team__seasons-bar {
justify-content: flex-start;
margin-bottom: 1.25rem;
}
/* display-neutrales Panel (kein group-tabs__panel-Grid) — das innere
matchcenter-team__grid bringt sein eigenes Layout mit. */
.matchcenter-team__season-panel[hidden] {
display: none;
}
.matchcenter-team__empty {
margin: 0;
color: var(--clr-text-muted);
}
/* kicker-Layout: Tabelle 2/3 links, Spiele 1/3 rechts; gestapelt auf Mobil. /* kicker-Layout: Tabelle 2/3 links, Spiele 1/3 rechts; gestapelt auf Mobil.
align-items:start + 0-Margins der Karten → beide Spalten starten oben bündig. */ align-items:start + 0-Margins der Karten → beide Spalten starten oben bündig. */
.matchcenter-team__grid { .matchcenter-team__grid {