219 lines
9.6 KiB
PHP
219 lines
9.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Operator;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\Operator\Concerns\ServesLegacyList;
|
|
use App\Support\Legacy\PortalException;
|
|
use App\Support\Legacy\ReferenceData;
|
|
use App\Support\Legacy\ReferenceWriter;
|
|
use App\Support\Legacy\ReferenceWriters;
|
|
use App\Support\Legacy\UnitScope;
|
|
use App\Support\Prototype\Satu\Catalog;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
/**
|
|
* Data Pelengkap (tabel referensi SATU) untuk akun DB lama: satu pengendali untuk semua slug yang punya sumber data
|
|
* (ReferenceData::LISTS + data perguruan tinggi). Baris diambil lewat data() (JSON); tambah/ubah/hapus lewat penulis
|
|
* (ReferenceWriters) untuk slug yang boleh ditulis, sisanya hanya baca.
|
|
*/
|
|
class DataPelengkapController extends Controller
|
|
{
|
|
use ServesLegacyList;
|
|
|
|
public function __construct(private readonly ReferenceData $reference, private readonly UnitScope $scope) {}
|
|
|
|
public function __invoke(string $slug): View
|
|
{
|
|
$page = Catalog::page($slug) ?? abort(404);
|
|
$this->scopeReference();
|
|
|
|
if ($slug === ReferenceData::UNIVERSITY) {
|
|
return view('operator.satu.reference-detail', [
|
|
'page' => $page,
|
|
'crumbs' => $this->crumbs($slug, $page),
|
|
'note' => 'Data perguruan tinggi asli dari unit SIAKAD jenis Universitas. Nama Inggris, jenis, dan lembaga naungan tidak tercatat.',
|
|
'values' => $this->reference->university(),
|
|
]);
|
|
}
|
|
|
|
abort_unless(ReferenceData::isList($slug), 404);
|
|
$definition = $this->reference->definition($slug);
|
|
$writer = ReferenceWriters::for($slug);
|
|
$writer?->scopeTo($this->scope->units((int) auth()->id()));
|
|
$abilities = $writer?->abilities($slug);
|
|
$form = $writer && ($abilities['create'] || $abilities['update']) ? $writer->form($slug) : null;
|
|
|
|
return view('operator.satu.remote-list', [
|
|
'page' => $writer ? $this->withTools($page, $abilities) : $page,
|
|
'crumbs' => $this->crumbs($slug, $page),
|
|
'note' => $definition['note'].' '.$this->capabilityNote($abilities),
|
|
'empty' => ['icon' => 'circle-stack', 'title' => 'Belum ada data', 'description' => 'Data tampil setelah tabel sumbernya terisi.'],
|
|
'url' => route('operator.pelengkap.data', $slug),
|
|
'filters' => $definition['filters'],
|
|
'columns' => $definition['columns'],
|
|
'detailColumns' => $definition['detailColumns'],
|
|
'perPage' => ReferenceData::PER_PAGE,
|
|
'data' => $this->payload($this->decorate($writer, $slug, $this->reference->list($slug, [], ReferenceData::PER_PAGE[0]))),
|
|
'strict' => true,
|
|
'form' => $form ? [
|
|
'title' => $form['title'],
|
|
'storeUrl' => route('operator.pelengkap.store', $slug),
|
|
'updateUrl' => route('operator.pelengkap.update', [$slug, '__ID__']),
|
|
'fields' => $form['fields'],
|
|
] : null,
|
|
'destroyUrl' => ($abilities['delete'] ?? false) ? route('operator.pelengkap.destroy', $slug) : null,
|
|
'destroy' => ['title' => 'Hapus data terpilih?', 'message' => 'Data yang masih dipakai tabel lain tidak akan dihapus.'],
|
|
]);
|
|
}
|
|
|
|
/** JSON untuk tabel (satuRemoteTable). Nilai filter di luar pilihan diabaikan. */
|
|
public function data(Request $request, string $slug): JsonResponse
|
|
{
|
|
$this->legacyOnly($request);
|
|
abort_unless(ReferenceData::isList($slug), 404);
|
|
$this->scopeReference();
|
|
|
|
$filter = ['q' => $this->query($request)];
|
|
foreach ($this->reference->definition($slug)['filters'] as $f) {
|
|
$value = $request->input($f['name']);
|
|
if (is_string($value) && $value !== '' && isset($f['options'][$value])) {
|
|
$filter[$f['name']] = $value;
|
|
}
|
|
}
|
|
|
|
return response()->json($this->payload($this->decorate(ReferenceWriters::for($slug), $slug, $this->reference->list(
|
|
$slug,
|
|
$filter,
|
|
$this->perPage($request, ReferenceData::PER_PAGE),
|
|
max(1, $request->integer('page', 1)),
|
|
))));
|
|
}
|
|
|
|
public function store(Request $request, string $slug): JsonResponse
|
|
{
|
|
$writer = $this->writer($request, $slug, 'create');
|
|
$input = $this->input($request, $writer, $slug);
|
|
|
|
try {
|
|
$id = $writer->create($slug, $input, (string) $request->user()->username, $request->ip());
|
|
} catch (PortalException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['message' => 'Data ditambahkan.', 'id' => $id], 201);
|
|
}
|
|
|
|
public function update(Request $request, string $slug, int $id): JsonResponse
|
|
{
|
|
$writer = $this->writer($request, $slug, 'update');
|
|
$this->scopeReference();
|
|
abort_unless($this->reference->inScope($slug, $id), 404);
|
|
$input = $this->input($request, $writer, $slug);
|
|
|
|
try {
|
|
$writer->update($slug, $id, $input, (string) $request->user()->username, $request->ip());
|
|
} catch (PortalException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['message' => 'Data diperbarui.']);
|
|
}
|
|
|
|
/** Hapus satu atau beberapa baris (POST {ids}); seluruhnya ditolak bila ada yang masih dipakai. */
|
|
public function destroy(Request $request, string $slug): JsonResponse
|
|
{
|
|
$writer = $this->writer($request, $slug, 'delete');
|
|
$ids = $request->validate(['ids' => ['required', 'array', 'min:1', 'max:100'], 'ids.*' => ['integer', 'distinct']], [], ['ids' => 'data'])['ids'];
|
|
// Baris di luar unit akun (halaman bybagian) tidak boleh dihapus.
|
|
$this->scopeReference();
|
|
$ids = array_values(array_filter($ids, fn ($id): bool => $this->reference->inScope($slug, (int) $id)));
|
|
if ($ids === []) {
|
|
return response()->json(['message' => 'Data berada di luar unit Anda.'], 422);
|
|
}
|
|
|
|
try {
|
|
$deleted = $writer->delete($slug, $ids);
|
|
} catch (PortalException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['message' => "{$deleted} data dihapus."]);
|
|
}
|
|
|
|
/** Penulis slug ini bila akun DB lama dan kemampuan yang diminta tersedia; selain itu 404. */
|
|
/** Batasi halaman unit (Fakultas, Prodi, Jurusan, Ruang, Gedung) ke unit akun seperti menu lama bybagian; akun pusat tanpa unit = semua. */
|
|
private function scopeReference(): void
|
|
{
|
|
$this->reference->scopeTo($this->scope->units((int) auth()->id()));
|
|
}
|
|
|
|
private function writer(Request $request, string $slug, string $ability): ReferenceWriter
|
|
{
|
|
$this->legacyOnly($request);
|
|
$writer = ReferenceWriters::for($slug) ?? abort(404);
|
|
abort_unless($writer->abilities($slug)[$ability], 404);
|
|
// Pilihan dan nilai unit/gedung/fakultas/jurusan dibatasi ke unit akun (menu lama bybagian).
|
|
$writer->scopeTo($this->scope->units((int) auth()->id()));
|
|
|
|
return $writer;
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
private function input(Request $request, ReferenceWriter $writer, string $slug): array
|
|
{
|
|
[$rules, $attributes] = $writer->rules($slug);
|
|
|
|
return $request->validate($rules, [], $attributes);
|
|
}
|
|
|
|
/** Sisipkan nilai isian formulir (row.form) pada baris halaman bila slug bisa diubah. */
|
|
private function decorate(?ReferenceWriter $writer, string $slug, LengthAwarePaginator $rows): LengthAwarePaginator
|
|
{
|
|
if ($writer === null || ! $writer->abilities($slug)['update']) {
|
|
return $rows;
|
|
}
|
|
|
|
$values = $writer->formValues($slug, $rows->getCollection()->pluck('id')->all());
|
|
|
|
return $rows->setCollection($rows->getCollection()->map(fn (array $row): array => isset($values[$row['id']]) ? $row + ['form' => $values[$row['id']]] : $row));
|
|
}
|
|
|
|
/**
|
|
* Tombol halaman menurut kemampuan: Tambah bila boleh tambah, Hapus (dan kotak centang) bila boleh hapus, kolom
|
|
* Aksi bila ada yang bisa dilakukan per baris. Tombol prototipe lain dibuang agar tidak ada tombol palsu.
|
|
*
|
|
* @param array<string, mixed> $page
|
|
* @param array{create: bool, update: bool, delete: bool} $abilities
|
|
*/
|
|
private function withTools(array $page, array $abilities): array
|
|
{
|
|
$columns = array_values(array_filter($page['columns'], fn (string $c): bool => ! in_array($c, ['#', 'Aksi'], true)));
|
|
if ($abilities['delete']) {
|
|
array_unshift($columns, '#');
|
|
}
|
|
if ($abilities['update'] || $abilities['delete']) {
|
|
$columns[] = 'Aksi';
|
|
}
|
|
|
|
$flags = ($abilities['create'] ? 'T' : '').($abilities['delete'] ? 'H' : '').(str_contains($page['flags'], 'C') ? 'C' : '');
|
|
|
|
return ['columns' => $columns, 'flags' => $flags] + $page;
|
|
}
|
|
|
|
/** @param array{create: bool, update: bool, delete: bool}|null $abilities */
|
|
private function capabilityNote(?array $abilities): string
|
|
{
|
|
if ($abilities === null) {
|
|
return 'Halaman ini hanya menampilkan data; pengelolaannya di halaman sumber (mis. Set Grup MK atau kurikulum).';
|
|
}
|
|
|
|
$do = array_filter(['tambah' => $abilities['create'], 'ubah' => $abilities['update'], 'hapus' => $abilities['delete']]);
|
|
|
|
return ucfirst(implode(', ', array_keys($do))).' langsung tersimpan di basis data'.($abilities['delete'] ? '; data yang masih dipakai tabel lain tidak bisa dihapus.' : '.');
|
|
}
|
|
}
|