middleware('auth'); // Mengatur user setelah middleware auth dijalankan $this->middleware(function ($request, $next) { $this->user = Auth::user(); return $next($request); }); } /** * Display a listing of the resource. */ public function index() { if (is_null($this->user) || !$this->user->can('location.read')) { abort(403, 'Sorry! You are not allowed to view villages.'); } $provinces = Province::all(); return view('location::villages.index',compact('provinces')); } public function store(VillageRequest $request) { if (is_null($this->user) || !$this->user->can('location.create')) { abort(403, 'Sorry! You are not allowed to create villages.'); } $validate = $request->validated(); if ($validate) { try { Village::create($validate); return redirect() ->route('locations.villages.index') ->with('success', 'Village created successfully'); } catch (Exception $e) { return redirect()->back()->with('error', 'Failed to create village. ' . $e->getMessage()); } } } public function create() { if (is_null($this->user) || !$this->user->can('location.create')) { abort(403, 'Sorry! You are not allowed to create villages.'); } $provinces = Province::all(); return view('location::villages.create', compact('provinces')); } public function edit($id) { if (is_null($this->user) || !$this->user->can('location.update')) { abort(403, 'Sorry! You are not allowed to update villages.'); } $village = Village::find($id); $provinces = Province::all(); $cities = City::where('province_code', $village->province_code)->get(); $districts = District::where('city_code', $village->city_code)->get(); return view('location::villages.create', compact('village', 'provinces', 'cities', 'districts')); } public function update(VillageRequest $request, $id) { if (is_null($this->user) || !$this->user->can('location.update')) { abort(403, 'Sorry! You are not allowed to update villages.'); } $validate = $request->validated(); if ($validate) { try { $village = Village::find($id); $village->update($validate); return redirect() ->route('locations.villages.index') ->with('success', 'Village updated successfully'); } catch (Exception $e) { return redirect()->back()->with('error', 'Failed to update village. ' . $e->getMessage()); } } } public function destroy($id) { if (is_null($this->user) || !$this->user->can('location.delete')) { return response()->json(['success' => false,'message' => 'Sorry! You are not allowed to delete villages.'], 403); } try { Village::destroy($id); return json_encode(['message' => 'Village deleted successfully', 'success' => true]); } catch (Exception $e) { return json_encode(['message' => 'Failed to delete Village', 'success' => false]); } } public function export(Request $request) { if (is_null($this->user) || !$this->user->can('location.export')) { abort(403, 'Sorry! You are not allowed to export villages.'); } return Excel::download(new VillagesExport, 'villages.xlsx'); } public function dataForDatatables(Request $request) { if (is_null($this->user) || !$this->user->can('location.read')) { return response()->json(['success' => false,'message' => 'Sorry! You are not allowed to view villages.'], 403); } // Retrieve data from the database $query = Village::query(); // Apply search filter if provided if ($request->has('search') && !empty($request->get('search'))) { $search = $request->get('search'); $search = explode('|', $search); if(isset($search[0]) && !empty($search[0])){ // Define special city codes once $specialCityCodes = ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71']; // Handle Papua province special cases if($search[0] == '92'){ $query->where('province_code', '92') ->whereNotIn('city_code', $specialCityCodes); } else if($search[0] == '92.1'){ $query->where('province_code', '92') ->whereIn('city_code', $specialCityCodes); } else { // For all other provinces $query->where('province_code', $search[0]); } } if(isset($search[1]) &&!empty($search[1])){ $query->where('city_code',$search[1]); } if(isset($search[2]) &&!empty($search[2])){ $query->where('district_code',$search[2]); } $query->where(function ($q) use ($search) { $q->where('code', 'LIKE', "%$search[3]%"); $q->orWhere('name', 'LIKE', "%$search[3]%"); }); } // Apply sorting if provided if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) { $order = $request->get('sortOrder'); $column = $request->get('sortField'); $query->orderBy($column, $order); } // Get the total count of records $totalRecords = $query->count(); // Apply pagination if provided if ($request->has('page') && $request->has('size')) { $page = $request->get('page'); $size = $request->get('size'); $offset = ($page - 1) * $size; // Calculate the offset $query->skip($offset)->take($size); } // Get the filtered count of records $filteredRecords = $query->count(); // Get the data for the current page $data = $query->with('district.city.province')->get(); // Calculate the page count $pageCount = ceil($totalRecords / $request->get('size')); // Calculate the current page number $currentPage = 0 + 1; $data = $data->map(function ($item) { if (in_array($item->city_code, ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'])) { $item->province_name = Province::where('code', '92.1')->first()->name; } else { $item->province_name = $item->district->city->province->name; } return $item; }); // Return the response data as a JSON object return response()->json([ 'draw' => $request->get('draw'), 'recordsTotal' => $totalRecords, 'recordsFiltered' => $filteredRecords, 'pageCount' => $pageCount, 'page' => $currentPage, 'totalCount' => $totalRecords, 'data' => $data, ]); } public function getVillagesByDistrictId($id){ if (is_null($this->user) || !$this->user->can('location.read')) { return response()->json(['success' => false,'message' => 'Sorry! You are not allowed to view villages.'], 403); } return response()->json(Village::where('district_code', $id)->get()); } public function getPostalCodesByVillageId($id){ if (is_null($this->user) || !$this->user->can('location.read')) { return response()->json(['success' => false,'message' => 'Sorry! You are not allowed to view villages.'], 403); } return response()->json(Village::where('code', $id)->first()); } }