Files
database-pertani-web/app/Http/Controllers/Web/Ekosistem/TabelBasisDataController.php
T

205 lines
6.0 KiB
PHP

<?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);
}
}