236 lines
12 KiB
PHP
236 lines
12 KiB
PHP
<?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
|
||
{
|
||
// Template SATU Pegawai tidak punya Tambah dan Hapus di bilah; keduanya dipasang di sini karena sudah bekerja.
|
||
$page = ['flags' => 'TH'] + 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. Tambah dan hapus langsung tersimpan di basis data; pegawai baru ditambahkan sebagai dosen dengan home base program studi Anda, dan pegawai yang masih dirujuk jabatan, jadwal, bimbingan, skripsi, atau akun login tidak dapat dihapus. Pilih Lihat untuk biodata lengkap; perubahan data dilakukan di dalam halaman detail.',
|
||
'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'),
|
||
'viewUrl' => route('operator.list-pegawai.show', '__ID__'),
|
||
// Hanya Tambah dan Hapus di bilah (tanpa Cetak) dan tanpa Ubah di baris: biodata diubah di halaman detail.
|
||
'tools' => ['T', 'H'],
|
||
'editable' => false,
|
||
'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($programs->all()),
|
||
'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 store(Request $request): JsonResponse
|
||
{
|
||
[$prodi] = $this->scope($request);
|
||
// Home base diperiksa bersama isian lain agar semua galat tampil sekaligus.
|
||
$input = $this->validated($request, creating: true);
|
||
$homeBase = (int) $input['prodi'];
|
||
|
||
try {
|
||
$id = $this->records->create($prodi, $homeBase, $input, (string) $request->user()->username, $request->ip());
|
||
} catch (PortalException $e) {
|
||
return response()->json(['message' => $e->getMessage()], 422);
|
||
}
|
||
|
||
return response()->json(['message' => 'Pegawai ditambahkan.', 'id' => $id], 201);
|
||
}
|
||
|
||
/** 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, bool $creating = false): array
|
||
{
|
||
return $request->validate($creating ? ['prodi' => ['required', 'integer']] + $this->rules() : $this->rules(), [], $this->attributes() + ['prodi' => 'home base']);
|
||
}
|
||
|
||
/** @return array<string, list<string>> */
|
||
private function rules(): array
|
||
{
|
||
return [
|
||
'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'],
|
||
];
|
||
}
|
||
|
||
/** @return array<string, string> */
|
||
private function attributes(): array
|
||
{
|
||
return [
|
||
'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 tambah (modal; tidak ada ubah di daftar). Biodata lengkap diubah di
|
||
* halaman detail.
|
||
*
|
||
* @param array<int, string> $programs
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function form(array $programs): array
|
||
{
|
||
return [
|
||
'title' => 'pegawai',
|
||
'storeUrl' => route('operator.list-pegawai.store'),
|
||
'fields' => [
|
||
['name' => 'prodi', 'label' => 'Home base (program studi)', 'type' => 'select', 'options' => $programs, 'required' => true, 'hint' => 'Pegawai baru ditambahkan sebagai dosen pada program studi ini.'],
|
||
['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'],
|
||
'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(),
|
||
];
|
||
}
|
||
}
|