'Volaille', 'OEUF' => 'Oeuf', 'LAPIN' => 'Lapin']; public function index($type) { $contenu = ContentOCB::where('type', strtoupper($type))->orderBy('order', 'asc')->get(); return view('bo.ocb.index') ->with('contenu', $contenu) ->with('type', $type) ; } public function create() { return view('bo.ocb.create')->with('types', $this->typesContenus); } public function store(Request $request) { $picture = $request->picture; $content = $request->content; $status = 0; $type = $request->type; $request->validate([ 'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1366,height=553', ]); $imageName = 'oui-cest-bon-' . strtolower($type) . '-' . date('d-m-Y-h-i-s') .'-le-gaulois' . '.' . $picture->extension(); $picture->move(public_path('images/ocb/' . date('Y') . '/' . date('m')), $imageName); $ocb = new ContentOCB(); $ocb->picture = date('Y') . '/' . date('m') . '/' . $imageName; $ocb->content = $content; $ocb->status = $status; $ocb->type = $type; $ocb->order = $this->getLastOrder($type); $ocb->save(); return redirect()->route('bo.ocb.content', strtolower($type)); } public function edit($id) { $ocb = ContentOCB::findOrFail($id); return view('bo.ocb.edit') ->with('ocb', $ocb) ->with('types', $this->typesContenus) ; } public function update(Request $request, $id) { $picture = $request->picture; $content = $request->content; $status = $request->status === null ? 0 : 1; $type = $request->type; if ($picture !== null) { $request->validate([ 'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1366,height=553', ]); } $ocb = ContentOCB::findOrFail($id); if ($picture !== null) { if (file_exists(public_path('images/menus-equilibres/') . $ocb->picture)) unlink(public_path('images/menus-equilibres/') . $ocb->picture); $imageName = 'oui-cest-bon-' . strtolower($type) . '-' . date('d-m-Y-h-i-s') .'-le-gaulois' . '.' . $picture->extension(); $picture->move(public_path('images/menus-equilibres/' . date('Y') . '/' . date('m')), $imageName); } $ocb->type = $type; $ocb->content = $content; if ($picture !== null) $ocb->picture = date('Y') . '/' . date('m') . '/' . $imageName; $ocb->status = $status; $ocb->type = $type; $ocb->save(); return redirect()->route('bo.ocb.edit', $ocb->id)->with('success', 'Enregistré !'); } public function destroy($id) { $ocb = ContentOCB::findOrFail($id); if (file_exists(public_path('images/ocb/') . $ocb->picture)) unlink(public_path('images/ocb/') . $ocb->picture); $ocb->delete(); return view('bo.ocb.content', 'volaille'); } public function changeOrder($id, $type, $action) { $current = ContentOCB::findOrFail($id); if ($action === 'up'){ $tmp = ContentOCB::where([ ['order', $current->order - 1], ['type', $type], ])->first(); $tmp->order = $tmp->order + 1; $current->order = $current->order - 1; }else{ $tmp = ContentOCB::where([ ['order', $current->order + 1], ['type', $type], ])->first(); $tmp->order = $tmp->order - 1; $current->order = $current->order + 1; } $tmp->save(); $current->save(); return redirect()->route('bo.ocb.content', strtolower($type)); } private function getLastOrder($type) { $order = ContentOCB::select('order')->where('type', $type)->orderBy('order', 'DESC')->first(); if ($order === NULL) return 1; return $order->order + 1; } }