From 07565f1413675c1213372a709a8fe0132280ec53 Mon Sep 17 00:00:00 2001 From: isUnknown Date: Mon, 29 Jul 2024 12:29:00 +0200 Subject: [PATCH] redesign merge dates function --- site/blueprints/pages/event.yml | 61 ++++++++++++----------- site/config/routes/mapado-api.php | 25 +--------- site/config/routes/month-dates.php | 80 +++++++++++++++++++----------- site/plugins/helpers/index.php | 53 ++++++++++++++++++++ site/snippets/calendar-strip.php | 10 +--- 5 files changed, 140 insertions(+), 89 deletions(-) diff --git a/site/blueprints/pages/event.yml b/site/blueprints/pages/event.yml index 918f739..247c297 100644 --- a/site/blueprints/pages/event.yml +++ b/site/blueprints/pages/event.yml @@ -5,57 +5,62 @@ tabs: label: Contenu icon: false columns: - - width: 1/1 + - width: 1/2 fields: - keyInfosHeadline: - type: headline - label: Infos clés - startDate: - label: Date de début - type: date - icon: calendar - display: DD/MM/YYYY - width: 1/5 - when: - isMapadoEvent: false mapadoKeyInfos: - label: Événement connecté à Mapado + label: Séances et durée récupérées de Mapado type: info - text: Les données de dates et de durée sont récupérées de Mapado ([gestion Mapado](https://desk.mapado.com/)). - width: 2/3 + text: | + [Accéder à l'interface de gestion de Mapado](https://desk.mapado.com/). when: isMapadoEvent: true - endDate: - label: Date de fin - type: date - icon: calendar - display: DD/MM/YYYY - width: 1/5 - when: - isMapadoEvent: false duration: label: Durée type: text icon: clock - width: 1/5 + width: 1/2 placeholder: 1h20 when: isMapadoEvent: false + - width: 1/2 + fields: public: type: text icon: users - width: 1/5 + width: 1/2 placeholder: dès 8 ans place: label: Lieu type: text icon: pin - width: 1/5 + width: 1/2 placeholder: NTB - line: - type: line - width: 1/1 fields: + sessions: + label: Séances + type: structure + columns: + date: + width: 1/2 + time: + width: 1/2 + when: + isMapadoEvent: false + fields: + date: + type: date + icon: calendar + display: DD/MM/YYYY + width: 1/2 + time: + label: Horaire de début + type: text + width: 1/2 + - width: 1/1 + fields: + line: + type: line presentationHeadline: type: headline label: Présentation diff --git a/site/config/routes/mapado-api.php b/site/config/routes/mapado-api.php index 9f3b6b5..dacae1f 100644 --- a/site/config/routes/mapado-api.php +++ b/site/config/routes/mapado-api.php @@ -6,29 +6,8 @@ return [ $jsonRequest = file_get_contents("php://input"); $request = json_decode($jsonRequest, true); - $token = site()->mapadoToken(); - $contractId = site()->mapadoContractId(); - $requestEndPoint = $request['requestEndPoint']; - $requestParams = $request['requestParams']; - $requestFields = $request['requestFields']; + $response = fetchMapado($request); - $url = "https://ticketing.mapado.net/v1/$requestEndPoint?contract=$contractId&"; - - $url .= buildParamsString($requestParams); - $url .= buildFieldsString($requestFields); - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, [ - "Authorization: Bearer " . $token - ]); - - $responseString = curl_exec($ch); - curl_close($ch); - - $responseJson = json_encode(json_decode($responseString)); - - return $responseJson; + return $response; } ]; diff --git a/site/config/routes/month-dates.php b/site/config/routes/month-dates.php index a8fab71..0a7ac2e 100644 --- a/site/config/routes/month-dates.php +++ b/site/config/routes/month-dates.php @@ -7,24 +7,6 @@ function getCorrespondingSeasons($request) { ); } -function getEventsByDay($eventsFromSameMonth) { - $eventsByDay = []; - foreach ($eventsFromSameMonth as $event) { - $day = $event->startDate()->toDate('d'); - if (!isset($eventsByDay[$day])) { - $eventsByDay[$day] = []; - } - $eventsByDay[$day][] = [ - "day" => $day, - "title" => $event->title()->value(), - "id" => null, - "startTime" => null, - "ticketingUrl" => null, - ]; - } - return $eventsByDay; -} - return [ 'pattern' => '/month-dates.json', 'method' => 'POST', @@ -32,23 +14,63 @@ return [ $jsonRequest = file_get_contents("php://input"); $request = json_decode($jsonRequest, true); - $mapadoDates = $request['dates']; - $requestMonth = $request['month']; + $dates = $request['dates']; + $requestMonth = $request['month']; - $correspondingSeasons = getCorrespondingSeasons($request); + $correspondingSeasons = getCorrespondingSeasons($request); - $eventsFromSameMonth = $correspondingSeasons->children()->filter( - fn ($event) => $event->isNotEmpty() && $event->startDate()->toDate('Y-m') == $requestMonth - ); + foreach ($correspondingSeasons->children() as $event) { + if ($event->isMapadoEvent() == 'false') { + foreach ($event->sessions()->toStructure() as $session) { + + $sessionMonth = $session->date()->toDate('Y-m'); + if ($sessionMonth === $requestMonth) { + $day = $session->date()->toDate('d'); + $dates[$day][] = [ + "title" => $event->title()->value(), + "time" => $session->time()->value(), + "duration" => $event->duration()->value() + ]; + }; + } + } + + if ($event->isMapadoEvent() == 'true') { + $request = [ + "requestEndPoint" => "ticketings/" . $event->mapadoId()->value(), + "requestParams" => [], + "requestFields" => [ + ["name" => "title"], + [ + "name" => "eventDateList", + "subfields" => [ + ["name" => "startDate"], + ["name" => "endDate"], + ["name" => "bookableStock"] + ] + ], + ] + ]; + + $event = fetchMapadoEvent($request); - $eventsByDay = getEventsByDay($eventsFromSameMonth); + foreach ($event->eventDateList as $session) { + $sessionMonth = substr($session->startDate, 0, 7); + if ($sessionMonth === $requestMonth) { + + $day = intval(substr($session->startDate, 8, 2)); + $time = substr($session->startDate, 11, 5); + + $dates[$day][] = [ + "title" => $event->title, + "time" => str_replace(':', 'h', $time) + ]; + } - foreach ($mapadoDates as $day => $mapadoDate) { - if (isset($eventsByDay[$day])) { - $mapadoDates[$day] = array_merge($mapadoDates[$day], $eventsByDay[$day]); + } } } - return json_encode($mapadoDates); + return json_encode($dates); } ]; diff --git a/site/plugins/helpers/index.php b/site/plugins/helpers/index.php index 1c6160f..97bf656 100644 --- a/site/plugins/helpers/index.php +++ b/site/plugins/helpers/index.php @@ -34,4 +34,57 @@ function buildParamsString($requestParams) { function getEventsByMonth($month) { $events = page('programme')->children(); +} + +function fetchMapadoEvent($request) { + $token = site()->mapadoToken(); + $requestEndPoint = $request['requestEndPoint']; + $requestParams = $request['requestParams']; + $requestFields = $request['requestFields']; + + $url = "https://ticketing.mapado.net/v1/$requestEndPoint?"; + + $url .= buildParamsString($requestParams); + $url .= buildFieldsString($requestFields); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "Authorization: Bearer " . $token + ]); + + $responseString = curl_exec($ch); + curl_close($ch); + + $response = json_decode($responseString); + + return $response; +} + +function fetchMapadoEvents($request) { + $token = site()->mapadoToken(); + $contractId = site()->mapadoContractId(); + $requestEndPoint = $request['requestEndPoint']; + $requestParams = $request['requestParams']; + $requestFields = $request['requestFields']; + + $url = "https://ticketing.mapado.net/v1/$requestEndPoint?contract=$contractId&"; + + $url .= buildParamsString($requestParams); + $url .= buildFieldsString($requestFields); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "Authorization: Bearer " . $token + ]); + + $responseString = curl_exec($ch); + curl_close($ch); + + $responseJson = json_encode(json_decode($responseString)); + + return $responseJson; } \ No newline at end of file diff --git a/site/snippets/calendar-strip.php b/site/snippets/calendar-strip.php index 22b39dc..7e84c48 100644 --- a/site/snippets/calendar-strip.php +++ b/site/snippets/calendar-strip.php @@ -16,16 +16,8 @@ async updateDates() { document.body.classList.toggle('progress') this.dates = getDatesInMonth(this.monthNumb); - - const mapadoDates = await getMapadoDates(this.monthNumb) - mapadoDates.forEach(mapadoDate => { - const day = parseInt(mapadoDate.day) - this.dates[day].push(mapadoDate) - }) - - const mergedDates = await getMergedDates(this.monthNumb, this.dates) - this.dates = mergedDates + this.dates = await getMergedDates(this.monthNumb, this.dates) console.log('Merged dates', this.dates) document.body.classList.toggle('progress')