[Superuser] Integrasi Pertama ke Database
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Legacy;
|
||||
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Hak akses (Superuser): otoritas asli t_otoritas beserta jumlah pemegangnya (t_otoritasuser), menu yang boleh dibuka
|
||||
* (t_menuotoritas atas t_menu), dan role SIAKAD tempat otoritas itu dipetakan (LegacyRole). Hanya baca.
|
||||
*/
|
||||
final class AuthorityDirectory
|
||||
{
|
||||
private function db(): ConnectionInterface
|
||||
{
|
||||
return DB::connection('untan');
|
||||
}
|
||||
|
||||
/** @return Collection<int, array<string, mixed>> */
|
||||
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<string, mixed>|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<string, mixed> */
|
||||
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,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user