Skip to content

Commit

Permalink
create AllFestivities and Nat.l_Reg.l data scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRDOrazio committed Feb 9, 2022
1 parent 15f16c3 commit 5b27417
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 0 deletions.
53 changes: 53 additions & 0 deletions LitCalAllFestivities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

include_once( 'includes/enums/LitLocale.php' );
include_once( 'includes/enums/RomanMissal.php' );

$requestHeaders = getallheaders();
if( isset( $requestHeaders[ "Origin" ] ) ) {
header( "Access-Control-Allow-Origin: {$requestHeaders[ "Origin" ]}" );
header( 'Access-Control-Allow-Credentials: true' );
}
else {
header( 'Access-Control-Allow-Origin: *' );
}
header( 'Access-Control-Max-Age: 86400' ); // cache for 1 day
header( 'Cache-Control: must-revalidate, max-age=259200' );
header( 'Content-Type: application/json' );

$FestivityCollection = [];

$LatinMissals = array_filter( RomanMissal::$values, function($item){
return str_starts_with( $item, "VATICAN_" );
});

$LOCALE = isset( $_GET["locale"] ) && LitLocale::isValid( strtoupper( $_GET["locale"] ) ) ? strtoupper( $_GET["locale"] ) : "LA";

foreach( $LatinMissals as $LatinMissal ) {
$DataFile = RomanMissal::getSanctoraleFileName( $LatinMissal );
if( $DataFile !== false ) {
$I18nPath = RomanMissal::getSanctoraleI18nFilePath( $LatinMissal );
if( $I18nPath !== false && file_exists( $I18nPath . "/" . strtolower( $LOCALE ) . ".json" ) ) {
$NAME = json_decode( file_get_contents( $I18nPath . "/" . strtolower( $LOCALE ) . ".json" ), true );
$DATA = json_decode( file_get_contents( $DataFile ), true );
foreach( $DATA as $idx => $festivity ) {
$key = $festivity[ "TAG" ];
$FestivityCollection[ $key ] = $festivity;
$FestivityCollection[ $key ][ "NAME" ] = $NAME[ $key ];
}
}
}
}

$responseObj = [ "LitCalAllFestivities" => $FestivityCollection ];

$response = json_encode( $responseObj );
$responseHash = md5( $response );
header("Etag: \"{$responseHash}\"");
if (!empty( $_SERVER['HTTP_IF_NONE_MATCH'] ) && $_SERVER['HTTP_IF_NONE_MATCH'] === $responseHash) {
header( $_SERVER[ "SERVER_PROTOCOL" ] . " 304 Not Modified" );
header('Content-Length: 0');
} else {
echo $response;
}
die();
184 changes: 184 additions & 0 deletions LitCalNationalAndRegionalData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php
error_reporting( E_ALL );
ini_set( 'display_errors', 1 );

include_once( 'includes/enums/AcceptHeader.php' );
include_once( 'includes/enums/RequestMethod.php' );
include_once( 'includes/enums/RequestContentType.php' );
include_once( 'includes/enums/ReturnType.php' );
include_once( 'includes/APICore.php' );

$allowedOrigins = [
"https://johnromanodorazio.com",
"https://www.johnromanodorazio.com",
"https://litcal.johnromanodorazio.com",
"https://litcal-staging.johnromanodorazio.com"
];

$LitCalDiocesanData = new LitCalNationalData();

$LitCalDiocesanData->APICore->setAllowedOrigins( $allowedOrigins );
$LitCalDiocesanData->APICore->setAllowedReferers( array_map( function($el){ return $el . "/"; }, $allowedOrigins ) );

$LitCalDiocesanData->APICore->setAllowedAcceptHeaders( [ AcceptHeader::JSON ] );
$LitCalDiocesanData->APICore->setAllowedRequestContentTypes( [ RequestContentType::JSON, RequestContentType::FORMDATA ] );
$LitCalDiocesanData->Init();

