produit porteurs code EAN, gammes image

This commit is contained in:
Olivier Legat 2021-09-06 13:36:50 +02:00
parent 6f359064a7
commit bd35ba2ddb
12 changed files with 147 additions and 6 deletions

1
.gitignore vendored
View file

@ -4,6 +4,7 @@ public/front/*
/public/storage
/public/fichiers
/public/images/produits
/public/images/ranges
/public/css/*
/public/js/*
resources/views/errors/*

View file

@ -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;

View file

@ -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 !');
}

View file

@ -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'))
],
];
}
}

View file

@ -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'))
],
];
}
}

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddImageToRangesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('ranges', function (Blueprint $table) {
$table->string('picture')->nullable()->before('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('ranges', function (Blueprint $table) {
$table->dropColumn('picture');
});
}
}

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddEanToProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->string('code_ean')->before('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('code_ean');
});
}
}

View file

@ -31,6 +31,7 @@
<thead class="thead-light">
<th scope="col">N° Article</th>
<th scope="col">Libellé</th>
<th scope="col">EAN</th>
<th scope="col">Image</th>
<th scope="col"></th>
</thead>
@ -39,10 +40,13 @@
<tr>
<td>{{$product->no_art}}</td>
<td>{{$product->name}}</td>
<td>{{$product->code_ean}}</td>
<td>
@if($product->picture)
<a data-fancybox="gallery" href="{{ asset('/images/produits/' . $product->picture ) }}">
<img src="{{ asset('/images/produits/' . $product->picture ) }}" alt="{{$product->name}}" style="max-height: 50px">
</a>
@endif
</td>
<td>
<a href="{{ route('bo.products.edit', $product->id) }}" class="btn btn-sm btn-primary">Modifier</a>

View file

@ -15,6 +15,10 @@
<label for="no_art">Code Article</label>
<input type="text" id="no_art" name="no_art" class="form-control" placeholder="no_art" value="{{ old('no_art', $product->no_art ?? '') }}">
</div>
<div class="form-group">
<label for="code_ean">Code EAN</label>
<input type="text" id="code_ean" name="code_ean" class="form-control" placeholder="code_ean" value="{{ old('code_ean', $product->code_ean ?? '') }}">
</div>
<div class="form-group">
<label for="picture">Image *<small class="text-muted">- JPEG/PNG, 2Mo max. Dimensions {{ Config::get('theme.product_image_maxwidth') }} px par {{ Config::get('theme.product_image_maxheight') }} px</small></label></br>
<input type="file" id="picture" name="picture" accept="image/png, image/jpeg">

View file

@ -2,7 +2,7 @@
@section('content')
<div class="card">
<form action="{{route('bo.ranges.edit', $range->id)}}" method="POST" enctype="multipart/form-data">
<form action="{{route('bo.ranges.update', $range->id)}}" method="POST" enctype="multipart/form-data">
@method('PUT')
@csrf
<div class="card-header">

View file

@ -30,12 +30,20 @@
<table class="table">
<thead class="thead-light">
<th scope="col">{{ __('Nom') }}</th>
<th scope="col">Image</th>
<th scope="col"></th>
</thead>
<tbody>
@foreach($ranges as $range)
<tr>
<td>{{$range->name}}</td>
<td>
@if($range->picture)
<a data-fancybox="gallery" href="{{ asset('/images/ranges/' . $range->picture ) }}">
<img src="{{ asset('/images/ranges/' . $range->picture ) }}" alt="{{$range->name}}" style="max-height: 50px">
</a>
@endif
</td>
<td>
<a href="{{ route('bo.ranges.edit', $range->id) }}" class="btn btn-sm btn-primary">{{ __('Modifier') }}</a>
<form action="{{route('bo.ranges.destroy', $range->id)}}" method="POST" class="d-inline">

View file

@ -11,3 +11,14 @@
<label for="name">Nom *</label>
<input type="text" id="name" name="name" class="form-control" placeholder="Nom de la gamme" value="{{ old('name', $range->name ?? '') }}" required>
</div>
<div class="form-group">
<label for="picture">Image *<small class="text-muted">- JPEG/PNG, 2Mo max. Dimensions {{ Config::get('theme.product_image_maxwidth') }} px par {{ Config::get('theme.product_image_maxheight') }} px</small></label></br>
<input type="file" id="picture" name="picture" accept="image/png, image/jpeg">
@if (isset($range->picture) and $range->picture !== null)
<br/>
<br/>
<a data-fancybox="gallery" href="{{ asset('/images/ranges/' . $range->picture ) }}">
<img src="{{ asset('/images/ranges/' . $range->picture ) }}" alt="{{$range->name}}" style="max-height: 100px">
</a>
@endif
</div>