192 lines
7.2 KiB
PHP
192 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web\Ekosistem;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Ekosistem\LahanCPCL;
|
|
use App\Models\Wilayah\Provinsi;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Enums\StatusLahan;
|
|
|
|
class LahanCPCLController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$query = LahanCPCL::with(['desaKelurahan.kecamatan.kabupatenKota.provinsi'])
|
|
->orderBy('created_at', 'desc');
|
|
|
|
if ($request->search) {
|
|
$query->where(function($q) use ($request) {
|
|
$q->where('nama_pemilik', 'like', '%' . $request->search . '%')
|
|
->orWhereHas('desaKelurahan', function($qDesa) use ($request) {
|
|
$qDesa->where('nama', 'like', '%' . $request->search . '%');
|
|
});
|
|
});
|
|
}
|
|
if ($request->filled('status_kepemilikan')) {
|
|
$query->where('status_kepemilikan', $request->status_kepemilikan);
|
|
}
|
|
if ($request->filled('desa_kelurahan_id')) {
|
|
$query->where('desa_kelurahan_id', $request->desa_kelurahan_id);
|
|
} elseif ($request->filled('kecamatan_id')) {
|
|
$query->whereHas('desakelurahan', function($q) use ($request) {
|
|
$q->where('kecamatan_id', $request->kecamatan_id);
|
|
});
|
|
} elseif ($request->filled('kabupatenkota_id')) {
|
|
$query->whereHas('desakelurahan.kecamatan', function($q) use ($request) {
|
|
$q->where('kabupatenkota_id', $request->kabupatenkota_id);
|
|
});
|
|
} elseif ($request->filled('provinsi_id')) {
|
|
$query->whereHas('desakelurahan.kecamatan.kabupatenkota', function($q) use ($request) {
|
|
$q->where('provinsi_id', $request->provinsi_id);
|
|
});
|
|
}
|
|
|
|
$lahan = $query->paginate($request->per_page ?? 10);
|
|
|
|
$provinsi = Provinsi::orderBy('nama', 'asc')->get();
|
|
|
|
$kabupaten = [];
|
|
$kecamatan = [];
|
|
$desa = [];
|
|
|
|
if ($request->filled('provinsi_id')) {
|
|
$kabupaten = KabupatenKota::where('provinsi_id', $request->provinsi_id)->orderBy('nama', 'asc')->get();
|
|
}
|
|
if ($request->filled('kabupatenkota_id')) {
|
|
$kecamatan = Kecamatan::where('kabupatenkota_id', $request->kabupatenkota_id)->orderBy('nama', 'asc')->get();
|
|
}
|
|
if ($request->filled('kecamatan_id')) {
|
|
$desa = DesaKelurahan::where('kecamatan_id', $request->kecamatan_id)->orderBy('nama', 'asc')->get();
|
|
}
|
|
|
|
return view('ekosistem.lahan-cpcl.index', compact('lahan', 'provinsi', 'kabupaten', 'kecamatan', 'desa'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$provinsi = Provinsi::orderBy('nama', 'asc')->get();
|
|
return view('ekosistem.lahan-cpcl.add', compact('provinsi'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
// dd($request->all());
|
|
$validator = Validator::make($request->all(), [
|
|
'nama' => ['required', 'string', 'max:255'],
|
|
'status_kepemilikan' => ['required', 'string', 'max:255'],
|
|
'nama_pemilik' => ['required', 'string', 'max:255'],
|
|
'desa_kelurahan_id' => ['nullable'],
|
|
'luas_lahan' => ['nullable', 'numeric'],
|
|
'path' => ['nullable', 'string'],
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
|
|
}
|
|
|
|
$pathData = null;
|
|
|
|
if ($request->filled('path')) {
|
|
$pathData = json_decode($request->path, true);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
return response()->json(['status' => 'error', 'message' => 'Format koordinat (path) bukan JSON array yang valid.'], 422);
|
|
}
|
|
}
|
|
|
|
LahanCPCL::create([
|
|
'nama' => $request->nama,
|
|
'status_kepemilikan' => $request->status_kepemilikan,
|
|
'nama_pemilik' => $request->nama_pemilik,
|
|
'desa_kelurahan_id' => $request->desa_kelurahan_id,
|
|
'luas_lahan' => $request->luas_lahan,
|
|
'path' => $pathData,
|
|
'created_by' => auth()->id(),
|
|
'updated_by' => auth()->id(),
|
|
]);
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Lahan berhasil ditambahkan.']);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$lahan = LahanCPCL::with(['desakelurahan.kecamatan.kabupatenkota.provinsi'])->findOrFail($id);
|
|
|
|
return view('ekosistem.lahan-cpcl.view', compact('lahan'));
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$lahan = LahanCPCL::with('desakelurahan.kecamatan.kabupatenkota.provinsi')->findOrFail($id);
|
|
|
|
$provinsi = Provinsi::orderBy('nama', 'asc')->get();
|
|
|
|
return view('ekosistem.lahan-cpcl.edit', compact('lahan', 'provinsi'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'nama' => ['required', 'string', 'max:255'],
|
|
'status_kepemilikan' => ['required', 'string', 'max:255'],
|
|
'nama_pemilik' => ['required', 'string', 'max:255'],
|
|
'desa_kelurahan_id' => ['nullable'],
|
|
'luas_lahan' => ['nullable', 'numeric'],
|
|
'path' => ['nullable', 'string'],
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
|
|
}
|
|
|
|
$lahan = LahanCPCL::findOrFail($id);
|
|
|
|
$pathData = null;
|
|
|
|
if ($request->filled('path')) {
|
|
$pathData = json_decode($request->path, true);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
return response()->json(['status' => 'error', 'message' => 'Format koordinat (path) bukan JSON array yang valid.'], 422);
|
|
}
|
|
}
|
|
|
|
$lahan->update([
|
|
'nama' => $request->nama,
|
|
'status_kepemilikan' => $request->status_kepemilikan,
|
|
'nama_pemilik' => $request->nama_pemilik,
|
|
'desa_kelurahan_id' => $request->desa_kelurahan_id,
|
|
'luas_lahan' => $request->luas_lahan,
|
|
'path' => $pathData,
|
|
'updated_by' => auth()->id(),
|
|
]);
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Data Lahan berhasil diperbarui.']);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$lahan = LahanCPCL::findOrFail($id);
|
|
|
|
$lahan->deleted_by = auth()->id(); // atau auth()->user()->id
|
|
$lahan->save();
|
|
|
|
$lahan->delete();
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => 'Data lahan berhasil dihapus.'
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Gagal menghapus data Lahan. Silakan coba lagi nanti.'
|
|
], 500);
|
|
}
|
|
}
|
|
}
|