From 78d542cbac0d6087250033f5a211a727fdb7cb2a Mon Sep 17 00:00:00 2001 From: isUnknown Date: Wed, 11 Sep 2024 15:17:44 +0200 Subject: [PATCH] redesign calendar api --- assets/js/calendar.js | 37 ++++++- site/config/config.php | 2 +- .../routes/get-current-season-calendar.php | 100 ++++++++++++++++++ 3 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 site/config/routes/get-current-season-calendar.php diff --git a/assets/js/calendar.js b/assets/js/calendar.js index 9619b2a..a413743 100644 --- a/assets/js/calendar.js +++ b/assets/js/calendar.js @@ -146,19 +146,48 @@ function getId(eventData) { return id; } +function createEmptyCalendar() { + const calendar = {}; + + for (let month = 9; month < 19; month++) { + const normalizedMonth = ((month - 1) % 12) + 1; + + const daysInMonth = dayjs() + .month(normalizedMonth - 1) + .daysInMonth(); + + calendar[normalizedMonth] = { + name: dayjs() + .month(normalizedMonth - 1) + .format("MMMM"), + }; + + for (let day = 1; day <= daysInMonth; day++) { + calendar[normalizedMonth][day] = null; + } + } + + return calendar; +} + async function getMergedDates(monthNumb, dates) { - const month = dayjs().month(monthNumb).format("YYYY-MM"); + const calendar = createEmptyCalendar(); + const requestParams = { - month, - dates, + calendar, }; const requestOptions = { method: "POST", body: JSON.stringify(requestParams), }; - const response = await fetch("/month-dates.json", requestOptions); + + const response = await fetch( + "/get-current-season-calendar.json", + requestOptions + ); const json = await response.json(); + console.log("calendar", json); return json; } diff --git a/site/config/config.php b/site/config/config.php index a6f346a..cc5b688 100644 --- a/site/config/config.php +++ b/site/config/config.php @@ -34,7 +34,7 @@ return [ ], 'routes' => [ require_once(__DIR__ . '/routes/mapado-api.php'), - require_once(__DIR__ . '/routes/month-dates.php'), + require_once(__DIR__ . '/routes/get-current-season-calendar.php'), require_once(__DIR__ . '/routes/update-mapado-event.php'), require_once(__DIR__ . '/routes/mapado-fetch.php'), require_once(__DIR__ . '/routes/brevo-create-contact.php'), diff --git a/site/config/routes/get-current-season-calendar.php b/site/config/routes/get-current-season-calendar.php new file mode 100644 index 0000000..546ec91 --- /dev/null +++ b/site/config/routes/get-current-season-calendar.php @@ -0,0 +1,100 @@ +children()->filter( + fn ($season) => str_contains($season->title()->value(), $year) + ); +} + +function getTimeDifference($start, $end) { + $start = new DateTime($start); + $end = new DateTime($end); + + $difference = $end->diff($start); + + return $difference->h . "h" . $difference->i; +} + +return [ + 'pattern' => '/get-current-season-calendar.json', + 'method' => 'POST', + 'action' => function () { + $jsonRequest = file_get_contents("php://input"); + $request = json_decode($jsonRequest, true); + + $calendar = $request['calendar']; + + $currentSeason = page('programme')->children()->first(); + + foreach ($currentSeason->children() as $event) { + + $eventInfos = [ + "title" => $event->title()->value(), + "place" => $event->place()->value(), + "authors" => $event->authors()->value() + ]; + + if ($event->isMapadoEvent() == 'false') { + foreach ($event->sessions()->toStructure() as $session) { + + $sessionMonth = $session->date()->toDate('M'); + + $sessionDay = intval($session->date()->toDate('d')); + $calendar[$sessionMonth][$sessionDay]['sessions'][] = array_merge($eventInfos, [ + "color" => $event->color()->value(), + "day" => $sessionDay, + "time" => $session->timeComplement()->isEmpty() == 'true' ? $session->time()->value() : $session->time()->value() . ' ' . $session->timeComplement()->value(), + "place" => $event->place()->value(), + "duration" => $event->duration()->value(), + "eventUrl" => $event->url(), + ]); + } + } + + if ($event->isMapadoEvent() == 'true') { + $request = createMapadoEventRequest($event); + $mapadoEvent = fetchMapadoEvent($request); + + $duration = null; + + $sessionsToSave = []; + + foreach ($mapadoEvent->eventDateList as $session) { + + $sessionMonth = (int) substr($session->startDate, 5, 2); + $sessionDay = intval(substr($session->startDate, 8, 2)); + + $startTime = substr($session->startDate, 11, 5); + + $eventDateId = explode('/', $session->{'@id'}); + $eventDateId = $eventDateId[count($eventDateId) - 1]; + + $ticketingUrl = option('ticketingUrl') . 'event/' . $mapadoEvent->slug . '?eventDate=' . $eventDateId; + $bookableStock = $session->notInStockContingentBookableStock; + + if (isset($session->endDate) && !$duration) { + $endTime = substr($session->endDate, 11 , 5); + $duration = getTimeDifference($startTime, $endTime); + } + + $calendar[$sessionMonth][$sessionDay]['sessions'][] = array_merge($eventInfos, [ + "color" => $event->color()->value(), + "day" => $sessionDay, + "time" => str_replace(':', 'h', $startTime), + "duration" => $duration, + "place" => $event->place()->value(), + "eventUrl" => $event->url(), + "ticketingUrl" => $ticketingUrl, + "bookableStock" => $bookableStock, + "totalStock" => (int) $event->totalStock()->value(), + ]); + } + + saveMapadoEvent($mapadoEvent, $event); + } + } + + return json_encode($calendar); + } +];