vlw.se/public/work/timeline.php

184 lines
No EOL
6.1 KiB
PHP

<?php
use Vegvisir\Path;
use Reflect\Response;
use VLW\Client\API;
use VLW\API\Endpoints;
use VLW\API\Databases\VLWdb\Models\Work\{
WorkModel,
WorkTagsModel,
WorkActionsModel
};
require_once VV::root("src/client/API.php");
require_once VV::root("api/src/Endpoints.php");
require_once VV::root("api/src/databases/models/Work/Work.php");
require_once VV::root("api/src/databases/models/Work/WorkTags.php");
require_once VV::root("api/src/databases/models/Work/WorkActions.php");
$work = new class extends API {
private const API_PARAM_LIMIT = "limit";
private readonly Response $resp;
private readonly Response $tags;
private readonly Response $actions;
public function __construct() {
parent::__construct();
$this->resp = $this->call(Endpoints::WORK->value)->params([
WorkModel::IS_LISTED->value => true,
self::API_PARAM_LIMIT => $_GET[self::API_PARAM_LIMIT] ?? null
])->get();
// Fetch metadata for work items if we got an ok from work endpoint
if ($this->resp->ok) {
$this->tags = $this->call(Endpoints::WORK_TAGS->value)->get();
$this->actions = $this->call(Endpoints::WORK_ACTIONS->value)->get();
}
}
/*
Order response from endpoint into a multi-dimensional array.
For example, a single item created at 14th of February 2024 would be ordered like this
[2024 => [[02 => [14 => [<row_data>]]]]]
*/
public function get_timeline(): array {
if (!$this->resp->ok) {
return [];
}
$timeline = [];
// Create array of arrays ordered by decending year, month, day, items
foreach ($this->resp->json() as $row) {
// Create array for current year if it doesn't exist
if (!array_key_exists($row[WorkModel::DATE_YEAR->value], $timeline)) {
$timeline[$row[WorkModel::DATE_YEAR->value]] = [];
}
// Create array for current month if it doesn't exist
if (!array_key_exists($row[WorkModel::DATE_MONTH->value], $timeline[$row[WorkModel::DATE_YEAR->value]])) {
$timeline[$row[WorkModel::DATE_YEAR->value]][$row[WorkModel::DATE_MONTH->value]] = [];
}
// Create array for current day if it doesn't exist
if (!array_key_exists($row[WorkModel::DATE_DAY->value], $timeline[$row[WorkModel::DATE_YEAR->value]][$row[WorkModel::DATE_MONTH->value]])) {
$timeline[$row[WorkModel::DATE_YEAR->value]][$row[WorkModel::DATE_MONTH->value]][$row[WorkModel::DATE_DAY->value]] = [];
}
// Append item to ordered array
$timeline[$row[WorkModel::DATE_YEAR->value]][$row[WorkModel::DATE_MONTH->value]][$row[WorkModel::DATE_DAY->value]][] = $row;
}
return $timeline;
}
public function get_tags(string $key): array {
if (!$this->resp->ok) {
return [];
}
return in_array($key, $this->tags->json()) ? $this->tags->json()[$key] : [];
}
public function get_actions(string $key): array {
if (!$this->resp->ok) {
return [];
}
return array_key_exists($key, $this->actions->json()) ? $this->actions->json()[$key] : [];
}
}
?>
<style><?= VV::css("public/assets/css/pages/work/timeline") ?></style>
<section class="git">
<?= VV::embed("public/assets/media/icons/codeberg.svg") ?>
<p>This timeline has most but not all of my FOSS software. If you want to see a list of all things I've created for the free software world, check out my repos on Codeberg or Forgejo.</p>
<div class="buttons">
<a href="https://codeberg.org/vlw"><button class="inline solid">Codeberg</button></a>
<a href="https://git.vlw.se"><button class="inline">Forgejo</button></a>
</div>
</section>
<section class="timeline">
<?php // Get year int from key and array of months for current year ?>
<?php foreach ($work->get_timeline() as $year => $months): ?>
<div class="year">
<div class="track">
<p><?= $year ?></p>
</div>
<div class="months">
<?php // Get month int from key and array of days for current month ?>
<?php foreach ($months as $month => $days): ?>
<div class="month">
<div class="track">
<?php // Append leading zero to month ?>
<p><?= sprintf("%02d", $month) ?></p>
</div>
<div class="days">
<?php // Get day int from key and array of items for current day ?>
<?php foreach ($days as $day => $items): ?>
<div class="day">
<div class="track">
<?php // Append leading zero to day ?>
<p><?= sprintf("%02d", $day) ?></p>
</div>
<div class="items">
<?php foreach ($items as $item): ?>
<div class="item">
<?php // List tags if available ?>
<?php if ($work->get_tags($item[WorkModel::ID->value])): ?>
<div class="tags">
<?php foreach ($work->get_tags($item[WorkModel::ID->value]) as $tag): ?>
<p class="tag <?= $tag[WorkTagsModel::NAME->value] ?>"><?= $tag[WorkTagsModel::NAME->value] ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php // Show large heading if defined ?>
<?php if (!empty($item[WorkModel::TITLE->value])): ?>
<h2><?= $item[WorkModel::TITLE->value] ?></h2>
<?php endif; ?>
<p><?= $item[WorkModel::SUMMARY->value] ?></p>
<div class="actions">
<?php if ($work->get_actions($item[WorkModel::ID->value])): ?>
<?php // Display each action button ?>
<?php foreach ($work->get_actions($item[WorkModel::ID->value]) as $action): ?>
<a href="<?= $action[WorkActionsModel::HREF->value] ?>"><button class="inline <?= $action[WorkActionsModel::CLASS_LIST->value] ?>"><?= $action[WorkActionsModel::DISPLAY_TEXT->value] ?></button></a>
<?php endforeach; ?>
<?php // Display a link to namespaced page on vlw.se if no action is defined ?>
<?php else: ?>
<a href="<?= $item[WorkModel::ID->value] ?>"><button class="inline">read more</button></a>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</section>
<script><?= VV::js("assets/js/pages/work/timeline") ?></script>