235 lines
9.3 KiB
PHP
235 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use App\Models\Moments;
|
|
use App\Models\Recipes;
|
|
|
|
class MomentsController extends Controller
|
|
{
|
|
private $colors = [
|
|
"color-1" => "Couleur 1",
|
|
"color-2" => 'Couleur 2',
|
|
"color-3" => 'Couleur 3',
|
|
"color-4" => "Couleur 4",
|
|
"color-5" => "Couleur 5",
|
|
"color-6" => "Couleur 6",
|
|
"color-7" => "Couleur 7",
|
|
"color-8" => "Couleur 8",
|
|
];
|
|
|
|
private $featuredTypes = [
|
|
"recettes" => "Recettes",
|
|
"produits" => "Produits",
|
|
"articles" => "Articles",
|
|
];
|
|
|
|
public function index()
|
|
{
|
|
$m = Moments::all();
|
|
return view('bo.moments.index', compact('m'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$r = Recipes::all();
|
|
return view('bo.moments.create', compact('r'))
|
|
->with('colors', $this->colors)
|
|
->with('featuredTypes', $this->featuredTypes)
|
|
;
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$title = $request->title;
|
|
$description = $request->description;
|
|
$color = $request->color;
|
|
$picture = $request->picture;
|
|
$picture_bg = $request->picture_bg;
|
|
$picture_bg_r = $request->picture_bg_r;
|
|
$parallax = $request->parallax === null ? 0 : 1;
|
|
$starMoment = $request->star_moment === null ? 0 : 1;
|
|
$status = 0;
|
|
$featuredTitle = $request->featured_title;
|
|
$featuredText = $request->featured_text;
|
|
$featuredType = $request->featured_type;
|
|
$featuredIds = $request->featured_ids;
|
|
$featuredStatus = $request->featured_status === null ? 0 : 1;
|
|
$voucherPicture = $request->voucher_picture;
|
|
$voucherStatus = $request->voucher_status === null ? 0 : 1;
|
|
|
|
$request->validate([
|
|
'title' => 'required|max:150',
|
|
'description' => 'required|max:400',
|
|
'color' => 'required|max:7',
|
|
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=750,height=400',
|
|
'picture_bg' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1200,height=750',
|
|
'picture_bg_r' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=500,height=300',
|
|
'featured_title' => 'required|max:150',
|
|
'featured_text' => 'required|max:4000',
|
|
'voucher_picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=580,height=330',
|
|
]);
|
|
|
|
$featuredIdsString = '';
|
|
foreach ($featuredIds as $featuredId) {
|
|
$featuredIdsString .= $featuredId . ';';
|
|
}
|
|
|
|
$imageName = Str::slug($title) . '-le-gaulois.'.$picture->extension();
|
|
$picture->move(public_path('images/moments/' . date('Y') . '/' . date('m') . '/'), $imageName);
|
|
$imageName_bg = Str::slug($title) . '-bg-le-gaulois.' . $picture_bg->extension();
|
|
$picture_bg->move(public_path('images/moments/' . date('Y') . '/' . date('m') . '/'), $imageName_bg);
|
|
$imageName_bg_r = Str::slug($title) . '-bg-r-le-gaulois.' . $picture_bg_r->extension();
|
|
$picture_bg_r->move(public_path('images/moments/' . date('Y') . '/' . date('m') . '/'), $imageName_bg_r);
|
|
$imageNameVoucher = Str::slug($title) . '-bon-reduction-le-gaulois.' . $voucherPicture->extension();
|
|
$voucherPicture->move(public_path('images/moments/' . date('Y') . '/' . date('m') . '/'), $imageNameVoucher);
|
|
|
|
$m = new Moments();
|
|
$m->title = $title;
|
|
$m->description = $description;
|
|
$m->color = $color;
|
|
$m->picture = date('Y') . '/' . date('m') . '/' . $imageName;
|
|
$m->picture_bg = date('Y') . '/' . date('m') . '/' . $imageName_bg;
|
|
$m->picture_bg_r = date('Y') . '/' . date('m') . '/' . $imageName_bg_r;
|
|
$m->parallax = $parallax;
|
|
$m->star_moment = $starMoment;
|
|
$m->status = $status;
|
|
$m->featured_title = $featuredTitle;
|
|
$m->featured_text = $featuredText;
|
|
$m->featured_type = $featuredType;
|
|
$m->featured_ids = $featuredIdsString;
|
|
$m->featured_status = $featuredStatus;
|
|
$m->voucher_picture = date('Y') . '/' . date('m') . '/' . $imageNameVoucher;
|
|
$m->voucher_status = $voucherStatus;
|
|
$m->save();
|
|
|
|
return redirect()->route('bo.moments.index');
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$m = Moments::findOrFail($id);
|
|
$r = Recipes::all();
|
|
return view('bo.moments.edit', compact('m', 'r'))
|
|
->with('colors', $this->colors)
|
|
->with('featuredTypes', $this->featuredTypes)
|
|
;
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$title = $request->title;
|
|
$description = $request->description;
|
|
$color = $request->color;
|
|
$picture = $request->picture;
|
|
$picture_bg = $request->picture_bg;
|
|
$picture_bg_r = $request->picture_bg_r;
|
|
$parallax = $request->parallax === null ? 0 : 1;
|
|
$starMoment = $request->star_moment === null ? 0 : 1;
|
|
$status = $request->status === null ? 0 : 1;
|
|
$featuredTitle = $request->featured_title;
|
|
$featuredText = $request->featured_text;
|
|
$featuredType = $request->featured_type;
|
|
$featuredIds = $request->featured_ids;
|
|
$featuredStatus = $request->featured_status === null ? 0 : 1;
|
|
$voucherPicture = $request->voucher_picture;
|
|
$voucherStatus = $request->voucher_status === null ? 0 : 1;
|
|
|
|
$request->validate([
|
|
'title' => 'required|max:150',
|
|
'description' => 'required|max:400',
|
|
'color' => 'required|max:7',
|
|
'featured_title' => 'required|max:150',
|
|
'featured_text' => 'required|max:4000',
|
|
]);
|
|
|
|
|
|
if ($picture !== null) {
|
|
$request->validate([
|
|
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=750,height=400',
|
|
]);
|
|
}
|
|
if ($picture_bg !== null) {
|
|
$request->validate([
|
|
'picture_bg' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1200,height=750',
|
|
]);
|
|
}
|
|
if ($picture_bg_r !== null) {
|
|
$request->validate([
|
|
'picture_bg_r' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=500,height=300',
|
|
]);
|
|
}
|
|
if ($voucherPicture !== null) {
|
|
$request->validate([
|
|
'voucher_picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=580,height=330',
|
|
]);
|
|
}
|
|
|
|
$featuredIdsString = '';
|
|
foreach ($featuredIds as $featuredId) {
|
|
$featuredIdsString .= $featuredId . ';';
|
|
}
|
|
|
|
$m = Moments::findOrFail($id);
|
|
|
|
if ($picture !== null) {
|
|
if (file_exists(public_path('images/moments/') . $m->picture))
|
|
unlink(public_path('images/moments/') . $m->picture);
|
|
$imageName = Str::slug($title) . '-le-gaulois.'.$picture->extension();
|
|
$picture->move(public_path('images/moments/' . date('Y') . '/' . date('m') . '/'), $imageName);
|
|
}
|
|
|
|
if ($picture_bg !== null) {
|
|
if (file_exists(public_path('images/moments/') . $m->picture_bg))
|
|
unlink(public_path('images/moments/') . $m->picture_bg);
|
|
$imageName_bg = Str::slug($title) . '-bg-le-gaulois.' . $picture_bg->extension();
|
|
$picture_bg->move(public_path('images/moments/' . date('Y') . '/' . date('m') . '/'), $imageName_bg);
|
|
}
|
|
|
|
if ($picture_bg_r !== null) {
|
|
if (file_exists(public_path('images/moments/') . $m->picture_bg_r))
|
|
unlink(public_path('images/moments/') . $m->picture_bg_r);
|
|
$imageName_bg_r = Str::slug($title) . '-bg-r-le-gaulois.' . $picture_bg_r->extension();
|
|
$picture_bg_r->move(public_path('images/moments/' . date('Y') . '/' . date('m') . '/'), $imageName_bg_r);
|
|
}
|
|
|
|
if ($voucherPicture !== null) {
|
|
if (file_exists(public_path('images/moments/') . $m->voucher_picture))
|
|
unlink(public_path('images/moments/') . $m->voucher_picture);
|
|
$imageNameVoucher = Str::slug($title) . '-bon-reduction-le-gaulois.' . $voucherPicture->extension();
|
|
$voucherPicture->move(public_path('images/moments/' . date('Y') . '/' . date('m') . '/'), $imageNameVoucher);
|
|
}
|
|
|
|
$m->title = $title;
|
|
$m->description = $description;
|
|
$m->color = $color;
|
|
if ($picture !== null)
|
|
$m->picture = date('Y') . '/' . date('m') . '/' . $imageName;
|
|
if ($picture_bg !== null)
|
|
$m->picture_bg = date('Y') . '/' . date('m') . '/' . $imageName_bg;
|
|
if ($picture_bg_r !== null)
|
|
$m->picture_bg_r = date('Y') . '/' . date('m') . '/' . $imageName_bg_r;
|
|
$m->parallax = $parallax;
|
|
$m->star_moment = $starMoment;
|
|
$m->status = $status;
|
|
$m->featured_title = $featuredTitle;
|
|
$m->featured_text = $featuredText;
|
|
$m->featured_type = $featuredType;
|
|
$m->featured_ids = $featuredIdsString;
|
|
$m->featured_status = $featuredStatus;
|
|
if ($voucherPicture !== null)
|
|
$m->voucher_picture = date('Y') . '/' . date('m') . '/' . $imageNameVoucher;
|
|
$m->voucher_status = $voucherStatus;
|
|
$m->save();
|
|
|
|
return redirect()->route('bo.moments.edit', $m->id)->with('success', 'Enregistré !');
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
//
|
|
}
|
|
}
|