Files
xamxam/app/public/admin/actions/export.php
Pontoporeia 678f9fc804 Index page: remove Mots-clés button, move export to bulk selection, fix ZipArchive error, move DB export to paramètres, sticky thead
- Remove 'Mots-clés' button from toolbar (redundant with admin sidebar tags)
- Replace export dialog with 'Exporter CSV' + 'Exporter fichiers' buttons in bulk selection bar
- Export dispatcher now accepts ?ids=1,2,3 for per-selection export
- All ExportController/Database methods accept optional thesisIds array
- Graceful error message when ZipArchive extension is missing on server
- Move DB export (SQLite download) to paramètres → Maintenance section
- Sticky table column headers (position: sticky, top: 0, z-index: 5) for index page table
2026-05-19 23:58:51 +02:00

116 lines
3.3 KiB
PHP

<?php
/**
* Unified export dispatcher.
*
* Query params:
* csv=1 — export CSV
* files=1 — export ZIP of files
*
* If both are requested, CSV is streamed first (as a download) then the ZIP.
* In practice, browsers handle this as two sequential downloads.
*/
require_once __DIR__ . "/../../../bootstrap.php";
require_once __DIR__ . '/../../../src/AdminAuth.php';
AdminAuth::requireLogin();
require_once APP_ROOT . '/src/Controllers/ExportController.php';
require_once APP_ROOT . '/src/AdminLogger.php';
require_once APP_ROOT . '/src/ErrorHandler.php';
$doCsv = !empty($_GET['csv']);
$doFiles = !empty($_GET['files']);
$doDb = !empty($_GET['db']);
// Optional: filter by selected thesis IDs (bulk selection export)
$idsRaw = $_GET['ids'] ?? '';
$selectedIds = [];
if ($idsRaw !== '') {
foreach (explode(',', $idsRaw) as $id) {
$id = (int) trim($id);
if ($id > 0) {
$selectedIds[] = $id;
}
}
}
if (!$doCsv && !$doFiles && !$doDb) {
$doCsv = true;
}
$controller = ExportController::create();
if ($doCsv) {
AdminLogger::make()->logCsvExport();
$filename = 'xamxam-export-' . date('Y-m-d') . '.csv';
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: no-cache, must-revalidate');
echo "\xEF\xBB\xBF";
$out = fopen('php://output', 'w');
fputcsv($out, ExportController::CSV_HEADERS, ',', '"', '');
$rows = $controller->exportAllTheses($selectedIds);
foreach ($rows as $csvLine) {
fputcsv($out, $csvLine, ',', '"', '');
}
fclose($out);
// If only CSV, we're done
if (!$doFiles) {
exit;
}
}
if ($doFiles) {
if (!class_exists('ZipArchive')) {
http_response_code(500);
exit('Module PHP <code>zip</code> non installé sur le serveur. Contactez l\'administrateur système (<code>apt install php8.4-zip</code>).');
}
try {
$zipPath = $controller->createExportZip($selectedIds);
$fileSize = filesize($zipPath);
$fileCount = count($controller->getAllThesisFiles($selectedIds));
AdminLogger::make()->logFilesExport($fileCount, (int)$fileSize);
$filename = 'xamxam-files-' . date('Y-m-d') . '.zip';
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . $fileSize);
header('Cache-Control: no-cache, must-revalidate');
readfile($zipPath);
@unlink($zipPath);
} catch (Exception $e) {
ErrorHandler::log('export', $e);
http_response_code(500);
exit('Erreur lors de la création de l\'archive : ' . htmlspecialchars(ErrorHandler::userMessage($e)));
}
}
if ($doDb) {
$dbPath = $controller->getDatabasePath();
if (!file_exists($dbPath)) {
http_response_code(500);
exit('Base de données introuvable.');
}
$filename = 'xamxam-db-' . date('Y-m-d') . '.sqlite';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . filesize($dbPath));
header('Cache-Control: no-cache, must-revalidate');
AdminLogger::make()->logDbExport();
readfile($dbPath);
}
exit;