From a6effd41b25620a3a005352a56f185c70f098574 Mon Sep 17 00:00:00 2001 From: "John R. D'Orazio" Date: Thu, 12 Sep 2024 11:34:15 +0200 Subject: [PATCH] update for new API routes --- includes/Festivity.php | 6 +-- includes/LiturgyOfTheDay.php | 88 ++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/includes/Festivity.php b/includes/Festivity.php index e3a851e..0bf8894 100644 --- a/includes/Festivity.php +++ b/includes/Festivity.php @@ -39,7 +39,7 @@ public function __construct(array $festivity) } $this->type = LitFeastType::isValid($festivity["type"]) ? $festivity["type"] : ""; $this->grade = LitGrade::isValid($festivity["grade"]) ? $festivity["grade"] : -1; - $this->displayGrade = $festivity["displayGrade"]; + $this->displayGrade = $festivity["display_grade"]; if (is_string($festivity["common"])) { //Festivity::debugWrite( "*** Festivity.php *** common vartype is string, value = $festivity["common"]" ); $this->common = LitCommon::areValid(explode(",", $festivity["common"])) ? explode(",", $festivity["common"]) : []; @@ -52,7 +52,7 @@ public function __construct(array $festivity) $this->common = []; } } - $this->liturgicalYear = $festivity["liturgicalYear"] ?? ''; - $this->isVigilMass = $festivity["isVigilMass"] ?? false; + $this->liturgicalYear = $festivity["liturgical_year"] ?? ''; + $this->isVigilMass = $festivity["is_vigil_mass"] ?? false; } } diff --git a/includes/LiturgyOfTheDay.php b/includes/LiturgyOfTheDay.php index 31ad7d5..d33720c 100644 --- a/includes/LiturgyOfTheDay.php +++ b/includes/LiturgyOfTheDay.php @@ -8,8 +8,8 @@ class LiturgyOfTheDay { - const METADATA_URL = 'https://litcal.johnromanodorazio.com/api/dev/metadata/'; - const LITCAL_URL = 'https://litcal.johnromanodorazio.com/api/dev/'; + const METADATA_URL = 'https://litcal.johnromanodorazio.com/api/dev/calendars'; + const LITCAL_URL = 'https://litcal.johnromanodorazio.com/api/dev/calendar'; //private string $logFile = 'debug.log'; private LitCommon $LitCommon; private LitGrade $LitGrade; @@ -17,12 +17,11 @@ class LiturgyOfTheDay private ?string $NationalCalendar = null; private ?string $DiocesanCalendar = null; private ?string $Timezone = null; - private array $SUPPORTED_DIOCESES = []; - private array $SUPPORTED_NATIONS = []; - //private array $queryArray = []; + private array $LitCalMetadata = []; private array $LitCalData = []; private array $LitCalFeed = []; private IntlDateFormatter $monthDayFmt; + private string $CalendarURL = ''; public function __construct() { @@ -35,6 +34,7 @@ public function __construct() } else { ini_set('date.timezone', $this->Timezone); } + $this->CalendarURL = self::LITCAL_URL; } private function prepareL10N(): void @@ -72,8 +72,7 @@ private function sendMetadataReq(): void } } else { $response = json_decode($result, true); - $this->SUPPORTED_DIOCESES = $response["LitCalMetadata"]["DiocesanCalendars"]; - $this->SUPPORTED_NATIONS = array_keys($response["LitCalMetadata"]["NationalCalendars"]); + $this->LitCalMetadata = $response["litcal_metadata"]; } } curl_close($ch); @@ -85,32 +84,37 @@ private function prepareReq(): array if ($this->Locale !== null) { $queryArray["locale"] = $this->Locale; } - if ($this->NationalCalendar !== null && in_array($this->NationalCalendar, $this->SUPPORTED_NATIONS)) { - $queryArray["nationalcalendar"] = $this->NationalCalendar; + if ( + $this->NationalCalendar !== null + && in_array($this->NationalCalendar, $this->LitCalMetadata['national_calendars_keys']) + ) { switch ($this->NationalCalendar) { case "ITALY": - $queryArray["locale"] = LitLocale::ITALIAN; $this->Locale = LitLocale::ITALIAN; break; case "USA": - $queryArray["locale"] = LitLocale::ENGLISH; $this->Locale = LitLocale::ENGLISH; break; } + $this->CalendarURL = self::LITCAL_URL . '/' . $this->NationalCalendar; } - if ($this->DiocesanCalendar !== null && array_key_exists($this->DiocesanCalendar, $this->SUPPORTED_DIOCESES)) { - $queryArray["diocesancalendar"] = $this->DiocesanCalendar; - $queryArray["nationalcalendar"] = $this->SUPPORTED_DIOCESES[$this->DiocesanCalendar]["nation"]; - switch ($this->SUPPORTED_DIOCESES[$this->DiocesanCalendar]["nation"]) { + if ( + $this->DiocesanCalendar !== null + && in_array($this->DiocesanCalendar, $this->LitCalMetadata['diocesan_calendars_keys']) + ) { + $calendarInfo = array_filter( + $this->LitCalMetadata['diocesan_calendars'], + fn($diocesanCalendar) => $diocesanCalendar['calendar_id'] === $this->DiocesanCalendar + )[0]; + switch ($calendarInfo["nation"]) { case "ITALY": - $queryArray["locale"] = LitLocale::ITALIAN; $this->Locale = LitLocale::ITALIAN; break; case "USA": - $queryArray["locale"] = LitLocale::ENGLISH; $this->Locale = LitLocale::ENGLISH; break; } + $this->CalendarURL = self::LITCAL_URL . '/' . $this->DiocesanCalendar; } //last resort is Latin for the Universal Calendar @@ -126,7 +130,7 @@ private function sendReq(array $queryArray) //die( json_encode( $queryArray ) ); $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_URL, self::LITCAL_URL); + curl_setopt($ch, CURLOPT_URL, $this->CalendarURL); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($queryArray)); $result = curl_exec($ch); @@ -137,7 +141,14 @@ private function sendReq(array $queryArray) if ($resultStatus != 200) { die("Request failed. HTTP status code: " . $resultStatus); } else { - $this->LitCalData = json_decode($result, true); + $jsonData = json_decode($result, true); + if (JSON_ERROR_NONE !== json_last_error()) { + die("Request failed. Could not decode JSON data."); + } + if (false === array_key_exists('litcal', $jsonData)) { + die("Request failed. Cannot elaborate JSON data."); + } + $this->LitCalData = $jsonData['litcal']; } } curl_close($ch); @@ -149,28 +160,25 @@ private function filterEventsToday() $dateToday = DateTime::createFromFormat('Y-m-d H:i:s', $dateTimeToday, new DateTimeZone('UTC')); $dateTodayTimestamp = intval($dateToday->format("U")); $dateToday->add(new DateInterval('PT10M')); - if (array_key_exists("LitCal", $this->LitCalData)) { - $LitCal = $this->LitCalData["LitCal"]; - $idx = 0; - foreach ($LitCal as $key => $value) { - //file_put_contents( $this->logFile, "Processing litcal event $key..." . "\n", FILE_APPEND ); - if ($value["date"] === $dateTodayTimestamp) { - //file_put_contents( $this->logFile, "Found litcal event $key with timestamp equal to today!" . "\n", FILE_APPEND ); - $publishDate = $dateToday->sub(new DateInterval('PT1M')); - // retransform each entry from an associative array to a Festivity class object - $festivity = new Festivity($value); - $festivity->tag = $key; - $mainText = $this->prepareMainText($festivity, $idx); - //file_put_contents( $this->logFile, "mainText = $mainText" . "\n", FILE_APPEND ); - $titleText = _("Liturgy of the Day") . " "; - if ($this->Locale === LitLocale::ENGLISH) { - $titleText .= $festivity->date->format('F jS'); - } else { - $titleText .= $this->monthDayFmt->format($festivity->date->format('U')); - } - $this->LitCalFeed[] = new LitCalFeedItem($key, $festivity, $publishDate, $titleText, $mainText); - $idx++; + $idx = 0; + foreach ($this->LitCalData as $key => $value) { + //file_put_contents( $this->logFile, "Processing litcal event $key..." . "\n", FILE_APPEND ); + if ($value["date"] === $dateTodayTimestamp) { + //file_put_contents( $this->logFile, "Found litcal event $key with timestamp equal to today!" . "\n", FILE_APPEND ); + $publishDate = $dateToday->sub(new DateInterval('PT1M')); + // retransform each entry from an associative array to a Festivity class object + $festivity = new Festivity($value); + $festivity->tag = $key; + $mainText = $this->prepareMainText($festivity, $idx); + //file_put_contents( $this->logFile, "mainText = $mainText" . "\n", FILE_APPEND ); + $titleText = _("Liturgy of the Day") . " "; + if ($this->Locale === LitLocale::ENGLISH) { + $titleText .= $festivity->date->format('F jS'); + } else { + $titleText .= $this->monthDayFmt->format($festivity->date->format('U')); } + $this->LitCalFeed[] = new LitCalFeedItem($key, $festivity, $publishDate, $titleText, $mainText); + $idx++; } } }