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("
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
<x-app-layout>
|
||||
<x-slot name="header">
|
||||
<h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200">Importer des lieux</h2>
|
||||
</x-slot>
|
||||
|
||||
<div class="py-8 max-w-2xl mx-auto px-4 sm:px-6 lg:px-8 space-y-6">
|
||||
|
||||
@if(session('error'))
|
||||
<div class="p-4 bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-700 text-red-800 dark:text-red-200 rounded-md">
|
||||
{{ session('error') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<div class="bg-white dark:bg-gray-800 shadow rounded-lg p-6">
|
||||
<form method="POST" action="{{ route('lieux.import.store') }}" enctype="multipart/form-data">
|
||||
@csrf
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
|
||||
Fichier CSV <span class="text-red-500">*</span>
|
||||
</label>
|
||||
<input type="file" name="fichier" accept=".csv,.txt" required
|
||||
class="block w-full text-sm text-gray-700 dark:text-gray-300
|
||||
file:mr-4 file:py-2 file:px-4 file:rounded-md file:border-0
|
||||
file:text-sm file:font-medium file:bg-indigo-50 file:text-indigo-700
|
||||
hover:file:bg-indigo-100 dark:file:bg-indigo-900/30 dark:file:text-indigo-300">
|
||||
@error('fichier')
|
||||
<p class="mt-1 text-sm text-red-600">{{ $message }}</p>
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-5">
|
||||
<button type="submit"
|
||||
class="px-5 py-2 bg-indigo-600 text-white rounded-md hover:bg-indigo-700 text-sm">
|
||||
Importer
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="bg-gray-50 dark:bg-gray-700 rounded-lg p-5 text-sm space-y-3">
|
||||
<p class="font-medium text-gray-700 dark:text-gray-300">Format attendu</p>
|
||||
<ul class="text-gray-600 dark:text-gray-400 space-y-1 list-disc list-inside">
|
||||
<li>Encodage <strong>UTF-8</strong>, séparateur <strong>point-virgule</strong> (<code>;</code>) ou virgule (<code>,</code>)</li>
|
||||
<li>Première ligne = en-têtes (noms exacts ci-dessous)</li>
|
||||
<li>Le parent est identifié par son <strong>nom long complet</strong> (ex : <code>Gironde, France</code>)</li>
|
||||
<li>Le type est identifié par son <strong>nom exact</strong> tel que défini dans les types de lieux</li>
|
||||
</ul>
|
||||
|
||||
<div class="mt-3 overflow-x-auto">
|
||||
<table class="text-xs border-collapse w-full">
|
||||
<thead>
|
||||
<tr class="bg-gray-200 dark:bg-gray-600">
|
||||
@foreach(['nom *', 'code', 'type', 'lieu_parent', 'latitude', 'longitude', 'note'] as $col)
|
||||
<th class="border border-gray-300 dark:border-gray-500 px-2 py-1 text-left font-mono text-gray-700 dark:text-gray-200">{{ $col }}</th>
|
||||
@endforeach
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr class="text-gray-500 dark:text-gray-400">
|
||||
<td class="border border-gray-300 dark:border-gray-500 px-2 py-1">Bordeaux</td>
|
||||
<td class="border border-gray-300 dark:border-gray-500 px-2 py-1">33063</td>
|
||||
<td class="border border-gray-300 dark:border-gray-500 px-2 py-1">Commune</td>
|
||||
<td class="border border-gray-300 dark:border-gray-500 px-2 py-1">Gironde, France</td>
|
||||
<td class="border border-gray-300 dark:border-gray-500 px-2 py-1">44.8378</td>
|
||||
<td class="border border-gray-300 dark:border-gray-500 px-2 py-1">-0.5792</td>
|
||||
<td class="border border-gray-300 dark:border-gray-500 px-2 py-1"></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<p class="text-gray-500 dark:text-gray-400 text-xs mt-2">
|
||||
Astuce : utilisez
|
||||
<a href="{{ route('lieux.export.csv') }}" class="text-indigo-600 hover:underline">l'export CSV</a>
|
||||
de la liste des lieux comme modèle.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<a href="{{ route('lieux.index') }}" class="text-sm text-indigo-600 hover:underline">← Retour aux lieux</a>
|
||||
</div>
|
||||
</x-app-layout>
|
||||
@@ -2,13 +2,23 @@
|
||||
<x-slot name="header">
|
||||
<div class="flex items-center justify-between">
|
||||
<h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200">Lieux</h2>
|
||||
<div class="flex items-center gap-2">
|
||||
<a href="{{ route('lieux.export.csv') }}"
|
||||
class="px-4 py-2 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 text-sm rounded-md hover:bg-gray-50 dark:hover:bg-gray-700">
|
||||
↓ CSV
|
||||
</a>
|
||||
@can('create', App\Models\Lieu::class)
|
||||
<a href="{{ route('lieux.import.create') }}"
|
||||
class="px-4 py-2 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 text-sm rounded-md hover:bg-gray-50 dark:hover:bg-gray-700">
|
||||
↑ Importer CSV
|
||||
</a>
|
||||
<a href="{{ route('lieux.create') }}"
|
||||
class="px-4 py-2 bg-indigo-600 text-white text-sm rounded-md hover:bg-indigo-700">
|
||||
+ Nouveau lieu
|
||||
</a>
|
||||
@endcan
|
||||
</div>
|
||||
</div>
|
||||
</x-slot>
|
||||
|
||||
<div class="py-8 max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 space-y-6">
|
||||
|
||||
@@ -47,6 +47,9 @@ Route::middleware('auth')->group(function () {
|
||||
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
|
||||
|
||||
Route::get('lieux/search', [LieuController::class, 'search'])->name('lieux.search');
|
||||
Route::get('lieux/export/csv', [LieuController::class, 'exportCsv'])->name('lieux.export.csv');
|
||||
Route::get('lieux/import', [LieuController::class, 'importCreate'])->name('lieux.import.create');
|
||||
Route::post('lieux/import', [LieuController::class, 'importStore'])->name('lieux.import.store');
|
||||
Route::resource('lieux', LieuController::class)->parameters(['lieux' => 'lieu']);
|
||||
|
||||
Route::resource('sources', SourceController::class);
|
||||
|
||||
Reference in New Issue
Block a user