fileService = $fileService; } public static function middleware(): array { return [ //new Middleware('permission:/setting/data_aktivitas'), ]; } public function index(Request $request) { $data['sectors'] = $this->fileService->getSectors(); $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.index', $data); } public function create() { $data['sectors'] = $this->fileService->getSectors(); $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.create', $data); } public function store(Request $request) { $request->validate([ 'sector' => 'required|string|max:255', 'sub_sector' => 'nullable|string|max:255', 'inventory_year' => 'required|integer', 'name' => 'required|string|max:255', 'file_document' => 'required|file|max:2048|mimes:pdf,doc,docx,xlsx,xls', ], [ 'file_document.max' => 'Ukuran file tidak boleh melebihi 2MB.', ]); try { $this->fileService->save($request->all()); return redirect()->route($this->route.'.index')->with('success', 'File Data Aktivitas berhasil diupload.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'File Data Aktivitas gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function edit($id) { $datas['data'] = $this->fileService->find($id); // dd($datas['data']); if ($datas['data']->file_upload) { $datas['storagePath'] = 'data_aktivitas/' . $datas['data']->sector . '/' . $datas['data']->inventory_year; } $datas['sectors'] = $this->fileService->getSectors(); $datas['route'] = $this->route; $datas['title'] = $this->title; return view($this->template.'.edit', $datas); } public function update(Request $request, $id) { $request->validate([ 'sector' => 'required|string|max:255', 'sub_sector' => 'nullable|string|max:255', 'inventory_year' => 'required|integer', 'name' => 'required|string|max:255', 'file_document' => 'nullable|file|max:2048|mimes:pdf,doc,docx,xlsx,xls', ], [ 'file_document.max' => 'Ukuran file tidak boleh melebihi 2MB.', ]); try { $data = $request->all(); $data['id'] = $id; // Set the ID of the record to update $this->fileService->save($data); return redirect()->route($this->route.'.index')->with('success', 'File Data Aktivitas berhasil diperbarui.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'File Data Aktivitas gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function destroy($id) { try { $data = $this->fileService->find($id); if ($data) { $storagePath = 'data_aktivitas/' . $data->sector . '/' . $data->inventory_year; } $this->fileService->destroy($data, $storagePath); return redirect()->route($this->route.'.index')->with('success', 'File Data Aktivitas berhasil dihapus.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'File Data Aktivitas gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function fetchSubSectors(Request $request) { $request->validate([ 'sector_code' => 'required|string' ]); $subSectors = ReferenceSubSector::rowActive()->where('sector_code', $request->sector_code)->get(); return response()->json($subSectors); } public function grid(Request $request) { // dd($request->all()); $_data = []; $user = Auth::user(); $scope = $user->getScope(); $data = CmsFileDataAktivitas::rowActive(); // dd($data->get()); if ($scope === LingkupAksesData::INTERNAL->value) { dd('a'); $data->whereHas('creator', function ($q) use ($user) { $q->where('agency_id', $user->agency_id); }); } else if ($scope === '') { // $data->where('created_by', null); } // Apply category filter if a category is selected if ($request->has('sectorFilter') && $request->sectorFilter != '' && $request->sectorFilter != 'SEMUA SEKTOR') { $data = $data->where('sector', $request->sectorFilter); } if ($request->has('subSectorFilter') && $request->subSectorFilter != '' && $request->subSectorFilter != 'SEMUA SUB SEKTOR') { $data = $data->where('sub_sector', $request->subSectorFilter); } // Apply inventory year filter if a year is selected if ($request->has('inventoryYearFilter') && $request->inventoryYearFilter != '') { $data = $data->where('inventory_year', $request->inventoryYearFilter); } foreach ($data->get() as $key => $row) { $btn = 'Edit'; $btn .= '
'; $sector = $row->sectorData->name; if ($row->sub_sector) { $sector = $sector . ': ' . $row->subSector->name; } if ($row->file_upload) { $storagePath = 'storage/data_aktivitas/' . $row->sector . '/' . $row->inventory_year; $file = ' '; } else { $file = 'No File'; } $_data[] = [ 'no' => $key+1, 'sector' => $sector, 'file' => $file, 'inventory_year' => $row->inventory_year, 'name' => $row->name, 'agency' => $row->agency, 'created_at' => $row->created_at->format('d-m-Y H:i:s'), 'action' => @$btn, ]; } return response()->json($_data); } }