mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-09-25 01:53:03 +02:00
The contenus page loaded both lookup tables in full via HTMX on page load: 760 tag rows + 216 language rows, ~2.4MB of HTML including ~1960 inline SVG icons and per-row CSRF forms. The DB queries were already fast (~6ms); the cost was pure client-side payload and DOM. - Add paged lookups: getTagsPage/getLanguagesPage + countTagsWithCount/ countLanguagesWithCount, and optional limit/offset on the existing unpaged variants (backward compatible). - Fragments serve 25 rows per request. A sentinel <tr> with hx-trigger="intersect once root:#<wrap>" appends the next page when it scrolls into the table's own scroll container (htmx 'revealed' only checks the window viewport, not nested scrollers). - Search forms still swap the whole wrapper and reset to offset 0. - Style the load-more row; add PagedLanguagesTagsTest coverage. Initial DOM for the page drops from ~2.4MB to ~160KB; scrolling reaches the full totals (736 tags, 216 languages) and stops cleanly.
155 lines
6.4 KiB
PHP
155 lines
6.4 KiB
PHP
<?php
|
|
/**
|
|
* contenus-motscles-fragment.php
|
|
*
|
|
* HTMX fragment: returns the mots-clés table for the contenus page,
|
|
* optionally filtered by a search query.
|
|
*
|
|
* Infinite scroll: rows are served in pages of PAGE_SIZE. When there are
|
|
* more rows, a sentinel <tr> is emitted with hx-trigger="revealed": when it
|
|
* scrolls into view htmx fetches the next offset and swaps itself
|
|
* (outerHTML) with the next chunk of rows + a new sentinel. This keeps the
|
|
* initial payload small instead of shipping all tags at once.
|
|
*
|
|
* When $offset > 0 the fragment returns only the <tr> rows (append mode),
|
|
* because the sentinel lives inside the existing <tbody>.
|
|
*/
|
|
require_once __DIR__ . '/../../bootstrap.php';
|
|
require_once __DIR__ . '/../../src/AdminAuth.php';
|
|
AdminAuth::requireLogin();
|
|
|
|
if (empty($_SESSION['csrf_token'])) {
|
|
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
|
|
}
|
|
|
|
require_once __DIR__ . '/../../src/Database.php';
|
|
|
|
const MOTSCLES_PAGE_SIZE = 25;
|
|
|
|
$searchQuery = trim($_GET['q'] ?? '');
|
|
$offset = max(0, (int) ($_GET['offset'] ?? 0));
|
|
|
|
try {
|
|
$db = new Database();
|
|
$tags = $db->getTagsPage($searchQuery, MOTSCLES_PAGE_SIZE, $offset);
|
|
$total = $db->countTagsWithCount($searchQuery);
|
|
} catch (Exception $e) {
|
|
die('<div class="flash-error">Erreur : ' . htmlspecialchars($e->getMessage()) . '</div>');
|
|
}
|
|
|
|
$hasMore = ($offset + count($tags)) < $total;
|
|
$nextUrl = '/admin/contenus-motscles-fragment.php?offset=' . ($offset + MOTSCLES_PAGE_SIZE)
|
|
. ($searchQuery !== '' ? '&q=' . urlencode($searchQuery) : '');
|
|
|
|
/**
|
|
* Render one <tr> for a tag row.
|
|
*/
|
|
function render_motscles_row(array $tag): void
|
|
{
|
|
?>
|
|
<tr>
|
|
<td style="width:1%"><input type="checkbox" name="selected_tags[]" value="<?= (int)$tag['id'] ?>" onchange="motsclesUpdateBulk()"></td>
|
|
<td id="motscles-name-<?= (int)$tag['id'] ?>" data-name="<?= htmlspecialchars($tag['name']) ?>">
|
|
<span class="tag-name-cell"><?= htmlspecialchars($tag['name']) ?></span>
|
|
<button type="button" class="admin-icon-btn admin-icon-btn--edit" title="Renommer"
|
|
onclick="motsclesStartRename(<?= (int)$tag['id'] ?>)">
|
|
<?= icon('pencil-note') ?>
|
|
</button>
|
|
</td>
|
|
<td class="admin-tags-count" style="width:1%;white-space:nowrap"><?= (int)$tag['thesis_count'] ?></td>
|
|
<td class="admin-actions-col" style="width:1%">
|
|
<div class="admin-actions">
|
|
<form method="post" action="actions/tag.php" class="admin-inline-form">
|
|
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="return" value="/admin/contenus.php">
|
|
<input type="hidden" name="tag_id" value="<?= (int)$tag['id'] ?>">
|
|
<button type="button" class="admin-icon-btn admin-icon-btn--delete" title="Supprimer"
|
|
onclick="motsclesConfirmDelete(this, <?= htmlspecialchars(json_encode($tag['name']), ENT_QUOTES) ?>)">
|
|
<?= icon('trash') ?>
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Render the "load more" sentinel row (or nothing when exhausted).
|
|
*/
|
|
function render_motscles_sentinel(bool $hasMore, string $nextUrl): void
|
|
{
|
|
if (!$hasMore) {
|
|
return;
|
|
}
|
|
?>
|
|
<tr class="admin-load-more" hx-get="<?= htmlspecialchars($nextUrl) ?>"
|
|
hx-trigger="intersect once root:#motscles-table-wrap" hx-swap="outerHTML" hx-target="this">
|
|
<td colspan="4" class="admin-load-more__cell">Chargement…</td>
|
|
</tr>
|
|
<?php
|
|
}
|
|
|
|
// ── Append mode: only the rows, swapped in place of the sentinel ──────────
|
|
if ($offset > 0) {
|
|
foreach ($tags as $tag) {
|
|
render_motscles_row($tag);
|
|
}
|
|
render_motscles_sentinel($hasMore, $nextUrl);
|
|
return;
|
|
}
|
|
|
|
// ── Initial load: full wrapper ────────────────────────────────────────────
|
|
?>
|
|
<div id="motscles-bulk-actions" class="admin-bulk-actions" style="display:none;position:sticky;top:0;z-index:10">
|
|
<strong><span id="motscles-selected-count">0</span> mot(s)-clé(s) sélectionné(s)</strong>
|
|
<div class="admin-bulk-btns">
|
|
<button type="button" class="btn btn--sm btn--secondary" onclick="motsclesCancelSelection()" title="Annuler la sélection">
|
|
<?= icon('x-close') ?>
|
|
Annuler
|
|
</button>
|
|
<button type="button" class="btn btn--sm btn--red admin-btn-delete"
|
|
onclick="motsclesConfirmBulkDelete()"
|
|
title="Supprimer les mots-clés sélectionnés">
|
|
<?= icon('trash') ?>
|
|
Supprimer
|
|
</button>
|
|
<button type="button" class="btn btn--sm btn--warning admin-btn-merge"
|
|
onclick="motsclesConfirmBulkMerge()"
|
|
title="Fusionner les mots-clés sélectionnés">
|
|
<?= icon('link-chain') ?>
|
|
Fusionner
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<form id="motscles-bulk-form" method="post" action="actions/tag.php">
|
|
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
|
|
<input type="hidden" name="action" value="merge_bulk">
|
|
<input type="hidden" name="return" value="/admin/contenus.php">
|
|
<input type="hidden" name="target_id" id="motscles-bulk-target" value="">
|
|
<div id="motscles-bulk-checkboxes"></div>
|
|
</form>
|
|
|
|
<table class="admin-table--sticky">
|
|
<thead>
|
|
<tr>
|
|
<th scope="col" style="width:1%"><input type="checkbox" onchange="motsclesToggleAll(this)"></th>
|
|
<th scope="col">Nom</th>
|
|
<th scope="col" style="width:1%;white-space:nowrap">TFE Associé</th>
|
|
<th scope="col" style="width:1%">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($tags)): ?>
|
|
<tr><td colspan="4" class="admin-empty">Aucun mot-clé trouvé.</td></tr>
|
|
<?php else: ?>
|
|
<?php foreach ($tags as $tag): ?>
|
|
<?php render_motscles_row($tag); ?>
|
|
<?php endforeach; ?>
|
|
<?php render_motscles_sentinel($hasMore, $nextUrl); ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|