89 lines
3.8 KiB
PHP
89 lines
3.8 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Operator;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Http\Controllers\Operator\Concerns\ServesLegacyList;
|
||
use App\Support\Legacy\AcademicOverview;
|
||
use App\Support\Legacy\EmployeeBiodata;
|
||
use App\Support\Legacy\EmployeeDirectory;
|
||
use App\Support\Legacy\LegacyFormat;
|
||
use App\Support\Legacy\PortalException;
|
||
use App\Support\Prototype\Satu\Catalog;
|
||
use Illuminate\Contracts\View\View;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Http\Request;
|
||
|
||
/**
|
||
* Detail pegawai (aksi Lihat pada Portal > Pegawai) untuk akun DB lama: ringkasan, biodata lengkap per bagian (pribadi,
|
||
* kontak, kepegawaian, pendidikan dan rekening, data dosen), dan penyimpanan per bagian lewat JSON. Hanya pegawai dalam
|
||
* cakupan daftar (dosen home base prodi akun atau pemegang jabatan aktif di prodi/fakultas akun).
|
||
*/
|
||
class EmployeeDetailController extends Controller
|
||
{
|
||
use ServesLegacyList;
|
||
|
||
private const string SLUG = 'list_pegawai';
|
||
|
||
public function __construct(
|
||
private readonly AcademicOverview $overview,
|
||
private readonly EmployeeDirectory $directory,
|
||
private readonly EmployeeBiodata $biodata,
|
||
) {}
|
||
|
||
public function show(Request $request, int $id): View
|
||
{
|
||
[$prodi, $faculties] = $this->scope($request);
|
||
$bio = $this->biodata->find($prodi, $faculties, $id) ?? abort(404);
|
||
$page = Catalog::page(self::SLUG);
|
||
$sections = $bio['sections'];
|
||
$extra = $bio['extra'];
|
||
|
||
// Keterangan hanya dibaca di bagian Kepegawaian.
|
||
$sections['kepegawaian']['readonly'] = [['name' => 'jenis', 'label' => 'Jenis pegawai'], ['name' => 'jabatan', 'label' => 'Jabatan aktif']];
|
||
$sections['kepegawaian']['display'] += ['jenis' => $extra['jenis'] ?? '–', 'jabatan' => $extra['jabatan'] === [] ? '–' : implode('; ', $extra['jabatan'])];
|
||
|
||
$v = $sections['pribadi']['values'];
|
||
|
||
return view('operator.satu.pegawai-detail', [
|
||
'page' => $page,
|
||
'crumbs' => ['Beranda' => route(auth()->user()->role->dashboardRoute())] + array_fill_keys(Catalog::trail(self::SLUG), null) + [$page['title'] => route('operator.halaman', self::SLUG), 'Detail' => null],
|
||
'sections' => $sections,
|
||
'extra' => $extra,
|
||
'name' => LegacyFormat::pegawaiName($v['gelardepan'], $v['nama'], $v['gelarbelakang']),
|
||
'back' => route('operator.halaman', self::SLUG),
|
||
'saveUrl' => route('operator.list-pegawai.biodata', [$id, '__SECTION__']),
|
||
]);
|
||
}
|
||
|
||
public function update(Request $request, int $id, string $section): JsonResponse
|
||
{
|
||
[$prodi, $faculties] = $this->scope($request);
|
||
abort_unless(in_array($section, EmployeeBiodata::sectionKeys(), true) && $this->biodata->exists($prodi, $faculties, $id), 404);
|
||
|
||
try {
|
||
$input = $this->biodata->validated($prodi, $faculties, $id, $section, $request->all());
|
||
$result = $this->biodata->update($prodi, $faculties, $id, $section, $input, (string) $request->user()->username, $request->ip());
|
||
} catch (PortalException $e) {
|
||
return response()->json(['message' => $e->getMessage()], 422);
|
||
}
|
||
|
||
return response()->json([
|
||
'message' => $result['changed'] === 0 ? 'Tidak ada perubahan.' : 'Biodata diperbarui.',
|
||
'values' => $result['section']['values'],
|
||
'display' => $result['section']['display'],
|
||
]);
|
||
}
|
||
|
||
/** @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)];
|
||
}
|
||
}
|