Extract status-badge.php partial; replace inline badge markup in index.php and account.php

Add templates/partials/status-badge.php — a single reusable partial that
renders the <span class="status-badge …"> element for three badge types:

  'publish'  — Publié / En attente derived from a boolean is_published value
  'access'   — access-type label (Libre / Interne / Interdit) with slug-based
               CSS modifier class and appropriate symbol (○ ◑ ●)
  'ok'       — generic green/yellow boolean badge with caller-supplied labels
               (used for 'Active'/'Non configurée' and 'Présent'/'Absent' in
               account.php)

All three variants emit aria-label with a context prefix and wrap the
decorative symbol in aria-hidden="true" — behaviour identical to the
inline code they replace.

Callers set $badgeType + $badgeValue (+ optional $badgeOkLabel /
$badgeWarnLabel / $badgeContext) before the include; the partial unsets
all working variables after rendering so they do not bleed into the
including scope.

Files changed:
  templates/partials/status-badge.php  — new partial
  public/admin/index.php               — table status column now uses partial
                                         (removes 15 lines of inline if/else/php)
  public/admin/account.php             — two credential status rows now use partial
                                         (removes 8 lines of inline if/else)
This commit is contained in:
Pontoporeia
2026-04-02 12:50:46 +02:00
parent 2143869b1e
commit 77bfd2f8e3
4 changed files with 72 additions and 24 deletions

View File

@@ -44,7 +44,7 @@ PHP has no component system, but `include`/`require` with variable scoping works
### Shared UI partials — `templates/partials/`
- [x] **`pagination.php`** — pagination nav is duplicated between `index.php` and `search.php` with minor variations; unify into one partial accepting `$page`, `$totalPages`, `$baseParams[]`
- [ ] **`status-badge.php`** — the published/pending badge + access badge pattern is repeated in `index.php` admin table rows; extract into a partial
- [x] **`status-badge.php`** — the published/pending badge + access badge pattern is repeated in `index.php` admin table rows; extract into a partial
- [ ] **`admin-alert.php`** — rename/merge `flash-messages.php` to also handle the 3 different legacy flash key patterns (`$_SESSION['error']`, `$_SESSION['admin_error']`, `$_SESSION['edit_error']`, etc.) that pages still consume manually instead of via `App::consumeFlash()`
## System Page Caching — Database-Backed Status Cache

View File

