ldc-jeusselin/app/Http/Controllers/Bo/HomeSlidersController.php
Jérémy Munsch de4dcfb283
Closes #2 and #4
- Add status checker + admin secure packages + upgrade enum + add sentry + upgrade to sitemap generator + bump to CRMV2
- Set customer role enum and user role enum
- Update customer migration to set roles
- phpcbf
- faker local to fr
- update lang
- composer update
- bump laravel ui
- Redo all user crud
- npm bump
- Create admin command
- rearrange views
- redo all auth system
- redo customer pro bo
2023-08-21 15:43:33 +02:00

236 lines
8 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Models\HomeSliders;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class HomeSlidersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$slides = HomeSliders::orderBy('order', 'ASC')->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(public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'), $imageName);
// $imageName_r = Str::slug($title) .'-jeusselin_r.'.$picture_r->extension();
// $picture_r->move(public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'), $imageName_r);
$hsp = new HomeSliders();
$hsp->title = $title;
$hsp->picture = 'uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/' . $imageName;
// $hsp->picture_r = 'uploads/images/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(public_path() . '/' . $hsp->picture)) {
unlink(public_path() . '/' . $hsp->picture);
}
$imageName = Str::slug($title) . '-jeusselin.' . $picture->extension();
$picture->move(public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'), $imageName);
}
if ($picture_r !== null) {
if (file_exists(public_path() . '/' . $hsp->picture_r)) {
unlink(public_path() . '/' . $hsp->picture_r);
}
$imageName_r = Str::slug($title) . '-jeusselin_r.' . $picture_r->extension();
$picture_r->move(public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'), $imageName_r);
}
$hsp->title = $title;
if ($picture !== null) {
$hsp->picture = 'uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/' . $imageName;
}
if ($picture_r !== null) {
$hsp->picture_r = 'uploads/images/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;
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(public_path() . '/' . $h->picture)) {
unlink(public_path() . '/' . $h->picture);
}
if (!is_null($h->picture_r)) {
if (file_exists(public_path() . '/' . $h->picture_r)) {
unlink(public_path() . '/' . $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;
}
}