program dan basis data ekosistem
This commit is contained in:
@@ -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.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
use App\Models\Core\Program;
|
||||
use App\Models\Wilayah\Provinsi;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
|
||||
class ProgramController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$title = 'Program';
|
||||
|
||||
$perPage = $request->query('per_page', 10);
|
||||
$search = $request->query('search', '');
|
||||
|
||||
$program = Program::where('nama', 'like', "%$search%")
|
||||
->paginate($perPage);
|
||||
|
||||
return view('core.program.index', compact('title', 'program'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$provinsi = Provinsi::orderBy('nama', 'asc')->get();
|
||||
$komoditas = Komoditas::orderBy('nama', 'asc')->get();
|
||||
return view('core.program.add', compact('provinsi', 'komoditas'));
|
||||
}
|
||||
|
||||
public function getProgram()
|
||||
{
|
||||
$program = Program::select(['id', 'nama', 'sub_domain'])->get();
|
||||
return response()->json(['data' => $program]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'nama' => ['required'],
|
||||
'sub_domain' => ['required', Rule::unique(Program::class, 'sub_domain')],
|
||||
'tahun' => ['required', 'integer'],
|
||||
'instansi' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => $validator->errors()->first()
|
||||
], 422);
|
||||
}
|
||||
|
||||
Program::create([...$validator->validated(),
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
return redirect()->route('program.index')->with('success', 'Data program berhasil disimpan!');
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$program = Program::find($id);
|
||||
if (!$program) {
|
||||
return redirect()->route('program.index')->with('error', 'Data program tidak ditemukan.');
|
||||
}
|
||||
|
||||
$komoditas = Komoditas::all();
|
||||
$provinsi = Provinsi::all();
|
||||
|
||||
return view('core.program.view', compact('program', 'komoditas', 'provinsi'));
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$program = Program::find($id);
|
||||
if (!$program) {
|
||||
return redirect()->route('program.index')->with('error', 'Data program tidak ditemukan.');
|
||||
}
|
||||
|
||||
return view('core.program.edit', compact('program'));
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'nama' => ['required'],
|
||||
'sub_domain' => ['required', Rule::unique(Program::class, 'sub_domain')->ignore($id)],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return redirect()->back()->withErrors($validator)->withInput();
|
||||
}
|
||||
|
||||
$program = Program::find($id);
|
||||
|
||||
if (!$program) {
|
||||
return redirect()->route('program.index')->with('error', 'Data program tidak ditemukan!');
|
||||
}
|
||||
|
||||
$program->update([
|
||||
'nama' => $request->nama,
|
||||
'sub_domain' => $request->sub_domain,
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
return redirect()->route('program.index')->with('success', 'Data program berhasil diperbarui!');
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$program = Program::find($id);
|
||||
if (!$program) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$program->deleted_by = auth()->user()->id;
|
||||
$program->save();
|
||||
$program->delete();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data program berhasil dihapus.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
use App\Models\Core\Program;
|
||||
use App\Models\Core\KomoditasProgram;
|
||||
use App\Models\Wilayah\Provinsi;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
|
||||
class ProgramKomoditasController extends Controller
|
||||
{
|
||||
|
||||
public function store(Request $request, int $programId)
|
||||
{
|
||||
$program = Program::find($programId);
|
||||
if (!$program) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data program tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'komoditas_id' => [
|
||||
'required',
|
||||
'exists:master_komoditas,id',
|
||||
Rule::unique('core_program_komoditas')
|
||||
->where(fn ($query) => $query->where('program_id', $programId))->whereNull('deleted_at'),
|
||||
],
|
||||
'default' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => $validator->errors()->first()
|
||||
], 422);
|
||||
}
|
||||
|
||||
KomoditasProgram::where('program_id', $programId)->update(['default' => false]);
|
||||
|
||||
KomoditasProgram::create([...$validator->validated(),
|
||||
'program_id' => $programId,
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data komoditas program berhasil disimpan.'
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, int $programId, int $id)
|
||||
{
|
||||
$komoditasProgram = KomoditasProgram::where('program_id', $programId)->where('id', $id)->first();
|
||||
if (!$komoditasProgram) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data komoditas program tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'default' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => $validator->errors()->first()
|
||||
], 422);
|
||||
}
|
||||
|
||||
KomoditasProgram::where('program_id', $programId)->update(['default' => false]);
|
||||
$komoditasProgram->update([...$validator->validated(),
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data komoditas program berhasil diperbarui.'
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(int $programId, int $id)
|
||||
{
|
||||
$komoditasProgram = KomoditasProgram::where('program_id', $programId)->where('id', $id)->first();
|
||||
if (!$komoditasProgram) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data komoditas program tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$komoditasProgram->deleted_by = auth()->user()->id;
|
||||
$komoditasProgram->save();
|
||||
$komoditasProgram->delete();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data komoditas program berhasil dihapus.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
use App\Models\Core\Program;
|
||||
use App\Models\Core\LokasiProgram;
|
||||
use App\Models\Wilayah\Provinsi;
|
||||
use App\Models\Pertanian\Komoditas;
|
||||
|
||||
class ProgramLokasiController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
}
|
||||
|
||||
public function create(int $programId)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getProgram()
|
||||
{
|
||||
$program = Program::select(['id', 'nama', 'sub_domain'])->get();
|
||||
return response()->json(['data' => $program]);
|
||||
}
|
||||
|
||||
public function store(Request $request, int $programId)
|
||||
{
|
||||
$program = Program::find($programId);
|
||||
if (!$program) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data program tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'provinsi_id' => ['required', 'exists:master_provinsi,id'],
|
||||
'kabupaten_kota_id' => ['nullable', 'exists:master_kabupaten_kota,id'],
|
||||
'kecamatan_id' => ['nullable', 'exists:master_kecamatan,id'],
|
||||
'desa_kelurahan_id' => ['nullable', 'exists:master_desa_kelurahan,id'],
|
||||
|
||||
'default' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => $validator->errors()->first()
|
||||
], 422);
|
||||
}
|
||||
|
||||
LokasiProgram::where('program_id', $programId)->update(['default' => false]);
|
||||
|
||||
LokasiProgram::create([...$validator->validated(),
|
||||
//'default' => $request->has('default'),
|
||||
'program_id' => $programId,
|
||||
'created_by' => auth()->id(),
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data lokasi program berhasil disimpan.'
|
||||
]);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$program = Program::find($id);
|
||||
if (!$program) {
|
||||
return redirect()->route('program.index')->with('error', 'Data program tidak ditemukan.');
|
||||
}
|
||||
|
||||
return view('core.program.view', compact('program'));
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$program = Program::find($id);
|
||||
if (!$program) {
|
||||
return redirect()->route('program.index')->with('error', 'Data program tidak ditemukan.');
|
||||
}
|
||||
|
||||
return view('core.program.edit', compact('program'));
|
||||
}
|
||||
|
||||
public function update(Request $request, int $programId, int $id)
|
||||
{
|
||||
$lokasiProgram = LokasiProgram::where('program_id', $programId)->where('id', $id)->first();
|
||||
if (!$lokasiProgram) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data lokasi program tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'default' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => $validator->errors()->first()
|
||||
], 422);
|
||||
}
|
||||
|
||||
LokasiProgram::where('program_id', $programId)->update(['default' => false]);
|
||||
$lokasiProgram->update([...$validator->validated(),
|
||||
'updated_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data lokasi program berhasil diperbarui!'
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(int $programId, int $id)
|
||||
{
|
||||
$lokasiProgram = LokasiProgram::where('program_id', $programId)->where('id', $id)->first();
|
||||
if (!$lokasiProgram) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => 'Data lokasi program tidak ditemukan.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$lokasiProgram->deleted_by = auth()->user()->id;
|
||||
$lokasiProgram->save();
|
||||
$lokasiProgram->delete();
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Data lokasi program berhasil dihapus.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user