Jahreskalender mit farblich markierten Touren und Jahres-Navigator

This commit is contained in:
torsten 2026-11-21 13:04:34 +01:00
parent 7695c70ad2
commit b92664f80e
7 changed files with 415 additions and 0 deletions

View file

@ -61,6 +61,88 @@ class TourController
]);
}
/** Jahreskalender, in dem alle Touren farblich markiert sind. */
public function calendar(): void
{
$currentYear = (int) date('Y');
$tourYears = Tour::years();
$firstYear = min(array_merge([$currentYear - 1], $tourYears));
$lastYear = max(array_merge([$currentYear + 1], $tourYears));
$requestedYear = input('jahr');
$selectedYear = $currentYear;
if (is_string($requestedYear) && ctype_digit($requestedYear)) {
$selectedYear = (int) $requestedYear;
}
$selectedYear = max($firstYear, min($lastYear, $selectedYear));
$toursByDay = [];
foreach (Tour::forYear($selectedYear) as $tour) {
$toursByDay[substr((string) $tour->start, 0, 10)][] = $tour;
}
view('tour.calendar', [
'selectedYear' => $selectedYear,
'years' => range($firstYear, $lastYear),
'months' => $this->buildCalendarMonths($selectedYear, $toursByDay),
'todayDate' => today(),
'tourCount' => array_sum(array_map('count', $toursByDay)),
]);
}
/**
* Baut das Monatsraster (Montag als erster Wochentag) für den Jahreskalender.
*
* @param array<string, array<int, Tour>> $toursByDay
*/
private function buildCalendarMonths(int $year, array $toursByDay): array
{
$labels = ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli',
'August', 'September', 'Oktober', 'November', 'Dezember'];
$months = [];
foreach ($labels as $index => $label) {
$month = $index + 1;
$first = new \DateTimeImmutable(sprintf('%04d-%02d-01', $year, $month));
$daysInMonth = (int) $first->format('t');
$leadingBlanks = (int) $first->format('N') - 1;
$weeks = [];
$week = array_fill(0, $leadingBlanks, null);
for ($day = 1; $day <= $daysInMonth; $day++) {
$date = $first->setDate($year, $month, $day)->format('Y-m-d');
$week[] = [
'day' => $day,
'date' => $date,
'tours' => $toursByDay[$date] ?? [],
];
if (count($week) === 7) {
$weeks[] = $week;
$week = [];
}
}
if ($week !== []) {
$weeks[] = array_pad($week, 7, null);
}
$months[] = [
'label' => $label,
'weeks' => $weeks,
];
}
return $months;
}
public function indexOld(): void
{
$tours = Tour::past();