378 lines
16 KiB
PHP
378 lines
16 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\MomentStrates;
|
|
use App\Models\Recipes;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Symfony\Component\HttpFoundation\File\File;
|
|
|
|
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();
|
|
$strates = json_encode([]);
|
|
return view('bo.moments.create', compact('r'))
|
|
->with('colors', $this->colors)
|
|
->with('featuredTypes', $this->featuredTypes)
|
|
->with('strates', $strates)
|
|
;
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$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',
|
|
'voucher_picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=580,height=330',
|
|
]);
|
|
$featuredText = $request->featured_text;
|
|
if (empty($featuredText))
|
|
return back()->withInput()->withErrors("Le texte de la remontée est recquis.");
|
|
|
|
$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;
|
|
$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;
|
|
$strates = json_decode($request->strates);
|
|
|
|
$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->slug = Str::slug($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();
|
|
|
|
$compteur = 0;
|
|
foreach($strates as $strate) {
|
|
$validator = Validator::make(get_object_vars($strate), [
|
|
'title' => 'required|max:255',
|
|
'picture' => 'required',
|
|
'content' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
$m->delete();
|
|
return back()->with('strates', json_encode($strates))->withInput()->withErrors("Les données de la strate " . $compteur . " sont incorrects, veuillez réessayer.");
|
|
}
|
|
|
|
$compteur++;
|
|
$s = new MomentStrates();
|
|
$s->moment_id = $m->id;
|
|
$s->title = $strate->title;
|
|
|
|
// Gestion de l'image
|
|
$image = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $strate->picture));
|
|
$extension = explode('/', mime_content_type($strate->picture))[1];
|
|
if (!in_array($extension, ['png', 'jpg', 'jpeg'])) {
|
|
$m->delete();
|
|
return back()->with('strates', json_encode($strates))->withInput()->withErrors("L'image de la strate " . $compteur . " n'est pas au bon format (formats acceptés : png, jpg, jpeg), veuillez réessayer.");
|
|
}
|
|
// Création d'un fichier temporaire pour ensuite le move si il est ok
|
|
$tmpFilePath=sys_get_temp_dir().'/'.uniqid();
|
|
file_put_contents($tmpFilePath, $image);
|
|
$tmpFile = new File($tmpFilePath);
|
|
$imgSize = getimagesize($tmpFilePath);
|
|
if ($imgSize[0] >= 220 || $imgSize[1] >= 220) {
|
|
$m->delete();
|
|
return back()->with('strates', json_encode($strates))->withInput()->withErrors("L'image de la strate " . $compteur . " a des dimensions incorrects, veuillez réessayer.");
|
|
}
|
|
$imageName = 'strate' . $compteur . '.' . $extension;
|
|
$tmpFile->move(public_path("images/moments/$m->id"), $imageName);
|
|
|
|
$s->picture = 'images/moments/' . $m->id . '/' . $imageName;
|
|
$s->picture_label = $strate->picture_label;
|
|
$s->content = $strate->content;
|
|
$s->button_label = $strate->button_label;
|
|
$s->button_link = $strate->button_link;
|
|
$s->status = $strate->status === null ? 0 : 1;
|
|
$s->save();
|
|
}
|
|
|
|
return redirect()->route('bo.moments.index');
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$m = Moments::findOrFail($id);
|
|
$r = Recipes::all();
|
|
$strates = count($m->strates) > 0 ? json_encode($m->strates) : json_encode([]);
|
|
return view('bo.moments.edit', compact('m', 'r'))
|
|
->with('colors', $this->colors)
|
|
->with('featuredTypes', $this->featuredTypes)
|
|
->with('strates', $strates)
|
|
;
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'title' => 'required|max:150',
|
|
'description' => 'required|max:400',
|
|
'color' => 'required|max:7',
|
|
'featured_title' => 'required|max:150',
|
|
]);
|
|
|
|
if ($request->featured_text === null)
|
|
return back()->withInput()->withErrors("Le champ texte de la remontée est obligatoire.");
|
|
|
|
$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;
|
|
$strates = json_decode($request->strates);
|
|
|
|
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;
|
|
|
|
$compteur = 0;
|
|
$ids = [];
|
|
foreach($strates as $strate) {
|
|
$validator = Validator::make(get_object_vars($strate), [
|
|
'title' => 'required|max:255',
|
|
'content' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails())
|
|
return back()->with('strates', json_encode($strates))->withInput()->withErrors("Les données de la strate " . $compteur . " sont incorrects, veuillez réessayer.");
|
|
|
|
$compteur++;
|
|
if ($strate->id !== null) {
|
|
$s = MomentStrates::find($strate->id);
|
|
$ids[] = $strate->id;
|
|
} else
|
|
$s = new MomentStrates();
|
|
$s->moment_id = $m->id;
|
|
$s->title = $strate->title;
|
|
|
|
// Gestion de l'image
|
|
if (($strate->id !== null && MomentStrates::find($strate->id)->picture !== $strate->picture) || ($strate->id === null && $strate->picture !== null)) {
|
|
$image = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $strate->picture));
|
|
$extension = explode('/', mime_content_type($strate->picture))[1];
|
|
if (!in_array($extension, ['png', 'jpg', 'jpeg']))
|
|
return back()->with('strates', json_encode($strates))->withInput()->withErrors("L'image de la strate " . $compteur . " n'est pas au bon format (formats acceptés : png, jpg, jpeg), veuillez réessayer.");
|
|
// Création d'un fichier temporaire pour ensuite le move si il est ok
|
|
$tmpFilePath=sys_get_temp_dir().'/'.uniqid();
|
|
file_put_contents($tmpFilePath, $image);
|
|
$tmpFile = new File($tmpFilePath);
|
|
$imgSize = getimagesize($tmpFilePath);
|
|
if ($imgSize[0] >= 220 || $imgSize[1] >= 220)
|
|
return back()->with('strates', json_encode($strates))->withInput()->withErrors("L'image de la strate " . $compteur . " a des dimensions incorrects, veuillez réessayer.");
|
|
$imageName = 'strate' . $compteur . '.' . $extension;
|
|
$tmpFile->move(public_path("images/moments/$m->id"), $imageName);
|
|
$s->picture = 'images/moments/' . $m->id . '/' . $imageName;
|
|
}
|
|
|
|
$s->picture_label = $strate->picture_label;
|
|
$s->content = $strate->content;
|
|
$s->button_label = $strate->button_label;
|
|
$s->button_link = $strate->button_link;
|
|
$s->status = $strate->status === null ? 0 : 1;
|
|
$s->save();
|
|
}
|
|
|
|
if (count($ids) > 0) {
|
|
$idsExists = [];
|
|
foreach ($m->strates as $strateExist)
|
|
$idsExists[] = $strateExist->id;
|
|
foreach ($idsExists as $idStrate)
|
|
if (!in_array($idStrate, $ids))
|
|
$m->strates()->find($idStrate)->delete();
|
|
}
|
|
|
|
$m->save();
|
|
|
|
// Mise en cache du moment si il est publié car c'est le dernier mis à jour obligatoirement
|
|
if ($m->status === 1)
|
|
Cache::put('momentFiche', $m);
|
|
|
|
return redirect()->route('bo.moments.edit', $m->id)->with('success', 'Enregistré !');
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
|
|
$m = Moments::findOrFail($id);
|
|
|
|
if (file_exists(public_path('images/moments/') . $m->picture))
|
|
unlink(public_path('images/moments/') . $m->picture);
|
|
|
|
if (file_exists(public_path('images/moments/') . $m->picture_bg))
|
|
unlink(public_path('images/moments/') . $m->picture_bg);
|
|
|
|
if (file_exists(public_path('images/moments/') . $m->picture_bg_r))
|
|
unlink(public_path('images/moments/') . $m->picture_bg_r);
|
|
|
|
foreach($m->strates as $strate) {
|
|
if (file_exists(public_path() . '/' . $strate->picture))
|
|
unlink(public_path() . '/' . $strate->picture);
|
|
}
|
|
|
|
if (count($m->strates) > 0) {
|
|
rmdir(public_path('images/moments/') . $m->id);
|
|
$m->strates()->delete();
|
|
}
|
|
|
|
$m->delete();
|
|
|
|
return redirect()->route('bo.moments.index');
|
|
}
|
|
}
|