fileService = $fileService; } public static function middleware(): array { return [ //new Middleware('permission:/setting/peraturan'), ]; } public function index(Request $request) { $datas['route'] = $this->route; $datas['title'] = $this->title; return view($this->template.'.index',$datas); } public function create() { $datas['route'] = $this->route; $datas['title'] = $this->title; return view($this->template.'.create',$datas); } public function store(Request $request) { $request->validate([ '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 Pengumuman / File Peraturan-Peraturan berhasil diupload.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'File Pengumuman / File Peraturan-Peraturan gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function edit($id) { $datas['data'] = $this->fileService->find($id); if ($datas['data']) { $datas['storagePath'] = 'peraturan'; } $datas['route'] = $this->route; $datas['title'] = $this->title; return view($this->template.'.edit', $datas); } public function update(Request $request, $id) { $request->validate([ '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; $this->fileService->save($data); return redirect()->route($this->route.'.index')->with('success', 'File Pengumuman / File Peraturan-Peraturan berhasil diperbarui.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'File Pengumuman / File Peraturan-Peraturan gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function destroy($id) { try { $data = $this->fileService->find($id); if ($data) { $storagePath = 'peraturan'; } $this->fileService->destroy($data, $storagePath); return redirect()->route($this->route.'.index')->with('success', 'File Pengumuman / File Peraturan-Peraturan berhasil dihapus.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'File Pengumuman / File Peraturan-Peraturan gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function grid(Request $request) { // dd($request->all()); $_data = []; $data = $this->fileService->getAll(); foreach ($data as $key => $row) { $btn = 'Edit'; $btn .= '
'; $btn .= csrf_field(); $btn .= method_field('DELETE'); $btn .= ''; $btn .= '
'; if ($row->file_upload) { $storagePath = 'storage/peraturan'; $file = ' '; } else { $file = 'No File'; } $_data[] = [ 'no' => $key+1, 'file' => $file, 'name' => $row->name, 'description' => $row->description, 'action' => @$btn, ]; } return response()->json($_data); } }