program dan basis data ekosistem

This commit is contained in:
2026-04-20 16:05:27 +07:00
parent 0bbcb22d50
commit 4032eb6e75
49 changed files with 4384 additions and 155 deletions
@@ -0,0 +1,138 @@
<?php
namespace App\Http\Controllers\Web\Ekosistem;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use App\Models\Ekosistem\GroupBasisData;
class GroupBasisDataController extends Controller
{
public function index(Request $request): View
{
Gate::authorize('super_admin');
$title = 'Program';
$perPage = $request->query('per_page', 10);
$search = strtolower($request->query('search', ''));
$group = GroupBasisData::with('tabel')
->whereRaw('lower(\'group\') LIKE ?', ["%$search%"])
->paginate($perPage);
return view('ekosistem.group-basis-data.index', compact('title', 'group'));
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
return view('ekosistem.group-basis-data.add');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
Gate::authorize('super_admin');
$validated = $request->validate([
'group' => ['required',
'string',
'regex:/^[a-z0-9]+(_[a-z0-9]+)*$/',
'unique:ekosistem_basis_data_group,group'
],
'label' => ['required', 'string'],
'urutan' => ['nullable', 'integer', 'min:1'],
'deskripsi' => ['nullable', 'string'],
]);
GroupBasisData::create([...$validated,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
return redirect()->route('group.index')->with('success', 'Kelompok basis data berhasil ditambahkan.');
}
/**
* Display the specified resource.
*/
public function show(string $id): View
{
Gate::authorize('super_admin');
$group = GroupBasisData::find($id);
if (!$group) {
return redirect()->route('group-basis-data.index')->with('error', 'Kelompok basis data tidak ditemukan.');
}
return view('ekosistem.group-basis-data.view', compact('group'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id): View
{
Gate::authorize('super_admin');
$group = GroupBasisData::find($id);
if (!$group) {
return redirect()->route('group-basis-data.index')->with('error', 'Kelompok basis data tidak ditemukan.');
}
return view('ekosistem.group-basis-data.edit', compact('group'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id): RedirectResponse
{
Gate::authorize('super_admin');
$group = GroupBasisData::find($id);
if (!$group) {
return redirect()->route('group-basis-data.index')->with('error', 'Kelompok basis data tidak ditemukan.');
}
$validated = $request->validate([
'group' => ['required',
'string',
'regex:/^[a-z0-9]+(_[a-z0-9]+)*$/',
'unique:ekosistem_basis_data_tabel,tabel'
],
'label' => ['required', 'string'],
'urutan' => ['nullable', 'integer', 'min:1'],
'deskripsi' => ['nullable', 'string'],
]);
$group->update([...$validated,
'updated_by' => auth()->id(),
]);
return redirect()->route('group.index')->with('success', 'Kelompok basis data berhasil diperbarui.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
Gate::authorize('super_admin');
$group = TabelBasisData::find($id);
if (!$group) {
return redirect()->route('group.index')->with('error', 'Kelompok basis data tidak ditemukan.');
}
$group->delete();
return redirect()->route('group.index')->with('success', 'Kelompok basis data berhasil dihapus.');
}
}
@@ -0,0 +1,218 @@
<?php
namespace App\Http\Controllers\Web\Ekosistem;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use Illuminate\Http\JsonResponse;
use App\Models\Ekosistem\KolomBasisData;
use App\Models\Ekosistem\TabelBasisData;
class KolomBasisDataController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request, string $tabelId): JsonResponse
{
Gate::authorize('super_admin');
$tabel = TabelBasisData::find($tabelId);
if (!$tabel) {
return response()->json([
'status' => 'error',
'message' => 'Data tabel tidak ditemukan.'
], 404);
}
$validated = $request->validate([
'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');
})
],
'label' => ['required', 'string'],
'tipe' => ['required', 'string', Rule::in(['varchar', 'integer', 'text', 'date', 'boolean', 'datetime', 'float'])],
'validasi' => ['nullable', 'json'],
]);
KolomBasisData::create([...$validated,
'tabel_id' => $tabelId,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
return response()->json([
'status' => 'success',
'message' => 'Kolom basis data berhasil ditambahkan.'
], 201);
}
/**
* Display the specified resource.
*/
public function show(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$kolom = KolomBasisData::where('tabel_id', $tabelId)->find($id);
if (!$kolom) {
return response()->json([
'status' => 'error',
'message' => 'Data kolom tidak ditemukan.'
], 404);
}
return response()->json(['data' => $kolom, 'status' => 'success']);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$kolom = KolomBasisData::where('tabel_id', $tabelId)->find($id);
if (!$kolom) {
return response()->json([
'status' => 'error',
'message' => 'Data kolom tidak ditemukan.'
], 404);
}
$validated = $request->validate([
'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');
})
],
'label' => ['required', 'string'],
'tipe' => ['required', 'string', Rule::in(['varchar', 'integer', 'text', 'date', 'boolean', 'datetime', 'float'])],
'validasi' => ['nullable', 'json'],
]);
$kolom->update([...$validated,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
return response()->json([
'status' => 'success',
'message' => 'Kolom basis data berhasil diperbarui.'
], 200);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$kolom = KolomBasisData::where('tabel_id', $tabelId)->find($id);
if (!$kolom) {
return response()->json([
'status' => 'error',
'message' => 'Data kolom tidak ditemukan.'
], 404);
}
$kolom->delete();
return response()->json([
'status' => 'success',
'message' => 'Kolom basis data berhasil dihapus.'
], 200);
}
public function moveUp(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$current = KolomBasisData::where('tabel_id', $tabelId)->find($id);
if (!$current) {
return response()->json([
'status' => 'error',
'message' => 'Data kolom tidak ditemukan.'
], 404);
}
$prev = KolomBasisData::where('tabel_id', $current->tabel_id)
->where('urutan', '<', $current->urutan)
->orderBy('urutan', 'desc')
->first();
if ($prev) {
// swap
[$current->urutan, $prev->urutan] = [$prev->urutan, $current->urutan];
$current->save();
$prev->save();
}
return response()->json([
'status' => 'success',
'message' => 'Kolom basis data berhasil diperbarui.'
], 200);
}
public function moveDown(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$current = KolomBasisData::where('tabel_id', $tabelId)->find($id);
if (!$current) {
return response()->json([
'status' => 'error',
'message' => 'Data kolom tidak ditemukan.'
], 404);
}
$next = KolomBasisData::where('tabel_id', $current->tabel_id)
->where('urutan', '>', $current->urutan)
->orderBy('urutan', 'asc')
->first();
if ($next) {
// swap
[$current->urutan, $next->urutan] = [$next->urutan, $current->urutan];
$current->save();
$next->save();
}
return response()->json([
'status' => 'success',
'message' => 'Kolom basis data berhasil diperbarui.'
], 200);
}
}
@@ -0,0 +1,204 @@
<?php
namespace App\Http\Controllers\Web\Ekosistem;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;
use App\Models\Ekosistem\TabelBasisData;
use App\Models\Ekosistem\GroupBasisData;
class TabelBasisDataController extends Controller
{
public function index(Request $request): View
{
Gate::authorize('super_admin');
$title = 'Program';
$perPage = $request->query('per_page', 10);
$search = strtolower($request->query('search', ''));
$tabel = TabelBasisData::with('kolom')
->whereRaw('lower(tabel) LIKE ?', ["%$search%"])
->paginate($perPage);
return view('ekosistem.tabel-basis-data.index', compact('title', 'tabel'));
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
$group = GroupBasisData::all();
return view('ekosistem.tabel-basis-data.add', compact('group'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
Gate::authorize('super_admin');
$validated = $request->validate([
'tabel' => ['required',
'string',
'regex:/^[a-z0-9]+(_[a-z0-9]+)*$/',
'unique:ekosistem_basis_data_tabel,tabel'
],
'label' => ['required', 'string'],
'menu' => ['required', 'string'],
'deskripsi' => ['nullable', 'string'],
'group_id' => ['nullable', 'exists:ekosistem_basis_data_group,id'],
]);
TabelBasisData::create([...$validated,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
return redirect()->route('tabel.index')->with('success', 'Tabel basis data berhasil ditambahkan.');
}
/**
* Display the specified resource.
*/
public function show(string $id): View
{
Gate::authorize('super_admin');
$tabel = TabelBasisData::with('kolom')->find($id);
if (!$tabel) {
return redirect()->route('tabel-basis-data.index')->with('error', 'Tabel basis data tidak ditemukan.');
}
return view('ekosistem.tabel-basis-data.view', compact('tabel'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id): View
{
Gate::authorize('super_admin');
$tabel = TabelBasisData::find($id);
if (!$tabel) {
return redirect()->route('tabel-basis-data.index')->with('error', 'Tabel basis data tidak ditemukan.');
}
$group = GroupBasisData::all();
return view('ekosistem.tabel-basis-data.edit', compact('tabel', 'group'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id): RedirectResponse
{
Gate::authorize('super_admin');
$tabel = TabelBasisData::find($id);
if (!$tabel) {
return redirect()->route('tabel-basis-data.index')->with('error', 'Tabel basis data tidak ditemukan.');
}
$validated = $request->validate([
'tabel' => ['required',
'string',
'regex:/^[a-z0-9]+(_[a-z0-9]+)*$/',
'unique:ekosistem_basis_data_tabel,tabel'
],
'label' => ['required', 'string'],
'menu' => ['required', 'string'],
'deskripsi' => ['nullable', 'string'],
'group_id' => ['nullable', 'exists:ekosistem_basis_data_group,id'],
]);
$tabel->update([...$validated,
'updated_by' => auth()->id(),
]);
return redirect()->route('tabel.index')->with('success', 'Tabel basis data berhasil diperbarui.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
Gate::authorize('super_admin');
$tabel = TabelBasisData::find($id);
if (!$tabel) {
return redirect()->route('tabel.index')->with('error', 'Tabel basis data tidak ditemukan.');
}
$tabel->delete();
return redirect()->route('tabel.index')->with('success', 'Tabel basis data berhasil dihapus.');
}
public function moveUp(Request $request, string $id): JsonResponse
{
Gate::authorize('super_admin');
$current = TabelBasisData::find($id);
if (!$current) {
return response()->json([
'status' => 'error',
'message' => 'Data tabel tidak ditemukan.'
], 404);
}
$prev = TabelBasisData::where('urutan', '<', $current->urutan)
->orderBy('urutan', 'desc')
->first();
if ($prev) {
// swap
[$current->urutan, $prev->urutan] = [$prev->urutan, $current->urutan];
$current->save();
$prev->save();
}
return response()->json([
'status' => 'success',
'message' => 'Tabel basis data berhasil diperbarui.'
], 200);
}
public function moveDown(Request $request, string $id): JsonResponse
{
Gate::authorize('super_admin');
$current = TabelBasisData::find($id);
if (!$current) {
return response()->json([
'status' => 'error',
'message' => 'Data tabel tidak ditemukan.'
], 404);
}
$next = TabelBasisData::where('urutan', '>', $current->urutan)
->orderBy('urutan', 'asc')
->first();
if ($next) {
// swap
[$current->urutan, $next->urutan] = [$next->urutan, $current->urutan];
$current->save();
$next->save();
}
return response()->json([
'status' => 'success',
'message' => 'Tabel basis data berhasil diperbarui.'
], 200);
}
}
@@ -0,0 +1,359 @@
<?php
namespace App\Http\Controllers\Web\Ekosistem;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;
use App\Models\Ekosistem\TabelBasisData;
use App\Models\Ekosistem\KolomBasisData;
use App\Models\Ekosistem\ValueBasisData;
use App\Models\Core\Program;
class ValueBasisDataController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request, string $tabelId): View
{
if (!Gate::any(['super_admin', 'admin'])) {
abort(403);
}
$perPage = $request->query('per_page', 10);
$search = strtolower($request->query('search', ''));
$program = null;
$programList = Program::get();
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.');
}
}
}
$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.');
}
$selects = [];
foreach ($kolom as $k) {
$selects[] = "
MAX(
CASE
WHEN kolom_id = {$k->id}
THEN value_{$k->tipe}
ELSE NULL
END
) AS \"{$k->kolom}\"
";
}
$base = ValueBasisData::query()
->where('tabel_id', $tabelId)
->when($program, function ($query) use ($program) {
$query->where('program_id', $program->id);
})
->selectRaw("
program_id,
tabel_id,
value_id,
" . implode(",\n", $selects) . "
")
->groupBy('program_id', 'tabel_id', 'value_id')
->orderBy('value_id', 'asc');
$values = DB::query()->fromSub($base, 'pivot')
->when($search, function ($query) use ($search, $kolom) {
$query->where(function ($q) use ($search, $kolom) {
foreach ($kolom as $k) {
if ($k->tipe == 'text' || $k->tipe == 'varchar') {
$q->orWhereRaw("lower({$k->kolom}) LIKE ?", ["%$search%"]);
}
}
});
})
->paginate($perPage);
return view('ekosistem.value-basis-data.index', compact('programList', 'program', 'tabel', 'kolom', 'values'));
}
/**
* Show the form for creating a new resource.
*/
public function create(Request $request, string $tabelId): 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.');
}
}
return view('ekosistem.value-basis-data.add', compact('tabel', 'kolom', 'program'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request, string $tabelId): RedirectResponse
{
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.');
}
}
$validationRules = [];
foreach ($kolom as $k) {
if ($k->validasi && is_array($k->validasi)) {
$validationRules[$k->kolom] = $k->validasi;
} else {
$validationRules[$k->kolom] = ['nullable'];
}
}
$validated = $request->validate($validationRules);
DB::transaction(function () use ($program, $tabelId, $validated, $kolom) {
$maxId = ValueBasisData::where('tabel_id', $tabelId)
->where('program_id', $program->id)
->whereNotNull('value_id')
->max('value_id');
foreach ($kolom as $k) {
$values = [
'program_id' => $program->id,
'tabel_id' => (int)$tabelId,
'kolom_id' => $k->id,
'value_id' => ($maxId ?? 0) + 1,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
];
$values['value_' . $k->tipe] = $validated[$k->kolom] ?? null;
ValueBasisData::create($values);
}
});
return redirect()->route('tabel.data.index', [$tabelId, 'program' => $program->id])->with('success', 'Data berhasil ditambahkan.');
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Request $request, string $tabelId, string $id)
{
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.');
}
$value_id = $id;
return view('ekosistem.value-basis-data.edit', compact('tabel', 'kolom', 'program', 'values', 'value_id'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $tabelId, string $id)
{
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.');
}
$validationRules = [];
foreach ($kolom as $k) {
if ($k->validasi && is_array($k->validasi)) {
$validationRules[$k->kolom] = $k->validasi;
} else {
$validationRules[$k->kolom] = ['nullable'];
}
}
$validated = $request->validate($validationRules);
DB::transaction(function () use ($program, $tabelId, $validated, $kolom, $id, $values) {
foreach ($values as $v) {
$k = $kolom->where('id', $v->kolom_id)->first();
$v->update([
'value_' . $k->tipe => $validated[$k->kolom] ?? null,
'updated_by' => auth()->id(),
]);
}
});
return redirect()->route('tabel.data.index', [$tabelId, 'program' => $program->id])->with('success', 'Data berhasil diperbarui.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Request $request, string $tabelId, string $id)
{
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.');
}
ValueBasisData::query()
->where('tabel_id', $tabelId)
->where('value_id', $id)
->where('program_id', $program->id)
->delete();
return redirect()->route('tabel.data.index', [$tabelId, 'program' => $program->id])->with('success', 'Data berhasil dihapus.');
}
}