ldc-legaulois/app/Http/Controllers/Bo/CategoryApproachesController.php
2026-02-27 17:35:59 +01:00

159 lines
No EOL
5.6 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\CategoryApproachRequest;
use App\Models\CategoryApproaches;
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(CategoryApproaches::class, 'categories_approach');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
{
$categories = CategoryApproaches::query();
$count = CategoryApproaches::count();
/** Sort columns with a query */
$this->sortQuery($categories);
$categories = $categories->getModels();
return view('bo.pages.categories_approaches.index', compact('categories', 'count'));
}
/**
* Show the form for creating a new resource.
*
* @param \App\Models\CategoryApproaches $categoryApproaches
* @return \Illuminate\Contracts\View\View
*/
public function create(CategoryApproaches $categoryApproaches)
{
$count = CategoryApproaches::count();
if ($count >= CategoryApproaches::MAX_CATEGORIES) {
return redirect()
->route('bo.categories.approaches.index')
->with('error', "Limite atteinte : vous ne pouvez pas créer plus de ".CategoryApproaches::MAX_CATEGORIES." catégories.");
}
return view('bo.pages.categories_approaches.create', ['categoryApproachesModel' =>$categoryApproaches]);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\Bo\CategoryApproachRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(CategoryApproachRequest $request): \Illuminate\Http\RedirectResponse
{
if (CategoryApproaches::count() >= CategoryApproaches::MAX_CATEGORIES) {
throw ValidationException::withMessages([
'name' => "Limite atteinte : maximum ".CategoryApproaches::MAX_CATEGORIES." catégories."
]);
}
return DB::transaction(function () use ($request) {
$category = new CategoryApproaches();
$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.custom'))
]));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\CategoryApproaches $categories_approach
* @return \Illuminate\Contracts\View\View
*/
public function edit(CategoryApproaches $categories_approach): \Illuminate\Contracts\View\View
{
return view('bo.pages.categories_approaches.edit', [
'categoryApproachesModel' => $categories_approach
]);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Request\Bo\CategoryApproachRequest $request
* @param \App\Models\CategoryApproaches $categories_approach
* @return \Illuminate\Http\RedirectResponse
*/
public function update(CategoryApproachRequest $request, CategoryApproaches $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('catégorie')] ));
}
return redirect()->route('bo.categories.approaches.edit', $categories_approach)
->with('error', trans('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst('catégorie')
]));
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\CategoryApproaches $categories_approach
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(CategoryApproaches $categories_approach): \Illuminate\Http\RedirectResponse
{
if (CategoryApproaches::count() === CategoryApproaches::MIN_CATEGORIES) {
return redirect()
->route('bo.categories.approaches.index')
->with('error', "Limite atteinte : vous ne pouvez pas supprimer tous 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('catégorie')]));
}
return redirect()->back()
->with('success', trans('crud.messages.cannot_be_deleted', [
'model' => Str::ucfirst('catégorie')]));
}
/**
* Remove the specified resource from storage.
* @param CategoryApproaches $section_approach
* @return \Illuminate\Contracts\View\View
*/
public function duplicate(CategoryApproaches $categories_approach)
{
return $this->create($categories_approach->replicate());
}
}