From 2a97c346de52483df453c0eb8d9d70fb75077b2b Mon Sep 17 00:00:00 2001 From: Ernest Walzel Date: Fri, 15 Mar 2024 10:16:36 +0100 Subject: [PATCH] CSV report --- .../Authority/RoleTranslationsController.php | 54 +++++++++++++------ .../index.blade.php | 4 +- routes/web.php | 3 ++ 3 files changed, 43 insertions(+), 18 deletions(-) rename resources/views/authorities/{role_translations => role-translations}/index.blade.php (96%) diff --git a/app/Http/Controllers/Admin/Authority/RoleTranslationsController.php b/app/Http/Controllers/Admin/Authority/RoleTranslationsController.php index 01400f5f8..e42a9c327 100644 --- a/app/Http/Controllers/Admin/Authority/RoleTranslationsController.php +++ b/app/Http/Controllers/Admin/Authority/RoleTranslationsController.php @@ -6,12 +6,49 @@ use App\Http\Controllers\Controller; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Lang; +use League\Csv\Writer; class RoleTranslationsController extends Controller { public function index() { - $translations = Authority::query() + $translations = $this->getTranslations(); + + $missingCount = $translations->reduce(function (int $carry, $translations) { + if ($translations->sk === null) { + $carry++; + } + if ($translations->cs === null) { + $carry++; + } + if ($translations->en === null) { + $carry++; + } + return $carry; + }, 0); + + return view('authorities.role-translations.index', compact('translations', 'missingCount')); + } + + public function download() + { + $csv = Writer::createFromString(); + $csv->insertOne(['id', 'sk', 'cs', 'en']); + + $rows = $this->getTranslations() + ->map(fn($t) => [$t->id, $t->sk, $t->cs, $t->en]) + ->toArray(); + + $csv->insertAll($rows); + + return response($csv->toString()) + ->header('Content-Type', 'text/csv') + ->header('Content-Disposition', 'attachment; filename=role_translations.csv'); + } + + private function getTranslations() + { + return Authority::query() ->select('roles') ->whereNotNull('roles') ->distinct() @@ -33,20 +70,5 @@ public function index() : null, ]; }); - - $missingCount = $translations->reduce(function (int $carry, $translations) { - if ($translations->sk === null) { - $carry++; - } - if ($translations->cs === null) { - $carry++; - } - if ($translations->en === null) { - $carry++; - } - return $carry; - }, 0); - - return view('authorities.role_translations.index', compact('translations', 'missingCount')); } } diff --git a/resources/views/authorities/role_translations/index.blade.php b/resources/views/authorities/role-translations/index.blade.php similarity index 96% rename from resources/views/authorities/role_translations/index.blade.php rename to resources/views/authorities/role-translations/index.blade.php index 504810452..275e68a90 100644 --- a/resources/views/authorities/role_translations/index.blade.php +++ b/resources/views/authorities/role-translations/index.blade.php @@ -31,9 +31,9 @@ @endif - {{-- + StiahnuƄ ako CSV - --}} +
diff --git a/routes/web.php b/routes/web.php index 922000211..95839db51 100644 --- a/routes/web.php +++ b/routes/web.php @@ -491,6 +491,9 @@ function() Route::get('authority/role-translations', [RoleTranslationsController::class, 'index'])->name( 'authority.role-translations.index' ); + Route::get('authority/role-translations/download', [RoleTranslationsController::class, 'download'])->name( + 'authority.role-translations.download' + ); Route::resource('authority', AuthorityController::class); Route::resource('sketchbook', SketchbookController::class); Route::resource('notices', NoticeController::class);