ldc-legaulois/app/Http/Controllers/Bo/CategoryApproachesController.php

189 lines
6.3 KiB
PHP
Raw Permalink Normal View History

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