2020-10-01 16:56:27 +02:00
< ? php
namespace App\Http\Controllers\Bo ;
use App\Http\Controllers\Controller ;
2020-12-18 11:03:49 +01:00
use App\Models\Articles ;
2020-10-01 16:56:27 +02:00
use Illuminate\Http\Request ;
use Illuminate\Support\Str ;
use App\Models\Moments ;
2020-12-15 15:27:13 +01:00
use App\Models\MomentStrates ;
2020-12-18 11:03:49 +01:00
use App\Models\Products ;
2020-10-01 16:56:27 +02:00
use App\Models\Recipes ;
2021-04-28 13:57:12 +02:00
use App\Traits\UpdateModelStatus ;
2020-12-07 16:35:12 +01:00
use Illuminate\Support\Facades\Cache ;
2021-04-28 15:49:47 +02:00
use Illuminate\Support\Facades\File as FacadesFile ;
2020-12-15 15:27:13 +01:00
use Illuminate\Support\Facades\Validator ;
use Symfony\Component\HttpFoundation\File\File ;
2020-10-01 16:56:27 +02:00
class MomentsController extends Controller
{
2021-04-28 13:57:12 +02:00
use UpdateModelStatus ;
2021-03-20 14:38:15 +01:00
2021-04-28 13:57:12 +02:00
/* TODO: remplacer ce truc moche par un enum */
2020-10-01 16:56:27 +02:00
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 " ,
];
2021-04-28 13:57:12 +02:00
/* TODO: remplacer ce truc moche par un enum */
2020-10-01 16:56:27 +02:00
private $featuredTypes = [
" recettes " => " Recettes " ,
" produits " => " Produits " ,
" articles " => " Articles " ,
];
public function index ()
{
2021-04-12 10:01:48 +02:00
$m = Moments :: paginate ();
2020-10-01 16:56:27 +02:00
return view ( 'bo.moments.index' , compact ( 'm' ));
}
public function create ()
{
2020-12-18 11:03:49 +01:00
$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 ();
2020-12-15 15:27:13 +01:00
$strates = json_encode ([]);
2020-12-18 11:03:49 +01:00
return view ( 'bo.moments.create' , compact ( 'recipes' , 'articles' , 'products' , 'strates' ))
2020-10-01 16:56:27 +02:00
-> with ( 'colors' , $this -> colors )
2021-03-20 14:38:15 +01:00
-> with ( 'featuredTypes' , $this -> featuredTypes );
2020-10-01 16:56:27 +02:00
}
public function store ( Request $request )
{
2020-12-15 15:27:13 +01:00
$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' ,
2021-01-14 13:44:59 +01:00
'picture_bg' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1200,height=650' ,
2020-12-15 15:27:13 +01:00
'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 ;
2021-03-20 14:38:15 +01:00
if ( empty ( $featuredText )) {
2020-12-15 15:27:13 +01:00
return back () -> withInput () -> withErrors ( " Le texte de la remontée est recquis. " );
2021-03-20 14:38:15 +01:00
}
2020-12-15 15:27:13 +01:00
2020-10-01 16:56:27 +02:00
$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 ;
2021-01-19 12:28:54 +01:00
$starMoment = 0 ;
2020-10-01 16:56:27 +02:00
$status = 0 ;
$featuredTitle = $request -> featured_title ;
2020-12-18 11:03:49 +01:00
$featuredType = strtolower ( $request -> featured_type );
2020-10-01 16:56:27 +02:00
$featuredIds = $request -> featured_ids ;
$featuredStatus = $request -> featured_status === null ? 0 : 1 ;
$voucherPicture = $request -> voucher_picture ;
$voucherStatus = $request -> voucher_status === null ? 0 : 1 ;
2020-12-15 15:27:13 +01:00
$strates = json_decode ( $request -> strates );
2020-10-01 16:56:27 +02:00
$featuredIdsString = '' ;
foreach ( $featuredIds as $featuredId ) {
$featuredIdsString .= $featuredId . ';' ;
}
2021-03-20 14:38:15 +01:00
$imageName = Str :: slug ( $title ) . '-le-gaulois.' . $picture -> extension ();
2021-02-16 17:32:51 +01:00
$picture -> move ( public_path ( 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' ), $imageName );
2020-10-01 16:56:27 +02:00
$imageName_bg = Str :: slug ( $title ) . '-bg-le-gaulois.' . $picture_bg -> extension ();
2021-02-16 17:32:51 +01:00
$picture_bg -> move ( public_path ( 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' ), $imageName_bg );
2020-10-01 16:56:27 +02:00
$imageName_bg_r = Str :: slug ( $title ) . '-bg-r-le-gaulois.' . $picture_bg_r -> extension ();
2021-02-16 17:32:51 +01:00
$picture_bg_r -> move ( public_path ( 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' ), $imageName_bg_r );
2021-01-19 12:28:54 +01:00
if ( $voucherPicture !== null ) {
$imageNameVoucher = Str :: slug ( $title ) . '-bon-reduction-le-gaulois.' . $voucherPicture -> extension ();
2021-02-16 17:32:51 +01:00
$voucherPicture -> move ( public_path ( 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' ), $imageNameVoucher );
2021-01-19 12:28:54 +01:00
}
2020-10-01 16:56:27 +02:00
$m = new Moments ();
$m -> title = $title ;
2020-11-04 15:07:21 +01:00
$m -> slug = Str :: slug ( $title );
2020-10-01 16:56:27 +02:00
$m -> description = $description ;
$m -> color = $color ;
2021-02-16 17:32:51 +01:00
$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 ;
2020-10-01 16:56:27 +02:00
$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 ;
2021-03-20 14:38:15 +01:00
if ( $voucherPicture !== null ) {
2021-02-16 17:32:51 +01:00
$m -> voucher_picture = 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' . $imageNameVoucher ;
2021-03-20 14:38:15 +01:00
}
2020-10-01 16:56:27 +02:00
$m -> voucher_status = $voucherStatus ;
$m -> save ();
2020-12-15 15:27:13 +01:00
$compteur = 0 ;
2021-03-20 14:38:15 +01:00
foreach ( $strates as $strate ) {
2020-12-15 15:27:13 +01:00
$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
2021-03-20 14:38:15 +01:00
$tmpFilePath = sys_get_temp_dir () . '/' . uniqid ();
2020-12-15 15:27:13 +01:00
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 ;
2021-02-16 17:32:51 +01:00
$tmpFile -> move ( public_path ( " uploads/images/moments/ $m->id " ), $imageName );
2020-12-15 15:27:13 +01:00
2021-02-16 17:32:51 +01:00
$s -> picture = 'uploads/images/moments/' . $m -> id . '/' . $imageName ;
2020-12-15 15:27:13 +01:00
$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 ();
}
2020-10-01 16:56:27 +02:00
return redirect () -> route ( 'bo.moments.index' );
}
2021-04-28 13:57:12 +02:00
public function edit ( Moments $m )
2020-10-01 16:56:27 +02:00
{
2020-12-18 11:03:49 +01:00
$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 ();
2020-12-15 15:27:13 +01:00
$strates = count ( $m -> strates ) > 0 ? json_encode ( $m -> strates ) : json_encode ([]);
2020-12-18 11:03:49 +01:00
return view ( 'bo.moments.edit' , compact ( 'm' , 'recipes' , 'articles' , 'products' ))
2020-10-01 16:56:27 +02:00
-> with ( 'colors' , $this -> colors )
-> with ( 'featuredTypes' , $this -> featuredTypes )
2021-03-20 14:38:15 +01:00
-> with ( 'strates' , $strates );
2020-10-01 16:56:27 +02:00
}
2021-04-28 13:57:12 +02:00
public function update ( Request $request , Moments $m )
2020-10-01 16:56:27 +02:00
{
2020-12-15 15:27:13 +01:00
$request -> validate ([
'title' => 'required|max:150' ,
'description' => 'required|max:400' ,
'color' => 'required|max:7' ,
'featured_title' => 'required|max:150' ,
]);
2021-03-20 14:38:15 +01:00
if ( $request -> featured_text === null ) {
2020-12-15 15:27:13 +01:00
return back () -> withInput () -> withErrors ( " Le champ texte de la remontée est obligatoire. " );
2021-03-20 14:38:15 +01:00
}
2020-12-15 15:27:13 +01:00
2020-10-01 16:56:27 +02:00
$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 ;
2020-12-18 11:03:49 +01:00
$featuredType = strtolower ( $request -> featured_type );
2020-10-01 16:56:27 +02:00
$featuredIds = $request -> featured_ids ;
$featuredStatus = $request -> featured_status === null ? 0 : 1 ;
$voucherPicture = $request -> voucher_picture ;
$voucherStatus = $request -> voucher_status === null ? 0 : 1 ;
2020-12-15 15:27:13 +01:00
$strates = json_decode ( $request -> strates );
2020-10-01 16:56:27 +02:00
if ( $picture !== null ) {
$request -> validate ([
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=750,height=400' ,
]);
}
if ( $picture_bg !== null ) {
$request -> validate ([
2021-01-14 13:44:59 +01:00
'picture_bg' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1200,height=650' ,
2020-10-01 16:56:27 +02:00
]);
}
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 ) {
2021-03-20 14:38:15 +01:00
if ( file_exists ( public_path () . $m -> picture )) {
2021-01-19 12:28:54 +01:00
unlink ( public_path () . $m -> picture );
2021-03-20 14:38:15 +01:00
}
$imageName = Str :: slug ( $title ) . '-le-gaulois.' . $picture -> extension ();
2021-02-16 17:32:51 +01:00
$picture -> move ( public_path ( 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' ), $imageName );
2020-10-01 16:56:27 +02:00
}
if ( $picture_bg !== null ) {
2021-03-20 14:38:15 +01:00
if ( file_exists ( public_path () . $m -> picture_bg )) {
2021-01-19 12:28:54 +01:00
unlink ( public_path () . $m -> picture_bg );
2021-03-20 14:38:15 +01:00
}
2020-10-01 16:56:27 +02:00
$imageName_bg = Str :: slug ( $title ) . '-bg-le-gaulois.' . $picture_bg -> extension ();
2021-02-16 17:32:51 +01:00
$picture_bg -> move ( public_path ( 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' ), $imageName_bg );
2020-10-01 16:56:27 +02:00
}
if ( $picture_bg_r !== null ) {
2021-03-20 14:38:15 +01:00
if ( file_exists ( public_path () . $m -> picture_bg_r )) {
2021-01-19 12:28:54 +01:00
unlink ( public_path () . $m -> picture_bg_r );
2021-03-20 14:38:15 +01:00
}
2020-10-01 16:56:27 +02:00
$imageName_bg_r = Str :: slug ( $title ) . '-bg-r-le-gaulois.' . $picture_bg_r -> extension ();
2021-02-16 17:32:51 +01:00
$picture_bg_r -> move ( public_path ( 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' ), $imageName_bg_r );
2020-10-01 16:56:27 +02:00
}
if ( $voucherPicture !== null ) {
2021-03-20 14:38:15 +01:00
if ( $m -> voucher_picture !== null && file_exists ( public_path () . $m -> voucher_picture )) {
2021-01-19 12:28:54 +01:00
unlink ( public_path () . $m -> voucher_picture );
2021-03-20 14:38:15 +01:00
}
2020-10-01 16:56:27 +02:00
$imageNameVoucher = Str :: slug ( $title ) . '-bon-reduction-le-gaulois.' . $voucherPicture -> extension ();
2021-02-16 17:32:51 +01:00
$voucherPicture -> move ( public_path ( 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' ), $imageNameVoucher );
2020-10-01 16:56:27 +02:00
}
$m -> title = $title ;
2021-01-14 13:44:59 +01:00
$m -> slug = Str :: slug ( $title );
2020-10-01 16:56:27 +02:00
$m -> description = $description ;
$m -> color = $color ;
2021-03-20 14:38:15 +01:00
if ( $picture !== null ) {
2021-02-16 17:32:51 +01:00
$m -> picture = 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' . $imageName ;
2021-03-20 14:38:15 +01:00
}
if ( $picture_bg !== null ) {
2021-02-16 17:32:51 +01:00
$m -> picture_bg = 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' . $imageName_bg ;
2021-03-20 14:38:15 +01:00
}
if ( $picture_bg_r !== null ) {
2021-02-16 17:32:51 +01:00
$m -> picture_bg_r = 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' . $imageName_bg_r ;
2021-03-20 14:38:15 +01:00
}
2020-10-01 16:56:27 +02:00
$m -> parallax = $parallax ;
$m -> status = $status ;
2021-01-22 11:43:17 +01:00
// Un moment non publié ne peut pas être moment star
2021-03-20 14:38:15 +01:00
if ( $status === 0 ) {
2021-01-22 11:43:17 +01:00
$m -> star_moment = 0 ;
2021-03-20 14:38:15 +01:00
}
2020-10-01 16:56:27 +02:00
$m -> featured_title = $featuredTitle ;
$m -> featured_text = $featuredText ;
$m -> featured_type = $featuredType ;
$m -> featured_ids = $featuredIdsString ;
$m -> featured_status = $featuredStatus ;
2021-03-20 14:38:15 +01:00
if ( $voucherPicture !== null ) {
2021-02-16 17:32:51 +01:00
$m -> voucher_picture = 'uploads/images/moments/' . date ( 'Y' ) . '/' . date ( 'm' ) . '/' . $imageNameVoucher ;
2021-03-20 14:38:15 +01:00
}
2020-10-01 16:56:27 +02:00
$m -> voucher_status = $voucherStatus ;
2020-12-15 15:27:13 +01:00
2021-01-22 11:43:17 +01:00
// Gestion des strates
2020-12-15 15:27:13 +01:00
$compteur = 0 ;
$ids = [];
2021-03-20 14:38:15 +01:00
foreach ( $strates as $strate ) {
2020-12-15 15:27:13 +01:00
$validator = Validator :: make ( get_object_vars ( $strate ), [
'title' => 'required|max:255' ,
'content' => 'required' ,
]);
2021-03-20 14:38:15 +01:00
if ( $validator -> fails ()) {
2020-12-15 15:27:13 +01:00
return back () -> with ( 'strates' , json_encode ( $strates )) -> withInput () -> withErrors ( " Les données de la strate " . $compteur . " sont incorrects, veuillez réessayer. " );
2021-03-20 14:38:15 +01:00
}
2020-12-15 15:27:13 +01:00
$compteur ++ ;
2021-03-20 14:38:15 +01:00
if ( $strate -> id !== null ) {
2020-12-15 15:27:13 +01:00
$s = MomentStrates :: find ( $strate -> id );
2021-03-20 14:38:15 +01:00
} else {
2020-12-15 15:27:13 +01:00
$s = new MomentStrates ();
2021-03-20 14:38:15 +01:00
}
2020-12-15 15:27:13 +01:00
$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 ];
2021-03-20 14:38:15 +01:00
if ( ! in_array ( $extension , [ 'png' , 'jpg' , 'jpeg' ])) {
2020-12-15 15:27:13 +01:00
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. " );
2021-03-20 14:38:15 +01:00
}
2020-12-15 15:27:13 +01:00
// Création d'un fichier temporaire pour ensuite le move si il est ok
2021-03-20 14:38:15 +01:00
$tmpFilePath = sys_get_temp_dir () . '/' . uniqid ();
2020-12-15 15:27:13 +01:00
file_put_contents ( $tmpFilePath , $image );
$tmpFile = new File ( $tmpFilePath );
$imgSize = getimagesize ( $tmpFilePath );
2021-03-20 14:38:15 +01:00
if ( $imgSize [ 0 ] >= 220 || $imgSize [ 1 ] >= 220 ) {
2020-12-15 15:27:13 +01:00
return back () -> with ( 'strates' , json_encode ( $strates )) -> withInput () -> withErrors ( " L'image de la strate " . $compteur . " a des dimensions incorrects, veuillez réessayer. " );
2021-03-20 14:38:15 +01:00
}
2020-12-15 15:27:13 +01:00
$imageName = 'strate' . $compteur . '.' . $extension ;
2021-02-16 17:32:51 +01:00
$tmpFile -> move ( public_path ( " uploads/images/moments/ $m->id " ), $imageName );
$s -> picture = 'uploads/images/moments/' . $m -> id . '/' . $imageName ;
2020-12-15 15:27:13 +01:00
}
$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 ();
2021-01-19 12:28:54 +01:00
$ids [] = $s -> id ;
2020-12-15 15:27:13 +01:00
}
if ( count ( $ids ) > 0 ) {
$idsExists = [];
2021-03-20 14:38:15 +01:00
foreach ( $m -> strates as $strateExist ) {
2020-12-15 15:27:13 +01:00
$idsExists [] = $strateExist -> id ;
2021-03-20 14:38:15 +01:00
}
foreach ( $idsExists as $idStrate ) {
if ( ! in_array ( $idStrate , $ids )) {
2020-12-15 15:27:13 +01:00
$m -> strates () -> find ( $idStrate ) -> delete ();
2021-03-20 14:38:15 +01:00
}
}
2020-12-15 15:27:13 +01:00
}
2020-10-01 16:56:27 +02:00
$m -> save ();
2021-02-16 17:32:51 +01:00
// Si c'est le moment star, on met à jour le cache des moments au cas ou le slug à changer
2021-03-20 14:38:15 +01:00
if ( $m -> star_moment === 1 && $m -> status ) {
2021-02-16 17:32:51 +01:00
Cache :: put ( 'momentFiche' , $m );
2021-03-20 14:38:15 +01:00
}
2021-02-16 17:32:51 +01:00
2020-10-01 16:56:27 +02:00
return redirect () -> route ( 'bo.moments.edit' , $m -> id ) -> with ( 'success' , 'Enregistré !' );
}
2021-04-28 13:57:12 +02:00
public function destroy ( Moments $m )
2020-10-01 16:56:27 +02:00
{
2021-04-28 15:49:47 +02:00
if ( FacadesFile :: exists ( $m -> picture )) {
FacadesFile :: delete ( $m -> picture );
2021-03-20 14:38:15 +01:00
}
2020-10-07 16:46:58 +02:00
2021-04-28 15:49:47 +02:00
if ( FacadesFile :: exists ( $m -> picture_bg )) {
FacadesFile :: delete ( $m -> picture_bg );
2021-03-20 14:38:15 +01:00
}
2020-10-07 16:46:58 +02:00
2021-04-28 15:49:47 +02:00
if ( FacadesFile :: exists ( $m -> picture_bg_r )) {
FacadesFile :: delete ( $m -> picture_bg_r );
2021-03-20 14:38:15 +01:00
}
2020-10-08 15:07:18 +02:00
2021-03-20 14:38:15 +01:00
foreach ( $m -> strates as $strate ) {
2021-04-28 15:49:47 +02:00
if ( FacadesFile :: exists ( $strate -> picture )) {
FacadesFile :: delete ( $strate -> picture );
2021-03-20 14:38:15 +01:00
}
2020-12-15 15:27:13 +01:00
}
if ( count ( $m -> strates ) > 0 ) {
2021-04-28 15:49:47 +02:00
if ( FacadesFile :: exists ( 'uploads/images/moments/' . $m -> id )) {
FacadesFile :: deleteDirectory ( 'uploads/images/moments/' . $m -> id );
}
2020-12-15 15:27:13 +01:00
$m -> strates () -> delete ();
}
2020-10-08 15:07:18 +02:00
$m -> delete ();
2020-10-07 16:46:58 +02:00
return redirect () -> route ( 'bo.moments.index' );
2020-10-01 16:56:27 +02:00
}
2021-01-19 12:28:54 +01:00
/**
* 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
*/
2021-03-20 14:38:15 +01:00
public function setStar ( $id )
{
2021-01-19 12:28:54 +01:00
$moment = Moments :: find ( $id );
2021-03-20 14:38:15 +01:00
if ( ! $moment ) {
2021-01-19 12:28:54 +01:00
abort ( 404 );
2021-03-20 14:38:15 +01:00
}
2021-01-19 12:28:54 +01:00
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 ();
2021-03-20 14:38:15 +01:00
if ( $moment -> star_moment === 1 && $moment -> status ) {
2021-01-19 12:28:54 +01:00
Cache :: put ( 'momentFiche' , $moment );
2021-03-20 14:38:15 +01:00
}
2021-01-19 12:28:54 +01:00
return redirect () -> route ( 'bo.moments.index' );
}
2020-10-01 16:56:27 +02:00
}