216 lines
7.5 KiB
PHP
216 lines
7.5 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\Product;
|
|
use Illuminate\Http\Request;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use App\Traits\Controllers\ChangesModelOrder;
|
|
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,
|
|
'first_name',
|
|
'last_name',
|
|
'email',
|
|
'role',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.custom.label'),
|
|
trans('validation.attributes.description'),
|
|
trans('validation.custom.details'),
|
|
trans('validation.custom.preparation'),
|
|
]);
|
|
|
|
/** 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
|
|
{
|
|
$approachDemarchModels = Approach::query()
|
|
->where('type', ApproachTypeEnum::demarche)
|
|
->where('published', true)
|
|
->orderBy('name', 'ASC')
|
|
->paginate(10);
|
|
$approachLabelModels = Approach::query()
|
|
->where('type', ApproachTypeEnum::label)
|
|
->where('published', true)
|
|
->orderBy('name', 'ASC')
|
|
->paginate(10);
|
|
return view('bo.pages.products.create', [
|
|
'productModel' => $product,
|
|
'approachDemarchModels' => $approachDemarchModels,
|
|
'approachLabelModels' => $approachLabelModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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']));
|
|
if ($product->saveOrFail()) {
|
|
$product->labels()->sync(collect($request->validated()['labels'])->pluck('id'));
|
|
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
|
|
{
|
|
$approachDemarchModels = Approach::query()
|
|
->where('type', ApproachTypeEnum::demarche)
|
|
->where('published', true)
|
|
->orderBy('name', 'ASC')
|
|
->paginate(10);
|
|
$approachLabelModels = Approach::query()
|
|
->where('type', ApproachTypeEnum::label)
|
|
->where('published', true)
|
|
->orderBy('name', 'ASC')
|
|
->paginate(10);
|
|
return view('bo.pages.products.edit', [
|
|
'productModel' => $product,
|
|
'approachDemarchModels' => $approachDemarchModels,
|
|
'approachLabelModels' => $approachLabelModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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']));
|
|
$product->labels()->sync(collect($request->validated()['labels'])->pluck('id'));
|
|
|
|
if ($product->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());
|
|
}
|
|
}
|