246 lines
8.4 KiB
PHP
246 lines
8.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Back;
|
||
|
|
|
||
|
|
use App\Http\Requests\Back\Notices\StoreNoticeRequest;
|
||
|
|
use App\Http\Requests\Back\Notices\UpdateNoticeRequest;
|
||
|
|
use App\Models\Notice;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
use Uwil\LaravelBackend\Controllers\BackendController;
|
||
|
|
use Uwil\LaravelBackend\Resources\CommonResource;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handles CRUD operations for Notice in the backend.
|
||
|
|
*/
|
||
|
|
class NoticeController extends BackendController
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Create the controller instance.
|
||
|
|
*/
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->authorizeResource(Notice::class);
|
||
|
|
$this->modelName = Notice::modelName();
|
||
|
|
$this->modelPage = Notice::modelPage();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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\Notice> $query */
|
||
|
|
$query = Notice::query();
|
||
|
|
|
||
|
|
if ($request->routeIs("back.$this->modelName.trash")) {
|
||
|
|
$query = $query->onlyTrashed();
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @var string $search Search field */
|
||
|
|
$search = $request->search;
|
||
|
|
if ($search) {
|
||
|
|
$this->searchQuery(
|
||
|
|
$query,
|
||
|
|
$search,
|
||
|
|
null,
|
||
|
|
// TODO: Fields here.
|
||
|
|
);
|
||
|
|
}
|
||
|
|
$searchFields = \implode(', ', [
|
||
|
|
// TODO: Translations here.
|
||
|
|
]);
|
||
|
|
|
||
|
|
/** 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\Notice $notice
|
||
|
|
* @return \Inertia\Response
|
||
|
|
*/
|
||
|
|
public function create(Notice $notice): \Inertia\Response
|
||
|
|
{
|
||
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Create", [
|
||
|
|
'data' => CommonResource::make($notice),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show a specific resource.
|
||
|
|
*
|
||
|
|
* @param \App\Models\Notice $notice
|
||
|
|
* @return \Inertia\Response
|
||
|
|
*/
|
||
|
|
public function show(Notice $notice): \Inertia\Response
|
||
|
|
{
|
||
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Show", [
|
||
|
|
'data' => CommonResource::make($notice),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Store a newly created resource.
|
||
|
|
*
|
||
|
|
* @param \App\Http\Requests\Back\Notices\StoreNoticeRequest $request
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function store(StoreNoticeRequest $request): \Illuminate\Http\RedirectResponse
|
||
|
|
{
|
||
|
|
return DB::transaction(function () use ($request) {
|
||
|
|
$notice = new Notice();
|
||
|
|
$notice->inertiaFill($request->validated());
|
||
|
|
|
||
|
|
if ($notice->saveOrFail()) {
|
||
|
|
return redirect()->route("back.$this->modelName.edit", $notice)
|
||
|
|
->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.
|
||
|
|
*
|
||
|
|
* @param \App\Models\Notice $notice
|
||
|
|
* @return \Inertia\Response
|
||
|
|
*/
|
||
|
|
public function edit(Notice $notice): \Inertia\Response
|
||
|
|
{
|
||
|
|
return $this->inertiaRender("back/Pages/$this->modelPage/Edit", [
|
||
|
|
'data' => CommonResource::make($notice),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the specified resource.
|
||
|
|
*
|
||
|
|
* @param \App\Http\Requests\Back\Notices\UpdateNoticeRequest $request
|
||
|
|
* @param \App\Models\Notice $notice
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function update(
|
||
|
|
UpdateNoticeRequest $request,
|
||
|
|
Notice $notice
|
||
|
|
): \Illuminate\Http\RedirectResponse {
|
||
|
|
return DB::transaction(function () use ($request, $notice) {
|
||
|
|
$notice->inertiaFill($request->validated());
|
||
|
|
|
||
|
|
if ($notice->saveOrFail()) {
|
||
|
|
return redirect()->route("back.$this->modelName.edit", $notice)
|
||
|
|
->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", $notice)
|
||
|
|
->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\Notice $notice
|
||
|
|
* @return \Inertia\Response
|
||
|
|
*/
|
||
|
|
public function duplicate(Notice $notice): \Inertia\Response
|
||
|
|
{
|
||
|
|
return $this->create($notice->replicate());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Restore the specified resource.
|
||
|
|
*
|
||
|
|
* @param integer $notice
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function restore(int $notice): \Illuminate\Http\RedirectResponse
|
||
|
|
{
|
||
|
|
$notice = Notice::withTrashed()->findOrFail($notice);
|
||
|
|
/** @var array<string,string> */
|
||
|
|
$previousQueries = [];
|
||
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
||
|
|
if ($notice->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.
|
||
|
|
*
|
||
|
|
* @param \App\Models\Notice $notice
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function destroy(Notice $notice): \Illuminate\Http\RedirectResponse
|
||
|
|
{
|
||
|
|
/** @var array<string,string> */
|
||
|
|
$previousQueries = [];
|
||
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
||
|
|
if ($notice->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.
|
||
|
|
*
|
||
|
|
* @param \App\Models\Notice|integer $notice
|
||
|
|
* @return \Illuminate\Http\RedirectResponse
|
||
|
|
*/
|
||
|
|
public function forceDestroy(Notice|int $notice): \Illuminate\Http\RedirectResponse
|
||
|
|
{
|
||
|
|
if (is_int($notice)) {
|
||
|
|
$notice = Notice::withTrashed()->findOrFail($notice);
|
||
|
|
}
|
||
|
|
/** @var array<string,string> */
|
||
|
|
$previousQueries = [];
|
||
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
||
|
|
if ($notice->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)),
|
||
|
|
]));
|
||
|
|
}
|
||
|
|
}
|