Fix relink: close modal + HTMX refresh for immediate pool update

- After relink, always close the modal (even if FilePond input not found,
  e.g. page refreshed by live-reload during the fetch).
- After closing, re-fetch #format-fichiers-block via HTMX from
  /admin/fragments/fichiers.php?_thesis_id=N which loads thesis files
  from DB and re-renders the fragment with pre-populated FilePond pools.
  The afterSwap handler auto-reinitializes FilePond instances.
- Updated admin/fragments/fichiers.php to accept _thesis_id, load
  existing files from DB, build per-queue-type JSON, and render in
  edit mode.
This commit is contained in:
Pontoporeia
2026-05-19 01:13:39 +02:00
parent b77bc486e5
commit 7c30d1c55d
3 changed files with 74 additions and 8 deletions

View File

@@ -1,13 +1,59 @@
<?php
/**
* Admin fragment: Format(s) + Fichiers block (HTMX partial).
*
* GET /admin/fragments/fichiers.php?_thesis_id=123
*
* When _thesis_id is provided, loads existing files from DB and renders
* the fragment in edit mode with pre-populated FilePond pools. Used by
* the relink flow to refresh pools after a file is linked server-side.
*/
require_once __DIR__ . '/../../../bootstrap.php';
require_once __DIR__ . '/../../../src/AdminAuth.php';
require_once APP_ROOT . '/src/Database.php';
App::boot();
AdminAuth::requireLogin();
$_POST['admin_mode'] = '1';
$thesisId = filter_var($_GET['_thesis_id'] ?? '', FILTER_VALIDATE_INT);
if ($thesisId) {
$db = Database::getInstance();
$currentFiles = $db->getThesisFiles($thesisId);
// Build per-queue-type existing-files JSON for FilePond edit mode
$buildQueueFilesJson = function (array $files, string $queueType): array {
$result = [];
foreach ($files as $f) {
$ft = $f['file_type'] ?? '';
$fp = $f['file_path'] ?? '';
if (str_starts_with($fp, 'http://') || str_starts_with($fp, 'https://')) continue;
if ($queueType === 'cover' && $ft !== 'cover') continue;
if ($queueType === 'note_intention' && $ft !== 'note_intention') continue;
if ($queueType === 'tfe' && ($ft === 'cover' || $ft === 'note_intention' || $ft === 'annex')) continue;
if ($queueType === 'annexe' && $ft !== 'annex') continue;
$result[] = [
'source' => (string)((int)$f['id']),
'options' => [
'type' => 'local',
'file' => [
'name' => $f['file_name'] ?? basename($f['file_path'] ?? ''),
'size' => (int)($f['file_size'] ?? 0),
'type' => $f['mime_type'] ?? 'application/octet-stream',
],
],
];
}
return $result;
};
$existingFilesJsonForCover = $buildQueueFilesJson($currentFiles, 'cover');
$existingFilesJsonForNoteIntention = $buildQueueFilesJson($currentFiles, 'note_intention');
$existingFilesJsonForTfe = $buildQueueFilesJson($currentFiles, 'tfe');
$existingFilesJsonForAnnexe = $buildQueueFilesJson($currentFiles, 'annexe');
$_POST['edit_mode'] = '1';
}
require_once APP_ROOT . '/templates/partials/form/fichiers-fragment.php';