241 lines
7.8 KiB
PHP
241 lines
7.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
use App\Http\Requests\Back\Products\IndexProductRequest;
|
|
use App\Http\Requests\Back\Products\StoreProductRequest;
|
|
use App\Http\Requests\Back\Products\UpdateProductRequest;
|
|
use App\Models\Product;
|
|
use Illuminate\Contracts\Session\Session;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Inertia\Response;
|
|
use Uwil\LaravelBackend\Resources\CommonResource;
|
|
|
|
/**
|
|
* Handles CRUD operations for Product in the backend.
|
|
*/
|
|
class ProductController extends BackendController
|
|
{
|
|
public function __construct(
|
|
Request $request,
|
|
Session $session,
|
|
) {
|
|
parent::__construct(
|
|
$request,
|
|
$session
|
|
);
|
|
|
|
$this->authorizeResource(Product::class);
|
|
$this->modelName = Product::modelName();
|
|
$this->modelPage = Product::modelPage();
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(IndexProductRequest $request): Response
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\Product> $query */
|
|
$query = Product::query();
|
|
|
|
if ($request->routeIs("back.{$this->modelName}.trash")) {
|
|
$query = $query->onlyTrashed();
|
|
}
|
|
|
|
$searchValue = $request->getSearchValue();
|
|
if ($searchValue) {
|
|
$this->searchQuery(
|
|
$query,
|
|
$searchValue,
|
|
null,
|
|
'name'
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.name'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($query);
|
|
|
|
/** Custom pagination */
|
|
$paginatedModels = $this->paginate($query);
|
|
|
|
return $this->inertiaRender("back/Pages/{$this->modelPage}/Index", [
|
|
'paginatedModels' => CommonResource::collection($paginatedModels),
|
|
'search' => $searchValue,
|
|
'searchFields' => $searchFields,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create(Product $product): Response
|
|
{
|
|
return $this->inertiaRender("back/Pages/{$this->modelPage}/Create", [
|
|
'data' => CommonResource::make($product),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show a specific resource.
|
|
*/
|
|
public function show(Product $product): Response
|
|
{
|
|
return $this->inertiaRender("back/Pages/{$this->modelPage}/Show", [
|
|
'data' => CommonResource::make($product),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource.
|
|
*/
|
|
public function store(StoreProductRequest $request): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$product = new Product();
|
|
$product->inertiaFill($request->validated());
|
|
|
|
if ($product->saveOrFail()) {
|
|
return redirect()->route("back.{$this->modelName}.edit", $product)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
|
|
]));
|
|
}
|
|
|
|
return redirect()->back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Product $product): Response
|
|
{
|
|
return $this->inertiaRender("back/Pages/{$this->modelPage}/Edit", [
|
|
'data' => CommonResource::make($product),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource.
|
|
*/
|
|
public function update(
|
|
UpdateProductRequest $request,
|
|
Product $product
|
|
): RedirectResponse {
|
|
return DB::transaction(function () use ($request, $product) {
|
|
$product->inertiaFill($request->validated());
|
|
|
|
if ($product->saveOrFail()) {
|
|
return redirect()->route("back.{$this->modelName}.edit", $product)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
|
|
]));
|
|
}
|
|
|
|
return redirect()->route("back.{$this->modelName}.edit", $product)
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*/
|
|
public function duplicate(Product $product): Response
|
|
{
|
|
return $this->create($product->replicate());
|
|
}
|
|
|
|
/**
|
|
* Restore the specified resource.
|
|
*/
|
|
public function restore(int $product): RedirectResponse
|
|
{
|
|
$product = Product::withTrashed()->findOrFail($product);
|
|
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
if ($product->restore()) {
|
|
return redirect()->route("back.{$this->modelName}.trash", $previousQueries)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_restored', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
|
|
]));
|
|
}
|
|
|
|
return redirect()->back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_restored', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Destroy the specified resource.
|
|
*/
|
|
public function destroy(Product $product): RedirectResponse
|
|
{
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
if ($product->delete()) {
|
|
return redirect()->route("back.{$this->modelName}.index", $previousQueries)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
|
|
]));
|
|
}
|
|
|
|
return redirect()->back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Destroy the specified resource.
|
|
*/
|
|
public function forceDestroy(Product | int $product): RedirectResponse
|
|
{
|
|
if (is_int($product)) {
|
|
$product = Product::withTrashed()->findOrFail($product);
|
|
}
|
|
|
|
$previousQueries = $this->getPreviousQueries();
|
|
|
|
if ($product->forceDelete()) {
|
|
return redirect()->route("back.{$this->modelName}.trash", $previousQueries)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
|
|
]));
|
|
}
|
|
|
|
return redirect()->back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.{$this->modelName}", 1)),
|
|
]));
|
|
}
|
|
|
|
/** @return array<array-key,string|array<mixed>> */
|
|
private function getPreviousQueries(): array
|
|
{
|
|
$previousQueries = [];
|
|
$previousQueryString = \parse_url($this->getPreviousUrl(), \PHP_URL_QUERY) ?? '' ?: '';
|
|
\parse_str($previousQueryString, $previousQueries);
|
|
|
|
return $previousQueries;
|
|
}
|
|
|
|
private function getPreviousUrl(): string
|
|
{
|
|
return url()->previous(route('back.dashboard.index'));
|
|
}
|
|
}
|