fix: merge corbeille lists + bulk restore button in cleanup

- Single unified 'Corbeille' table replaces the two separate lists
  (stale/orphelin + restorable). Each row has a Statut column showing
  '↺ Restaurable' or 'Orphelin', plus a checkbox for bulk ops.

- Checkboxes on restorable rows carry data-restorable='1' attribute
  so the bulk JS can distinguish them.

- Bulk actions bar now has both 'Supprimer la sélection' and
  '↺ Restaurer la sélection' buttons. The restore button only appears
  when at least one restorable item is checked.

- New cleanupBulkRestore() JS function populates a dedicated hidden
  form and confirms before submitting. Only restorable items are sent.

- restore-trash.php refactored to handle both single (trash_file) and
  bulk (trash_files[]) inputs via a loop, accumulating restored/skipped
  counts and re-rendering the fragment on HTMX requests.
This commit is contained in:
Pontoporeia
2026-07-10 16:33:06 +02:00
parent 3cecee10c9
commit 7013f79fc0
5 changed files with 180 additions and 152 deletions
@@ -32,9 +32,11 @@ $fpMeta = $fpStale . ' dossier' . ($fpStale > 1 ? 's' : '');
if ($fpStale > 0) {
$fpMeta .= ' · ' . ($d['filepond_stale_human'] ?? '');
}
$trMeta = $trStale . ' fichier' . ($trStale > 1 ? 's' : '');
if ($trStale > 0) {
$trMeta .= ' · ' . ($d['trash_stale_human'] ?? '');
$trMeta = ($trStale + $trActive) . ' fichier' . (($trStale + $trActive) > 1 ? 's' : '');
$trTotalSize = ($d['trash_stale_size'] ?? 0) + ($d['trash_active_size'] ?? 0);
$trTotalHuman = humanBytes($trTotalSize);
if (($trStale + $trActive) > 0) {
$trMeta .= ' · ' . $trTotalHuman;
}
?>
<?php if ($totalStale === 0 && $totalFiles === 0): ?>
@@ -45,6 +47,9 @@ if ($trStale > 0) {
<div id="cleanup-bulk-actions" class="admin-bulk-actions" style="display:none">
<strong><span id="cleanup-selected-count">0</span> fichier(s) sélectionné(s)</strong>
<div class="admin-bulk-btns">
<button type="button" class="btn btn--sm" onclick="cleanupBulkRestore()" id="cleanup-bulk-restore-btn" style="display:none">
↺ Restaurer la sélection
</button>
<button type="button" class="btn btn--sm btn--red" onclick="cleanupBulkDelete()">
<?= icon('trash') ?> Supprimer la sélection
</button>
@@ -62,6 +67,17 @@ if ($trStale > 0) {
<div id="cleanup-bulk-checkboxes"></div>
</form>
<!-- Hidden bulk restore form -->
<form id="cleanup-bulk-restore-form" method="post" action="/admin/actions/restore-trash.php"
hx-post="/admin/actions/restore-trash.php"
hx-target="#tmp-cleanup-stats-wrapper"
hx-swap="innerHTML"
hx-indicator="#tmp-cleanup-stats-wrapper"
style="display:none">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
<div id="cleanup-bulk-restore-checkboxes"></div>
</form>
<?php if ($fpStale > 0): ?>
<h3 id="tmp-filepond-heading">Téléversements abandonnés <span class="n-meta"><?= htmlspecialchars($fpMeta) ?></span></h3>
<table class="n-table" aria-labelledby="tmp-filepond-heading">
@@ -91,60 +107,44 @@ if ($trStale > 0) {
</table>
<?php endif; ?>
<?php if ($trStale > 0): ?>
<h3 id="tmp-trash-heading">Corbeille (à nettoyer) <span class="n-meta"><?= htmlspecialchars($trMeta) ?></span></h3>
<table class="n-table" aria-labelledby="tmp-trash-heading">
<thead><tr><th width="1%"><input type="checkbox" onchange="cleanupToggleAll(this, 'trash')" title="Tout sélectionner"></th><th>Nom</th><th>Taille</th><th>Âge</th><th width="1%"></th></tr></thead>
<tbody>
<?php foreach ($d['trash_stale_files'] as $f): ?>
<tr>
<td><input type="checkbox" name="trash_files[]" value="<?= htmlspecialchars($f['name']) ?>" data-cleanup-group="trash" onchange="cleanupUpdateBulk()"></td>
<td><strong><?= htmlspecialchars($f['name']) ?></strong></td>
<td style="white-space:nowrap"><?= htmlspecialchars($f['human']) ?></td>
<td style="white-space:nowrap">~<?= (int)$f['age_days'] ?> j</td>
<td style="white-space:nowrap">
<button type="button" class="btn btn--sm btn--danger" style="font-size:0.85em;padding:2px var(--space-xs)"
hx-post="/admin/actions/cleanup-tmp.php"
hx-confirm="Supprimer définitivement ce fichier de la corbeille ?"
hx-vals='{"csrf_token":"<?= htmlspecialchars($_SESSION['csrf_token']) ?>","trash_file":"<?= htmlspecialchars($f['name']) ?>"}'
hx-target="#tmp-cleanup-stats-wrapper"
hx-swap="innerHTML"
hx-indicator="#tmp-cleanup-stats-wrapper">
<?= icon('trash') ?>
Supprimer
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php
$trActiveFiles = $d['trash_active_files'] ?? [];
if (!empty($trActiveFiles)):
$trActiveMeta = $trActive . ' fichier' . ($trActive > 1 ? 's' : '');
if ($trActive > 0) {
$trActiveMeta .= ' · ' . ($d['trash_active_human'] ?? '');
}
?>
<h3 id="tmp-trash-restore-heading">Corbeille (restaurable) <span class="n-meta"><?= htmlspecialchars($trActiveMeta) ?></span></h3>
<p style="font-size:0.85em;color:var(--text-secondary);margin:0 0 var(--space-sm)">
Fichiers récemment supprimés pour lesquels le TFE associé existe encore. Vous pouvez les restaurer ou les supprimer définitivement.
</p>
<table class="n-table" aria-labelledby="tmp-trash-restore-heading">
<thead><tr><th>Nom</th><th>Taille</th><th>Âge</th><th>Origine</th><th width="1%"></th></tr></thead>
// ── Merge stale + active trash files into a single list for display ─────
// stale = orphaned (no sidecar, or too old); active = restorable (has sidecar)
$allTrashFiles = [];
foreach (($d['trash_active_files'] ?? []) as $f) {
$f['_restorable'] = true;
$f['_stale'] = false;
$allTrashFiles[] = $f;
}
foreach (($d['trash_stale_files'] ?? []) as $f) {
$f['_restorable'] = false;
$f['_stale'] = true;
$f['has_sidecar'] = false;
$f['original_name'] = '';
$f['file_type'] = '';
$allTrashFiles[] = $f;
}
$hasTrash = !empty($allTrashFiles);
if ($hasTrash): ?>
<h3 id="tmp-trash-heading">Corbeille <span class="n-meta"><?= htmlspecialchars($trMeta) ?></span></h3>
<table class="n-table" aria-labelledby="tmp-trash-heading">
<thead><tr><th width="1%"><input type="checkbox" onchange="cleanupToggleAll(this, 'trash')" title="Tout sélectionner"></th><th>Nom</th><th>Taille</th><th>Âge</th><th>Statut</th><th width="1%"></th></tr></thead>
<tbody>
<?php foreach ($trActiveFiles as $f): ?>
<tr>
<?php foreach ($allTrashFiles as $f): ?>
<tr<?= $f['_restorable'] ? ' class="trash-restorable-row"' : '' ?>>
<td><input type="checkbox" name="trash_files[]" value="<?= htmlspecialchars($f['name']) ?>" data-cleanup-group="trash"<?= $f['_restorable'] ? ' data-restorable="1"' : '' ?> onchange="cleanupUpdateBulk()"></td>
<td><strong><?= htmlspecialchars($f['name']) ?></strong></td>
<td style="white-space:nowrap"><?= htmlspecialchars($f['human']) ?></td>
<td style="white-space:nowrap">~<?= (int)$f['age_days'] ?> j</td>
<td style="font-size:0.85em;color:var(--text-secondary)">
<?= $f['has_sidecar'] ? htmlspecialchars($f['original_name'] ?? '?') . ' (' . htmlspecialchars($f['file_type'] ?? '?') . ')' : 'métadonnées indisponibles' ?>
<td>
<?php if ($f['_restorable']): ?>
<span style="font-size:0.85em;color:var(--accent-green)" title="<?= htmlspecialchars($f['original_name'] ?? '') ?>">↺ Restaurable</span>
<?php else: ?>
<span style="font-size:0.85em;color:var(--text-secondary)">Orphelin</span>
<?php endif; ?>
</td>
<td style="white-space:nowrap">
<?php if ($f['has_sidecar']): ?>
<?php if ($f['_restorable']): ?>
<button type="button" class="btn btn--sm"
style="font-size:0.85em;padding:2px var(--space-xs);margin-right:4px"
hx-post="/admin/actions/restore-trash.php"