Display current space state on page

This commit is contained in:
Thijs Raymakers 2025-02-27 02:57:28 +01:00
parent 3eee76642f
commit fa52427f66
No known key found for this signature in database
6 changed files with 121 additions and 11 deletions

View file

@ -1,8 +1,8 @@
<?php
require_once("language/language.php");
require_once("space/spacestate.php");
header("Access-Control-Allow-Origin: *");
header("X-Powered-By: Club Mate");
$lang = Language::getPreferredLanguage();
?>

View file

@ -8,13 +8,17 @@
* make sure you update the language strings in both
* - language/nl.php
* - language/en.php
* The $lang->get("KEY") function will automatically select the preferred
* display language, based on the Accept-Language client header.
*
* Thank you for keeping the website accessible for people that
* are not fluent in either English or Dutch!
*
*/
require_once("header.php"); ?>
require_once("header.php");
?>
<html lang="<?= $lang->languageCode ?>">
<head>
<meta charset="UTF-8" />
@ -32,15 +36,24 @@ require_once("header.php"); ?>
</head>
<body>
<header>
<!-- Logo and space state -->
<!-- Logo -->
<img src="/icons/favicon.svg" width="100px" alt="<?= $lang->get("LOGO_ALT_TEXT") ?>" />
<!-- Space state -->
<?php if ($spacestate->open) { ?>
<?= $lang->get('OPEN_SINCE') ?> <?= $spacestate->formattedLastChange() ?> <?= $lang->get('AGO') ?>
<?php } else { ?>
<?= $lang->get('CLOSED') ?>
<?php } ?>
<!-- Title and short introduction -->
<h1>Technologia Incognita</h1>
<?= $lang->get('SHORT_DESCRIPTION_BODY') ?>
<p><?= $lang->get('SHORT_DESCRIPTION_BODY') ?></p>
</header>
<main></main>
<main>
</main>
<footer></footer>
</body>
</html>

View file

@ -7,7 +7,16 @@ class English extends Language {
$this->keys = array(
"SHORT_DESCRIPTION_HEAD" => "TechInc is a hackerspace in Amsterdam.",
"SHORT_DESCRIPTION_BODY" => 'TechInc is a <a href="https://en.wikipedia.org/wiki/Hackerspace">hackerspace</a> in Amsterdam.',
"LOGO_ALT_TEXT" => "Stylized boat's wheel. The handles and spokes have different colors, as if it is decorated with RGB LEDs. At the axle of the wheel is an abstract electronic circuit board."
"LOGO_ALT_TEXT" => "Stylized boat's wheel. The handles and spokes have different colors, as if it is decorated with RGB LEDs. At the axle of the wheel is an abstract electronic circuit board.",
"OPEN_SINCE" => "Open since",
"AGO" => "ago",
"SECOND" => "second",
"SECONDS" => "seconds",
"MINUTE" => "minute",
"MINUTES" => "minutes",
"HOUR" => "hour",
"HOURS" => "hours",
"CLOSED" => "Closed"
);
}
}

View file

@ -1,11 +1,11 @@
<?php
class Language {
private static $obj;
public $languageCode;
protected $keys;
public function __construct($languageCode) {
protected function __construct($languageCode) {
$this->languageCode = $languageCode;
}
@ -14,6 +14,10 @@ class Language {
}
public static function getPreferredLanguage() {
if(isset(self::$obj)) {
return self::$obj;
}
if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$language = "en";
} else {
@ -29,14 +33,18 @@ class Language {
switch ($preferredLanguage) {
case "nl":
return require_once("nl.php");
self::$obj = require_once("nl.php");
break;
case "en":
default:
return require_once("en.php");
self::$obj = require_once("en.php");
break;
}
return self::$obj;
}
}
$lang = Language::getPreferredLanguage();
?>

View file

@ -7,7 +7,16 @@ class Dutch extends Language {
$this->keys = array(
"SHORT_DESCRIPTION_HEAD" => "TechInc is een hackerspace in Amsterdam.",
"SHORT_DESCRIPTION_BODY" => 'TechInc is een <a href="https://nl.wikipedia.org/wiki/Hackerspace">hackerspace</a> in Amsterdam.',
"LOGO_ALT_TEXT" => "Gestileerd stuurwiel van een boot. De handgrepen en spaken hebben verschillende kleuren, alsof het is versierd met RGB LED's. Op de as van het wiel zit een abstracte elektronische printplaat."
"LOGO_ALT_TEXT" => "Gestileerd stuurwiel van een boot. De handgrepen en spaken hebben verschillende kleuren, alsof het is versierd met RGB LED's. Op de as van het wiel zit een abstracte elektronische printplaat.",
"OPEN_SINCE" => "Open sinds",
"AGO" => "geleden",
"SECOND" => "seconde",
"SECONDS" => "seconden",
"MINUTE" => "minuut",
"MINUTES" => "minuten",
"HOUR" => "uur",
"HOURS" => "uur",
"CLOSED" => "Gesloten"
);
}
}

71
space/spacestate.php Normal file
View file

@ -0,0 +1,71 @@
<?php
require_once("language/language.php");
class SpaceState {
private static $obj;
public $open = false;
public $lastchange;
private final function __construct() {
$this->requestJson();
}
public static function get() {
if(!isset(self::$obj)) {
self::$obj = new SpaceState();
}
return self::$obj;
}
public function formattedLastChange() {
global $lang;
$now = time();
$diff = $now - $this->lastchange;
// One second ago
if ($diff == 1) {
return $diff." ".$lang->get("SECOND");
// Less than a minute ago
} else if ($diff < 60) {
return $diff." ".$lang->get("SECONDS");
// One minute ago
} else if ($diff < 120) {
return floor($diff / 60)." ".$lang->get("MINUTE");
// Less than an hour ago
} else if ($diff < 3600) {
return floor($diff / 60)." ".$lang->get("MINUTES");
// One hour ago
} else if ($diff < 7200) {
return floor($diff / 3600)." ".$lang->get("HOUR");
// More than one hour ago
} else {
return floor($diff / 3600)." ".$lang->get("HOURS");
}
}
private function requestJson() {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://techinc.nl/space/spacestate.json",
CURLOPT_TIMEOUT => 1,
CURLOPT_FORBID_REUSE => true,
CURLOPT_RETURNTRANSFER => true
));
$request = curl_exec($ch);
if ($request === false) {
return;
}
$json = json_decode($request);
if (is_null($json)) {
return;
}
$this->open = $json?->open ?? false;
$this->lastchange = $json?->lastchange ?? time();
}
}
$spacestate = SpaceState::get();
?>