ldc-legaulois/app/Http/Controllers/Bo/HomeSliderPicturesController.php
Jérémy Munsch f38e95bce8 Migration fonctionnelle sur :
- article_category
 - article
 - eleveur
 - eleveurs_sites
 - survey_response
 - fos_user
 - aimant_user
 - content_ocb
 - encemoment_fiche
 - encemoment_strates
 - gamme
 - home_slider
 - home_mini_slider
 - moment_consommation
 - pays
 - type_viande
 - societe
 - famille_vente
 - produit
 - recipe
 - menu
 - produit (update)
 - pictos
 - pictosproducts
 - inco_composition
 - qrcode
 - product_slider
 - recipe Articles
 - reduction
 - products reductions
 - saga
 - user_favorite_recipe

Introduces Enum system caster and trait

Html Colors auto cast for HomeSliderPictures and Ranges

All migrations struct, data and model traits except HomeSliderPicture

Days enum system and HomeSliderPicture migration
2021-02-08 09:47:36 +01:00

205 lines
7.7 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;
class HomeSliderPicturesController extends Controller
{
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('images/slider_accueil/' . date('Y') . '/' . date('m') . '/'), $imageName);
$imageName_r = Str::slug($title) .'-le-gaulois_r.'.$picture_r->extension();
$picture_r->move(public_path('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 = 'images/slider_accueil/' . date('Y') . '/' . date('m') . '/' . $imageName;
$hsp->picture_r = '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();
foreach (['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] as $day) {
if (!\isNull($request[$day] ?? null)) {
$hsp->addDay(Days::make($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'
]);
}
$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('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('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 = 'images/slider_accueil/' . date('Y') . '/' . date('m') . '/' . $imageName;
if ($picture_r !== null)
$hsp->picture_r = '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 (['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] as $day) {
if (!\isNull($request[$day] ?? null)) {
$hsp->addDay(Days::make($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');
}
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()->route('bo.home-slider-pictures.index');
}
private function getLastOrder()
{
$order = HomeSliderPictures::select('order')->orderBy('order', 'DESC')->first();
if ($order === NULL)
return 1;
return $order->order + 1;
}
}