Format dates in publiser tool

Add helper functions to display human-friendly Norwegian dates
and times in the publishing dashboard and editor. Add documentation
for the publiser tool and update security checklists.
This commit is contained in:
Ruben 2026-08-30 22:08:02 +02:00
parent b609c8596c
commit 747e730366
3 changed files with 161 additions and 3 deletions

View file

@ -87,6 +87,36 @@ function publiserPublicUrl(string $section, string $folder): string {
return '/' . rawurlencode($section) . '/' . rawurlencode($folder) . '/';
}
/**
* Human-friendly display of a stored publish_at value (e.g. "2026-08-30T14:30",
* the raw <input type=datetime-local> format). Mirrors the day/month/year
* order app/plugins/global/languages.php's formatDate() uses for regular
* post dates, extended with a time-of-day. Only for display - never use
* this for the datetime-local input's own value attribute, which must stay
* in machine format.
*/
function publiserNorwegianDate(int $ts): string {
static $months = ['januar', 'februar', 'mars', 'april', 'mai', 'juni', 'juli', 'august', 'september', 'oktober', 'november', 'desember'];
$day = (int)date('j', $ts);
$month = $months[(int)date('n', $ts) - 1];
return "{$day}. {$month} " . date('Y', $ts);
}
/** Human-friendly display of a date-only value (e.g. "2026-08-25"). Never
* use this for the <input type=date> value attribute, which must stay ISO. */
function publiserFormatDate(?string $raw): string {
if (!$raw) return '';
$ts = strtotime($raw);
return $ts === false ? $raw : publiserNorwegianDate($ts);
}
function publiserFormatScheduleTime(?string $raw): string {
if (!$raw) return '';
$ts = strtotime($raw);
if ($ts === false) return $raw;
return publiserNorwegianDate($ts) . ' kl. ' . date('H:i', $ts);
}
function publiserImageUrl(string $location, string $section, string $folder, string $filename): string {
if ($location === 'live') {
return '/' . rawurlencode($section) . '/' . rawurlencode($folder) . '/' . rawurlencode($filename);
@ -482,9 +512,9 @@ function publiserRenderList(string $csrf): void {
<div class="p-list-info">
<div class="p-list-title"><?= h($item['title']) ?></div>
<div class="p-list-meta">
<?= h($item['date']) ?>
<?= h(publiserFormatDate($item['date'])) ?>
<?php if ($tab === 'scheduled' && $item['publishAt']): ?>
&middot; publiseres <?= h($item['publishAt']) ?>
&middot; publiseres <?= h(publiserFormatScheduleTime($item['publishAt'])) ?>
<?php endif; ?>
</div>
</div>
@ -696,7 +726,7 @@ function publiserRenderEditor(string $csrf): void {
<?php if ($location === 'draft'): ?>
<?php if (($meta['status'] ?? '') === 'scheduled'): ?>
<p class="p-muted">Planlagt: <?= h($meta['publish_at'] ?? '') ?></p>
<p class="p-muted">Planlagt: <?= h(publiserFormatScheduleTime($meta['publish_at'] ?? null)) ?></p>
<button type="submit" name="unschedule" value="1" class="button">Avbryt planlegging</button>
<?php endif; ?>
<label>Planlegg publisering (valgfritt)