Skip to content

Commit

Permalink
set metadata early
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRDOrazio committed Nov 2, 2024
1 parent 8e10ac0 commit 598efad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
5 changes: 2 additions & 3 deletions php/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
$directAccess = (basename(__FILE__) === basename($_SERVER['SCRIPT_FILENAME']));

$baseLocale = Locale::getPrimaryLanguage(setlocale(LC_ALL, 0));
$litSettings = new LitSettings($_GET, $directAccess);
$Metadata = Utilities::retrieveMetadata();
$litSettings = new LitSettings($_GET, $directAccess, $Metadata);

// debug value of expected textdomain path
echo '<!-- expected textdomain path: ' . $litSettings->expectedTextDomainPath . ' -->';
Expand All @@ -39,9 +40,7 @@
$nationalCalendarOptions = '<option value="">---</option>';
$diocesanCalendarOptions = '<option value="">---</option>';

$Metadata = Utilities::retrieveMetadata();
if ($Metadata !== null) {
$litSettings->setMetadata($Metadata, $stagingURL);
$nationalCalendarOptions = Utilities::buildNationOptions($Metadata["national_calendars_keys"], $litSettings->NationalCalendar, $litSettings->Locale);
[$diocesanCalendarOptions, $diocesesCount] = Utilities::buildDioceseOptions($Metadata, $litSettings->NationalCalendar, $litSettings->DiocesanCalendar);
} else {
Expand Down
14 changes: 8 additions & 6 deletions php/src/LitSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class LitSettings
* @param array $DATA An array of input parameters to initialize the settings.
* @param bool $directAccess A flag indicating if the access is direct, default is false.
*/
public function __construct(array $DATA, bool $directAccess = false)
public function __construct(array $DATA, bool $directAccess = false, array|null $Metadata = null)
{
// set default year value
$this->Year = (int)date("Y");
Expand All @@ -76,6 +76,8 @@ public function __construct(array $DATA, bool $directAccess = false)
$this->currentTextDomainPath = bindtextdomain("litexmplphp", $this->expectedTextDomainPath);
$this->directAccess = $directAccess;

$this->setMetadata($Metadata);

$this->setVars($DATA);
}

Expand Down Expand Up @@ -169,9 +171,8 @@ private function isValidDiocesanCalendar($value)
* NationalCalendarMetadata array.
* If the directAccess flag is set to true, the function also sets the locale for the current PHP script using the
* setlocale() function, and sets a cookie to store the current locale.
* @param string $stagingURL the URL of the staging site (used to set the domain of the cookie)
*/
private function updateSettingsByNation(string $stagingURL)
private function updateSettingsByNation()
{
$NationalCalendarMetadata = null;
if ($this->NationalCalendar !== null && $this->NationalCalendar !== "VA") {
Expand Down Expand Up @@ -207,6 +208,8 @@ private function updateSettingsByNation(string $stagingURL)
];
setlocale(LC_ALL, $localeArray);
if (!isset($_COOKIE["currentLocale"]) || $_COOKIE["currentLocale"] !== $this->Locale) {
$isStaging = ( strpos($_SERVER['HTTP_HOST'], "-staging") !== false );
$stagingURL = $isStaging ? "-staging" : "";
setcookie(
"currentLocale", //name
$this->Locale, //value
Expand All @@ -224,15 +227,14 @@ private function updateSettingsByNation(string $stagingURL)
* Updates the internal metadata reference and then updates the settings based on the selected nation.
*
* @param array $Metadata A list of metadata about the diocesan calendars available.
* @param string $stagingURL If the current page is a staging site, this will contain the identifier of the staging site such as "-staging".
* @return void
*/
public function setMetadata(array $Metadata, string $stagingURL)
public function setMetadata(array $Metadata)
{
$this->Metadata = $Metadata;
if ($this->DiocesanCalendar !== null) {
$this->NationalCalendar = array_values(array_filter($this->Metadata["diocesan_calendars"], fn ($calendar) => $calendar["calendar_id"] === $this->DiocesanCalendar))[0]["nation"];
}
$this->updateSettingsByNation($stagingURL);
$this->updateSettingsByNation();
}
}

0 comments on commit 598efad

Please sign in to comment.