diff --git a/.gitignore b/.gitignore index 484d7c8..817c6d2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ public/front/* /public/storage /public/fichiers /public/images/produits +/public/images/ranges /public/css/* /public/js/* resources/views/errors/* diff --git a/app/Http/Controllers/bo/ProductsController.php b/app/Http/Controllers/bo/ProductsController.php index b9889ea..eba92b1 100644 --- a/app/Http/Controllers/bo/ProductsController.php +++ b/app/Http/Controllers/bo/ProductsController.php @@ -51,11 +51,13 @@ class ProductsController extends Controller $name = $request->name; $slug = Str::slug($name); $no_art = $request->no_art; + $code_ean = $request->code_ean; $picture = $request->picture; $product = new Product(); $product->name = $name; $product->no_art = $no_art; + $product->code_ean = $code_ean; $product->range()->associate($request->range_id); $product->save(); @@ -112,6 +114,7 @@ class ProductsController extends Controller $name = $request->name; $slug = Str::slug($name); $no_art = $request->no_art; + $code_ean = $request->code_ean; $picture = $request->picture; $product = Product::findOrFail($id); @@ -127,6 +130,7 @@ class ProductsController extends Controller $product->name = $name; $product->no_art = $no_art; + $product->code_ean = $code_ean; if ($picture !== null) { $product->picture = $imageName; diff --git a/app/Http/Controllers/bo/RangeController.php b/app/Http/Controllers/bo/RangeController.php index 78e4151..ca0ac23 100644 --- a/app/Http/Controllers/bo/RangeController.php +++ b/app/Http/Controllers/bo/RangeController.php @@ -6,6 +6,7 @@ use App\Http\Controllers\Controller; use App\Http\Requests\StoreRangeRequest; use App\Http\Requests\UpdateRangeRequest; use App\Models\Range; +use Illuminate\Support\Str; class RangeController extends Controller { @@ -38,9 +39,21 @@ class RangeController extends Controller */ public function store(StoreRangeRequest $request) { - if (!(new Range($request->validated()))->save()) { - return back()->withErrors('Impossible de sauvegarder la gamme'); + $name = $request->name; + $slug = Str::slug($name); + $picture = $request->picture; + + $range = new Range(); + $range->name = $name; + $range->save(); + + if ($picture !== null) { + $imageName = $range->id . '_' . $slug . '_' . Str::random(5) . '.' . $picture->extension(); + $picture->move(public_path('images/ranges'), $imageName); + $range->picture = $imageName; + $range->save(); } + return \response()->redirectToRoute('bo.ranges.index') ->with('success', 'La gamme à bien été enregistré !'); } @@ -76,8 +89,26 @@ class RangeController extends Controller */ public function update(UpdateRangeRequest $request, Range $range) { - $range->fill($request->validated()); + $name = $request->name; + $slug = Str::slug($name); + $picture = $request->picture; + + if ($picture !== null) { + if ($range->picture !== null && file_exists(public_path('images/ranges/') . $range->picture)) { + unlink(public_path('images/ranges/') . $range->picture); + } + $imageName = $range->id . '_' . $slug . '_' . Str::random(5) . '.' . $picture->extension(); + $picture->move(public_path('images/ranges'), $imageName); + } + + $range->name = $name; + + if ($picture !== null) { + $range->picture = $imageName; + } $range->saveOrFail(); + + return \response()->redirectToRoute('bo.ranges.edit', $range) ->with('success', 'La gamme à bien été mise à jour !'); } diff --git a/app/Http/Requests/StoreRangeRequest.php b/app/Http/Requests/StoreRangeRequest.php index 0771fbf..3c665de 100644 --- a/app/Http/Requests/StoreRangeRequest.php +++ b/app/Http/Requests/StoreRangeRequest.php @@ -3,6 +3,7 @@ namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Validation\Rule; class StoreRangeRequest extends FormRequest { @@ -24,7 +25,13 @@ class StoreRangeRequest extends FormRequest public function rules() { return [ - 'name' => 'required|string|min:2|max:255' + 'name' => 'required|string|min:2|max:255', + 'picture' => [ + 'image', + 'mimes:jpeg,png,jpg', + 'max:2048', + Rule::dimensions()->maxWidth(config('theme.product_image_maxwidth'))->maxHeight(config('theme.product_image_maxheight')) + ], ]; } } diff --git a/app/Http/Requests/UpdateRangeRequest.php b/app/Http/Requests/UpdateRangeRequest.php index b2bbf40..14d5fde 100644 --- a/app/Http/Requests/UpdateRangeRequest.php +++ b/app/Http/Requests/UpdateRangeRequest.php @@ -3,6 +3,7 @@ namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Validation\Rule; class UpdateRangeRequest extends FormRequest { @@ -24,7 +25,13 @@ class UpdateRangeRequest extends FormRequest public function rules() { return [ - 'name' => 'required|string|min:2|max:255' + 'name' => 'required|string|min:2|max:255', + 'picture' => [ + 'image', + 'mimes:jpeg,png,jpg', + 'max:2048', + Rule::dimensions()->maxWidth(config('theme.product_image_maxwidth'))->maxHeight(config('theme.product_image_maxheight')) + ], ]; } } diff --git a/database/migrations/2021_09_06_123234_add_image_to_ranges_table.php b/database/migrations/2021_09_06_123234_add_image_to_ranges_table.php new file mode 100644 index 0000000..1fb86b1 --- /dev/null +++ b/database/migrations/2021_09_06_123234_add_image_to_ranges_table.php @@ -0,0 +1,32 @@ +string('picture')->nullable()->before('created_at'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('ranges', function (Blueprint $table) { + $table->dropColumn('picture'); + }); + } +} diff --git a/database/migrations/2021_09_06_123345_add_ean_to_products_table.php b/database/migrations/2021_09_06_123345_add_ean_to_products_table.php new file mode 100644 index 0000000..1bafb3a --- /dev/null +++ b/database/migrations/2021_09_06_123345_add_ean_to_products_table.php @@ -0,0 +1,32 @@ +string('code_ean')->before('created_at'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('products', function (Blueprint $table) { + $table->dropColumn('code_ean'); + }); + } +} diff --git a/resources/views/bo/products/index.blade.php b/resources/views/bo/products/index.blade.php index aa2bd15..bfb8cdb 100644 --- a/resources/views/bo/products/index.blade.php +++ b/resources/views/bo/products/index.blade.php @@ -31,6 +31,7 @@ N° Article Libellé + EAN Image @@ -39,10 +40,13 @@ {{$product->no_art}} {{$product->name}} + {{$product->code_ean}} + @if($product->picture) {{$product->name}} + @endif Modifier diff --git a/resources/views/bo/products/input-forms.blade.php b/resources/views/bo/products/input-forms.blade.php index 83f1a90..cc05fae 100644 --- a/resources/views/bo/products/input-forms.blade.php +++ b/resources/views/bo/products/input-forms.blade.php @@ -15,6 +15,10 @@ +
+ + +

