résolution enregistrement d'images plus produit

This commit is contained in:
Olivier Legat 2022-01-10 17:03:36 +01:00
parent e5328a883a
commit b524ae0131
6 changed files with 203 additions and 136 deletions

View file

@ -5,6 +5,7 @@ namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreProductRequest;
use App\Http\Requests\UpdateProductRequest;
use App\Lib\Utils;
use App\Models\Gamme;
use App\Models\Product;
@ -31,7 +32,25 @@ class ProductsController extends Controller
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
public function create(Product $product)
{
return view('back.products.create', compact('product'));
$gammes = Gamme::all()->sortBy('title');
//default structure + produits
$tabPlusProduit1 = array(
'id' => 0,
'name' => '',
'url' => '',
);
$tabPlusProduit2 = array(
'id' => 1,
'name' => '',
'url' => '',
);
$tabPlusProduit3 = array(
'id' => 2,
'name' => '',
'url' => '',
);
$product->plusproduit = json_encode([$tabPlusProduit1, $tabPlusProduit2, $tabPlusProduit3]);
return view('back.products.create', compact('product','gammes'));
}
/**
@ -42,15 +61,25 @@ class ProductsController extends Controller
*/
public function store(StoreProductRequest $request)
{
$plusproduit = json_decode($request->plusproduit);
$product = new Product($request->validated());
foreach($plusproduit as $plus) {
$picto = 'pictogramme'.$plus->id;
$uploadfile = $request->$picto;
if($uploadfile) {
$plus->url = Utils::storeImage($uploadfile, $product->getTable());
}
}
$product->plusproduit = json_encode($plusproduit);
if (!$product->saveOrFail()) {
return redirect()->route(
'bo.products.edit',
$product->id
)->with('error', 'Le pictogramme n\'a pas été crée');
)->with('error', 'Le produit n\'a pas été crée');
}
return redirect()->route('bo.products.edit', $product->id)->with('success', 'Pictogramme créé !');
return redirect()->route('bo.products.edit', $product->id)->with('success', 'Produit créé !');
}
/**
@ -75,8 +104,26 @@ class ProductsController extends Controller
*/
public function update(Product $product, UpdateProductRequest $request)
{
//valeurs en base pour garder les anciennes images quand elles ne sont pas updatées
$dbplusproduit = json_decode($product->plusproduit);
//valeurs transmises dans le post
$plusproduit = json_decode($request->plusproduit);
$product->fill($request->validated());
foreach($plusproduit as $plus) {
//boucle pour stockage des images uploadées
$picto = 'pictogramme'.$plus->id;
$uploadfile = $request->$picto;
//set de l'url
if($uploadfile) {
$plus->url = Utils::storeImage($uploadfile, $product->getTable());
} else {
//reprise de l'ancienne valeur
$plus->url = $dbplusproduit[$plus->id]->url;
}
}
//encodage avant save
$product->plusproduit = json_encode($plusproduit);
if (!$product->saveOrFail()) {
return redirect()->route('bo.products.edit', $product->id)
->with('error', trans('La modification a échoué'));

View file

@ -14,130 +14,137 @@ class StoreProductRequest extends FormRequest
*/
public function authorize()
{
$tabFour = array(
'id' => 0,
'name' => 'Four',
'url' => '/assets/images/four.png',
'time' => $this->time_cook0,
'temp' => $this->temp_cook0,
'status' => $this->is_activated0 ? true : false
);
$tabFriteuse = array(
'id' => 1,
'name' => 'Friteuse',
'url' => '/assets/images/friteuse.png',
'time' => $this->time_cook1,
'temp' => $this->temp_cook1,
'status' => $this->is_activated1 ? true : false
);
$tabPoele = array(
'id' => 2,
'name' => 'Poêle',
'url' => '/assets/images/poele.png',
'time' => $this->time_cook2,
'temp' => $this->temp_cook2,
'status' => $this->is_activated2 ? true : false
);
$tabCocotte = array(
'id' => 3,
'name' => 'Cocotte',
'url' => '/assets/images/cocotte.png',
'time' => $this->time_cook3,
'temp' => $this->temp_cook3,
'status' => $this->is_activated3 ? true : false
);
$tabPlancha = array(
'id' => 4,
'name' => 'Plancha',
'url' => '/assets/images/plancha.png',
'time' => $this->time_cook4,
'temp' => $this->temp_cook4,
'status' => $this->is_activated4 ? true : false
);
$tabBainMarie = array(
'id' => 5,
'name' => 'Bain Marie',
'url' => '/assets/images/bainmarie.png',
'time' => $this->time_cook5,
'temp' => $this->temp_cook5,
'status' => $this->is_activated5 ? true : false
);
$tabBarbecue = array(
'id' => 6,
'name' => 'Barbecue',
'url' => '/assets/images/barbecue.png',
'time' => $this->time_cook6,
'temp' => $this->temp_cook6,
'status' => $this->is_activated6 ? true : false
);
$tabMicroOndes = array(
'id' => 7,
'name' => 'Micro Ondes',
'url' => '/assets/images/microondes.png',
'time' => $this->time_cook7,
'temp' => $this->temp_cook7,
'status' => $this->is_activated7 ? true : false
);
$tabCuisson = json_encode([
'four' => $tabFour,
'friteuse' => $tabFriteuse,
'poele' => $tabPoele,
'cocotte' => $tabCocotte,
'plancha' => $tabPlancha,
'bainmarie' => $tabBainMarie,
'barbecue' => $tabBarbecue,
'microondes' => $tabMicroOndes
]);
// var_dump(public_path() . "\assets\images\sans-label.jpg");
// var_dump(storage_path('app\public'));
// var_dump(pathinfo(storage_path('app\public')));
// exit;
// TODO storage json pictures
$urlPlusProduit = "/assets/images/sans-label.jpg";
if ($this->pictogramme0) {
$urlPlusProduit = "/assets/images/" . $this->pictogramme0->getClientOriginalName();
}
$tabPlusProduit1 = array(
'id' => 0,
'name' => $this->content0,
'url' => $urlPlusProduit,
);
$urlPlusProduit = "/assets/images/sans-label.jpg";
if ($this->pictogramme1) {
$urlPlusProduit = "/assets/images/" . $this->pictogramme1->getClientOriginalName();
}
$tabPlusProduit2 = array(
'id' => 1,
'name' => $this->content1,
'url' => $urlPlusProduit,
);
$urlPlusProduit = "/assets/images/sans-label.jpg";
if ($this->pictogramme2) {
$urlPlusProduit = "/assets/images/" . $this->pictogramme2->getClientOriginalName();
}
$tabPlusProduit3 = array(
'id' => 2,
'name' => $this->content2,
'url' => $urlPlusProduit,
);
$tabPlusProduit = json_encode([$tabPlusProduit1, $tabPlusProduit2, $tabPlusProduit3]);
$this->merge([
'slug' => Str::slug($this->name),
'gamme_id' => $this->gamme_id === '0' ? null : $this->gamme_id,
'pictos' => $tabCuisson,
'plusproduit' => $tabPlusProduit,
'is_published' => $this->is_published ? true : false,
'is_on_top' => $this->is_on_top ? true : false,
'is_food_service' => $this->is_food_service ? true : false,
'is_halal' => $this->is_halal ? true : false,
]);
return auth()->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
{
try {
$tabFour = array(
'id' => 0,
'name' => 'Four',
'url' => '/assets/images/four.png',
'time' => $this->time_cook0,
'temp' => $this->temp_cook0,
'status' => $this->is_activated0 ? true : false
);
$tabFriteuse = array(
'id' => 1,
'name' => 'Friteuse',
'url' => '/assets/images/friteuse.png',
'time' => $this->time_cook1,
'temp' => $this->temp_cook1,
'status' => $this->is_activated1 ? true : false
);
$tabPoele = array(
'id' => 2,
'name' => 'Poêle',
'url' => '/assets/images/poele.png',
'time' => $this->time_cook2,
'temp' => $this->temp_cook2,
'status' => $this->is_activated2 ? true : false
);
$tabCocotte = array(
'id' => 3,
'name' => 'Cocotte',
'url' => '/assets/images/cocotte.png',
'time' => $this->time_cook3,
'temp' => $this->temp_cook3,
'status' => $this->is_activated3 ? true : false
);
$tabPlancha = array(
'id' => 4,
'name' => 'Plancha',
'url' => '/assets/images/plancha.png',
'time' => $this->time_cook4,
'temp' => $this->temp_cook4,
'status' => $this->is_activated4 ? true : false
);
$tabBainMarie = array(
'id' => 5,
'name' => 'Bain Marie',
'url' => '/assets/images/bainmarie.png',
'time' => $this->time_cook5,
'temp' => $this->temp_cook5,
'status' => $this->is_activated5 ? true : false
);
$tabBarbecue = array(
'id' => 6,
'name' => 'Barbecue',
'url' => '/assets/images/barbecue.png',
'time' => $this->time_cook6,
'temp' => $this->temp_cook6,
'status' => $this->is_activated6 ? true : false
);
$tabMicroOndes = array(
'id' => 7,
'name' => 'Micro Ondes',
'url' => '/assets/images/microondes.png',
'time' => $this->time_cook7,
'temp' => $this->temp_cook7,
'status' => $this->is_activated7 ? true : false
);
$tabCuisson = json_encode([
'four' => $tabFour,
'friteuse' => $tabFriteuse,
'poele' => $tabPoele,
'cocotte' => $tabCocotte,
'plancha' => $tabPlancha,
'bainmarie' => $tabBainMarie,
'barbecue' => $tabBarbecue,
'microondes' => $tabMicroOndes
]);
$urlPlusProduit = "";
if ($this->pictogramme0) {
$urlPlusProduit = "/assets/images/" . $this->pictogramme0->getClientOriginalName();
}
$tabPlusProduit1 = array(
'id' => 0,
'name' => $this->content0,
'url' => $urlPlusProduit,
);
$urlPlusProduit = "";
if ($this->pictogramme1) {
$urlPlusProduit = "/assets/images/" . $this->pictogramme1->getClientOriginalName();
}
$tabPlusProduit2 = array(
'id' => 1,
'name' => $this->content1,
'url' => $urlPlusProduit,
);
$urlPlusProduit = "";
if ($this->pictogramme2) {
$urlPlusProduit = "/assets/images/" . $this->pictogramme2->getClientOriginalName();
}
$tabPlusProduit3 = array(
'id' => 2,
'name' => $this->content2,
'url' => $urlPlusProduit,
);
$tabPlusProduit = json_encode([$tabPlusProduit1, $tabPlusProduit2, $tabPlusProduit3]);
$this->merge([
'slug' => Str::slug($this->name),
'gamme_id' => $this->gamme_id === '0' ? null : $this->gamme_id,
'pictos' => $tabCuisson,
'plusproduit' => $tabPlusProduit,
'is_published' => $this->is_published ? true : false,
'is_on_top' => $this->is_on_top ? true : false,
'is_food_service' => $this->is_food_service ? true : false,
'is_halal' => $this->is_halal ? true : false,
]);
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
} catch (InvalidFormatException $e) {
// On fait rien
}
}
/**
* Get the validation rules that apply to the request.
*
@ -154,6 +161,9 @@ class StoreProductRequest extends FormRequest
'alt_text_picture' => 'required|max:255',
'pictos' => 'sometimes|nullable',
'plusproduit' => 'sometimes|nullable',
'pictogramme0' => 'nullable|image|mimes:png|max:1024|dimensions:width=141,height=100',
'pictogramme1' => 'nullable|image|mimes:png|max:1024|dimensions:width=141,height=100',
'pictogramme2' => 'nullable|image|mimes:png|max:1024|dimensions:width=141,height=100',
'is_published' => 'nullable|boolean',
'is_on_top' => 'nullable|boolean',
'is_food_service' => 'nullable|boolean',

View file

@ -65,6 +65,16 @@ class Product extends Model
}
}
}
//delete des images de plus produit
$plusproduit = json_decode($product->plusproduit);
foreach($plusproduit as $plus) {
$urlPicture = public_path() . $plus->url;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
});
parent::boot();
}

View file

@ -27,8 +27,8 @@ class CreateProductsTable extends Migration
$table->boolean('is_on_top');
$table->boolean('is_food_service');
$table->boolean('is_halal');
$table->text('seo_title');
$table->text('seo_description');
$table->text('seo_title')->nullable();
$table->text('seo_description')->nullable();
$table->timestamps();
});
}

View file

@ -1,17 +1,17 @@
@extends('layouts.backend')
@section('content')
<form action="{{route('bo.gammes.store')}}" method="POST" enctype="multipart/form-data">
<form action="{{route('bo.products.store')}}" method="POST" enctype="multipart/form-data">
@csrf
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2"><a href="{{route('bo.gammes.index')}}" class="h2 text-decoration-none">
< </a> {{ __('Gamme') }} <small class="text-muted">{{ __('Création') }}</small></h1>
<h1 class="h2"><a href="{{route('bo.products.index')}}" class="h2 text-decoration-none">
< </a> {{ __('Produit') }} <small class="text-muted">{{ __('Création') }}</small></h1>
<div class="btn-toolbar mb-2 mb-md-0">
<button type="submit" class="btn btn-sm btn-primary">{{ __('Enregistrer') }}</a>
</div>
</div>
@include('back.gammes.form-inputs')
@include('back.products.form-inputs')
</form>
@endsection
@ -63,4 +63,4 @@
console.error(error);
});
</script>
@endsection
@endsection

View file

@ -100,19 +100,19 @@ $plusproduit = json_decode($product->plusproduit);
<tbody>
@foreach ($plusproduit as $plus)
<tr>
<td><input type="text" id="content{{$plus->id}}" name="content{{$plus->id}}" class="form-control" placeholder="{{ __('Texte') }} *" value="{{ old('content', $plus->name ?? '') }}" required></td>
<td><input type="text" id="content{{$plus->id}}" name="content{{$plus->id}}" class="form-control" placeholder="{{ __('Texte') }} *" value="{{ old('content'.$plus->id, $plus->name ?? '') }}" required></td>
@include('back.modules.input-error', ['inputName'=>'content{{$plus->id}}'])
<td>
<div class="form-group">
<label for="pictogramme{{$plus->id}}">{{ __('Pictogramme') }} <small class="text-muted">- PNG, 500Ko max. Dimensions {{ Config::get('bo.plusproduit_image_maxwidth') }} px par {{ Config::get('bo.plusproduit_image_maxheight') }} px</small></label><br/>
<label for="pictogramme{{$plus->id}}">{{ __('Pictogramme') }} <small class="text-muted">- PNG, 500Ko max. Dimensions 141 px par 100 px</small></label><br/>
<input type="file" id="pictogramme{{$plus->id}}" name="pictogramme{{$plus->id}}" accept="image/png">
@if ($plus->url !== null)
@if (!empty($plus->url))
<br/>
<br/>
<img src="{{$plus->url}}" alt="{{$plus->name}}" id="aleg{{$plus->id}}" name="aleg{{$plus->id}}" style="max-height: 100px;">
@endif
@include('back.modules.input-error', ['inputName'=>'pictogramme{{$plus->id}}'])
</div>
</div>
</td>
</tr>
@endforeach
@ -141,4 +141,4 @@ $plusproduit = json_decode($product->plusproduit);
<input class="form-check-input" type="checkbox" id="is_halal" name="is_halal" @if (old('is_halal', $product->is_halal) === 1 || old('is_halal', $product->is_halal) === 'on')checked="checked" @endif>
<label class="form-check-label" for="is_halal">{{ __('Produit Halal') }}</label>
@include('back.modules.input-error', ['inputName'=>'is_halal'])
</div>
</div>