> */ public function all(): Collection { return $this->db()->table('t_otoritas as t') ->selectRaw('t.idotoritas, t.otoritas, (select count(*) from t_otoritasuser o where o.idotoritas = t.idotoritas) as pengguna, (select count(*) from t_menuotoritas m where m.idotoritas = t.idotoritas) as menu') ->orderBy('t.idotoritas') ->get() ->map(fn (object $row): array => $this->present($row)); } /** * Satu otoritas dengan menunya dikelompokkan menurut induk (modul), berurutan kode menu. * * @return array|null */ public function find(int $id): ?array { $row = $this->db()->table('t_otoritas as t') ->selectRaw('t.idotoritas, t.otoritas, (select count(*) from t_otoritasuser o where o.idotoritas = t.idotoritas) as pengguna, (select count(*) from t_menuotoritas m where m.idotoritas = t.idotoritas) as menu') ->where('t.idotoritas', $id)->first(); if ($row === null) { return null; } $menus = $this->db()->table('t_menuotoritas as mo') ->join('t_menu as m', 'm.idmenu', '=', 'mo.idmenu') ->leftJoin('t_menu as p', 'p.idmenu', '=', 'm.idmenuinduk') ->where('mo.idotoritas', $id) ->orderBy('m.kdmenu')->orderBy('m.idmenu') ->get(['m.idmenu', 'm.kdmenu', 'm.menu', 'm.islink', 'mo.bybagian', 'p.menu as induk']); $groups = $menus ->groupBy(fn (object $menu): string => filled($menu->induk) ? trim($menu->induk) : 'Menu utama') ->map(fn (Collection $items): array => $items->map(fn (object $menu): array => [ 'kode' => trim((string) $menu->kdmenu), 'nama' => trim((string) $menu->menu), 'unit' => (bool) $menu->bybagian, ])->all()) ->all(); return $this->present($row) + ['groups' => $groups]; } /** @return array */ private function present(object $row): array { $role = LegacyRole::all(LegacyRole::JENIS_PEGAWAI, [(int) $row->idotoritas], false)[0] ?? null; return [ 'id' => (int) $row->idotoritas, 'nama' => trim((string) $row->otoritas), 'peran' => $role?->label() ?? 'Tanpa peran', 'pengguna' => (int) $row->pengguna, 'menu' => (int) $row->menu, ]; } }