184 lines
5.4 KiB
PHP
184 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Products\StoreProductRequest;
|
|
use App\Http\Requests\Bo\Products\UpdateProductRequest;
|
|
use App\Models\Product;
|
|
use App\Models\Range;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Product::class);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
/** @var Builder $productModels */
|
|
$productModels = Product::query()->with('range');
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$productModels,
|
|
$search,
|
|
null,
|
|
'name',
|
|
'ean_code',
|
|
);
|
|
}
|
|
$searchFields = \sprintf(
|
|
'%s, %s ...',
|
|
Str::ucfirst(__('validation.attributes.name')),
|
|
__('validation.custom.ean_code'),
|
|
);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($productModels);
|
|
|
|
/** Custom pagination */
|
|
$productModels = $this->paginate($productModels);
|
|
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.products.index';
|
|
|
|
return view($view, ['productModels' => $productModels, 'search' => $search, 'searchFields' => $searchFields]);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Product $product): View
|
|
{
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.products.show';
|
|
|
|
return view($view, ['productModel' => $product]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create(Product $product): View | RedirectResponse
|
|
{
|
|
$rangeModels = Range::all();
|
|
if (! count($rangeModels)) {
|
|
return to_route('bo.ranges.index')
|
|
->with(
|
|
'error',
|
|
'Vous devez ajouter une gamme avant de pouvoir ajouter un produit'
|
|
);
|
|
}
|
|
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.products.create';
|
|
|
|
return view($view, [
|
|
'productModel' => $product,
|
|
'rangeModels' => $rangeModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StoreProductRequest $request): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$product = new Product();
|
|
$product->fill($request->validated());
|
|
if ($product->saveOrFail()) {
|
|
return to_route('bo.products.edit', $product)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
}
|
|
|
|
return back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Product $product): View
|
|
{
|
|
$rangeModels = Range::all();
|
|
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.products.edit';
|
|
|
|
return view($view, [
|
|
'productModel' => $product,
|
|
'rangeModels' => $rangeModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(UpdateProductRequest $request, Product $product): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $product) {
|
|
$product->fill($request->validated());
|
|
if ($product->saveOrFail()) {
|
|
return to_route('bo.products.edit', $product)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_updated',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
}
|
|
|
|
return back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_updated',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Product $product): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($product) {
|
|
if ($product->deleteOrFail()) {
|
|
return to_route('bo.products.index')
|
|
->with('success', __(
|
|
'crud.messages.has_been_deleted',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
}
|
|
|
|
return back()
|
|
->with('error', __(
|
|
':crud.messages.cannot_be_deleted',
|
|
['model' => Str::singular(__('models.classes.product'))]
|
|
));
|
|
});
|
|
}
|
|
}
|