2025-12-01 15:55:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
|
|
2026-02-12 17:14:08 +01:00
|
|
|
use App\Http\Requests\Back\DocumentCategories\StoreDocumentCategoryRequest;
|
|
|
|
|
use App\Http\Requests\Back\DocumentCategories\UpdateDocumentCategoryRequest;
|
2025-12-09 11:29:10 +01:00
|
|
|
use App\Models\DocumentCategory;
|
2025-12-01 15:55:32 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Inertia\Inertia;
|
|
|
|
|
use Uwil\LaravelBackend\Resources\CommonResource;
|
|
|
|
|
use Uwil\LaravelCommon\Controllers\Controller;
|
2026-01-21 12:31:07 +01:00
|
|
|
use Uwil\LaravelCommon\Enums\ItemsPerPaginationEnum;
|
2025-12-01 15:55:32 +01:00
|
|
|
|
2025-12-09 11:29:10 +01:00
|
|
|
class DocumentCategoryController extends Controller
|
2025-12-01 15:55:32 +01:00
|
|
|
{
|
2026-02-10 15:43:47 +01:00
|
|
|
/** @var string $modelName Name of the model in kebab case. */
|
2025-12-09 11:29:10 +01:00
|
|
|
private $modelName = "document-categories";
|
2025-12-01 15:55:32 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create the controller instance.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
2025-12-09 11:29:10 +01:00
|
|
|
$this->authorizeResource(DocumentCategory::class);
|
2025-12-01 15:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Inertia\Response
|
|
|
|
|
*/
|
|
|
|
|
public function index(Request $request): \Inertia\Response
|
|
|
|
|
{
|
2025-12-09 11:29:10 +01:00
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\DocumentCategory> $query */
|
|
|
|
|
$query = DocumentCategory::query()->withCount('documents');
|
2025-12-01 15:55:32 +01:00
|
|
|
|
2026-01-21 12:31:07 +01:00
|
|
|
if ($request->routeIs("back.$this->modelName.trash")) {
|
|
|
|
|
$query = $query->onlyTrashed();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 15:55:32 +01:00
|
|
|
/** @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 */
|
|
|
|
|
$docModels = $this->paginate($query);
|
2025-12-09 11:29:10 +01:00
|
|
|
return Inertia::render('DocumentCategory/Index', [
|
2025-12-18 19:11:36 +01:00
|
|
|
'documentCategoryModel' => CommonResource::collection($docModels),
|
|
|
|
|
'searchFields' => $searchFields,
|
|
|
|
|
'modelName' => $this->modelName,
|
2025-12-01 15:55:32 +01:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
2025-12-09 11:29:10 +01:00
|
|
|
* @param \App\Models\DocumentCategory $document_category
|
2025-12-01 15:55:32 +01:00
|
|
|
* @return \Inertia\Response
|
|
|
|
|
*/
|
2025-12-09 11:29:10 +01:00
|
|
|
public function create(DocumentCategory $document_category): \Inertia\Response
|
2025-12-01 15:55:32 +01:00
|
|
|
{
|
2025-12-09 11:29:10 +01:00
|
|
|
return Inertia::render('DocumentCategory/Create', [
|
2025-12-18 19:11:36 +01:00
|
|
|
'data' => CommonResource::make($document_category),
|
|
|
|
|
'modelName' => $this->modelName,
|
2025-12-01 15:55:32 +01:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show a specific resource.
|
|
|
|
|
*
|
2025-12-09 11:29:10 +01:00
|
|
|
* @param \App\Models\DocumentCategory $document_category
|
2025-12-01 15:55:32 +01:00
|
|
|
* @return \Inertia\Response
|
|
|
|
|
*/
|
2025-12-09 11:29:10 +01:00
|
|
|
public function show(DocumentCategory $document_category): \Inertia\Response
|
2025-12-01 15:55:32 +01:00
|
|
|
{
|
2026-01-21 12:31:07 +01:00
|
|
|
$documents = $document_category->documents()->paginate(ItemsPerPaginationEnum::five->value());
|
2025-12-09 11:29:10 +01:00
|
|
|
return Inertia::render('DocumentCategory/Show', [
|
2025-12-18 19:11:36 +01:00
|
|
|
'data' => CommonResource::make($document_category),
|
2025-12-09 11:29:10 +01:00
|
|
|
'documents' => CommonResource::collection($documents),
|
2025-12-18 19:11:36 +01:00
|
|
|
'modelName' => $this->modelName,
|
2025-12-01 15:55:32 +01:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created resource.
|
|
|
|
|
*
|
2026-02-12 17:14:08 +01:00
|
|
|
* @param \App\Http\Requests\Back\DocumentCategories\StoreDocumentCategoryRequest $request
|
2025-12-01 15:55:32 +01:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
2025-12-09 11:29:10 +01:00
|
|
|
public function store(StoreDocumentCategoryRequest $request): \Illuminate\Http\RedirectResponse
|
2025-12-01 15:55:32 +01:00
|
|
|
{
|
|
|
|
|
return DB::transaction(function () use ($request) {
|
2025-12-18 19:11:36 +01:00
|
|
|
$documentCategoryModel = new DocumentCategory();
|
|
|
|
|
$documentCategoryModel->fill($request->validated());
|
|
|
|
|
if ($documentCategoryModel->saveOrFail()) {
|
|
|
|
|
return redirect()->route("back.$this->modelName.edit", $documentCategoryModel);
|
2025-12-01 15:55:32 +01:00
|
|
|
}
|
2025-12-18 19:11:36 +01:00
|
|
|
return redirect()->route("back.$this->modelName.create");
|
2025-12-01 15:55:32 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
|
*
|
2025-12-09 11:29:10 +01:00
|
|
|
* @param \App\Models\DocumentCategory $document_category
|
2025-12-01 15:55:32 +01:00
|
|
|
* @return \Inertia\Response
|
|
|
|
|
*/
|
2025-12-09 11:29:10 +01:00
|
|
|
public function edit(DocumentCategory $document_category): \Inertia\Response
|
2025-12-01 15:55:32 +01:00
|
|
|
{
|
2025-12-09 11:29:10 +01:00
|
|
|
return Inertia::render('DocumentCategory/Edit', [
|
|
|
|
|
'data' => CommonResource::make($document_category),
|
2025-12-18 19:11:36 +01:00
|
|
|
'modelName' => $this->modelName,
|
2025-12-01 15:55:32 +01:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource.
|
|
|
|
|
*
|
2026-02-12 17:14:08 +01:00
|
|
|
* @param \App\Http\Requests\Back\DocumentCategories\UpdateDocumentCategoryRequest $request
|
|
|
|
|
* @param \App\Models\DocumentCategory $document_category
|
2025-12-01 15:55:32 +01:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
2026-02-10 15:43:47 +01:00
|
|
|
public function update(
|
|
|
|
|
UpdateDocumentCategoryRequest $request,
|
|
|
|
|
DocumentCategory $document_category,
|
|
|
|
|
): \Illuminate\Http\RedirectResponse {
|
2025-12-09 11:29:10 +01:00
|
|
|
return DB::transaction(function () use ($request, $document_category) {
|
|
|
|
|
$document_category->fill($request->validated());
|
|
|
|
|
if ($document_category->saveOrFail()) {
|
2025-12-18 19:11:36 +01:00
|
|
|
return redirect()->route("back.$this->modelName.edit", $document_category);
|
2025-12-01 15:55:32 +01:00
|
|
|
}
|
2025-12-18 19:11:36 +01:00
|
|
|
return redirect()->route("back.$this->modelName.edit", $document_category);
|
2025-12-01 15:55:32 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 12:31:07 +01:00
|
|
|
/**
|
|
|
|
|
* Restore the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $document_category
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function restore(int $document_category): \Illuminate\Http\RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$document_category = DocumentCategory::withTrashed()->findOrFail($document_category);
|
|
|
|
|
/** @var array<string,string> */
|
|
|
|
|
$previousQueries = [];
|
|
|
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
|
|
|
if ($document_category->restore()) {
|
|
|
|
|
return redirect()->route("back.$this->modelName.trash", $previousQueries);
|
|
|
|
|
}
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2025-12-01 15:55:32 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroy the specified resource.
|
|
|
|
|
*
|
2025-12-09 11:29:10 +01:00
|
|
|
* @param \App\Models\DocumentCategory $document_category
|
2025-12-01 15:55:32 +01:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
2025-12-09 11:29:10 +01:00
|
|
|
public function destroy(DocumentCategory $document_category): \Illuminate\Http\RedirectResponse
|
2025-12-01 15:55:32 +01:00
|
|
|
{
|
|
|
|
|
/** @var array<string,string> */
|
|
|
|
|
$previousQueries = [];
|
|
|
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
2025-12-09 11:29:10 +01:00
|
|
|
if ($document_category->delete()) {
|
2025-12-18 19:11:36 +01:00
|
|
|
return redirect()->route("back.$this->modelName.index", $previousQueries);
|
2025-12-01 15:55:32 +01:00
|
|
|
}
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2026-01-21 12:31:07 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroy the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\DocumentCategory|integer $document_category
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function forceDestroy(DocumentCategory|int $document_category): \Illuminate\Http\RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
if (is_int($document_category)) {
|
|
|
|
|
$document_category = DocumentCategory::withTrashed()->findOrFail($document_category);
|
|
|
|
|
}
|
|
|
|
|
/** @var array<string,string> */
|
|
|
|
|
$previousQueries = [];
|
|
|
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
|
|
|
if ($document_category->forceDelete()) {
|
|
|
|
|
return redirect()->route("back.$this->modelName.trash", $previousQueries);
|
|
|
|
|
}
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2025-12-01 15:55:32 +01:00
|
|
|
}
|