2020-09-21 17:31:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
2021-03-20 14:38:15 +01:00
|
|
|
namespace App\Http\Controllers\Bo;
|
2020-09-21 17:31:09 +02:00
|
|
|
|
2021-02-02 10:26:36 +01:00
|
|
|
use App\Enums\Days;
|
2020-09-21 17:31:09 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use App\Models\HomeSliderPictures;
|
2021-04-28 13:57:12 +02:00
|
|
|
use App\Traits\UpdateModelStatus;
|
2020-09-21 17:31:09 +02:00
|
|
|
|
|
|
|
|
class HomeSliderPicturesController extends Controller
|
|
|
|
|
{
|
2021-04-28 13:57:12 +02:00
|
|
|
use UpdateModelStatus;
|
|
|
|
|
|
2020-09-21 17:31:09 +02:00
|
|
|
public function index()
|
|
|
|
|
{
|
2021-04-21 09:50:44 +02:00
|
|
|
$h = HomeSliderPictures::orderBy('order', 'asc')->get();
|
2020-09-22 10:45:51 +02:00
|
|
|
return view('bo.home-slider-pictures.index', compact('h'));
|
2020-09-21 17:31:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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([
|
2020-09-23 14:49:08 +02:00
|
|
|
'catch_phrase' => 'required|max:100',
|
|
|
|
|
'catch_color' => 'required|max:7',
|
|
|
|
|
'title' => 'required|max:100',
|
|
|
|
|
'title_color' => 'required|max:7',
|
2020-09-21 17:31:09 +02:00
|
|
|
'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',
|
2020-09-23 14:49:08 +02:00
|
|
|
'link_label' => 'required|max:100',
|
|
|
|
|
'link' => 'required|max:255',
|
|
|
|
|
'link_color' => 'required|max:7',
|
2020-09-21 17:31:09 +02:00
|
|
|
]);
|
|
|
|
|
|
2021-03-12 10:27:19 +01:00
|
|
|
$imageName = Str::slug($title) . '-le-gaulois.' . $picture->extension();
|
2021-02-16 17:32:51 +01:00
|
|
|
$picture->move(public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'), $imageName);
|
2021-03-12 10:27:19 +01:00
|
|
|
$imageName_r = Str::slug($title) . '-le-gaulois_r.' . $picture_r->extension();
|
2021-06-14 15:15:52 +02:00
|
|
|
$picture_r->move(
|
|
|
|
|
public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'),
|
|
|
|
|
$imageName_r
|
|
|
|
|
);
|
2020-09-21 17:31:09 +02:00
|
|
|
|
2020-09-22 10:45:51 +02:00
|
|
|
$hsp = new HomeSliderPictures();
|
2020-09-21 17:31:09 +02:00
|
|
|
$hsp->catch_phrase = $catchPhrase;
|
|
|
|
|
$hsp->catch_color = $catchColor;
|
|
|
|
|
$hsp->title = $title;
|
|
|
|
|
$hsp->title_color = $titleColor;
|
2021-02-16 17:32:51 +01:00
|
|
|
$hsp->picture = 'uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/' . $imageName;
|
|
|
|
|
$hsp->picture_r = 'uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/' . $imageName_r;
|
2020-09-21 17:31:09 +02:00
|
|
|
$hsp->link_label = $linkLabel;
|
|
|
|
|
$hsp->link = $link;
|
|
|
|
|
$hsp->link_color = $linkColor;
|
|
|
|
|
$hsp->new_tab = $newTab;
|
2021-05-17 14:26:31 +02:00
|
|
|
$hsp->days = Days::none()->value;
|
2021-03-12 10:27:19 +01:00
|
|
|
foreach (Days::toArray() as $day) {
|
|
|
|
|
if (isset($request[$day->value])) {
|
|
|
|
|
$hsp->addDay($day);
|
2021-02-02 10:26:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-21 17:31:09 +02:00
|
|
|
$hsp->status = $status;
|
2020-11-24 14:35:22 +01:00
|
|
|
$hsp->order = $this->getLastOrder();
|
2020-09-21 17:31:09 +02:00
|
|
|
$hsp->save();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('bo.home-slider-pictures.index');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit($id)
|
|
|
|
|
{
|
2020-09-22 10:45:51 +02:00
|
|
|
$h = HomeSliderPictures::findOrFail($id);
|
|
|
|
|
return view('bo.home-slider-pictures.edit', compact('h'));
|
2020-09-21 17:31:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
|
{
|
2020-09-22 10:45:51 +02:00
|
|
|
$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;
|
|
|
|
|
|
2020-09-23 14:49:08 +02:00
|
|
|
$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',
|
|
|
|
|
]);
|
|
|
|
|
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($picture !== null && $picture_r !== null) {
|
2020-09-22 10:45:51 +02:00
|
|
|
$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',
|
|
|
|
|
]);
|
2021-03-20 14:38:15 +01:00
|
|
|
} elseif ($picture !== null && $picture_r === null) {
|
2020-09-22 10:45:51 +02:00
|
|
|
$request->validate([
|
|
|
|
|
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1366,height=553'
|
|
|
|
|
]);
|
2021-03-20 14:38:15 +01:00
|
|
|
} elseif ($picture === null && $picture_r !== null) {
|
2020-09-22 10:45:51 +02:00
|
|
|
$request->validate([
|
|
|
|
|
'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=500,height=671'
|
|
|
|
|
]);
|
|
|
|
|
}
|
2021-03-12 10:27:19 +01:00
|
|
|
/** @var \App\Models\HomeSliderPictures */
|
2020-09-22 10:45:51 +02:00
|
|
|
$hsp = HomeSliderPictures::findOrFail($id);
|
|
|
|
|
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($picture !== null) {
|
|
|
|
|
if (file_exists(public_path() . '/' . $hsp->picture)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $hsp->picture);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2021-03-12 10:27:19 +01:00
|
|
|
$imageName = Str::slug($title) . '-le-gaulois.' . $picture->extension();
|
2021-06-14 15:15:52 +02:00
|
|
|
$picture->move(
|
|
|
|
|
public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'),
|
|
|
|
|
$imageName
|
|
|
|
|
);
|
2020-09-22 10:45:51 +02:00
|
|
|
}
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($picture_r !== null) {
|
|
|
|
|
if (file_exists(public_path() . '/' . $hsp->picture_r)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $hsp->picture_r);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-09-24 15:00:08 +02:00
|
|
|
$imageName_r = Str::slug($title) . '-le-gaulois_r.' . $picture_r->extension();
|
2021-06-14 15:15:52 +02:00
|
|
|
$picture_r->move(
|
|
|
|
|
public_path('uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/'),
|
|
|
|
|
$imageName_r
|
|
|
|
|
);
|
2020-09-22 10:45:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$hsp->catch_phrase = $catchPhrase;
|
|
|
|
|
$hsp->catch_color = $catchColor;
|
|
|
|
|
$hsp->title = $title;
|
|
|
|
|
$hsp->title_color = $titleColor;
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($picture !== null) {
|
2021-02-16 17:32:51 +01:00
|
|
|
$hsp->picture = 'uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/' . $imageName;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
if ($picture_r !== null) {
|
2021-02-16 17:32:51 +01:00
|
|
|
$hsp->picture_r = 'uploads/images/slider_accueil/' . date('Y') . '/' . date('m') . '/' . $imageName_r;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-09-22 10:45:51 +02:00
|
|
|
$hsp->link_label = $linkLabel;
|
|
|
|
|
$hsp->link = $link;
|
|
|
|
|
$hsp->link_color = $linkColor;
|
|
|
|
|
$hsp->new_tab = $newTab;
|
2021-03-12 10:27:19 +01:00
|
|
|
foreach (Days::toArray() as $day) {
|
|
|
|
|
if (isset($request[$day->value])) {
|
|
|
|
|
$hsp->addDay($day);
|
|
|
|
|
} else {
|
|
|
|
|
$hsp->removeDay($day);
|
2021-02-02 10:26:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-22 10:45:51 +02:00
|
|
|
$hsp->status = $status;
|
|
|
|
|
$hsp->save();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('bo.home-slider-pictures.edit', $hsp->id)->with('success', 'Enregistré !');
|
2020-09-21 17:31:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function destroy($id)
|
|
|
|
|
{
|
2020-09-22 10:45:51 +02:00
|
|
|
$h = HomeSliderPictures::findOrFail($id);
|
|
|
|
|
|
2021-03-20 14:38:15 +01:00
|
|
|
if (file_exists(public_path() . '/' . $h->picture)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $h->picture);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-11-24 14:35:22 +01:00
|
|
|
|
2021-03-20 14:38:15 +01:00
|
|
|
if (file_exists(public_path() . '/' . $h->picture_r)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $h->picture_r);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-09-22 10:45:51 +02:00
|
|
|
|
|
|
|
|
$h->delete();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('bo.home-slider-pictures.index');
|
2020-09-21 17:31:09 +02:00
|
|
|
}
|
2020-11-24 14:35:22 +01:00
|
|
|
|
|
|
|
|
public function changeOrder($id, $action)
|
|
|
|
|
{
|
|
|
|
|
$current = HomeSliderPictures::findOrFail($id);
|
|
|
|
|
|
2021-03-12 10:27:19 +01:00
|
|
|
if ($action === 'up') {
|
2020-11-24 14:35:22 +01:00
|
|
|
$tmp = HomeSliderPictures::where('order', '<', $current->order)->orderBy('order', 'DESC')->first();
|
|
|
|
|
$newOrder = $tmp->order;
|
|
|
|
|
$tmp->order = $current->order;
|
|
|
|
|
$current->order = $newOrder;
|
2021-03-12 10:27:19 +01:00
|
|
|
} else {
|
2020-11-24 14:35:22 +01:00
|
|
|
$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()->route('bo.home-slider-pictures.index');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getLastOrder()
|
|
|
|
|
{
|
|
|
|
|
$order = HomeSliderPictures::select('order')->orderBy('order', 'DESC')->first();
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($order === null) {
|
2020-11-24 14:35:22 +01:00
|
|
|
return 1;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-11-24 14:35:22 +01:00
|
|
|
return $order->order + 1;
|
|
|
|
|
}
|
2020-09-21 17:31:09 +02:00
|
|
|
}
|