feat: harden tenant settings and import pipeline

This commit is contained in:
2025-09-25 11:50:18 +02:00
parent b22d91ed32
commit 9248d7a3f5
29 changed files with 577 additions and 293 deletions

View File

@@ -3,12 +3,11 @@
namespace App\Filament\Resources\EmotionResource\Pages;
use App\Filament\Resources\EmotionResource;
use App\Models\Emotion;
use App\Services\EmotionImportService;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Page;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class ImportEmotions extends Page
@@ -35,13 +34,13 @@ class ImportEmotions extends Page
$this->validate();
$path = $this->form->getState()['file'] ?? null;
if (!$path || !Storage::disk('public')->exists($path)) {
if (! $path || ! Storage::disk('public')->exists($path)) {
Notification::make()->danger()->title(__('admin.notifications.file_not_found'))->send();
return;
}
$fullPath = Storage::disk('public')->path($path);
[$ok, $fail] = $this->importEmotionsCsv($fullPath);
[$ok, $fail] = app(EmotionImportService::class)->import($fullPath);
Notification::make()
->success()
@@ -54,61 +53,4 @@ class ImportEmotions extends Page
{
return __('admin.emotions.import.heading');
}
private function importEmotionsCsv(string $file): array
{
$handle = fopen($file, 'r');
if (!$handle) {
return [0, 0];
}
$ok = 0;
$fail = 0;
$headers = fgetcsv($handle, 0, ',');
if (!$headers) {
return [0, 0];
}
$map = array_flip($headers);
while (($row = fgetcsv($handle, 0, ',')) !== false) {
try {
DB::transaction(function () use ($row, $map, &$ok) {
$nameDe = trim($row[$map['name_de']] ?? '');
$nameEn = trim($row[$map['name_en']] ?? '');
if (empty($nameDe) && empty($nameEn)) {
throw new \Exception('Name is required.');
}
$emotion = Emotion::create([
'name' => ['de' => $nameDe, 'en' => $nameEn],
'icon' => $row[$map['icon']] ?? null,
'color' => $row[$map['color']] ?? null,
'description' => [
'de' => $row[$map['description_de']] ?? null,
'en' => $row[$map['description_en']] ?? null,
],
'sort_order' => (int)($row[$map['sort_order']] ?? 0),
'is_active' => (int)($row[$map['is_active']] ?? 1) ? 1 : 0,
]);
$eventTypes = $row[$map['event_types']] ?? '';
if ($eventTypes) {
$slugs = array_filter(array_map('trim', explode('|', $eventTypes)));
if ($slugs) {
$eventTypeIds = DB::table('event_types')->whereIn('slug', $slugs)->pluck('id')->all();
$emotion->eventTypes()->attach($eventTypeIds);
}
}
$ok++;
});
} catch (\Throwable $e) {
$fail++;
}
}
fclose($handle);
return [$ok, $fail];
}
}