ldc-legaulois/app/Http/Controllers/Bo/HomeSliderPicturesController.php
2022-02-10 16:14:58 +01:00

227 lines
8.3 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Enums\Days;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\HomeSliderPictures;
use App\Traits\UpdateModelStatus;
class HomeSliderPicturesController extends Controller
{
use UpdateModelStatus;
public function index()
{
$h = HomeSliderPictures::orderBy('order', 'asc')->get();
return view('bo.home-slider-pictures.index', compact('h'));
}
public function create()
{
return view('bo.home-slider-pictures.create');
}
public function store(Request $request)
{
$catchPhrase = $request->catch_phrase;
$catchColor = $request->catch_color;
$title = $request->title;
$titleColor = $request->title_color;
$picture = $request->picture;
$picture_r = $request->picture_r;
$linkLabel = $request->link_label;
$link = $request->link;
$linkColor = $request->link_color;
$newTab = $request->new_tab === null ? 0 : 1;
$status = 0;
$request->validate([
'catch_phrase' => 'required|max:100',
'catch_color' => 'required|max:7',
'title' => 'required|max:100',
'title_color' => 'required|max:7',
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1366,height=553',
'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=500,height=671',
'link_label' => 'required|max:100',
'link' => 'required|max:255',
'link_color' => 'required|max:7',
]);
$imageName = Str::slug($title) . '-le-gaulois.' . $picture->extension();
$picture->move(public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'), $imageName);
$imageName_r = Str::slug($title) . '-le-gaulois_r.' . $picture_r->extension();
$picture_r->move(
public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'),
$imageName_r
);
$hsp = new HomeSliderPictures();
$hsp->catch_phrase = $catchPhrase;
$hsp->catch_color = $catchColor;
$hsp->title = $title;
$hsp->title_color = $titleColor;
$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->link_label = $linkLabel;
$hsp->link = $link;
$hsp->link_color = $linkColor;
$hsp->new_tab = $newTab;
$hsp->days = Days::none()->value;
foreach (Days::toArray() as $day) {
if (isset($request[$day->value])) {
$hsp->addDay($day);
}
}
$hsp->status = $status;
$hsp->order = $this->getLastOrder();
$hsp->save();
return redirect()->route('bo.home-slider-pictures.index');
}
public function edit($id)
{
$h = HomeSliderPictures::findOrFail($id);
return view('bo.home-slider-pictures.edit', compact('h'));
}
public function update(Request $request, $id)
{
$catchPhrase = $request->catch_phrase;
$catchColor = $request->catch_color;
$title = $request->title;
$titleColor = $request->title_color;
$picture = $request->picture;
$picture_r = $request->picture_r;
$linkLabel = $request->link_label;
$link = $request->link;
$linkColor = $request->link_color;
$newTab = $request->new_tab === null ? 0 : 1;
$status = $request->status === null ? 0 : 1;
$request->validate([
'catch_phrase' => 'required|max:100',
'catch_color' => 'required|max:7',
'title' => 'required|max:100',
'title_color' => 'required|max:7',
'link_label' => 'required|max:100',
'link' => 'required|max:255',
'link_color' => 'required|max:7',
]);
if ($picture !== null && $picture_r !== null) {
$request->validate([
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1366,height=553',
'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:width=1366,height=553'
]);
} elseif ($picture === null && $picture_r !== null) {
$request->validate([
'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=500,height=671'
]);
}
/** @var \App\Models\HomeSliderPictures */
$hsp = HomeSliderPictures::findOrFail($id);
if ($picture !== null) {
if (file_exists(public_path() . '/' . $hsp->picture)) {
unlink(public_path() . '/' . $hsp->picture);
}
$imageName = Str::slug($title) . '-le-gaulois.' . $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) . '-le-gaulois_r.' . $picture_r->extension();
$picture_r->move(
public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'),
$imageName_r
);
}
$hsp->catch_phrase = $catchPhrase;
$hsp->catch_color = $catchColor;
$hsp->title = $title;
$hsp->title_color = $titleColor;
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->link_label = $linkLabel;
$hsp->link = $link;
$hsp->link_color = $linkColor;
$hsp->new_tab = $newTab;
foreach (Days::toArray() as $day) {
if (isset($request[$day->value])) {
$hsp->addDay($day);
} else {
$hsp->removeDay($day);
}
}
$hsp->status = $status;
$hsp->save();
return redirect()->route('bo.home-slider-pictures.edit', $hsp->id)->with('success', 'Enregistré !');
}
public function destroy($id)
{
$h = HomeSliderPictures::findOrFail($id);
if (file_exists(public_path() . '/' . $h->picture)) {
unlink(public_path() . '/' . $h->picture);
}
if (file_exists(public_path() . '/' . $h->picture_r)) {
unlink(public_path() . '/' . $h->picture_r);
}
$h->delete();
return redirect()->route('bo.home-slider-pictures.index')
->with('warning', 'Le contenu à bien été supprimé');
}
public function changeOrder($id, $action)
{
$current = HomeSliderPictures::findOrFail($id);
if ($action === 'up') {
$tmp = HomeSliderPictures::where('order', '<', $current->order)->orderBy('order', 'DESC')->first();
$newOrder = $tmp->order;
$tmp->order = $current->order;
$current->order = $newOrder;
} else {
$tmp = HomeSliderPictures::where('order', '>', $current->order)->orderBy('order', 'ASC')->first();
$newOrder = $tmp->order;
$tmp->order = $current->order;
$current->order = $newOrder;
}
$tmp->save();
$current->save();
return redirect()->back();
}
private function getLastOrder()
{
$order = HomeSliderPictures::select('order')->orderBy('order', 'DESC')->first();
if ($order === null) {
return 1;
}
return $order->order + 1;
}
}