187 lines
6.3 KiB
PHP
187 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
use App\Http\Requests\Back\PageConfigs\StorePageConfigRequest;
|
|
use App\Http\Requests\Back\PageConfigs\UpdatePageConfigRequest;
|
|
use App\Models\PageConfig;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Inertia\Inertia;
|
|
use Uwil\LaravelBackend\Resources\CommonResource;
|
|
use Uwil\LaravelCommon\Controllers\Controller;
|
|
|
|
class PageConfigController extends Controller
|
|
{
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(PageConfig::class);
|
|
}
|
|
|
|
/**
|
|
* 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 $pageConfigModels */
|
|
$pageConfigModels = PageConfig::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$pageConfigModels,
|
|
$search,
|
|
null,
|
|
'first_name',
|
|
'last_name',
|
|
'email',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.title'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($pageConfigModels);
|
|
|
|
/** Custom pagination */
|
|
$pageConfigModels = $this->paginate($pageConfigModels);
|
|
|
|
return Inertia::render('PageConfigs/Index', [
|
|
'pageConfigModels' => CommonResource::collection($pageConfigModels),
|
|
'search' => $search,
|
|
'searchFields' => $searchFields,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\PageConfig $page_config
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function create(PageConfig $page_config): \Inertia\Response
|
|
{
|
|
return Inertia::render('PageConfigs/Create', [
|
|
'data' => CommonResource::make($page_config),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show a specific resource.
|
|
*
|
|
* @param \App\Models\PageConfig $page_config
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function show(PageConfig $page_config): \Inertia\Response
|
|
{
|
|
return Inertia::render('PageConfigs/Show', [
|
|
'data' => CommonResource::make($page_config),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Back\PageConfigs\StorePageConfigRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StorePageConfigRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$pageConfigModel = new PageConfig();
|
|
$pageConfigModel->fill($request->validated());
|
|
|
|
if ($pageConfigModel->saveOrFail()) {
|
|
return redirect()->route('back.page-configs.edit', $pageConfigModel)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.page-config', \INF))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.page-config', \INF))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\PageConfig $page_config
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function edit(PageConfig $page_config): \Inertia\Response
|
|
{
|
|
return Inertia::render('PageConfigs/Edit', [
|
|
'data' => CommonResource::make($page_config->load('pages')),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Back\PageConfigs\UpdatePageConfigRequest $request
|
|
* @param \App\Models\PageConfig $page_config
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdatePageConfigRequest $request, PageConfig $page_config): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $page_config) {
|
|
$page_config->fill($request->validated());
|
|
|
|
if ($page_config->saveOrFail()) {
|
|
return redirect()->route('back.page-configs.edit', $page_config)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.page-config', 1))
|
|
]));
|
|
}
|
|
return redirect()->route('back.page-configs.edit', $page_config)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.page-config', 1))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\PageConfig $page_config
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(PageConfig $page_config): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($page_config->deleteOrFail()) {
|
|
return redirect()->route('back.page-configs.index', $previousQueries)
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.page-config', \INF))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.page-config', \INF))
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\PageConfig $page_config
|
|
* @return \Inertia\Response
|
|
*/
|
|
public function duplicate(PageConfig $page_config): \Inertia\Response
|
|
{
|
|
return $this->create($page_config->replicate());
|
|
}
|
|
}
|