Files
xamxam/templates/partials/status-badge.php
Pontoporeia 77bfd2f8e3 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)
2026-04-02 12:50:46 +02:00

68 lines
3.2 KiB
PHP

<?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);