2025-09-16 16:44:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
|
|
|
|
|
|
use App\Http\Requests\Back\DynamicPages\StoreDynamicPageRequest;
|
|
|
|
|
use App\Http\Requests\Back\DynamicPages\UpdateDynamicPageRequest;
|
|
|
|
|
use App\Models\DynamicPage;
|
|
|
|
|
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 DynamicPageController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Create the controller instance.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->authorizeResource(DynamicPage::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Inertia\Response
|
|
|
|
|
*/
|
|
|
|
|
public function index(Request $request): \Inertia\Response
|
|
|
|
|
{
|
2025-09-19 16:20:48 +02:00
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\DynamicPage> $query */
|
|
|
|
|
$query = DynamicPage::query()->with('config');
|
|
|
|
|
|
|
|
|
|
if ($request->routeIs("back.dynamic-pages.trash")) {
|
2025-09-23 13:20:23 +02:00
|
|
|
$query = $query->onlyTrashed()->with(['config' => fn($q) => $q->withTrashed()]);
|
2025-09-19 16:20:48 +02:00
|
|
|
}
|
2025-09-16 16:44:49 +02:00
|
|
|
|
|
|
|
|
/** @var string $search Search field */
|
|
|
|
|
$search = $request->search;
|
|
|
|
|
if ($search) {
|
|
|
|
|
$this->searchQuery(
|
2025-09-19 16:20:48 +02:00
|
|
|
$query,
|
2025-09-16 16:44:49 +02:00
|
|
|
$search,
|
|
|
|
|
null,
|
2025-09-19 16:20:48 +02:00
|
|
|
'title',
|
|
|
|
|
'content',
|
2025-09-16 16:44:49 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$searchFields = \implode(', ', [
|
|
|
|
|
trans('validation.attributes.title'),
|
2025-09-19 16:20:48 +02:00
|
|
|
trans('validation.attributes.content'),
|
2025-09-16 16:44:49 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
/** Sort columns with a query */
|
2025-09-19 16:20:48 +02:00
|
|
|
$this->sortQuery($query);
|
2025-09-16 16:44:49 +02:00
|
|
|
|
|
|
|
|
/** Custom pagination */
|
2025-09-19 16:20:48 +02:00
|
|
|
$dynamicPageModels = $this->paginate($query);
|
2025-09-16 16:44:49 +02:00
|
|
|
|
|
|
|
|
return Inertia::render('DynamicPages/Index', [
|
|
|
|
|
'dynamicPageModels' => CommonResource::collection($dynamicPageModels),
|
|
|
|
|
'search' => $search,
|
|
|
|
|
'searchFields' => $searchFields,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\DynamicPage $dynamic_page
|
|
|
|
|
* @return \Inertia\Response
|
|
|
|
|
*/
|
|
|
|
|
public function create(DynamicPage $dynamic_page): \Inertia\Response
|
|
|
|
|
{
|
|
|
|
|
$pageConfigModels = PageConfig::query()->get();
|
|
|
|
|
return Inertia::render('DynamicPages/Create', [
|
|
|
|
|
'data' => CommonResource::make($dynamic_page),
|
|
|
|
|
'pageConfigModels' => $pageConfigModels,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show a specific resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\DynamicPage $dynamic_page
|
|
|
|
|
* @return \Inertia\Response
|
|
|
|
|
*/
|
|
|
|
|
public function show(DynamicPage $dynamic_page): \Inertia\Response
|
|
|
|
|
{
|
|
|
|
|
return Inertia::render('DynamicPages/Show', [
|
|
|
|
|
'data' => CommonResource::make($dynamic_page->load('config')),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-19 16:20:48 +02:00
|
|
|
* Store a newly created resource.
|
2025-09-16 16:44:49 +02:00
|
|
|
*
|
|
|
|
|
* @param \App\Http\Requests\Back\DynamicPages\StoreDynamicPageRequest $request
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function store(StoreDynamicPageRequest $request): \Illuminate\Http\RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
return DB::transaction(function () use ($request) {
|
|
|
|
|
$dynamicPageModel = new DynamicPage();
|
|
|
|
|
$dynamicPageModel->fill($request->validated());
|
|
|
|
|
|
|
|
|
|
if ($dynamicPageModel->saveOrFail()) {
|
|
|
|
|
return redirect()->route('back.dynamic-pages.edit', ['dynamic_page' => $dynamicPageModel])
|
|
|
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', \INF))
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
return redirect()->back()
|
|
|
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', \INF))
|
|
|
|
|
]));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\DynamicPage $dynamic_page
|
|
|
|
|
* @return \Inertia\Response
|
|
|
|
|
*/
|
|
|
|
|
public function edit(DynamicPage $dynamic_page): \Inertia\Response
|
|
|
|
|
{
|
|
|
|
|
$pageConfigModels = PageConfig::query()->get();
|
|
|
|
|
return Inertia::render('DynamicPages/Edit', [
|
|
|
|
|
'data' => CommonResource::make($dynamic_page),
|
|
|
|
|
'pageConfigModels' => $pageConfigModels,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-19 16:20:48 +02:00
|
|
|
* Update the specified resource.
|
2025-09-16 16:44:49 +02:00
|
|
|
*
|
|
|
|
|
* @param \App\Http\Requests\Back\DynamicPages\UpdateDynamicPageRequest $request
|
|
|
|
|
* @param \App\Models\DynamicPage $dynamic_page
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function update(
|
|
|
|
|
UpdateDynamicPageRequest $request,
|
|
|
|
|
DynamicPage $dynamic_page
|
|
|
|
|
): \Illuminate\Http\RedirectResponse {
|
|
|
|
|
return DB::transaction(function () use ($request, $dynamic_page) {
|
|
|
|
|
$dynamic_page->fill($request->validated());
|
|
|
|
|
|
|
|
|
|
if ($dynamic_page->saveOrFail()) {
|
|
|
|
|
return redirect()->route('back.dynamic-pages.edit', ['dynamic_page' => $dynamic_page])
|
|
|
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', \INF))
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
return redirect()->route('back.dynamic-pages.edit', ['dynamic_page' => $dynamic_page])
|
|
|
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', \INF))
|
|
|
|
|
]));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-19 16:20:48 +02:00
|
|
|
* Duplicate the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\DynamicPage $dynamic_page
|
|
|
|
|
* @return \Inertia\Response
|
|
|
|
|
*/
|
|
|
|
|
public function duplicate(DynamicPage $dynamic_page): \Inertia\Response
|
|
|
|
|
{
|
|
|
|
|
return $this->create($dynamic_page->replicate());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Restore the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $dynamic_page
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function restore(int $dynamic_page): \Illuminate\Http\RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$dynamic_page = DynamicPage::withTrashed()->findOrFail($dynamic_page);
|
|
|
|
|
/** @var array<string,string> */
|
|
|
|
|
$previousQueries = [];
|
|
|
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
2025-09-23 13:20:23 +02:00
|
|
|
if ($dynamic_page->restore()) {
|
2025-09-19 16:20:48 +02:00
|
|
|
return redirect()->route('back.dynamic-pages.trash', $previousQueries)
|
2025-09-23 13:20:23 +02:00
|
|
|
->with('success', __('crud.messages.has_been_restored', [
|
2025-09-19 16:20:48 +02:00
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', \INF))
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
return redirect()->back()
|
2025-09-23 13:20:23 +02:00
|
|
|
->with('error', __('crud.messages.cannot_be_restored', [
|
2025-09-19 16:20:48 +02:00
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', \INF))
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroy the specified resource.
|
2025-09-16 16:44:49 +02:00
|
|
|
*
|
|
|
|
|
* @param \App\Models\DynamicPage $dynamic_page
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function destroy(DynamicPage $dynamic_page): \Illuminate\Http\RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
/** @var array<string,string> */
|
|
|
|
|
$previousQueries = [];
|
|
|
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
2025-09-23 13:20:23 +02:00
|
|
|
if ($dynamic_page->delete()) {
|
2025-09-16 16:44:49 +02:00
|
|
|
return redirect()->route('back.dynamic-pages.index', $previousQueries)
|
|
|
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', \INF))
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
return redirect()->back()
|
|
|
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', \INF))
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-19 16:20:48 +02:00
|
|
|
* Destroy the specified resource.
|
2025-09-16 16:44:49 +02:00
|
|
|
*
|
2025-09-19 16:20:48 +02:00
|
|
|
* @param \App\Models\DynamicPage|integer $dynamic_page
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2025-09-16 16:44:49 +02:00
|
|
|
*/
|
2025-09-19 16:20:48 +02:00
|
|
|
public function forceDestroy(DynamicPage|int $dynamic_page): \Illuminate\Http\RedirectResponse
|
2025-09-16 16:44:49 +02:00
|
|
|
{
|
2025-09-19 16:20:48 +02:00
|
|
|
if (is_int($dynamic_page)) {
|
|
|
|
|
$dynamic_page = DynamicPage::withTrashed()->findOrFail($dynamic_page);
|
|
|
|
|
}
|
|
|
|
|
/** @var array<string,string> */
|
|
|
|
|
$previousQueries = [];
|
|
|
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
|
|
|
if ($dynamic_page->forceDelete()) {
|
|
|
|
|
return redirect()->route('back.dynamic-pages.trash', $previousQueries)
|
|
|
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', \INF))
|
|
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
return redirect()->back()
|
|
|
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
|
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', \INF))
|
|
|
|
|
]));
|
2025-09-16 16:44:49 +02:00
|
|
|
}
|
|
|
|
|
}
|