119 lines
3.6 KiB
PHP
119 lines
3.6 KiB
PHP
<?php
|
|
|
|
function getCorrespondingSeasons($request) {
|
|
$year = explode('-', $request['month'])[0];
|
|
return page('programme')->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' => '/month-dates.json',
|
|
'method' => 'POST',
|
|
'action' => function () {
|
|
$jsonRequest = file_get_contents("php://input");
|
|
$request = json_decode($jsonRequest, true);
|
|
|
|
$dates = $request['dates'];
|
|
$requestMonth = $request['month'];
|
|
|
|
$correspondingSeasons = getCorrespondingSeasons($request);
|
|
|
|
foreach ($correspondingSeasons->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('Y-m');
|
|
if ($sessionMonth === $requestMonth) {
|
|
$day = intval($session->date()->toDate('d'));
|
|
$dates[$day][] = array_merge($eventInfos, [
|
|
"day" => $day,
|
|
"time" => $session->time()->value(),
|
|
"duration" => $event->duration()->value(),
|
|
]);
|
|
};
|
|
}
|
|
}
|
|
|
|
if ($event->isMapadoEvent() == 'true') {
|
|
$request = [
|
|
"requestEndPoint" => "ticketings/" . $event->mapadoId()->value(),
|
|
"requestParams" => [],
|
|
"requestFields" => [
|
|
["name" => "title"],
|
|
["name" => "address"],
|
|
["name" => "slug"],
|
|
[
|
|
"name" => "eventDateList",
|
|
"subfields" => [
|
|
["name" => "startDate"],
|
|
["name" => "endDate"],
|
|
["name" => "bookableStock"]
|
|
]
|
|
],
|
|
]
|
|
];
|
|
|
|
$mapadoEvent = fetchMapadoEvent($request);
|
|
|
|
$duration = null;
|
|
|
|
$sessionsToSave = [];
|
|
|
|
foreach ($mapadoEvent->eventDateList as $session) {
|
|
|
|
$sessionMonth = substr($session->startDate, 0, 7);
|
|
|
|
if ($sessionMonth === $requestMonth) {
|
|
$day = intval(substr($session->startDate, 8, 2));
|
|
$startTime = substr($session->startDate, 11, 5);
|
|
$eventDateId = explode('/', $session->{'@id'});
|
|
$eventDateId = $eventDateId[count($eventDateId) - 1];
|
|
$ticketingUrl = 'https://cdn-besancon.mapado.com/event/' . $mapadoEvent->slug . '?eventDate=' . $eventDateId;
|
|
|
|
if (isset($session->endDate) && !$duration) {
|
|
$endTime = substr($session->endDate, 11 , 5);
|
|
$duration = getTimeDifference($startTime, $endTime);
|
|
}
|
|
|
|
$dates[$day][] = array_merge($eventInfos, [
|
|
"day" => $day,
|
|
"time" => str_replace(':', 'h', $startTime),
|
|
"duration" => $duration,
|
|
"ticketingUrl" => $ticketingUrl
|
|
]);
|
|
|
|
$sessionsToSave[] = [
|
|
"date" => substr($session->startDate, 0, 10),
|
|
"time" => str_replace(':', 'h', $startTime),
|
|
"ticketingUrl" => $ticketingUrl
|
|
];
|
|
}
|
|
}
|
|
$event->update([
|
|
"mapadoSlug" => $mapadoEvent->slug,
|
|
"remoteDuration" => $duration,
|
|
"remoteSessions" => $sessionsToSave
|
|
]);
|
|
}
|
|
}
|
|
|
|
return json_encode($dates);
|
|
}
|
|
];
|