Merge branch 'issue-12' into 'develop'

Products can now be published/unpublished from the index list

Closes #12

See merge request ldc-web/projets/boutique-cnc-sable!22
This commit is contained in:
Vincent BRIDIER 2023-06-30 10:41:14 +00:00
commit 9935384a3e
5 changed files with 58 additions and 12 deletions

View file

@ -14,12 +14,15 @@ use App\Models\Range;
use App\Models\Tag;
use App\Models\TaxRate;
use App\Models\XmlInco;
use App\Traits\Controllers\UpdateModelStatus;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\File;
class ProductsController extends Controller
{
use UpdateModelStatus;
/**
* Display a listing of the resource.
*
@ -199,12 +202,11 @@ class ProductsController extends Controller
/**
* Show the form for editing the specified resource.
*
* @param integer $id
* @param App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(int $id)
public function edit(Product $product)
{
$product = Product::findOrFail($id);
if (!empty($product->product_type)) {
$product->additionnalProductInfo = $product->additionnalProductInfo($product->product_type);
/**
@ -494,13 +496,11 @@ class ProductsController extends Controller
/**
* destroy a product (soft delete)
*
* @param integer $id
* @param App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(int $id)
public function destroy(Product $product)
{
$product = Product::findOrFail($id);
if ($product->picture !== null && file_exists(public_path('images/produits/') . $product->picture)) {
unlink(public_path('images/produits/') . $product->picture);
}

View file

@ -2,6 +2,7 @@
namespace App\Providers;
use App\Models\Product;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
@ -42,6 +43,8 @@ class RouteServiceProvider extends ServiceProvider
->namespace($this->namespace)
->group(base_path('routes/web-fo.php'));
});
Route::model('product', Product::class);
}
/**

View file

@ -0,0 +1,30 @@
<?php
namespace App\Traits\Controllers;
use Illuminate\Database\Eloquent\Model;
/**
* This trait works with Route::bind in App service provider
* to resolve the model and the DB row
* It does not work with binder Service
*/
trait UpdateModelStatus
{
/**
* Change model published
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Http\RedirectResponse
*/
public function changePublished(Model $model)
{
$model->status = !$model->status;
$model->saveOrFail();
if (!$model->saveOrFail()) {
return redirect()->back()->with('error', __('The status could not be changed'));
}
return redirect()->back()->with('success', __('The status has been changed'));
}
}

View file

@ -93,11 +93,19 @@
<td>{{$product->slug}}</td>
<td>{{$product->range->name}}</td>
<td>
@if ($product->status === 0)
<i class="fas fa-times cross-red"></i>
@else
<i class="fas fa-check check-green"></i>
@endif
<form action="{{ route('bo.products.change-published', $product) }}" method="POST">
@csrf
<button type="submit"
class="btn btn-sm"
title="{{ __($product->status ? __('Dépublier') : __('Publier')) }}"
data-bs-toggle="tooltip">
@if($product->status)
<i class="fas fa-check check-green"></i>
@else
<i class="fas fa-times cross-red"></i>
@endif
</button>
</form>
</td>
<td class="text-center">
<a href="{{route('bo.products.edit', $product->id)}}" class="btn btn-sm btn-primary">Éditer</a>

View file

@ -82,6 +82,11 @@ Route::namespace('App\Http\Controllers\Bo')
'products',
'\App\Http\Controllers\Bo\ProductsController'
)->except(['create', 'show']);
Route::post(
'/products/{product}/change-published',
[\App\Http\Controllers\Bo\ProductsController::class, 'changePublished']
)
->name('products.change-published');
Route::resource('slides', SlidesController::class)->except(['show']);
Route::resource('sliders', SlidersController::class)->except(['show']);
Route::resource('tags', TagsController::class)->except(['show']);