class LitCalNationalData {

private object $DATA;
private object $RESPONSE;

public APICore $APICore;

public function __construct(){
$this->APICore = new APICore();
$this->RESPONSE = new stdClass();
$this->RESPONSE->requestHeadersReceived = $this->APICore->getJsonEncodedRequestHeaders();
}

private function handleGetPostRequests( array $REQUEST ) {
$this->APICore->validateAcceptHeader( true );
if( $this->APICore->getRequestContentType() === 'application/json' ) {
$this->DATA = $this->APICore->retrieveRequestParamsFromJsonBody();
} else {
$this->DATA = (object)$REQUEST;
}
$this->retrieveNationalCalendar();
}

private function handlePutPatchDeleteRequests( string $requestMethod ) {
$this->APICore->validateAcceptHeader( false );
$this->APICore->enforceAjaxRequest();
$this->APICore->enforceReferer();
if( $this->APICore->getRequestContentType() === 'application/json' ) {
$this->DATA = $this->APICore->retrieveRequestParamsFromJsonBody();
if( RequestMethod::PUT === $requestMethod ) {
$this->writeNationalCalendar();
} elseif( RequestMethod::DELETE === $requestMethod ) {
$this->deleteNationalCalendar();
}
} else{
header( $_SERVER[ "SERVER_PROTOCOL" ]." 415 Unsupported Media Type", true, 415 );
die( '{"error":"You seem to be forming a strange kind of request? Only \'application/json\' is allowed as the Content Type for the body of the Request when using Request Methods PUT, PATCH, or DELETE: the Content Type for the body of your Request was '.$_SERVER[ 'CONTENT_TYPE' ].' and you are using Request Method ' . $_SERVER[ 'REQUEST_METHOD' ] . '"}' );
}
}

private function handleRequestedMethod() {
switch( strtoupper( $_SERVER[ "REQUEST_METHOD" ] ) ) {
case RequestMethod::GET:
$this->handleGetPostRequests( $_GET );
break;
case RequestMethod::POST:
$this->handleGetPostRequests( $_POST );
break;
case RequestMethod::PUT:
case RequestMethod::PATCH:
$this->handlePutPatchDeleteRequests( RequestMethod::PUT );
break;
case RequestMethod::DELETE:
$this->handlePutPatchDeleteRequests( RequestMethod::DELETE );
break;
case RequestMethod::OPTIONS:
//continue;
break;
default:
header( $_SERVER[ "SERVER_PROTOCOL" ]." 405 Method Not Allowed", true, 405 );
$errorMessage = '{"error":"You seem to be forming a strange kind of request? Allowed Request Methods are ';
$errorMessage .= implode( ' and ', $this->AllowedRequestMethods );
$errorMessage .= ', but your Request Method was ' . strtoupper( $_SERVER[ 'REQUEST_METHOD' ] ) . '"}';
die( $errorMessage );
}
}

private function retrieveNationalCalendar() {
if( property_exists( $this->DATA, 'category' ) ) {
$category = $this->DATA->category; //nationalCalendar or widerRegionCalendar
if( property_exists( $this->DATA, 'key' ) ) {
$key = $this->DATA->key;
if( $category === "widerRegionCalendar" ) {
$calendarDataFile = "nations/{$key}.json";
}
else if( $category === "nationalCalendar" ) {
$calendarDataFile = "nations/{$key}/{$key}.json";
}
if( file_exists( $calendarDataFile ) ) {
$response = json_decode( file_get_contents( $calendarDataFile ) );
$uKey = strtoupper( $key );
if( $category === "widerRegionCalendar" ) {
$response->isMultilingual = is_dir( "nations/{$uKey}" );
$locale = strtolower( $this->DATA->locale );
if( file_exists( "nations/{$uKey}/{$locale}.json" ) ) {
$localeData = json_decode( file_get_contents( "nations/{$uKey}/{$locale}.json" ) );
foreach( $response->LitCal as $idx => $el ) {
$response->LitCal[$idx]->Festivity->name = $localeData->{$response->LitCal[$idx]->Festivity->tag};
}
}
}
$responseStr = json_encode( $response );
echo $responseStr;
die();
}
}
}
}

private function writeNationalCalendar() {
if( !property_exists( $this->DATA, 'calendar' ) || !property_exists( $this->DATA, 'diocese' ) || !property_exists( $this->DATA, 'nation' ) ) {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 400 Bad request", true, 400 );
die( '{"error":"Required parameters were not received"}' );
} else {
$this->RESPONSE->Nation = strip_tags( $this->DATA->nation );
$this->RESPONSE->Diocese = strip_tags( $this->DATA->diocese );
$CalData = json_decode( $this->DATA->calendar );
if( json_last_error() !== JSON_ERROR_NONE ) {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 400 Bad request", true, 400 );
die( '{"error":"Malformed data received in <calendar> parameters"}' );
}
if( property_exists( $this->DATA, 'overrides' ) ) {
$CalData->Overrides = $this->DATA->overrides;
}
$this->RESPONSE->Calendar = json_encode( $CalData );
if( property_exists( $this->DATA, 'group' ) ) {
$this->RESPONSE->Group = strip_tags( $this->DATA->group );
}
$path = "nations/{$this->RESPONSE->Nation}";
if( !file_exists( $path ) ){
mkdir( $path, 0755, true );
}

file_put_contents( $path . "/{$this->RESPONSE->Diocese}.json", $this->RESPONSE->Calendar . PHP_EOL );

//$this->createOrUpdateIndex( $path );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 201 Created", true, 201 );
die( '{"success":"Diocesan calendar created or updated for diocese \"'. $this->RESPONSE->Diocese .'\""}' );

}
}

private function deleteNationalCalendar() {
if( !property_exists( $this->DATA, 'calendar' ) || !property_exists( $this->DATA, 'diocese' ) || !property_exists( $this->DATA, 'nation' ) ) {
header( $_SERVER[ "SERVER_PROTOCOL" ]." 400 Bad request", true, 400 );
die( '{"error":"Required parameters were not received"}' );
} else {
$this->RESPONSE->Nation = strip_tags( $this->DATA->nation );
$this->RESPONSE->Diocese = strip_tags( $this->DATA->diocese );
$path = "nations/{$this->RESPONSE->Nation}";
if( file_exists( $path . "/{$this->RESPONSE->Diocese}.json" ) ){
unlink($path . "/{$this->RESPONSE->Diocese}.json");
}

//$this->createOrUpdateIndex( $path, true );
header( $_SERVER[ "SERVER_PROTOCOL" ]." 200 OK", true, 200 );
die( '{"success":"Diocesan calendar deleted for diocese \"'. $this->RESPONSE->Diocese .'\""}' );

}
}

public function Init() {
$this->APICore->Init();
$this->APICore->setResponseContentTypeHeader();
$this->handleRequestedMethod();
}

}

0 comments on commit 5b27417

Please sign in to comment.