get(); return view('bo.home-sliders.index', compact('slides')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('bo.home-sliders.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $title = $request->title; $picture = $request->picture; $text = $request->text; $linkLabel = $request->link_label; $link = $request->link; $newTab = $request->link_new_tab === null ? 0 : 1; $status = 0; $request->validate([ 'title' => 'required|max:255', 'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=300,max-height=300', 'link_label' => 'sometimes|max:100', 'link' => 'sometimes|max:255', ]); $imageName = Str::slug($title) . '-jeusselin.' . $picture->extension(); $picture->move( storage_path(sprintf('app/public/modelfiles/slider_accueil/%s/%s', date('Y'), date('m'))), $imageName ); // $imageName_r = Str::slug($title) .'-jeusselin_r.'.$picture_r->extension(); // $picture_r->move(storage_path('modelfiles/slider_accueil/slider_accueil/' . date('Y') . '/' . date('m') . '/'), $imageName_r); $hsp = new HomeSliders(); $hsp->title = $title; $hsp->picture = sprintf('storage/modelfiles/slider_accueil/%s/%s/%s', date('Y'), date('m'), $imageName); // $hsp->picture_r = 'storage/modelfiles/slider_accueil/' . date('Y') . '/' . date('m') . '/' . $imageName_r; $hsp->text = $text; $hsp->link_label = $linkLabel; $hsp->link = $link; $hsp->link_new_tab = $newTab; $hsp->status = $status; $hsp->order = $this->getLastOrder(); $hsp->save(); if ($hsp->saveOrFail()) { return redirect() ->route('bo.home-sliders.index') ->with('success', trans( ':model a été crée', ['model' => 'Slide'] )); } return back(); } /** * Show the form for editing the specified resource. * * @param integer $id * @return \Illuminate\Http\Response */ public function edit($id) { $h = HomeSliders::findOrFail($id); return view('bo.home-sliders.edit', compact('h')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param integer $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $title = $request->title; $picture = $request->picture; $picture_r = null; // $request->picture_r; $text = $request->text; $linkLabel = $request->link_label; $link = $request->link; $newTab = $request->link_new_tab === null ? 0 : 1; $status = $request->status === null ? 0 : 1; $request->validate([ 'title' => 'required|max:255', 'link_label' => 'sometimes|max:100', 'link' => 'sometimes|max:255', ]); if ($picture !== null && $picture_r !== null) { $request->validate([ 'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=300,max-height=300', 'picture_r' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=500,height=671', ]); } elseif ($picture !== null && $picture_r === null) { $request->validate([ 'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=300,max-height=300' ]); } elseif ($picture === null && $picture_r !== null) { $request->validate([ 'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=500,height=671' ]); } $hsp = HomeSliders::findOrFail($id); if ($picture !== null) { if (file_exists(storage_path(Str::replaceFirst('storage', 'app/public', $hsp->picture)))) { unlink(storage_path(Str::replaceFirst('storage', 'app/public', $hsp->picture))); } $imageName = Str::slug($title) . '-jeusselin.' . $picture->extension(); $picture->move( storage_path(sprintf('app/public/modelfiles/slider_accueil/%s/%s', date('Y'), date('m'))), $imageName ); } if ($picture_r !== null) { if (file_exists(storage_path(Str::replaceFirst('storage', 'app/public', $hsp->picture_r)))) { unlink(storage_path(Str::replaceFirst('storage', 'app/public', $hsp->picture_r))); } $imageName_r = Str::slug($title) . '-jeusselin_r.' . $picture_r->extension(); $picture_r->move( storage_path(sprintf('app/public/modelfiles/slider_accueil/%s/%s', date('Y'), date('m'))), $imageName_r ); } $hsp->title = $title; if ($picture !== null) { $hsp->picture = sprintf('storage/modelfiles/slider_accueil/%s/%s/%s', date('Y'), date('m'), $imageName); } if ($picture_r !== null) { $hsp->picture_r = sprintf('storage/modelfiles/slider_accueil/%s/%s/%s', date('Y'), date('m'), $imageName_r); } $hsp->text = $text; $hsp->link_label = $linkLabel; $hsp->link = $link; $hsp->link_new_tab = $newTab; $hsp->status = $status; if ($hsp->saveOrFail()) { return redirect() ->route('bo.home-sliders.index') ->with('success', trans( ':model a été mis à jour', ['model' => 'Slide'] )); } return back(); } /** * Remove the specified resource from storage. * * @param integer $id * @return \Illuminate\Http\Response */ public function destroy($id) { $h = HomeSliders::findOrFail($id); if (file_exists(storage_path(Str::replaceFirst('storage', 'app/public', $h->picture)))) { unlink(storage_path(Str::replaceFirst('storage', 'app/public', $h->picture))); } if (!is_null($h->picture_r)) { if (file_exists(storage_path(Str::replaceFirst('storage', 'app/public', $h->picture_r)))) { unlink(storage_path(Str::replaceFirst('storage', 'app/public', $h->picture_r))); } } if ($h->deleteOrFail()) { return redirect() ->route('bo.home-sliders.index') ->with('warning', trans( ':model a bien été supprimé', ['model' => 'Slide'] )); } return back(); } public function changeOrder($id, $action) { $current = HomeSliders::findOrFail($id); if ($action === 'up') { $tmp = HomeSliders::where('order', '<', $current->order)->orderBy('order', 'DESC')->first(); $newOrder = $tmp->order; $tmp->order = $current->order; $current->order = $newOrder; } else { $tmp = HomeSliders::where('order', '>', $current->order)->orderBy('order', 'ASC')->first(); $newOrder = $tmp->order; $tmp->order = $current->order; $current->order = $newOrder; } $tmp->save(); $current->save(); return redirect()->route('bo.home-sliders.index'); } private function getLastOrder() { $order = HomeSliders::select('order')->orderBy('order', 'DESC')->first(); if ($order === null) { return 1; } return $order->order + 1; } }