Files
siakad/app/Http/Controllers/Operator/EmployeeListController.php
T

118 lines
5.3 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\Http\Controllers\Operator;
use App\Http\Controllers\Controller;
use App\Models\Legacy\Account;
use App\Support\Legacy\AcademicOverview;
use App\Support\Legacy\EmployeeDirectory;
use App\Support\Prototype\Satu\Catalog;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* Portal > Pegawai (slug list_pegawai) untuk akun DB lama: pegawai asli prodi/fakultas akun dengan format tabel
* SATU yang sama. Dipanggil SatuPageController; filter, pencarian, dan halaman diambil lewat data() (JSON).
*/
class EmployeeListController extends Controller
{
private const string SLUG = 'list_pegawai';
public function __construct(
private readonly AcademicOverview $overview,
private readonly EmployeeDirectory $directory,
) {}
public function __invoke(Request $request): View
{
$page = Catalog::page(self::SLUG);
$iduser = (int) $request->user()->getKey();
$programs = $this->overview->programs($iduser);
[$labelAdvisor, $labelActive, $labelStatus] = $page['filters'];
return view('operator.satu.remote-list', [
'page' => $page,
'crumbs' => ['Beranda' => route('operator.dashboard')] + array_fill_keys(Catalog::trail(self::SLUG), null) + [$page['title'] => null],
'note' => 'Data pegawai asli dari SIAKAD untuk program studi dan fakultas yang Anda kelola. Tombol aksi belum mengubah data.',
'empty' => ['icon' => 'building-library', 'title' => 'Akun belum terhubung ke program studi', 'description' => 'Daftar pegawai tampil setelah akun Anda dipetakan ke program studi oleh Pusat Komputer.'],
'url' => route('operator.pegawai.data'),
'filters' => [
['name' => 'advisor', 'label' => $labelAdvisor, 'options' => EmployeeDirectory::ADVISOR_OPTIONS],
['name' => 'active', 'label' => $labelActive, 'options' => EmployeeDirectory::ACTIVE_OPTIONS],
['name' => 'status', 'label' => $labelStatus, 'options' => $this->directory->statusOptions()->all()],
],
'columns' => [
['key' => 'nip', 'label' => 'NIP'],
['key' => 'nama', 'label' => 'Nama'],
['key' => 'kelamin', 'label' => 'L/P'],
['key' => 'nidn', 'label' => 'NIDN'],
['key' => 'nuptk', 'label' => 'NUPTK'],
['key' => 'telepon', 'label' => 'No. Telp.'],
['key' => 'email', 'label' => 'Email'],
['key' => 'status', 'label' => 'Status', 'badge' => true],
],
'perPage' => EmployeeDirectory::PER_PAGE,
'data' => $programs->isEmpty() ? null : $this->payload($this->search($request, $iduser, $programs->keys()->all())),
]);
}
/** JSON untuk tabel (satuRemoteTable). Hanya akun DB lama yang memegang prodi. */
public function data(Request $request): JsonResponse
{
abort_unless($request->user() instanceof Account, 404);
$iduser = (int) $request->user()->getKey();
$programs = $this->overview->programs($iduser);
abort_if($programs->isEmpty(), 404);
return response()->json($this->payload($this->search($request, $iduser, $programs->keys()->all())));
}
/**
* Nilai filter di luar pilihan diabaikan.
*
* @param list<int> $prodi
*/
private function search(Request $request, int $iduser, array $prodi): LengthAwarePaginator
{
$advisor = (string) $request->string('advisor');
$active = (string) $request->string('active');
$status = $request->filled('status') && $this->directory->statusOptions()->has($request->integer('status')) ? $request->integer('status') : null;
$perPage = in_array($request->integer('per'), EmployeeDirectory::PER_PAGE, true) ? $request->integer('per') : EmployeeDirectory::PER_PAGE[0];
return $this->directory->search($prodi, $this->directory->faculties($iduser), [
'q' => mb_substr(trim((string) $request->string('q')), 0, 100),
'advisor' => isset(EmployeeDirectory::ADVISOR_OPTIONS[$advisor]) ? $advisor : null,
'active' => isset(EmployeeDirectory::ACTIVE_OPTIONS[$active]) ? $active : null,
'status' => $status,
], $perPage);
}
/** @return array{rows: list<array<string, mixed>>, page: int, last_page: int, total: int, per_page: int} */
private function payload(LengthAwarePaginator $rows): array
{
$dash = fn (?string $value): string => $value ?? '–';
return [
'rows' => collect($rows->items())->map(fn (array $p): array => [
'id' => $p['id'],
'nip' => $dash($p['nip']),
'nama' => $p['nama'],
'kelamin' => $dash($p['kelamin']),
'nidn' => $dash($p['nidn']),
'nuptk' => '–',
'telepon' => $dash($p['telepon']),
'email' => $dash($p['email']),
'status' => $p['status']['label'],
'tone' => $p['status']['tone'] === 'neutral' ? null : $p['status']['tone'],
])->all(),
'page' => $rows->currentPage(),
'last_page' => $rows->lastPage(),
'total' => $rows->total(),
'per_page' => $rows->perPage(),
];
}
}