@@ -33,20 +33,12 @@ if (empty($_SESSION['csrf_token'])) {
<div class="admin-account-status">
<div class="admin-account-status__row">
<span class="admin-account-status__label">Authentification PHP</span>
<?php if ($hasPassword): ?>
<span class="status-badge status-published">Active</span>
<?php else: ?>
<span class="status-badge status-pending">Non configurée</span>
<?php endif; ?>
<?php $badgeType = 'ok'; $badgeValue = $hasPassword; $badgeOkLabel = 'Active'; $badgeWarnLabel = 'Non configurée'; include APP_ROOT . '/templates/partials/status-badge.php'; ?>
</div>
<div class="admin-account-status__row">
<span class="admin-account-status__label">Fichier de configuration</span>
<code class="admin-account-status__code">config/admin_credentials.php</code>
<?php if (file_exists($credentialsFile)): ?>
<span class="status-badge status-published">Présent</span>
<?php else: ?>
<span class="status-badge status-pending">Absent</span>
<?php endif; ?>
<?php $badgeType = 'ok'; $badgeValue = file_exists($credentialsFile); $badgeOkLabel = 'Présent'; $badgeWarnLabel = 'Absent'; include APP_ROOT . '/templates/partials/status-badge.php'; ?>
</div>
<?php if (!$hasPassword): ?>
<p class="admin-account-status__note">

View File

@@ -187,20 +187,9 @@ document.addEventListener('DOMContentLoaded', () => {
<td><?= htmlspecialchars($thesis['orientation'] ?? 'N/A') ?></td>
<td><?= htmlspecialchars($thesis['ap_program'] ?? 'N/A') ?></td>
<td>
<?php if ($thesis['is_published']): ?>
<span class="status-badge status-published" aria-label="Statut : Publié"><span aria-hidden="true">● </span>Publié</span>
<?php else: ?>
<span class="status-badge status-pending" aria-label="Statut : En attente"><span aria-hidden="true">◌ </span>En attente</span>
<?php endif; ?>
<?php $badgeType = 'publish'; $badgeValue = $thesis['is_published']; include APP_ROOT . '/templates/partials/status-badge.php'; ?>
<?php if (!empty($thesis['access_type'])): ?>
<?php
$accessSlug = strtolower(preg_replace('/[^a-z]/i', '', $thesis['access_type']));
$accessSymbols = ['libre' => '○', 'interne' => '◑', 'interdit' => '●'];
$accessSymbol = $accessSymbols[$accessSlug] ?? '●';
?>
<br><span class="status-badge status-access status-access--<?= $accessSlug ?>" aria-label="Accès : <?= htmlspecialchars($thesis['access_type']) ?>">
<span aria-hidden="true"><?= $accessSymbol ?> </span><?= htmlspecialchars($thesis['access_type']) ?>
</span>
<br><?php $badgeType = 'access'; $badgeValue = $thesis['access_type']; include APP_ROOT . '/templates/partials/status-badge.php'; ?>
<?php endif; ?>
</td>
<td>

View File

@@ -0,0 +1,67 @@
<?php
/**
* Status badge partial.
*
* Renders a single <span class="status-badge …"> with a decorative symbol,
* an aria-label, and the visible label text.
*
* Variables (set before include):
*
* $badgeType (string) One of:
* 'publish' — publish/pending state (uses $badgeValue as bool)
* 'access' — access-type label (uses $badgeValue as string name)
* 'ok' — generic green/yellow ok-or-warn (uses $badgeValue as bool)
*
* $badgeValue (mixed) Meaning depends on $badgeType (see above).
*
* $badgeOkLabel (string, optional) Label when ok/published/true. Default: 'Oui'
* $badgeWarnLabel (string, optional) Label when warn/pending/false. Default: 'Non'
* $badgeContext (string, optional) Prefix for aria-label, e.g. 'Statut'.
* When empty the visible label is used directly.
*
* Example — publish badge:
* <?php $badgeType = 'publish'; $badgeValue = $thesis['is_published']; include PARTIALS . '/status-badge.php'; ?>
*
* Example — access badge:
* <?php $badgeType = 'access'; $badgeValue = $thesis['access_type']; include PARTIALS . '/status-badge.php'; ?>
*
* Example — generic ok badge:
* <?php $badgeType = 'ok'; $badgeValue = $hasPassword; $badgeOkLabel = 'Active'; $badgeWarnLabel = 'Non configurée'; $badgeContext = ''; include PARTIALS . '/status-badge.php'; ?>
*/
$badgeType = $badgeType ?? 'ok';
$badgeValue = $badgeValue ?? false;
$badgeOkLabel = $badgeOkLabel ?? 'Oui';
$badgeWarnLabel = $badgeWarnLabel ?? 'Non';
$badgeContext = $badgeContext ?? null; // null = use visible label as aria-label
if ($badgeType === 'publish') {
$isOk = (bool) $badgeValue;
$symbol = $isOk ? '●' : '◌';
$cssClass = $isOk ? 'status-published' : 'status-pending';
$visibleLabel = $isOk ? 'Publié' : 'En attente';
$ariaLabel = 'Statut : ' . $visibleLabel;
} elseif ($badgeType === 'access') {
$accessName = (string) $badgeValue;
$accessSlug = strtolower(preg_replace('/[^a-z]/i', '', $accessName));
$symbols = ['libre' => '○', 'interne' => '◑', 'interdit' => '●'];
$symbol = $symbols[$accessSlug] ?? '●';
$cssClass = 'status-access status-access--' . $accessSlug;
$visibleLabel = $accessName;
$ariaLabel = 'Accès : ' . $visibleLabel;
} else {
// 'ok' — generic boolean badge
$isOk = (bool) $badgeValue;
$symbol = $isOk ? '●' : '◌';
$cssClass = $isOk ? 'status-published' : 'status-pending';
$visibleLabel = $isOk ? $badgeOkLabel : $badgeWarnLabel;
$ariaLabel = $badgeContext !== null
? ($badgeContext !== '' ? $badgeContext . ' : ' . $visibleLabel : $visibleLabel)
: $visibleLabel;
}
?>
<span class="status-badge <?= $cssClass ?>" aria-label="<?= htmlspecialchars($ariaLabel) ?>"><span aria-hidden="true"><?= $symbol ?> </span><?= htmlspecialchars($visibleLabel) ?></span>
<?php
// Clean up variables so they don't bleed into the including scope.
unset($badgeType, $badgeValue, $badgeOkLabel, $badgeWarnLabel, $badgeContext,
$isOk, $accessName, $accessSlug, $symbols, $symbol, $cssClass, $visibleLabel, $ariaLabel);