ekosistem basis data tambah field enum
This commit is contained in:
@@ -49,18 +49,27 @@ class KolomBasisDataController extends Controller
|
||||
'kolom' => ['required',
|
||||
'string',
|
||||
'regex:/^[a-z0-9]+(_[a-z0-9]+)*$/',
|
||||
Rule::unique('ekosistem_basis_data_kolom')->where(function ($query) use ($request) {
|
||||
return $query->where('tabel_id', $request->tabel_id)->whereNull('deleted_at');
|
||||
Rule::unique('ekosistem_basis_data_kolom')->where(function ($query) use ($tabelId) {
|
||||
return $query->where('tabel_id', $tabelId)->whereNull('deleted_at');
|
||||
})
|
||||
],
|
||||
'label' => ['required', 'string'],
|
||||
'tipe' => ['required', 'string', Rule::in(['varchar', 'integer', 'text', 'date', 'boolean', 'datetime', 'float', 'foreign_key'])],
|
||||
'tipe' => ['required', 'string',
|
||||
Rule::in(['varchar', 'integer', 'text', 'date', 'boolean', 'datetime', 'float', 'foreign_key', 'enum'])],
|
||||
'validasi' => ['nullable', 'json'],
|
||||
'relasi_tabel' => ['nullable', 'string'],
|
||||
'relasi_kolom_id' => ['nullable', 'string'],
|
||||
'relasi_kolom_label' => ['nullable', 'string'],
|
||||
'enum_opsi' => ['nullable', 'json'],
|
||||
]);
|
||||
|
||||
if (!empty($validated['enum_opsi'])) {
|
||||
$validated['enum_opsi'] = json_decode($validated['enum_opsi'], true);
|
||||
}
|
||||
if (!empty($validated['validasi'])) {
|
||||
$validated['validasi'] = json_decode($validated['validasi'], true);
|
||||
}
|
||||
|
||||
KolomBasisData::create([...$validated,
|
||||
'tabel_id' => $tabelId,
|
||||
'created_by' => auth()->id(),
|
||||
@@ -123,11 +132,13 @@ class KolomBasisDataController extends Controller
|
||||
})
|
||||
],
|
||||
'label' => ['required', 'string'],
|
||||
'tipe' => ['required', 'string', Rule::in(['varchar', 'integer', 'text', 'date', 'boolean', 'datetime', 'float', 'foreign_key'])],
|
||||
'tipe' => ['required', 'string',
|
||||
Rule::in(['varchar', 'integer', 'text', 'date', 'boolean', 'datetime', 'float', 'foreign_key', 'enum'])],
|
||||
'validasi' => ['nullable', 'json'],
|
||||
'relasi_tabel' => ['nullable', 'string'],
|
||||
'relasi_kolom_id' => ['nullable', 'string'],
|
||||
'relasi_kolom_label' => ['nullable', 'string'],
|
||||
'enum_opsi' => ['nullable', 'json'],
|
||||
]);
|
||||
|
||||
$kolom->update([...$validated,
|
||||
|
||||
@@ -75,6 +75,23 @@ class ValueBasisDataController extends Controller
|
||||
END
|
||||
) AS \"{$k->kolom}\"
|
||||
";
|
||||
} elseif ($k->tipe == 'enum') {
|
||||
$selects[] = "
|
||||
MAX(
|
||||
CASE
|
||||
WHEN kolom_id = {$k->id}
|
||||
THEN (
|
||||
SELECT elem->>'label'
|
||||
FROM ekosistem_basis_data_kolom k,
|
||||
jsonb_array_elements(k.enum_opsi::jsonb) AS elem
|
||||
WHERE k.id = kolom_id
|
||||
AND elem->>'value' = value_enum
|
||||
LIMIT 1
|
||||
)
|
||||
ELSE NULL
|
||||
END
|
||||
) AS \"{$k->kolom}\"
|
||||
";
|
||||
} else {
|
||||
$selects[] = "
|
||||
MAX(
|
||||
@@ -178,6 +195,7 @@ class ValueBasisDataController extends Controller
|
||||
}
|
||||
}
|
||||
$list_relasi = collect($list_relasi);
|
||||
// dd($kolom->where('tipe', 'enum')->first()?->enum_opsi);
|
||||
|
||||
return view('ekosistem.value-basis-data.add', compact('tabel', 'kolom', 'program', 'list_relasi'));
|
||||
}
|
||||
@@ -250,12 +268,63 @@ class ValueBasisDataController extends Controller
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
public function show(Request $request, string $tabelId, string $id): View
|
||||
{
|
||||
//
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$tabel = TabelBasisData::find($tabelId);
|
||||
if (!$tabel) {
|
||||
abort(404, 'Data tabel tidak ditemukan.');
|
||||
}
|
||||
$kolom = KolomBasisData::where('tabel_id', $tabelId)->get();
|
||||
if ($kolom->isEmpty()) {
|
||||
abort(404, 'Data kolom tidak ditemukan untuk tabel ini.');
|
||||
}
|
||||
|
||||
if (app()->has('SystemProgram')) {
|
||||
$program = app('SystemProgram');
|
||||
} else {
|
||||
if ($request->has('program_id') && $request->query('program_id')) {
|
||||
$programId = $request->query('program_id');
|
||||
$program = Program::find($programId);
|
||||
if (!$program) {
|
||||
abort(400, 'Program tidak ditemukan.');
|
||||
}
|
||||
} else {
|
||||
abort(400, 'Program tidak ditemukan.');
|
||||
}
|
||||
}
|
||||
|
||||
$values = ValueBasisData::query()
|
||||
->where('tabel_id', $tabelId)
|
||||
->where('value_id', $id)
|
||||
->where('program_id', $program->id)
|
||||
->get();
|
||||
if ($values->isEmpty()) {
|
||||
abort(404, 'Data tidak ditemukan.');
|
||||
}
|
||||
|
||||
foreach ($values as $v) {
|
||||
$k = $kolom->where('id', $v->kolom_id)->first();
|
||||
if ($k->tipe == 'foreign_key') {
|
||||
$v->value_relasi_label = DB::table($k->relasi_tabel)->where($k->relasi_kolom_id, $v->value_relasi)->value($k->relasi_kolom_label);
|
||||
} else if ($k->tipe == 'enum') {
|
||||
$opsi = collect($k->enum_opsi);
|
||||
$found = $opsi->first(function ($item) use ($v) {
|
||||
return (string) $item['value'] === (string) $v->value_enum;
|
||||
});
|
||||
$v->value_enum_label = $found['label'] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
$value_id = $id;
|
||||
|
||||
return view('ekosistem.value-basis-data.view', compact('tabel', 'kolom', 'program', 'values', 'value_id'));
|
||||
}
|
||||
|
||||
public function edit(Request $request, string $tabelId, string $id)
|
||||
public function edit(Request $request, string $tabelId, string $id): View
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
@@ -317,7 +386,7 @@ class ValueBasisDataController extends Controller
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $tabelId, string $id)
|
||||
public function update(Request $request, string $tabelId, string $id): RedirectResponse
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
@@ -408,7 +477,7 @@ class ValueBasisDataController extends Controller
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Request $request, string $tabelId, string $id)
|
||||
public function destroy(Request $request, string $tabelId, string $id): RedirectResponse
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin'])) {
|
||||
abort(403);
|
||||
|
||||
Reference in New Issue
Block a user