111 lines
4.4 KiB
PHP
111 lines
4.4 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\SurveyAnswerTypes;
|
|
use App\Support\Prototype\Satu\Catalog;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* Setting > Kuesioner Layanan > Jenis Jawaban (slug list_jenisjawabanangket) untuk akun DB lama: daftar dengan centang
|
|
* baris, kolom Jenis Jawaban/Jawaban/Aksi seperti SATU, tambah/ubah lewat modal (nama + daftar jawaban bernilai),
|
|
* hapus per baris atau massal lewat konfirmasi (t_jenisjawabanangket + t_jawabanangket).
|
|
*/
|
|
class JenisJawabanController extends Controller
|
|
{
|
|
use ServesLegacyList;
|
|
|
|
private const string SLUG = 'list_jenisjawabanangket';
|
|
|
|
public function __construct(private readonly SurveyAnswerTypes $types) {}
|
|
|
|
public function __invoke(): View
|
|
{
|
|
$page = Catalog::page(self::SLUG);
|
|
$ready = $this->types->ready();
|
|
|
|
return view('operator.satu.jenis-jawaban', [
|
|
'page' => $page,
|
|
'crumbs' => $this->crumbs(self::SLUG, $page),
|
|
'note' => $ready
|
|
? 'Jenis jawaban kuesioner tersimpan di SIAKAD (tabel t_jenisjawabanangket dan t_jawabanangket). Tambah, ubah, dan hapus langsung tersimpan di basis data.'
|
|
: 'Tabel jenis jawaban belum disiapkan di basis data (jalankan php artisan siakad:siapkan-portal); daftar masih kosong dan belum bisa diubah.',
|
|
'ready' => $ready,
|
|
'rows' => $this->types->all()->all(),
|
|
'urls' => [
|
|
'data' => route('operator.jenis-jawaban.data'),
|
|
'store' => route('operator.jenis-jawaban.store'),
|
|
'update' => route('operator.jenis-jawaban.update', '__ID__'),
|
|
'destroy' => route('operator.jenis-jawaban.destroy'),
|
|
],
|
|
'perPage' => [10, 25, 50, 100],
|
|
]);
|
|
}
|
|
|
|
public function data(Request $request): JsonResponse
|
|
{
|
|
$this->legacyOnly($request);
|
|
|
|
return response()->json(['rows' => $this->types->all()->all()]);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$this->legacyOnly($request);
|
|
$input = $this->validated($request);
|
|
|
|
try {
|
|
$id = $this->types->create($input, (string) $request->user()->username, $request->ip());
|
|
} catch (PortalException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['message' => 'Jenis jawaban ditambahkan.', 'id' => $id, 'rows' => $this->types->all()->all()], 201);
|
|
}
|
|
|
|
public function update(Request $request, int $id): JsonResponse
|
|
{
|
|
$this->legacyOnly($request);
|
|
$input = $this->validated($request);
|
|
|
|
try {
|
|
$this->types->update($id, $input, (string) $request->user()->username, $request->ip());
|
|
} catch (PortalException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['message' => 'Jenis jawaban diperbarui.', 'rows' => $this->types->all()->all()]);
|
|
}
|
|
|
|
/** Hapus satu atau beberapa jenis (POST {ids: [...]}, seperti tombol Hapus massal SATU). */
|
|
public function destroy(Request $request): JsonResponse
|
|
{
|
|
$this->legacyOnly($request);
|
|
$ids = $request->validate(['ids' => ['required', 'array', 'min:1'], 'ids.*' => ['integer']], [], ['ids' => 'jenis jawaban'])['ids'];
|
|
|
|
try {
|
|
$deleted = $this->types->delete($ids);
|
|
} catch (PortalException $e) {
|
|
return response()->json(['message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['message' => "{$deleted} jenis jawaban dihapus.", 'rows' => $this->types->all()->all()]);
|
|
}
|
|
|
|
/** @return array{nama: string, jawaban: list<array{nilai: int, teks: string}>} */
|
|
private function validated(Request $request): array
|
|
{
|
|
return $request->validate([
|
|
'nama' => ['required', 'string', 'max:'.SurveyAnswerTypes::NAMA_MAX],
|
|
'jawaban' => ['required', 'array', 'min:1'],
|
|
'jawaban.*.nilai' => ['required', 'integer', 'between:0,100'],
|
|
'jawaban.*.teks' => ['required', 'string', 'max:'.SurveyAnswerTypes::JAWABAN_MAX],
|
|
], [], ['nama' => 'nama jenis jawaban', 'jawaban' => 'jawaban', 'jawaban.*.nilai' => 'nilai jawaban', 'jawaban.*.teks' => 'teks jawaban']);
|
|
}
|
|
}
|