268 lines
9.4 KiB
PHP
268 lines
9.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
use App\Http\Requests\Back\Brands\StoreBrandRequest;
|
|
use App\Http\Requests\Back\Brands\UpdateBrandRequest;
|
|
use App\Models\Brand;
|
|
use App\Models\Site;
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Uwil\LaravelBackend\Controllers\BackendController;
|
|
use Uwil\LaravelBackend\Resources\CommonResource;
|
|
|
|
/**
|
|
* Handles CRUD operations for Brand in the backend.
|
|
*/
|
|
class BrandController extends BackendController
|
|
{
|
|
/**
|
|
* Gitlab projects collection.
|
|
*
|
|
* @var \Illuminate\Support\Collection<int,\App\Models\Site>
|
|
*/
|
|
private readonly \Illuminate\Support\Collection $siteModels;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->authorizeResource(Brand::class);
|
|
$this->modelName = Brand::modelName();
|
|
$this->modelPage = Brand::modelPage();
|
|
$this->siteModels = Site::getModelsCached();
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function index(Request $request): \Inertia\Response
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\Brand> $query */
|
|
$query = Brand::query();
|
|
|
|
if ($request->routeIs("{$this->routeName}{$this->modelName}.trash")) {
|
|
$query = $query->onlyTrashed();
|
|
}
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$query,
|
|
$search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.name'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($query);
|
|
|
|
/** Custom pagination */
|
|
$models = $this->paginate($query);
|
|
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Index", [
|
|
'models' => CommonResource::collection($models),
|
|
'search' => $search,
|
|
'searchFields' => $searchFields,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function create(Brand $brand): \Inertia\Response
|
|
{
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Create", [
|
|
'data' => CommonResource::make($brand),
|
|
'siteModels' => $this->siteModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show a specific resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function show(Brand $brand): \Inertia\Response
|
|
{
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Show", [
|
|
'data' => CommonResource::make($brand->load([
|
|
'sites' => fn($q) => $q->withoutGlobalScope(SoftDeletingScope::class),
|
|
])),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource.
|
|
*
|
|
* @param \App\Http\Requests\Back\Brands\StoreBrandRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreBrandRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$validatedData = $request->validated();
|
|
$brand = new Brand();
|
|
$brand->fill(Arr::except($validatedData, 'sites'));
|
|
|
|
if ($brand->saveOrFail()) {
|
|
$brand->sites()->syncWithoutDetaching(Arr::get($validatedData, 'sites'));
|
|
return to_route("{$this->routeName}{$this->modelName}.edit", $brand)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
return 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.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function edit(Brand $brand): \Inertia\Response
|
|
{
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Edit", [
|
|
'data' => CommonResource::make($brand->load(['sites'])),
|
|
'siteModels' => $this->siteModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource.
|
|
*
|
|
* @param \App\Http\Requests\Back\Brands\UpdateBrandRequest $request
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(
|
|
UpdateBrandRequest $request,
|
|
Brand $brand,
|
|
): \Illuminate\Http\RedirectResponse {
|
|
return DB::transaction(function () use ($request, $brand) {
|
|
$validatedData = $request->validated();
|
|
$brand->fill(Arr::except($validatedData, 'sites'));
|
|
|
|
if ($brand->saveOrFail()) {
|
|
$brand->sites()->sync(Arr::get($validatedData, 'sites'));
|
|
return to_route("{$this->routeName}{$this->modelName}.edit", $brand)
|
|
->with('success', trans('uwil-backend::crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
return to_route("{$this->routeName}{$this->modelName}.edit", $brand)
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function duplicate(Brand $brand): \Inertia\Response
|
|
{
|
|
$brand->loadMissing(Brand::getCacheRelations());
|
|
$replica = $brand->replicate();
|
|
collect($brand->getRelations())->each(fn($value, $relation) => $replica->setRelation($relation, $value));
|
|
return $this->create($replica);
|
|
}
|
|
|
|
/**
|
|
* Restore the specified resource.
|
|
*
|
|
* @param integer $brand
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function restore(int $brand): \Illuminate\Http\RedirectResponse
|
|
{
|
|
$brand = Brand::withTrashed()->findOrFail($brand);
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str((string) \parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($brand->restore()) {
|
|
return to_route("{$this->routeName}{$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 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.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Brand $brand): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str((string) \parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($brand->delete()) {
|
|
return to_route("{$this->routeName}{$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 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.
|
|
*
|
|
* @param \App\Models\Brand|integer $brand
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function forceDestroy(Brand|int $brand): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if (is_int($brand)) {
|
|
$brand = Brand::withTrashed()->findOrFail($brand);
|
|
}
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str((string) \parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($brand->forceDelete()) {
|
|
return to_route("{$this->routeName}{$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 back()
|
|
->with('error', trans('uwil-backend::crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice("models.classes.$this->modelName", 1)),
|
|
]));
|
|
}
|
|
}
|