Skip to content

Commit

Permalink
CSV report
Browse files Browse the repository at this point in the history
  • Loading branch information
eronisko committed Mar 15, 2024
1 parent d1fe96f commit 2a97c34
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
54 changes: 38 additions & 16 deletions app/Http/Controllers/Admin/Authority/RoleTranslationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
</x-admin.alert>
@endif

{{-- <x-admin.button btn primary outline :link="route('authority.role_translations.download')">
<x-admin.button sm btn primary outline :link="route('authority.role-translations.download')" class="ml-2">
<i class="fa fa-download"></i> Stiahnuť ako CSV
</x-admin.button> --}}
</x-admin.button>

<div class="tw-mt-4 tw-overflow-hidden tw-rounded-sm">
<table class="tw-w-full tw-text-sm">
Expand Down
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2a97c34

Please sign in to comment.