nouveau-theatre-de-besancon/site/controllers/program.php
2025-08-01 16:15:26 +02:00

89 lines
No EOL
2.7 KiB
PHP

<?php
function sortByMonth($sessions) {
$frenchMonths = [
'janvier', 'février', 'mars', 'avril', 'mai', 'juin',
'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'
];
$englishMonths = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
$grouped = [];
foreach ($sessions as $item) {
$monthIndex = date('n', strtotime($item['date'])) - 1;
$month = date('F', strtotime($item['date']));
$monthInFrench = str_replace($englishMonths, $frenchMonths, $month);
// Décalage pour commencer par septembre (index 8)
$shiftedIndex = ($monthIndex + 12 - 8) % 12;
if (!isset($grouped[$shiftedIndex])) {
$grouped[$shiftedIndex] = ['name' => $monthInFrench, 'sessions' => []];
}
$grouped[$shiftedIndex]['sessions'][] = $item;
}
ksort($grouped); // Trie en commençant par septembre
$orderedSessions = [];
foreach ($grouped as $month) {
$orderedSessions[$month['name']] = $month['sessions'];
}
return $orderedSessions;
}
function createArraySession($event, $session) {
$isMapadoEvent = $event->isMapadoEvent() == 'true';
$arraySession = $session->toArray();
$arraySession['title'] = $event->title()->value();
$arraySession['authors'] = $event->authors()->inline();
$arraySession['place'] = $event->place();
$arraySession['public'] = $event->public();
$arraySession['event-url'] = $event->url();
$arraySession['duration'] = $event->duration();
$arraySession['ticketingUrl'] = $isMapadoEvent ? $session->ticketingUrl()->value() : $event->bookingUrl()->value();
$arraySession['bookableStock'] = $isMapadoEvent ? $session->bookableStock()->value() : 'free';
$arraySession['totalStock'] = $isMapadoEvent ? (int) $event->totalStock()->value() : 'free';
$arraySession['color'] = $event->color();
$arraySession['categories'] = $event->categories();
return $arraySession;
}
return function($kirby, $page) {
$currentSeason = $page->children()->first();
$today = date('Ymd');
$currentSeasonSessions = [];
foreach ($currentSeason->children() as $event) {
$sessions = $event->isMapadoEvent() == 'true' ? $event->remoteSessions() : $event->sessions();
$isStillShowing = false;
foreach ($sessions->toStructure() as $session) {
$arraySession = createArraySession($event, $session);
$currentSeasonSessions[] = $arraySession;
$sessionDate = str_replace('-', '', $session->date()->toDate('YMMdd'));
if ($sessionDate >= $today) {
$isStillShowing = true;
}
}
}
return [
'events' => splitNextAndPreviousEvents($kirby->collection('ordered-season')),
'currentSeasonSessions' => sortByMonth($currentSeasonSessions)
];
};