diff --git a/resources/views/bo/ranges/edit.blade.php b/resources/views/bo/ranges/edit.blade.php index bb0366d..6a60b6b 100644 --- a/resources/views/bo/ranges/edit.blade.php +++ b/resources/views/bo/ranges/edit.blade.php @@ -2,7 +2,7 @@ @section('content')
-
+ @method('PUT') @csrf
diff --git a/resources/views/bo/ranges/index.blade.php b/resources/views/bo/ranges/index.blade.php index 99a3fd2..bb35ee1 100644 --- a/resources/views/bo/ranges/index.blade.php +++ b/resources/views/bo/ranges/index.blade.php @@ -30,12 +30,20 @@ + @foreach($ranges as $range) +
{{ __('Nom') }}Image
{{$range->name}} + @if($range->picture) + + {{$range->name}} + + @endif + {{ __('Modifier') }} diff --git a/resources/views/bo/ranges/input-forms.blade.php b/resources/views/bo/ranges/input-forms.blade.php index 8069d20..d637307 100644 --- a/resources/views/bo/ranges/input-forms.blade.php +++ b/resources/views/bo/ranges/input-forms.blade.php @@ -11,3 +11,14 @@ +
+
+ + @if (isset($range->picture) and $range->picture !== null) +
+
+ + {{$range->name}} + + @endif +