474 lines
19 KiB
PHP
474 lines
19 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Articles;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use App\Models\Moments;
|
|
use App\Models\MomentStrates;
|
|
use App\Models\Products;
|
|
use App\Models\Recipes;
|
|
use App\Traits\UpdateModelStatus;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\File as FacadesFile;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Symfony\Component\HttpFoundation\File\File;
|
|
|
|
class MomentsController extends Controller
|
|
{
|
|
use UpdateModelStatus;
|
|
|
|
// TODO: remplacer ce truc moche par un enum
|
|
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",
|
|
];
|
|
|
|
// TODO: remplacer ce truc moche par un enum
|
|
private $featuredTypes = [
|
|
"recettes" => "Recettes",
|
|
"produits" => "Produits",
|
|
"articles" => "Articles",
|
|
];
|
|
|
|
public function index()
|
|
{
|
|
$m = Moments::paginate();
|
|
return view('bo.moments.index', compact('m'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$recipes = Recipes::select('id', 'title')->where('status', 1)->get();
|
|
$articles = Articles::select('id', 'title')->where('status', 1)->get();
|
|
$products = Products::select('id', 'name as title')->where('status', 1)->get();
|
|
$strates = json_encode([]);
|
|
return view('bo.moments.create', compact('recipes', 'articles', 'products', 'strates'))
|
|
->with('colors', $this->colors)
|
|
->with('featuredTypes', $this->featuredTypes);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'title' => 'required|unique:moments,title|min:10|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=650',
|
|
'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 = 0;
|
|
$status = 0;
|
|
$featuredTitle = $request->featured_title;
|
|
$featuredType = strtolower($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('uploads/images/moments/' . date('Y') . '/' . date('m') . '/'), $imageName);
|
|
$imageName_bg = Str::slug($title) . '-bg-le-gaulois.' . $picture_bg->extension();
|
|
$picture_bg->move(public_path('uploads/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('uploads/images/moments/' . date('Y') . '/' . date('m') . '/'),
|
|
$imageName_bg_r
|
|
);
|
|
|
|
if ($voucherPicture !== null) {
|
|
$imageNameVoucher = Str::slug($title) . '-bon-reduction-le-gaulois.' . $voucherPicture->extension();
|
|
$voucherPicture->move(
|
|
public_path('uploads/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 = 'uploads/images/moments/' . date('Y') . '/' . date('m') . '/' . $imageName;
|
|
$m->picture_bg = 'uploads/images/moments/' . date('Y') . '/' . date('m') . '/' . $imageName_bg;
|
|
$m->picture_bg_r = 'uploads/images/moments/' . 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 = 'uploads/images/moments/' . 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("uploads/images/moments/$m->id"), $imageName);
|
|
|
|
$s->picture = 'uploads/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();
|
|
}//end foreach
|
|
|
|
return redirect()->route('bo.moments.index');
|
|
}
|
|
|
|
public function edit(Moments $m)
|
|
{
|
|
$recipes = Recipes::select('id', 'title')->where('status', 1)->get();
|
|
$articles = Articles::select('id', 'title')->where('status', 1)->get();
|
|
$products = Products::select('id', 'name as title')->where('status', 1)->get();
|
|
$strates = count($m->strates) > 0 ? json_encode($m->strates) : json_encode([]);
|
|
return view('bo.moments.edit', compact('m', 'recipes', 'articles', 'products'))
|
|
->with('colors', $this->colors)
|
|
->with('featuredTypes', $this->featuredTypes)
|
|
->with('strates', $strates);
|
|
}
|
|
|
|
public function update(Request $request, Moments $m)
|
|
{
|
|
$request->validate([
|
|
'title' => 'required|unique:moments,title,' . $m->id . '|min:10|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;
|
|
$status = $request->status === null ? 0 : 1;
|
|
$featuredTitle = $request->featured_title;
|
|
$featuredText = $request->featured_text;
|
|
$featuredType = strtolower($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=650',
|
|
]);
|
|
}
|
|
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 . ';';
|
|
}
|
|
|
|
if ($picture !== null) {
|
|
if (file_exists(public_path() . $m->picture)) {
|
|
unlink(public_path() . $m->picture);
|
|
}
|
|
$imageName = Str::slug($title) . '-le-gaulois.' . $picture->extension();
|
|
$picture->move(public_path('uploads/images/moments/' . date('Y') . '/' . date('m') . '/'), $imageName);
|
|
}
|
|
|
|
if ($picture_bg !== null) {
|
|
if (file_exists(public_path() . $m->picture_bg)) {
|
|
unlink(public_path() . $m->picture_bg);
|
|
}
|
|
$imageName_bg = Str::slug($title) . '-bg-le-gaulois.' . $picture_bg->extension();
|
|
$picture_bg->move(
|
|
public_path('uploads/images/moments/' . date('Y') . '/' . date('m') . '/'),
|
|
$imageName_bg
|
|
);
|
|
}
|
|
|
|
if ($picture_bg_r !== null) {
|
|
if (file_exists(public_path() . $m->picture_bg_r)) {
|
|
unlink(public_path() . $m->picture_bg_r);
|
|
}
|
|
$imageName_bg_r = Str::slug($title) . '-bg-r-le-gaulois.' . $picture_bg_r->extension();
|
|
$picture_bg_r->move(
|
|
public_path('uploads/images/moments/' . date('Y') . '/' . date('m') . '/'),
|
|
$imageName_bg_r
|
|
);
|
|
}
|
|
|
|
if ($voucherPicture !== null) {
|
|
if ($m->voucher_picture !== null && file_exists(public_path() . $m->voucher_picture)) {
|
|
unlink(public_path() . $m->voucher_picture);
|
|
}
|
|
$imageNameVoucher = Str::slug($title) . '-bon-reduction-le-gaulois.' . $voucherPicture->extension();
|
|
$voucherPicture->move(
|
|
public_path('uploads/images/moments/' . date('Y') . '/' . date('m') . '/'),
|
|
$imageNameVoucher
|
|
);
|
|
}
|
|
|
|
$m->title = $title;
|
|
$m->slug = Str::slug($title);
|
|
$m->description = $description;
|
|
$m->color = $color;
|
|
if ($picture !== null) {
|
|
$m->picture = 'uploads/images/moments/' . date('Y') . '/' . date('m') . '/' . $imageName;
|
|
}
|
|
if ($picture_bg !== null) {
|
|
$m->picture_bg = 'uploads/images/moments/' . date('Y') . '/' . date('m') . '/' . $imageName_bg;
|
|
}
|
|
if ($picture_bg_r !== null) {
|
|
$m->picture_bg_r = 'uploads/images/moments/' . date('Y') . '/' . date('m') . '/' . $imageName_bg_r;
|
|
}
|
|
$m->parallax = $parallax;
|
|
$m->status = $status;
|
|
// Un moment non publié ne peut pas être moment star
|
|
if ($status === 0) {
|
|
$m->star_moment = 0;
|
|
}
|
|
$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 = 'uploads/images/moments/' . date('Y') . '/' . date('m') . '/' . $imageNameVoucher;
|
|
}
|
|
$m->voucher_status = $voucherStatus;
|
|
|
|
// Gestion des strates
|
|
$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);
|
|
} 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("uploads/images/moments/$m->id"), $imageName);
|
|
$s->picture = 'uploads/images/moments/' . $m->id . '/' . $imageName;
|
|
}//end if
|
|
|
|
$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();
|
|
$ids[] = $s->id;
|
|
}//end foreach
|
|
|
|
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();
|
|
|
|
// Si c'est le moment star, on met à jour le cache des moments au cas ou le slug à changer
|
|
if ($m->star_moment === 1 && $m->status) {
|
|
Cache::put('momentFiche', $m);
|
|
}
|
|
|
|
return redirect()->route('bo.moments.edit', $m->id)->with('success', 'Enregistré !');
|
|
}
|
|
|
|
public function destroy(Moments $m)
|
|
{
|
|
if (FacadesFile::exists($m->picture)) {
|
|
FacadesFile::delete($m->picture);
|
|
}
|
|
|
|
if (FacadesFile::exists($m->picture_bg)) {
|
|
FacadesFile::delete($m->picture_bg);
|
|
}
|
|
|
|
if (FacadesFile::exists($m->picture_bg_r)) {
|
|
FacadesFile::delete($m->picture_bg_r);
|
|
}
|
|
|
|
foreach ($m->strates as $strate) {
|
|
if (FacadesFile::exists($strate->picture)) {
|
|
FacadesFile::delete($strate->picture);
|
|
}
|
|
}
|
|
|
|
if (count($m->strates) > 0) {
|
|
if (FacadesFile::exists('uploads/images/moments/' . $m->id)) {
|
|
FacadesFile::deleteDirectory('uploads/images/moments/' . $m->id);
|
|
}
|
|
$m->strates()->delete();
|
|
}
|
|
|
|
$m->delete();
|
|
|
|
return redirect()->route('bo.moments.index')
|
|
->with('warning', 'Le contenu à bien été supprimé');
|
|
}
|
|
|
|
/**
|
|
* Passe un moment en moment star. Si il y avait déjà un moment star, il ne l'est plus et le nouveau le remplace
|
|
*
|
|
* @param id L'id du moment qui doit devenir moment star
|
|
*/
|
|
public function setStar($id)
|
|
{
|
|
$moment = Moments::find($id);
|
|
if (!$moment) {
|
|
abort(404);
|
|
}
|
|
|
|
if ($moment->star_moment === 0) {
|
|
$oldStar = Moments::where('star_moment', 1)->first();
|
|
if ($oldStar) {
|
|
$oldStar->star_moment = 0;
|
|
$oldStar->save();
|
|
}
|
|
}
|
|
|
|
$moment->star_moment = $moment->star_moment === 0 ? 1 : 0;
|
|
$moment->save();
|
|
|
|
if ($moment->star_moment === 1 && $moment->status) {
|
|
Cache::put('momentFiche', $moment);
|
|
}
|
|
|
|
return redirect()->route('bo.moments.index');
|
|
}
|
|
}
|