Import CSV des relevés d'une source
- ImportController : create() (formulaire) + store() (traitement) - Détection automatique du séparateur ; ou , - Suppression du BOM UTF-8 - Correspondance colonnes ↔ champs par libellé - Parsing des types : date (avec calendrier), booléen, nombre, lieu (recherche par nom_long), texte - Vue sources/import.blade.php : formulaire + liste des colonnes attendues - Routes sources.import.create / sources.import.store - Bouton "↑ Importer CSV" dans la fiche source (soumis aux droits create releve) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,131 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Enums\FieldType;
|
||||||
|
use App\Models\Lieu;
|
||||||
|
use App\Models\Releve;
|
||||||
|
use App\Models\Source;
|
||||||
|
use App\Support\DbCompat;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\View\View;
|
||||||
|
|
||||||
|
class ImportController extends Controller
|
||||||
|
{
|
||||||
|
public function create(Source $source): View
|
||||||
|
{
|
||||||
|
$this->authorize('create', [Releve::class, $source]);
|
||||||
|
|
||||||
|
$source->load('sourceType.fields');
|
||||||
|
|
||||||
|
return view('sources.import', compact('source'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request, Source $source): RedirectResponse
|
||||||
|
{
|
||||||
|
$this->authorize('create', [Releve::class, $source]);
|
||||||
|
|
||||||
|
$request->validate([
|
||||||
|
'fichier' => ['required', 'file', 'mimes:csv,txt', 'max:10240'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$source->load('sourceType.fields');
|
||||||
|
$fieldsByLabel = $source->sourceType->fields->keyBy('label');
|
||||||
|
|
||||||
|
$path = $request->file('fichier')->getRealPath();
|
||||||
|
$handle = fopen($path, 'r');
|
||||||
|
|
||||||
|
// Suppression du BOM UTF-8 éventuel
|
||||||
|
$bom = fread($handle, 3);
|
||||||
|
if ($bom !== "\xEF\xBB\xBF") {
|
||||||
|
rewind($handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Détection automatique du séparateur (; ou ,)
|
||||||
|
$firstLine = fgets($handle);
|
||||||
|
rewind($handle);
|
||||||
|
if ($bom === "\xEF\xBB\xBF") {
|
||||||
|
fread($handle, 3);
|
||||||
|
}
|
||||||
|
$sep = substr_count($firstLine, ';') >= substr_count($firstLine, ',') ? ';' : ',';
|
||||||
|
|
||||||
|
$header = fgetcsv($handle, 0, $sep);
|
||||||
|
if (! $header) {
|
||||||
|
fclose($handle);
|
||||||
|
return back()->with('error', 'Fichier CSV vide ou invalide.');
|
||||||
|
}
|
||||||
|
$header = array_map('trim', $header);
|
||||||
|
|
||||||
|
$imported = 0;
|
||||||
|
$userId = auth()->id();
|
||||||
|
|
||||||
|
while (($line = fgetcsv($handle, 0, $sep)) !== false) {
|
||||||
|
if (count(array_filter($line, fn ($v) => $v !== '')) === 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
foreach ($header as $i => $label) {
|
||||||
|
$field = $fieldsByLabel->get($label);
|
||||||
|
if (! $field) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$data[$field->name] = $this->parseValue(trim($line[$i] ?? ''), $field->type);
|
||||||
|
}
|
||||||
|
|
||||||
|
$source->releves()->create([
|
||||||
|
'data' => $data,
|
||||||
|
'created_by' => $userId,
|
||||||
|
'updated_by' => $userId,
|
||||||
|
]);
|
||||||
|
$imported++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose($handle);
|
||||||
|
|
||||||
|
if ($imported === 0) {
|
||||||
|
return back()->with('error', 'Aucun relevé importé — vérifiez que les en-têtes correspondent aux libellés des champs.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect()->route('sources.releves.index', $source)
|
||||||
|
->with('success', "{$imported} relevé(s) importé(s) avec succès.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parseValue(string $raw, FieldType $type): mixed
|
||||||
|
{
|
||||||
|
return match ($type) {
|
||||||
|
FieldType::Boolean => in_array(mb_strtolower($raw), ['oui', 'yes', '1', 'true'], true),
|
||||||
|
FieldType::Number => $raw !== '' ? (float) str_replace(',', '.', $raw) : null,
|
||||||
|
FieldType::Date => $this->parseDate($raw),
|
||||||
|
FieldType::Place => $this->parsePlace($raw),
|
||||||
|
default => $raw !== '' ? $raw : null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parseDate(string $raw): array
|
||||||
|
{
|
||||||
|
if ($raw === '') {
|
||||||
|
return ['valeur' => null, 'calendrier' => 'gregorien'];
|
||||||
|
}
|
||||||
|
// Format export : "YYYY-MM-DD" ou "YYYY-MM-DD (calendrier)"
|
||||||
|
if (preg_match('/^(.+?)\s*\((\w+)\)\s*$/', $raw, $m)) {
|
||||||
|
return ['valeur' => trim($m[1]), 'calendrier' => trim($m[2])];
|
||||||
|
}
|
||||||
|
return ['valeur' => $raw, 'calendrier' => 'gregorien'];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parsePlace(string $raw): ?array
|
||||||
|
{
|
||||||
|
if ($raw === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$like = DbCompat::like();
|
||||||
|
$lieu = Lieu::where('nom_long', $raw)->first(['id', 'nom_long'])
|
||||||
|
?? Lieu::where('nom_long', $like, $raw . '%')->first(['id', 'nom_long']);
|
||||||
|
|
||||||
|
return $lieu
|
||||||
|
? ['id' => $lieu->id, 'nom_long' => $lieu->nom_long]
|
||||||
|
: ['id' => null, 'nom_long' => $raw];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
<x-app-layout>
|
||||||
|
<x-slot name="header">
|
||||||
|
<div>
|
||||||
|
<h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200">Importer des relevés</h2>
|
||||||
|
<p class="text-sm text-gray-500 dark:text-gray-400 mt-0.5">
|
||||||
|
Source : <a href="{{ route('sources.show', $source) }}" class="text-indigo-600 hover:underline">{{ $source->nom }}</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</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 space-y-5">
|
||||||
|
<form method="POST" action="{{ route('sources.import.store', $source) }}" 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>
|
||||||
|
|
||||||
|
{{-- Format attendu --}}
|
||||||
|
<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 correspondant aux <strong>libellés</strong> des champs</li>
|
||||||
|
<li>Dates au format <code>AAAA-MM-JJ</code>, ou <code>AAAA-MM-JJ (calendrier)</code> pour julien/républicain</li>
|
||||||
|
<li>Booléens : <code>Oui</code> / <code>Non</code></li>
|
||||||
|
<li>Lieux : nom long du lieu (ex : <code>Bordeaux, Gironde, France</code>)</li>
|
||||||
|
</ul>
|
||||||
|
@if($source->releves->isNotEmpty() ?? $source->releves()->exists())
|
||||||
|
<p class="text-gray-500 dark:text-gray-400 text-xs mt-2">
|
||||||
|
Astuce : utilisez
|
||||||
|
<a href="{{ route('export.source.csv', $source) }}" class="text-indigo-600 hover:underline">l'export CSV</a>
|
||||||
|
d'une source existante comme modèle.
|
||||||
|
</p>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
@if($source->sourceType->fields->isNotEmpty())
|
||||||
|
<div class="mt-3">
|
||||||
|
<p class="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase mb-2">Colonnes attendues pour ce type de source</p>
|
||||||
|
<div class="flex flex-wrap gap-2">
|
||||||
|
@foreach($source->sourceType->fields->sortBy('order') as $field)
|
||||||
|
<span class="inline-flex items-center gap-1 px-2 py-0.5 rounded bg-white dark:bg-gray-600 border border-gray-200 dark:border-gray-500 text-xs text-gray-700 dark:text-gray-300">
|
||||||
|
{{ $field->label }}
|
||||||
|
@if($field->required)
|
||||||
|
<span class="text-red-500">*</span>
|
||||||
|
@endif
|
||||||
|
<span class="text-gray-400 dark:text-gray-500">({{ $field->type->value }})</span>
|
||||||
|
</span>
|
||||||
|
@endforeach
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<a href="{{ route('sources.show', $source) }}" class="text-sm text-indigo-600 hover:underline">← Retour à la source</a>
|
||||||
|
</div>
|
||||||
|
</x-app-layout>
|
||||||
@@ -142,6 +142,10 @@
|
|||||||
</a>
|
</a>
|
||||||
@endif
|
@endif
|
||||||
@can('create', [App\Models\Releve::class, $source])
|
@can('create', [App\Models\Releve::class, $source])
|
||||||
|
<a href="{{ route('sources.import.create', $source) }}"
|
||||||
|
class="px-3 py-1.5 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 text-xs rounded-md hover:bg-gray-50 dark:hover:bg-gray-600">
|
||||||
|
↑ Importer CSV
|
||||||
|
</a>
|
||||||
<a href="{{ route('sources.releves.create', $source) }}"
|
<a href="{{ route('sources.releves.create', $source) }}"
|
||||||
class="px-3 py-1.5 bg-indigo-600 text-white text-xs rounded-md hover:bg-indigo-700">
|
class="px-3 py-1.5 bg-indigo-600 text-white text-xs rounded-md hover:bg-indigo-700">
|
||||||
+ Nouveau relevé
|
+ Nouveau relevé
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
use App\Http\Controllers\CarteController;
|
use App\Http\Controllers\CarteController;
|
||||||
use App\Http\Controllers\DashboardController;
|
use App\Http\Controllers\DashboardController;
|
||||||
use App\Http\Controllers\ExportController;
|
use App\Http\Controllers\ExportController;
|
||||||
|
use App\Http\Controllers\ImportController;
|
||||||
use App\Http\Controllers\LieuController;
|
use App\Http\Controllers\LieuController;
|
||||||
use App\Http\Controllers\NotificationController;
|
use App\Http\Controllers\NotificationController;
|
||||||
use App\Http\Controllers\ProfileController;
|
use App\Http\Controllers\ProfileController;
|
||||||
@@ -61,6 +62,8 @@ Route::middleware('auth')->group(function () {
|
|||||||
Route::get('carte', [CarteController::class, 'index'])->name('carte');
|
Route::get('carte', [CarteController::class, 'index'])->name('carte');
|
||||||
Route::get('carte/data', [CarteController::class, 'data'])->name('carte.data');
|
Route::get('carte/data', [CarteController::class, 'data'])->name('carte.data');
|
||||||
Route::get('export/source/{source}/csv', [ExportController::class, 'sourceCsv'])->name('export.source.csv');
|
Route::get('export/source/{source}/csv', [ExportController::class, 'sourceCsv'])->name('export.source.csv');
|
||||||
|
Route::get('sources/{source}/import', [ImportController::class, 'create'])->name('sources.import.create');
|
||||||
|
Route::post('sources/{source}/import', [ImportController::class, 'store'])->name('sources.import.store');
|
||||||
|
|
||||||
Route::get('notifications', [NotificationController::class, 'index'])->name('notifications.index');
|
Route::get('notifications', [NotificationController::class, 'index'])->name('notifications.index');
|
||||||
Route::post('notifications/{id}/read', [NotificationController::class, 'markAsRead'])->name('notifications.read');
|
Route::post('notifications/{id}/read', [NotificationController::class, 'markAsRead'])->name('notifications.read');
|
||||||
|
|||||||
Reference in New Issue
Block a user