All checks were successful
Deploy Production / Deploy to Production (push) Successful in 25s
70 lines
2.4 KiB
PHP
70 lines
2.4 KiB
PHP
<?php
|
|
|
|
Kirby::plugin('world-game/csv-export', [
|
|
'sections' => [
|
|
'csv-export' => [],
|
|
],
|
|
|
|
'routes' => [
|
|
[
|
|
'pattern' => 'wg-export/contacts',
|
|
'method' => 'GET',
|
|
'action' => function () {
|
|
if (!kirby()->user()) {
|
|
http_response_code(403);
|
|
exit;
|
|
}
|
|
|
|
$livresBlancs = page('livres-blancs');
|
|
if (!$livresBlancs) {
|
|
http_response_code(500);
|
|
exit;
|
|
}
|
|
|
|
// Pré-charger les titres des livres blancs par UUID
|
|
// (évite la résolution UUID récursive dans la boucle)
|
|
$wpTitles = [];
|
|
foreach ($livresBlancs->children() as $wp) {
|
|
$wpTitles[$wp->uuid()->toString()] = $wp->slug();
|
|
}
|
|
|
|
$entries = $livresBlancs->contactDatabase()->toStructure();
|
|
|
|
header('Content-Type: text/csv; charset=UTF-8');
|
|
header('Content-Disposition: attachment; filename="' . date('ymd') . '-contacts.csv"');
|
|
header('Cache-Control: no-cache');
|
|
|
|
$out = fopen('php://output', 'w');
|
|
fputs($out, "\xEF\xBB\xBF"); // BOM pour Excel
|
|
fputcsv($out, ['Prénom', 'Nom', 'Email', 'Entreprise', 'Fonction', 'Livres blancs', 'Date'], ';', '"', '\\');
|
|
|
|
foreach ($entries as $entry) {
|
|
$uuids = $entry->whitePaper()->value();
|
|
if (!is_array($uuids)) {
|
|
$uuids = [];
|
|
}
|
|
|
|
$titles = [];
|
|
foreach ($uuids as $uuid) {
|
|
if (isset($wpTitles[$uuid])) {
|
|
$titles[] = $wpTitles[$uuid];
|
|
}
|
|
}
|
|
|
|
fputcsv($out, [
|
|
$entry->firstName()->value(),
|
|
$entry->lastName()->value(),
|
|
$entry->email()->value(),
|
|
$entry->company()->value(),
|
|
$entry->role()->value(),
|
|
implode(' | ', $titles),
|
|
$entry->downloadedAt()->value(),
|
|
], ';', '"', '\\');
|
|
}
|
|
|
|
fclose($out);
|
|
exit;
|
|
},
|
|
],
|
|
],
|
|
]);
|