Files
siakad/app/Http/Controllers/Operator/EmployeeListController.php
T
2026-11-09 10:14:07 +07:00

216 lines
10 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\Http\Controllers\Operator\Concerns\ServesLegacyList;
use App\Models\Legacy\Account;
use App\Support\Legacy\AcademicOverview;
use App\Support\Legacy\EmployeeDirectory;
use App\Support\Legacy\EmployeeRecords;
use App\Support\Legacy\PortalException;
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
{
use ServesLegacyList;
private const string SLUG = 'list_pegawai';
public function __construct(
private readonly AcademicOverview $overview,
private readonly EmployeeDirectory $directory,
private readonly EmployeeRecords $records,
) {}
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. Ubah dan hapus langsung tersimpan di basis data; pegawai yang masih dirujuk jabatan, jadwal, bimbingan, skripsi, atau akun login tidak dapat dihapus.',
'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())),
'form' => $programs->isEmpty() ? null : $this->form(),
'destroyUrl' => $programs->isEmpty() ? null : route('operator.list-pegawai.destroy'),
'destroy' => ['title' => 'Hapus pegawai?', 'message' => 'Hanya pegawai yang tidak dirujuk jabatan, jadwal, bimbingan, skripsi, atau akun login yang dapat dihapus.'],
]);
}
public function update(Request $request, int $id): JsonResponse
{
[$prodi, $faculties] = $this->scope($request);
$input = $this->validated($request);
try {
$this->records->update($prodi, $faculties, $id, $input, (string) $request->user()->username, $request->ip());
} catch (PortalException $e) {
return response()->json(['message' => $e->getMessage()], 422);
}
return response()->json(['message' => 'Data pegawai diperbarui.']);
}
/** Hapus satu atau beberapa pegawai (POST {ids}). */
public function destroy(Request $request): JsonResponse
{
[$prodi, $faculties] = $this->scope($request);
$ids = $request->validate(['ids' => ['required', 'array', 'min:1', 'max:100'], 'ids.*' => ['integer']], [], ['ids' => 'pegawai'])['ids'];
try {
$deleted = $this->records->delete($prodi, $faculties, $ids);
} catch (PortalException $e) {
return response()->json(['message' => $e->getMessage()], 422);
}
return response()->json(['message' => "{$deleted} pegawai dihapus."]);
}
/** @return array{0: list<int>, 1: list<int>} prodi dan fakultas akun */
private function scope(Request $request): array
{
$this->legacyOnly($request);
$iduser = (int) $request->user()->getKey();
$prodi = $this->overview->programs($iduser)->keys()->all();
abort_if($prodi === [], 404);
return [$prodi, $this->directory->faculties($iduser)];
}
/** @return array<string, mixed> */
private function validated(Request $request): array
{
return $request->validate([
'nip' => ['required', 'string', 'max:20'],
'nama' => ['required', 'string', 'max:50'],
'gelardepan' => ['nullable', 'string', 'max:20'],
'gelarbelakang' => ['nullable', 'string', 'max:25'],
'kelamin' => ['nullable', 'integer', 'in:1,2'],
'nidn' => ['nullable', 'string', 'max:25'],
'hp' => ['nullable', 'string', 'max:20'],
'telepon' => ['nullable', 'string', 'max:15'],
'email' => ['nullable', 'string', 'email', 'max:125'],
'status' => ['nullable', 'integer'],
'aktif' => ['required', 'integer', 'in:0,1'],
], [], [
'nip' => 'NIP', 'nama' => 'nama', 'gelardepan' => 'gelar depan', 'gelarbelakang' => 'gelar belakang', 'kelamin' => 'jenis kelamin',
'nidn' => 'NIDN', 'hp' => 'no. HP', 'telepon' => 'no. telepon', 'email' => 'email', 'status' => 'status kepegawaian', 'aktif' => 'status aktif',
]);
}
/**
* Formulir ubah (modal); template SATU tidak punya tombol Tambah untuk Pegawai.
*
* @return array<string, mixed>
*/
private function form(): array
{
return [
'title' => 'pegawai',
'storeUrl' => null,
'updateUrl' => route('operator.list-pegawai.update', '__ID__'),
'fields' => [
['name' => 'nip', 'label' => 'NIP', 'type' => 'text', 'required' => true, 'max' => 20],
['name' => 'nama', 'label' => 'Nama', 'type' => 'text', 'required' => true, 'max' => 50],
['name' => 'gelardepan', 'label' => 'Gelar depan', 'type' => 'text', 'max' => 20],
['name' => 'gelarbelakang', 'label' => 'Gelar belakang', 'type' => 'text', 'max' => 25],
['name' => 'kelamin', 'label' => 'Jenis kelamin', 'type' => 'select', 'options' => EmployeeRecords::GENDER],
['name' => 'nidn', 'label' => 'NIDN', 'type' => 'text', 'max' => 25],
['name' => 'hp', 'label' => 'No. HP', 'type' => 'text', 'max' => 20],
['name' => 'telepon', 'label' => 'No. telepon', 'type' => 'text', 'max' => 15],
['name' => 'email', 'label' => 'Email', 'type' => 'text', 'max' => 125],
['name' => 'status', 'label' => 'Status kepegawaian', 'type' => 'select', 'options' => $this->records->statusOptions()],
['name' => 'aktif', 'label' => 'Status aktif', 'type' => 'select', 'options' => EmployeeRecords::ACTIVE, 'required' => true, 'default' => 1],
],
];
}
/** 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'],
'form' => $p['form'],
'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(),
];
}
}