Skip to content

Commit ba5e8a2

Browse files
committed
fix admin UI
* use newer CalendarSelect syntax * detect localhost vs production and make requests accordingly * update test assertion expected_value to new RFC 3339 type * fix lit events rebuilding
1 parent 593d8b7 commit ba5e8a2

File tree

9 files changed

+195
-103
lines changed

9 files changed

+195
-103
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ vendor/
22
.env
33
.env.*
44
!.env.example
5+
logs/
6+
*.log
7+
credentials.php

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"phpcs.autoConfigSearch": true,
3+
"phpcs.enable": true,
4+
"phpcs.executablePath": "./vendor/bin/phpcs",
5+
"phpcs.ignorePatterns": [
6+
"**/vendor/*"
7+
]
8+
}

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
}
3535
},
3636
{
37-
"label": "php-server",
37+
"label": "litcal-tests-webui",
3838
"type": "shell",
3939
"command": "php -S ${APP_HOST}:${APP_PORT}",
4040
"group": {

admin.php

Lines changed: 95 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<?php
2-
error_reporting(E_ALL);
3-
ini_set("display_errors", 1);
2+
3+
ini_set('date.timezone', 'Europe/Vatican');
4+
45
require_once 'vendor/autoload.php';
56
require_once 'includes/auth.php';
67

78
use LiturgicalCalendar\Components\CalendarSelect;
9+
use LiturgicalCalendar\Components\CalendarSelect\OptionsType;
10+
use Dotenv\Dotenv;
811

912
if (!authenticated()) {
1013
header("WWW-Authenticate: Basic realm=\"Please insert your credentials\"");
@@ -13,11 +16,63 @@
1316
die();
1417
}
1518

16-
$apiVersion = isset($_GET['apiVersion']) ? $_GET['apiVersion'] : 'dev';
19+
/**
20+
* Returns true if the server is running on localhost.
21+
*
22+
* @return bool true if the server is running on localhost, false otherwise
23+
*/
24+
function isLocalhost(): bool
25+
{
26+
$serverAddress = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '';
27+
$remoteAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
28+
$serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
29+
$localhostAddresses = ['127.0.0.1', '::1', '0.0.0.0'];
30+
$localhostNames = ['localhost', ...$localhostAddresses];
31+
return in_array($serverAddress, $localhostAddresses) ||
32+
in_array($remoteAddress, $localhostAddresses) ||
33+
in_array($serverName, $localhostNames);
34+
}
1735

18-
$ch = curl_init();
36+
$dotenv = Dotenv::createMutable($projectFolder, ['.env', '.env.local', '.env.development', '.env.production'], false);
37+
$dotenv->safeLoad();
38+
39+
$dotenv->ifPresent(['API_PROTOCOL', 'API_HOST'])->notEmpty();
40+
$dotenv->ifPresent(['API_PROTOCOL'])->allowedValues(['http', 'https']);
41+
$dotenv->ifPresent(['API_PORT'])->isInteger();
42+
$dotenv->ifPresent(['APP_ENV'])->notEmpty()->allowedValues(['development', 'production']);
43+
44+
$logsFolder = 'logs';
45+
if (!file_exists($logsFolder)) {
46+
if (!mkdir($logsFolder, 0755, true)) {
47+
throw new RuntimeException('Failed to create logs directory: ' . $logsFolder);
48+
}
49+
}
50+
$logFile = $logsFolder . DIRECTORY_SEPARATOR . 'litcaltests-error.log';
51+
52+
if (
53+
isLocalhost() || ( isset($_ENV['APP_ENV']) && $_ENV['APP_ENV'] === 'development' )
54+
) {
55+
ini_set('display_errors', 1);
56+
ini_set('display_startup_errors', 1);
57+
ini_set('log_errors', 1);
58+
ini_set('error_log', $logFile);
59+
error_reporting(E_ALL);
60+
} else {
61+
ini_set('display_errors', 0);
62+
ini_set('display_startup_errors', 0);
63+
ini_set('log_errors', 1);
64+
ini_set('error_log', $logFile);
65+
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
66+
}
1967

20-
curl_setopt($ch, CURLOPT_URL, "https://litcal.johnromanodorazio.com/api/$apiVersion/tests");
68+
$schema = isset($_ENV['API_PROTOCOL']) ? $_ENV['API_PROTOCOL'] : (isLocalhost() ? 'http' : 'https');
69+
$host = isset($_ENV['API_HOST']) ? $_ENV['API_HOST'] : (isLocalhost() ? 'localhost' : 'litcal.johnromanodorazio.com');
70+
$port = isset($_ENV['API_PORT']) ? (int) $_ENV['API_PORT'] : (isLocalhost() ? 8000 : 443);
71+
$apiVersion = isset($_GET['apiVersion']) ? $_GET['apiVersion'] : 'dev';
72+
$ch = curl_init();
73+
$baseUrl = isLocalhost() ? "$schema://$host:$port" : "$schema://$host/api/$apiVersion";
74+
75+
curl_setopt($ch, CURLOPT_URL, "$baseUrl/tests");
2176
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2277
$response = curl_exec($ch);
2378
if (curl_errno($ch)) {
@@ -26,11 +81,8 @@
2681
die($error_msg);
2782
}
2883
curl_close($ch);
29-
if ($apiVersion === 'dev' || $apiVersion === 'v4') {
30-
$LitCalTests = json_decode($response)->litcal_tests;
31-
} else {
32-
$LitCalTests = json_decode($response);
33-
}
84+
85+
$LitCalTests = json_decode($response)->litcal_tests;
3486

3587
include_once 'layout/head.php';
3688
include_once 'layout/sidebar.php';
@@ -57,16 +109,15 @@
57109
<div class="row justify-content-center align-items-start">
58110
<div class="form-group col col-md-3 border border-top-0 border-bottom-0 border-end-0 border-secondary">
59111
<?php
60-
$options = ['locale' => $i18n->locale];
61-
$CalendarSelect = new CalendarSelect($options);
62-
echo $CalendarSelect->getSelect([
63-
'class' => 'form-select',
64-
'id' => 'APICalendarSelect',
65-
'options' => 'all',
66-
'label' => true,
67-
'labelStr' => _('Calendar to test')
68-
]);
69-
?>
112+
$options = ['locale' => $i18n->locale, 'url' => $baseUrl];
113+
$CalendarSelect = new CalendarSelect($options)
114+
->class('form-select')
115+
->id('APICalendarSelect')
116+
->label(true)
117+
->labelText(_('Calendar to test'))
118+
->setOptions(OptionsType::ALL);
119+
echo $CalendarSelect->getSelect();
120+
?>
70121
</div>
71122
<div class="form-group col col-md-9">
72123
<label><?php echo _("Test Type"); ?></label>
@@ -127,23 +178,21 @@
127178
</main>
128179
<!-- End of Main Content -->
129180
<?php
130-
if ($apiVersion === 'dev' || $apiVersion === 'v4') {
131-
$eventsEndpoint = "https://litcal.johnromanodorazio.com/api/dev/events";
132-
$ch = curl_init();
133-
curl_setopt($ch, CURLOPT_URL, $eventsEndpoint);
134-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
135-
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Accept-Language: " . $i18n->locale]);
136-
$eventsRaw = curl_exec($ch);
137-
if (curl_errno($ch)) {
138-
die('Could not fetch data from ' . $eventsEndpoint);
139-
}
140-
[ "litcal_events" => $LitCalAllLitEvents ] = json_decode(
141-
$eventsRaw,
142-
true
143-
);
144-
if (JSON_ERROR_NONE !== json_last_error()) {
145-
die('Could not parse JSON from ' . $eventsEndpoint . ' : ' . json_last_error_msg());
146-
}
181+
$eventsEndpoint = "$baseUrl/events";
182+
$ch = curl_init();
183+
curl_setopt($ch, CURLOPT_URL, $eventsEndpoint);
184+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
185+
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Accept-Language: " . $i18n->locale]);
186+
$eventsRaw = curl_exec($ch);
187+
if (curl_errno($ch)) {
188+
die('Could not fetch data from ' . $eventsEndpoint);
189+
}
190+
[ "litcal_events" => $LitCalAllLitEvents ] = json_decode(
191+
$eventsRaw,
192+
true
193+
);
194+
if (JSON_ERROR_NONE !== json_last_error()) {
195+
die('Could not parse JSON from ' . $eventsEndpoint . ' : ' . json_last_error_msg());
147196
}
148197
include_once 'components/NewTestModal.php';
149198
?>
@@ -181,8 +230,15 @@ class="bi bi-exclamation-triangle-fill flex-shrink-0 me-2" viewBox="0 0 16 16"
181230
<div id="responseMessage"></div>
182231
</div>
183232
<?php
184-
$testsIndex = json_encode($LitCalTests);
233+
$testsIndex = json_encode($LitCalTests);
185234
$litcal_events = json_encode($LitCalAllLitEvents);
186-
echo "<script>const LitCalTests = Object.freeze($testsIndex); const LitcalEvents = Object.freeze($litcal_events);</script>";
235+
$javascript = <<<SCRIPT
236+
<script>
237+
const LitCalTests = Object.freeze($testsIndex);
238+
const LitcalEvents = Object.freeze($litcal_events);
239+
const baseUrl = '$baseUrl';
240+
</script>
241+
SCRIPT;
242+
echo $javascript;
187243
include_once 'layout/footer.php';
188244
?>

assets/js/AssertionsBuilder.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const TestType = Object.freeze({
1818
*/
1919
const AssertType = Object.freeze({
2020
EventNotExists: 'eventNotExists',
21-
EventTypeExact: 'eventExists AND hasExpectedTimestamp'
21+
EventTypeExact: 'eventExists AND hasExpectedDate'
2222
});
2323

2424
/**
@@ -98,9 +98,8 @@ class AssertionsBuilder {
9898
AssertionsBuilder.yearSince = test.year_since;
9999
}
100100
AssertionsBuilder.test = litcal_events.filter(el => el.event_key === test.event_key)[0] ?? null;
101-
console.log( 'new instance of AssertionsBuilder, test = ');
102-
console.log(AssertionsBuilder.test);
103-
console.log(litcal_events);
101+
console.log('new instance of AssertionsBuilder, test = ', AssertionsBuilder.test);
102+
console.log('litcal_events = ', litcal_events);
104103
}
105104

106105
/**
@@ -147,18 +146,17 @@ class AssertionsBuilder {
147146
* @returns {jQuery} the built html as a jquery object
148147
*/
149148
buildHtml() {
150-
console.log(`building html for AssertionsBuilder with test = ${AssertionsBuilder.test}`);
149+
console.log('building html for AssertionsBuilder with test = ', AssertionsBuilder.test);
151150
let assertionBuildStr = '';
152151
//console.log(this.assertions);
153152
this.assertions.forEach( (assertion, idy) => {
154153
AssertionsBuilder.#setColors( assertion );
155154
//console.log(assertion);
156-
const expectedDateStr = assertion.expected_value !== null ? DTFormat.format(assertion.expected_value * 1000) : '---';
155+
const expectedDateStr = assertion.expected_value !== null ? assertion.expected_value : '---';
157156
const commentStr = commentIcon( assertion.hasOwnProperty('comment'), assertion?.comment);
158157
let sundayCheck = '';
159158
if(AssertionsBuilder.test.grade <= LitGrade.FEAST && AssertionsBuilder.test.month && AssertionsBuilder.test.day) {
160-
const eventDate = new Date(assertion.expected_value*1000);
161-
//console.log( DTFormat.format(assertion.expected_value*1000) );
159+
const eventDate = new Date(assertion.expected_value);
162160
if( eventDate.getUTCDay() === 0 ) {
163161
//console.log('this day is a Sunday!');
164162
sundayCheck = 'bg-warning text-dark';

0 commit comments

Comments
 (0)