Files
siakad/app/Support/Legacy/LegacyMenu.php
T
2026-11-12 12:22:06 +07:00

119 lines
5.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Support\Legacy;
use App\Models\Legacy\Account;
use Illuminate\Support\Facades\DB;
/**
* Pohon menu SIAKAD lama (t_menu: modul → menu → submenu) untuk sidebar akun DB lama, disaring menurut menu yang dipegang
* otoritas akun (t_otoritasuser → t_menuotoritas). Hanya menu yang sudah punya halaman di SIAKAD baru ($routes) yang
* ditautkan; menu lain disembunyikan agar tidak ada tautan mati, dan modul/submenu yang menjadi kosong ikut hilang.
* Nama menu dan urutannya persis seperti di DB (kdmenu).
*/
final class LegacyMenu
{
/**
* Menu lama yang sudah punya halaman untuk Superuser: kdmenu => slug halaman katalog operator (dibuka lewat rute
* operator dengan cakupan seluruh universitas, lihat EnsureLegacyMenuAccess) atau [nama rute, pola rute aktif].
*
* @var array<string, string|array{0: string, 1: string}>
*/
public const array SUPERUSER = [
// M01 Organisasi
'M010101' => 'list_gedung', // Setup Gedung
'M010102' => 'ms_ruang', // Setup Ruangan
'M0103' => 'list_pegawai', // Pegawai
'M0104' => 'list_fakultas', // Fakultas
'M0105' => 'list_prodi', // Program Studi
'M010602' => 'ms_golpangkat', // Setup Pangkat Dosen
'M0108' => 'list_jurusan', // Jurusan
// M03 Jadwal: pengaktifan periode per prodi (akun pusat: seluruh prodi); daftar master periode ditautkan dari halaman itu
'M0301' => 'set_periodeaktif', // Setup Periode Kuliah Aktif
// M04 Mahasiswa, M09 Administrasi: halaman milik Superuser sendiri
'M0405' => ['superuser.reset-password', 'superuser.reset-password*'], // Password Mahasiswa → Atur ulang kata sandi
'M0901' => ['superuser.hak-akses', 'superuser.hak-akses*'], // Administrasi Group User → Hak akses
'M0902' => ['superuser.pengguna.index', 'superuser.pengguna*'], // Administrasi User → Pengguna
];
/** Ikon modul lama M01–M09. */
private const array MODULE_ICONS = [
'M01' => 'building-office-2', 'M02' => 'book-open', 'M03' => 'calendar-days', 'M04' => 'users', 'M05' => 'banknotes',
'M06' => 'clipboard-document-check', 'M07' => 'academic-cap', 'M08' => 'chart-bar', 'M09' => 'cog-6-tooth',
];
/** @var array<int, array{ids: array<int, true>, codes: array<string, true>}> menu yang dipegang tiap akun, sekali per permintaan */
private array $held = [];
/**
* @param array<string, string|array{0: string, 1: string}> $routes
* @return list<array{kode: string, nama: string, icon: string, open: bool, items: list<array<string, mixed>>}>
* modul dengan items = tautan {kode, nama, href, active} atau submenu {kode, nama, open, items}
*/
public function tree(int $iduser, array $routes): array
{
$held = $this->held($iduser)['ids'];
$children = DB::connection('untan')->table('t_menu')->orderBy('kdmenu')->orderBy('idmenu')->get(['idmenu', 'kdmenu', 'menu', 'idmenuinduk'])
->groupBy(fn (object $r): int => $r->idmenuinduk === null ? 0 : (int) $r->idmenuinduk);
$build = function (object $row) use (&$build, $children, $held, $routes): ?array {
$kode = trim((string) $row->kdmenu);
$nama = trim((string) $row->menu);
$kids = $children->get((int) $row->idmenu, collect())->map($build)->filter()->values()->all();
if ($kids !== []) {
return ['kode' => $kode, 'nama' => $nama, 'open' => collect($kids)->contains(fn (array $k): bool => $k['active'] ?? $k['open'] ?? false), 'items' => $kids];
}
if (! isset($held[(int) $row->idmenu], $routes[$kode])) {
return null;
}
return ['kode' => $kode, 'nama' => $nama] + self::link($routes[$kode]);
};
return $children->get(0, collect())
->map(fn (object $module): ?array => $build($module))
->filter(fn (?array $node): bool => $node !== null && isset($node['items']))
->map(fn (array $node): array => $node + ['icon' => self::MODULE_ICONS[$node['kode']] ?? 'folder'])
->values()->all();
}
/** Boleh membuka halaman katalog {slug}? Ya bila salah satu menu lama yang menunjuk slug itu dipegang otoritas akun. */
public function allowsSlug(Account $account, string $slug, array $routes = self::SUPERUSER): bool
{
$codes = array_keys(array_filter($routes, fn (string|array $target): bool => $target === $slug));
return $codes !== [] && array_intersect_key(array_flip($codes), $this->held((int) $account->getKey())['codes']) !== [];
}
/** @return array{href: string, active: bool} */
private static function link(string|array $target): array
{
if (is_string($target)) {
return ['href' => route('operator.halaman', $target), 'active' => MenuAccess::slugFromPath(request()->path()) === $target];
}
[$name, $pattern] = $target;
return ['href' => route($name), 'active' => request()->routeIs($pattern)];
}
/** @return array{ids: array<int, true>, codes: array<string, true>} */
private function held(int $iduser): array
{
if (isset($this->held[$iduser])) {
return $this->held[$iduser];
}
$rows = DB::connection('untan')->table('t_menuotoritas as mo')
->join('t_otoritasuser as ou', 'ou.idotoritas', '=', 'mo.idotoritas')
->join('t_menu as m', 'm.idmenu', '=', 'mo.idmenu')
->where('ou.iduser', $iduser)
->distinct()->get(['m.idmenu', 'm.kdmenu']);
return $this->held[$iduser] = [
'ids' => $rows->mapWithKeys(fn (object $r): array => [(int) $r->idmenu => true])->all(),
'codes' => $rows->mapWithKeys(fn (object $r): array => [trim((string) $r->kdmenu) => true])->all(),
];
}
}