ldc-legaulois/app/Http/Controllers/Bo/CategoryApproachesController.php
2026-09-25 16:25:31 +02:00

188 lines
6.3 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\CategoryApproaches\CategoryApproachRequest;
use App\Models\CategoryApproach;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
class CategoryApproachesController extends Controller
{
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(CategoryApproach::class, 'categories_approach');
}
/**
* Display a listing of the resource.
*/
public function index(Request $request): View
{
$categories = CategoryApproach::query();
$count = CategoryApproach::count();
/** @var string $search Search field */
$search = $request->search;
if ($search) {
$this->searchQuery(
$categories,
$search,
null,
'name',
);
}
$searchFields = \implode(', ', [
trans('validation.attributes.name'),
]);
/** Sort columns with a query */
$this->sortQuery($categories);
$categories = $categories->getModels();
return view(
'bo.pages.categories_approaches.index',
compact('categories', 'count', 'searchFields', 'search')
);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function create(CategoryApproach $categoryApproaches)
{
$count = CategoryApproach::count();
if ($count >= CategoryApproach::MAX_CATEGORIES) {
return redirect()
->route('bo.categories.approaches.index')
->with('error', 'Limite atteinte : vous ne pouvez pas créer plus de ' . CategoryApproach::MAX_CATEGORIES . ' catégories.');
}
return view('bo.pages.categories_approaches.create', ['categoryApproachesModel' => $categoryApproaches]);
}
/**
* Store a newly created resource in storage.
*/
public function store(CategoryApproachRequest $request): RedirectResponse
{
if (CategoryApproach::count() >= CategoryApproach::MAX_CATEGORIES) {
throw ValidationException::withMessages([
'name' => 'Limite atteinte : maximum ' . CategoryApproach::MAX_CATEGORIES . ' catégories.',
]);
}
return DB::transaction(function () use ($request) {
$category = new CategoryApproach();
$category->fill($request->validated());
if ($category->saveOrFail()) {
return redirect()->route('bo.categories.approaches.index')
->with('success', trans('crud.messages.has_been_created', [
'model' => Str::ucfirst(trans('validation.custom.category')),
]));
}
});
return redirect()->back()
->with('error', trans('crud.messages.cannot_be_created', [
'model' => Str::ucfirst(trans('validation.custom.category')),
]));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(CategoryApproach $categories_approach): View
{
return view('bo.pages.categories_approaches.edit', [
'categoryApproachesModel' => $categories_approach,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function show(CategoryApproach $categories_approach): View
{
return view('bo.pages.categories_approaches.show', [
'categoryApproachesModel' => $categories_approach,
]);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Request\Bo\CategoryApproaches\CategoryApproachRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function update(CategoryApproachRequest $request, CategoryApproach $categories_approach)
{
if ($categories_approach->update($request->validated())) {
return redirect()
->route('bo.categories.approaches.index')
->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.categoryApproach', 1))]));
}
return redirect()->route('bo.categories.approaches.edit', $categories_approach)
->with('error', trans('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.categoryApproach', 1)),
]));
}
/**
* Remove the specified resource from storage.
*/
public function destroy(CategoryApproach $categories_approach): RedirectResponse
{
if (CategoryApproach::count() === CategoryApproach::MIN_CATEGORIES) {
return redirect()
->route('bo.categories.approaches.index')
->with('error', 'Limite atteinte : vous ne pouvez pas supprimer toutes les catégories (une catégorie minimum).');
}
if ($categories_approach->delete()) {
return redirect()
->route('bo.categories.approaches.index')
->with('success', trans('crud.messages.has_been_deleted', [
'model' => Str::ucfirst(trans_choice('models.classes.categoryApproach', 1))]));
}
return redirect()->back()
->with('success', trans('crud.messages.cannot_be_deleted', [
'model' => Str::ucfirst(trans_choice('models.classes.categoryApproach', 1))]));
}
/**
* Remove the specified resource from storage.
*
* @param CategoryApproach $section_approach
* @return \Illuminate\Contracts\View\View
*/
public function duplicate(CategoryApproach $categories_approach)
{
return $this->create($categories_approach->replicate());
}
/**
* Get a paginate resource.
*/
public function jsonPaginateCategory(Request $request): JsonResponse
{
$categoryApproachModel = $request->targetModel::query()->orderBy('name', 'ASC')->paginate();
return \response()->json($categoryApproachModel);
}
}