262 lines
10 KiB
PHP
262 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Enums\Approaches\ApproachTypeEnum;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Products\StoreProductRequest;
|
|
use App\Http\Requests\Bo\Products\UpdateProductRequest;
|
|
use App\Models\Approach;
|
|
use App\Models\Claim;
|
|
use App\Models\IncoNutritionalValue;
|
|
use App\Models\Meat;
|
|
use App\Models\Product;
|
|
use App\Models\ProductType;
|
|
use App\Models\Range;
|
|
use App\Models\Submeat;
|
|
use App\Models\Subrange;
|
|
use Illuminate\Http\Request;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use App\Traits\Controllers\ChangesModelOrder;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
use ChangesModelOrder;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Product::class);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $productModels */
|
|
$productModels = Product::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$productModels,
|
|
$search,
|
|
null,
|
|
'label',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.custom.label')
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($productModels);
|
|
|
|
/** Custom pagination */
|
|
$productModels = $this->paginate($productModels);
|
|
|
|
return view('bo.pages.products.index', compact('productModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function show(Product $product): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.products.show', ['productModel' => $product]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Product $product): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.products.create', [
|
|
'productModel' => $product,
|
|
'rangeModels' => $this->getModelsPaginate((new Range())->query()),
|
|
'subrangeModels' => $this->getModelsPaginate((new Subrange())->query()),
|
|
'meatModels' => $this->getModelsPaginate((new Meat())->query()),
|
|
'submeatModels' => $this->getModelsPaginate((new Submeat())->query()),
|
|
'approachDemarchModels' => $this->getModelsPaginate(
|
|
Approach::query()->where('type', ApproachTypeEnum::demarche),
|
|
'name'
|
|
),
|
|
'approachLabelModels' => $this->getModelsPaginate(
|
|
Approach::query()->where('type', ApproachTypeEnum::label),
|
|
'name'
|
|
),
|
|
'productTypeModels' => $this->getModelsPaginate((new ProductType())->query()),
|
|
'claimModels' => $this->getModelsPaginate((new Claim())->query()),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Products\StoreProductRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreProductRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$product = new Product();
|
|
$product->fill(Arr::except(
|
|
$request->validated(),
|
|
['labels', 'productTypes', 'claims', 'nutritionnal_values']
|
|
));
|
|
|
|
|
|
if ($product->saveOrFail()) {
|
|
$product->labels()->sync(collect($request->validated()['labels'] ?? [])->pluck('id'));
|
|
$product->productTypes()->sync(collect($request->validated()['productTypes'] ?? [])->pluck('id'));
|
|
$product->claims()->sync(collect($request->validated()['claims'] ?? [])->pluck('id'));
|
|
|
|
$nutritionnalValueModel = new IncoNutritionalValue();
|
|
$nutritionnalValueModel->fill(Arr::first($request->safe()->only('nutritionnal_values')));
|
|
$nutritionnalValueModel->product()->associate($product);
|
|
|
|
if ($nutritionnalValueModel->saveOrFail()) {
|
|
return redirect()->route('bo.products.edit', $product)
|
|
->with('success', trans('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans('models.classes.product'))
|
|
]));
|
|
}
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans('models.classes.product'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Product $product): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.products.edit', [
|
|
'productModel' => $product,
|
|
'nutritionnalValueModel' => $product->getNutritionnalValue(),
|
|
'rangeModels' => $this->getModelsPaginate((new Range())->query()),
|
|
'subrangeModels' => $this->getModelsPaginate((new Subrange())->query()),
|
|
'meatModels' => $this->getModelsPaginate((new Meat())->query()),
|
|
'submeatModels' => $this->getModelsPaginate((new Submeat())->query()),
|
|
'approachDemarchModels' => $this->getModelsPaginate(
|
|
Approach::query()->where('type', ApproachTypeEnum::demarche),
|
|
'name'
|
|
),
|
|
'approachLabelModels' => $this->getModelsPaginate(
|
|
Approach::query()->where('type', ApproachTypeEnum::label),
|
|
'name'
|
|
),
|
|
'productTypeModels' => $this->getModelsPaginate((new ProductType())->query()),
|
|
'claimModels' => $this->getModelsPaginate((new Claim())->query()),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Products\UpdateProductRequest $request
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateProductRequest $request, Product $product): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $product) {
|
|
$product->fill(Arr::except($request->validated(), ['labels', 'productTypes', 'claims', 'nutritionnal_values']));
|
|
$product->labels()->sync(collect($request->validated()['labels'] ?? [])->pluck('id'));
|
|
$product->productTypes()->sync(collect($request->validated()['productTypes'] ?? [])->pluck('id'));
|
|
$product->claims()->sync(collect($request->validated()['claims'] ?? [])->pluck('id'));
|
|
|
|
$nutritionnalValueModel = $product->getNutritionnalValue();
|
|
if (is_null($nutritionnalValueModel)) {
|
|
$nutritionnalValueModel = new IncoNutritionalValue();
|
|
$nutritionnalValueModel->product()->associate($product);
|
|
}
|
|
$nutritionnalValueModel->fill(Arr::first($request->safe()->only('nutritionnal_values')));
|
|
|
|
if ($product->saveOrFail() && $nutritionnalValueModel->saveOrFail()) {
|
|
return redirect()->route('bo.products.edit', $product)
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans('models.classes.product'))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.products.edit', $product)
|
|
->with('error', trans('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans('models.classes.product'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Product $product): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($product->deleteOrFail()) {
|
|
return redirect()->route('bo.products.index', $previousQueries)
|
|
->with('success', trans('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans('models.classes.product'))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans('models.classes.product'))
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Product $product
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(Product $product): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($product->replicate());
|
|
}
|
|
|
|
/**
|
|
* Undocumented function
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @param string $fieldName
|
|
* @return \Illuminate\Pagination\LengthAwarePaginator
|
|
*/
|
|
private function getModelsPaginate(
|
|
Builder $query,
|
|
string $fieldName = "label"
|
|
): \Illuminate\Pagination\LengthAwarePaginator {
|
|
return $query->where('published', true)
|
|
->orderBy($fieldName, 'ASC')
|
|
->paginate(10);
|
|
}
|
|
}
|