Import/export CSV des lieux
Export (GET lieux/export/csv) : - Colonnes : nom, code, type, lieu_parent (nom_long), latitude, longitude, note - BOM UTF-8, séparateur point-virgule Import (GET/POST lieux/import) : - Correspondance par nom de colonne (colonne nom obligatoire) - Résolution du parent par nom_long exact - Résolution du type par nom exact - Détection automatique du séparateur ; ou , - nom_long recalculé automatiquement par le modèle Boutons ↓ CSV, ↑ Importer CSV et + Nouveau lieu dans la liste des lieux. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ use App\Support\DbCompat;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\View\View;
|
||||
|
||||
@@ -129,6 +130,112 @@ class LieuController extends Controller
|
||||
->with('success', 'Lieu supprimé.');
|
||||
}
|
||||
|
||||
public function exportCsv(): Response
|
||||
{
|
||||
$this->authorize('viewAny', Lieu::class);
|
||||
|
||||
$lieux = Lieu::with(['lieuType', 'parent'])->orderBy('nom_long')->get();
|
||||
|
||||
$handle = fopen('php://temp', 'r+');
|
||||
fwrite($handle, "\xEF\xBB\xBF");
|
||||
|
||||
fputcsv($handle, ['nom', 'code', 'type', 'lieu_parent', 'latitude', 'longitude', 'note'], ';');
|
||||
|
||||
foreach ($lieux as $lieu) {
|
||||
fputcsv($handle, [
|
||||
$lieu->nom,
|
||||
$lieu->code ?? '',
|
||||
$lieu->lieuType?->nom ?? '',
|
||||
$lieu->parent?->nom_long ?? '',
|
||||
$lieu->latitude ?? '',
|
||||
$lieu->longitude ?? '',
|
||||
$lieu->note ?? '',
|
||||
], ';');
|
||||
}
|
||||
|
||||
rewind($handle);
|
||||
$csv = stream_get_contents($handle);
|
||||
fclose($handle);
|
||||
|
||||
return response($csv, 200, [
|
||||
'Content-Type' => 'text/csv; charset=UTF-8',
|
||||
'Content-Disposition' => 'attachment; filename="lieux.csv"',
|
||||
]);
|
||||
}
|
||||
|
||||
public function importCreate(): View
|
||||
{
|
||||
$this->authorize('create', Lieu::class);
|
||||
|
||||
return view('lieux.import');
|
||||
}
|
||||
|
||||
public function importStore(Request $request): RedirectResponse
|
||||
{
|
||||
$this->authorize('create', Lieu::class);
|
||||
|
||||
$request->validate([
|
||||
'fichier' => ['required', 'file', 'mimes:csv,txt', 'max:10240'],
|
||||
]);
|
||||
|
||||
$handle = fopen($request->file('fichier')->getRealPath(), 'r');
|
||||
|
||||
$bom = fread($handle, 3);
|
||||
if ($bom !== "\xEF\xBB\xBF") {
|
||||
rewind($handle);
|
||||
}
|
||||
|
||||
$firstLine = fgets($handle);
|
||||
rewind($handle);
|
||||
if ($bom === "\xEF\xBB\xBF") {
|
||||
fread($handle, 3);
|
||||
}
|
||||
$sep = substr_count($firstLine, ';') >= substr_count($firstLine, ',') ? ';' : ',';
|
||||
|
||||
$header = array_map('trim', fgetcsv($handle, 0, $sep) ?: []);
|
||||
$colIdx = array_flip($header);
|
||||
|
||||
$lieuTypesCache = LieuType::all()->keyBy('nom');
|
||||
$imported = 0;
|
||||
|
||||
while (($line = fgetcsv($handle, 0, $sep)) !== false) {
|
||||
$nom = trim($line[$colIdx['nom'] ?? -1] ?? '');
|
||||
if ($nom === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parentNomLong = trim($line[$colIdx['lieu_parent'] ?? -1] ?? '');
|
||||
$typeName = trim($line[$colIdx['type'] ?? -1] ?? '');
|
||||
|
||||
$parentId = $parentNomLong
|
||||
? Lieu::where('nom_long', $parentNomLong)->value('id')
|
||||
: null;
|
||||
$lieuTypeId = $typeName
|
||||
? $lieuTypesCache->get($typeName)?->id
|
||||
: null;
|
||||
|
||||
Lieu::create([
|
||||
'nom' => $nom,
|
||||
'code' => trim($line[$colIdx['code'] ?? -1] ?? '') ?: null,
|
||||
'lieu_type_id' => $lieuTypeId,
|
||||
'lieu_parent_id'=> $parentId,
|
||||
'latitude' => ($v = trim($line[$colIdx['latitude'] ?? -1] ?? '')) !== '' ? (float) str_replace(',', '.', $v) : null,
|
||||
'longitude' => ($v = trim($line[$colIdx['longitude'] ?? -1] ?? '')) !== '' ? (float) str_replace(',', '.', $v) : null,
|
||||
'note' => trim($line[$colIdx['note'] ?? -1] ?? '') ?: null,
|
||||
]);
|
||||
$imported++;
|
||||
}
|
||||
|
||||
fclose($handle);
|
||||
|
||||
if ($imported === 0) {
|
||||
return back()->with('error', 'Aucun lieu importé — vérifiez que la colonne « nom » est présente.');
|
||||
}
|
||||
|
||||
return redirect()->route('lieux.index')
|
||||
->with('success', "{$imported} lieu(x) importé(s) avec succès.");
|
||||
}
|
||||
|
||||
private function getDescendantAndSelfIds(int $lieuId): array
|
||||
{
|
||||
$rows = DB::select("
|
||||
|
||||
Reference in New Issue
Block a user