From c98d5eb36b266cd487fe00ac87df199591ee9baa Mon Sep 17 00:00:00 2001 From: isUnknown Date: Wed, 28 Aug 2024 08:46:00 +0200 Subject: [PATCH] update mapado event working --- assets/css/src/collapsable-section.css | 10 ++- assets/js/calendar.js | 44 +++++++++++++ site/blueprints/pages/event.yml | 18 +++-- site/config/config.php | 3 +- site/config/hooks/update-mapado-event.php | 65 ++---------------- site/config/routes/update-mapado-event.php | 62 ++---------------- site/plugins/helpers/index.php | 76 ++++++++++++++++++++++ site/templates/event.php | 32 +++++++++ 8 files changed, 184 insertions(+), 126 deletions(-) diff --git a/assets/css/src/collapsable-section.css b/assets/css/src/collapsable-section.css index 2d7bef7..caea444 100644 --- a/assets/css/src/collapsable-section.css +++ b/assets/css/src/collapsable-section.css @@ -4,7 +4,9 @@ padding: 0 !important; } -.collapsable > * { +.collapsable:not(:first-child) > *, +.collapsable:first-child > button, +.collapsable .session { padding: var(--row-padding); box-sizing: border-box; width: 100%; @@ -34,6 +36,12 @@ .collapsable__content { font-size: var(--font-size-s); } + +.collapsable__content .session { + font-size: var(--font-size-m); + grid-template-columns: 2fr 1fr 1fr 1fr; +} + .collapsable__content .production p:not(:last-child) { margin-bottom: 2rem; } diff --git a/assets/js/calendar.js b/assets/js/calendar.js index 3cac83a..5d966a5 100644 --- a/assets/js/calendar.js +++ b/assets/js/calendar.js @@ -13,6 +13,33 @@ function getDatesInMonth(month) { return dates; } +async function getMapadoEvent(id) { + const requestEndPoint = "ticketings/" + id; + + const requestParams = []; + + const requestFields = [ + { name: "startDate" }, + { name: "bookableStock" }, + { + name: "eventDateList", + subfields: [{ name: "@id" }, { name: "bookableStock" }], + }, + ]; + + const requestOptions = { + method: "POST", + body: JSON.stringify({ requestEndPoint, requestParams, requestFields }), + }; + + const response = await fetch("/mapado-api.json", requestOptions); + const json = await response.json(); + + const event = json["hydra:member"]; + + console.log(`Événement Mapado`, event); +} + async function getMapadoDates(monthNumb) { const contractId = "1941"; @@ -129,3 +156,20 @@ async function getMergedDates(monthNumb, dates) { const json = await response.json(); return json; } + +async function updateMapadoEvent(pageUri) { + const headers = new Headers(); + + const requestOptions = { + method: "POST", + headers: headers, + redirect: "follow", + body: JSON.stringify({ + pageUri, + }), + }; + + const response = await fetch("/update-mapado-event.json", requestOptions); + const json = await response.json(); + return json; +} diff --git a/site/blueprints/pages/event.yml b/site/blueprints/pages/event.yml index 9216c05..0d0b9e0 100644 --- a/site/blueprints/pages/event.yml +++ b/site/blueprints/pages/event.yml @@ -79,11 +79,13 @@ tabs: disabled: true columns: date: - width: 1/3 + width: 1/4 time: - width: 1/3 + width: 1/4 ticketingUrl: - width: 1/3 + width: 1/4 + bookableStock: + width: 1/4 when: isMapadoEvent: true fields: @@ -91,15 +93,19 @@ tabs: type: date icon: calendar display: DD/MM/YYYY - width: 1/3 + width: 1/4 time: label: Horaire de début type: text - width: 1/3 + width: 1/4 ticketingUrl: label: Lien billetterie type: url - width: 1/3 + width: 1/4 + bookableStock: + label: Places disponibles + type: text + width: 1/4 - width: 1/1 fields: line: diff --git a/site/config/config.php b/site/config/config.php index cdcfd79..54937e4 100644 --- a/site/config/config.php +++ b/site/config/config.php @@ -34,7 +34,8 @@ return [ ], 'routes' => [ require_once(__DIR__ . '/routes/mapado-api.php'), - require_once(__DIR__ . '/routes/month-dates.php') + require_once(__DIR__ . '/routes/month-dates.php'), + require_once(__DIR__ . '/routes/update-mapado-event.php'), ], 'hooks' => [ 'page.update:after' => require_once(__DIR__ . '/hooks/update-mapado-event.php') diff --git a/site/config/hooks/update-mapado-event.php b/site/config/hooks/update-mapado-event.php index 95d9440..8a29f77 100644 --- a/site/config/hooks/update-mapado-event.php +++ b/site/config/hooks/update-mapado-event.php @@ -1,67 +1,10 @@ isMapadoEvent() != 'true') return; - $request = [ - "requestEndPoint" => "ticketings/" . $newPage->mapadoId()->value(), - "requestParams" => [], - "requestFields" => [ - ["name" => "title"], - ["name" => "address"], - ["name" => "slug"], - [ - "name" => "eventDateList", - "subfields" => [ - ["name" => "startDate"], - ["name" => "endDate"], - ["name" => "bookableStock"] - ] - ], - ] - ]; - + + $request = createMapadoEventRequest($newPage); $mapadoEvent = fetchMapadoEvent($request); - $duration = null; - $sessionsToSave = []; - - if ($mapadoEvent->{"@type"} === 'hydra:Error') { - - $error = null; - if ($newPage->mapadoId()->isEmpty()) { - $error = 'Entrez l\'identifiant de l\'événement Mapado dans l\'onglet "Réglages".'; - } else { - $error = 'Aucun événement Mapado ne correspond à l\'identifiant ' . $newPage->mapado()->value() . '. En êtes-vous sûr•e ?'; - } - - throw new Exception($error, 1); - } - - foreach ($mapadoEvent->eventDateList as $session) { - - $sessionMonth = substr($session->startDate, 0, 7); - - $day = 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; - - if (isset($session->endDate) && !$duration) { - $endTime = substr($session->endDate, 11 , 5); - $duration = getTimeDifference($startTime, $endTime); - } - - $sessionsToSave[] = [ - "date" => substr($session->startDate, 0, 10), - "time" => str_replace(':', 'h', $startTime), - 'ticketingUrl' => $ticketingUrl - ]; - } - - $newPage->update([ - "mapadoSlug" => $mapadoEvent->slug, - "remoteDuration" => $duration, - "remoteSessions" => $sessionsToSave - ]); + saveMapadoEvent($mapadoEvent, $newPage); }; \ No newline at end of file diff --git a/site/config/routes/update-mapado-event.php b/site/config/routes/update-mapado-event.php index e9bc046..8234ed9 100644 --- a/site/config/routes/update-mapado-event.php +++ b/site/config/routes/update-mapado-event.php @@ -3,67 +3,15 @@ return [ 'pattern' => '/update-mapado-event.json', 'method' => 'POST', - 'action' => function () { + 'action' => function() { $jsonRequest = file_get_contents("php://input"); $request = json_decode($jsonRequest, true); - $id = $request['id']; - - $request = [ - "requestEndPoint" => "ticketings/" . $id, - "requestParams" => [], - "requestFields" => [ - ["name" => "title"], - ["name" => "address"], - ["name" => "slug"], - [ - "name" => "eventDateList", - "subfields" => [ - ["name" => "startDate"], - ["name" => "endDate"], - ["name" => "bookableStock"] - ] - ], - ] - ]; + $page = page($request['pageUri']); + $request = createMapadoEventRequest($page); $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]; - - 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" => option('ticketingUrl') . 'event/' . $mapadoEvent->slug . '?eventDate=' . $eventDateId - ]); - - $sessionsToSave[] = [ - "date" => substr($session->startDate, 0, 10), - "time" => str_replace(':', 'h', $startTime) - ]; - } - } - $event->update([ - "remoteDuration" => $duration, - "remoteSessions" => $sessionsToSave - ]); + return saveMapadoEvent($mapadoEvent, $page); } -]; +]; \ No newline at end of file diff --git a/site/plugins/helpers/index.php b/site/plugins/helpers/index.php index 97bf656..51e056c 100644 --- a/site/plugins/helpers/index.php +++ b/site/plugins/helpers/index.php @@ -87,4 +87,80 @@ function fetchMapadoEvents($request) { $responseJson = json_encode(json_decode($responseString)); return $responseJson; +} + +function createMapadoEventRequest($page) { + $request = [ + "requestEndPoint" => "ticketings/" . $page->mapadoId()->value(), + "requestParams" => [], + "requestFields" => [ + ["name" => "title"], + ["name" => "address"], + ["name" => "slug"], + [ + "name" => "eventDateList", + "subfields" => [ + ["name" => "startDate"], + ["name" => "endDate"], + ["name" => "bookableStock"] + ] + ], + ] + ]; + + return $request; +} + +function saveMapadoEvent($mapadoEvent, $page) { + $kirby = kirby(); + $kirby->impersonate('kirby'); + + $duration = ''; + $sessionsToSave = []; + + if ($mapadoEvent->{"@type"} === 'hydra:Error') { + + $error = null; + if ($page->mapadoId()->isEmpty()) { + $error = 'Entrez l\'identifiant de l\'événement Mapado dans l\'onglet "Réglages".'; + } else { + $error = 'Aucun événement Mapado ne correspond à l\'identifiant ' . $page->mapado()->value() . '. En êtes-vous sûr•e ?'; + } + + throw new Exception($error, 1); + } + + + foreach ($mapadoEvent->eventDateList as $session) { + + $sessionMonth = substr($session->startDate, 0, 7); + + $day = 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; + + if (isset($session->endDate) && !$duration) { + $endTime = substr($session->endDate, 11 , 5); + $duration = getTimeDifference($startTime, $endTime); + } + + $sessionsToSave[] = [ + "date" => substr($session->startDate, 0, 10), + "time" => str_replace(':', 'h', $startTime), + "ticketingUrl" => $ticketingUrl, + "bookableStock" => $session->bookableStock + ]; + } + + $dataToSave = [ + "mapadoSlug" => $mapadoEvent->slug, + "remoteDuration" => $duration, + "remoteSessions" => $sessionsToSave + ]; + + $page->update($dataToSave); + + return $dataToSave; } \ No newline at end of file diff --git a/site/templates/event.php b/site/templates/event.php index 92d5312..88ca0e3 100644 --- a/site/templates/event.php +++ b/site/templates/event.php @@ -19,6 +19,38 @@
+ 'Prenez vos places !'], slots: true) ?> + +
+ isMapadoEvent() != 'true' ? $page->sessions()->toStructure() : $page->remoteSessions()->toStructure(); + + foreach ($sessions as $session) : + $date = new DateTime($session->date()); + $formatter = new IntlDateFormatter('fr_FR', IntlDateFormatter::FULL, IntlDateFormatter::NONE); + $formattedDate = $formatter->format($date); + ?> +
+

+

time() ?>

+

place() ?>

+

test

+
+ +
+ + + 'Distribution et production'], slots: true) ?>