From b524ae0131e216fa7ece393d7c0daff2b66b1ea1 Mon Sep 17 00:00:00 2001 From: Olivier Legat Date: Mon, 10 Jan 2022 17:03:36 +0100 Subject: [PATCH] =?UTF-8?q?r=C3=A9solution=20enregistrement=20d'images=20p?= =?UTF-8?q?lus=20produit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Bo/ProductsController.php | 53 +++- app/Http/Requests/StoreProductRequest.php | 252 +++++++++--------- app/Models/Product.php | 10 + ...021_12_22_100045_create_products_table.php | 4 +- .../views/back/products/create.blade.php | 10 +- .../views/back/products/form-inputs.blade.php | 10 +- 6 files changed, 203 insertions(+), 136 deletions(-) diff --git a/app/Http/Controllers/Bo/ProductsController.php b/app/Http/Controllers/Bo/ProductsController.php index f49f405..6e72394 100644 --- a/app/Http/Controllers/Bo/ProductsController.php +++ b/app/Http/Controllers/Bo/ProductsController.php @@ -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é')); diff --git a/app/Http/Requests/StoreProductRequest.php b/app/Http/Requests/StoreProductRequest.php index d0f4292..224affe 100644 --- a/app/Http/Requests/StoreProductRequest.php +++ b/app/Http/Requests/StoreProductRequest.php @@ -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', diff --git a/app/Models/Product.php b/app/Models/Product.php index 33235b1..6b60059 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -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(); } diff --git a/database/migrations/2021_12_22_100045_create_products_table.php b/database/migrations/2021_12_22_100045_create_products_table.php index 14c0adc..f6cfa27 100644 --- a/database/migrations/2021_12_22_100045_create_products_table.php +++ b/database/migrations/2021_12_22_100045_create_products_table.php @@ -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(); }); } diff --git a/resources/views/back/products/create.blade.php b/resources/views/back/products/create.blade.php index 3c62236..760e6af 100644 --- a/resources/views/back/products/create.blade.php +++ b/resources/views/back/products/create.blade.php @@ -1,17 +1,17 @@ @extends('layouts.backend') @section('content') -
+ @csrf
-

- < {{ __('Gamme') }} {{ __('Création') }}

+

+ < {{ __('Produit') }} {{ __('Création') }}

- @include('back.gammes.form-inputs') + @include('back.products.form-inputs')
@endsection @@ -63,4 +63,4 @@ console.error(error); }); -@endsection \ No newline at end of file +@endsection diff --git a/resources/views/back/products/form-inputs.blade.php b/resources/views/back/products/form-inputs.blade.php index 3ae7622..f0d7018 100644 --- a/resources/views/back/products/form-inputs.blade.php +++ b/resources/views/back/products/form-inputs.blade.php @@ -100,19 +100,19 @@ $plusproduit = json_decode($product->plusproduit); @foreach ($plusproduit as $plus) - + @include('back.modules.input-error', ['inputName'=>'content{{$plus->id}}'])
-
+
- @if ($plus->url !== null) + @if (!empty($plus->url))

{{$plus->name}} @endif @include('back.modules.input-error', ['inputName'=>'pictogramme{{$plus->id}}']) -
+ @endforeach @@ -141,4 +141,4 @@ $plusproduit = json_decode($product->plusproduit); is_halal) === 1 || old('is_halal', $product->is_halal) === 'on')checked="checked" @endif> @include('back.modules.input-error', ['inputName'=>'is_halal']) - \ No newline at end of file +