Files
siakad/app/Http/Controllers/PengumumanController.php
T
2026-11-11 18:26:23 +07:00

49 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Enums\Role;
use App\Models\Legacy\Account;
use App\Support\Prototype\Sample;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
class PengumumanController extends Controller
{
public function index(): View
{
$this->closedForLegacySuperuser();
return view('pengumuman.index', ['items' => $this->visible()]);
}
public function show(int $id): View
{
$this->closedForLegacySuperuser();
$item = $this->visible()->firstWhere('id', $id);
abort_if($item === null, 404);
return view('pengumuman.show', ['item' => $item]);
}
/** Pengumuman belum punya sumber data di DB lama: Superuser akun DB lama tidak melihat contoh (menunya juga disembunyikan). */
private function closedForLegacySuperuser(): void
{
$user = auth()->user();
abort_if($user instanceof Account && $user->role === Role::Superuser, 404);
}
/** Pengumuman terbit yang ditujukan ke semua pengguna atau ke role pengguna ini. @return Collection<int, array<string, mixed>> */
private function visible(): Collection
{
$audience = ['Semua pengguna', auth()->user()->role->label()];
return Sample::pengumuman()
->where('status', 'Terbit')
->filter(fn (array $item): bool => in_array($item['audiens'], $audience, true))
->sortByDesc('tanggal')
->values();
}
}