ldc-legaulois/app/Http/Controllers/Bo/SectionApproachController.php
2026-02-24 11:43:07 +01:00

174 lines
5.2 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\SectionApproachRequest;
use App\Models\CategoryApproaches;
use App\Models\SectionApproach;
use App\Models\StaticPage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class SectionApproachController extends Controller
{
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(SectionApproach::class, 'section_approach');
}
/**
* Display a listing of the resource.
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
{
$countSection = SectionApproach::count();
$sections = SectionApproach::query();
$categories = CategoryApproaches::all();
$pages = StaticPage::where('type', '>=', 23)->get();
$this->sortQuery($sections);
$sections = $sections->getModels();
return view('bo.pages.sections_approaches.index',
compact('sections', 'countSection', 'categories', 'pages'));
}
/**
* Show the form for creating a new resource.
*
* @param \App\Models\SectionApproach $section_approach
* @return \Illuminate\Contracts\View\View
*/
public function create(SectionApproach $section_approach)
{
return view('bo.pages.sections_approaches.create', ['sectionApproachModel' => $section_approach]);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\Bo\SectionApproachRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(SectionApproachRequest $request)
{
$validated = $request->validated();
return DB::transaction(function () use ($validated) {
$pageId = (int) $validated['page_id'];
$categoryId = (int) $validated['category_id'];
// number of section exist in page and category
$count = SectionApproach::query()
->where('page_id', $pageId)
->where('category_id', $categoryId)
->lockForUpdate()
->count();
// max limit
if ($count >= SectionApproach::MAX_SECTIONS) {
return redirect()
->back()
->withInput()
->withErrors([
'category_id' => "Limite atteinte : cette catégorie contient déjà ".SectionApproach::MAX_SECTIONS." rubriques pour cette page."
]);
}
$section = new SectionApproach();
$section->fill($validated);
$section->save();
return redirect()
->route('bo.sections.approaches.index')
->with('success', trans('crud.messages.has_been_created', [
'model' => Str::ucfirst(trans('section'))
]));
});
}
/**
* Show the specified resource.
*
* @param \App\Models\SectionApproach $section_approach
* @return \Illuminate\Contracts\View\View
*/
public function show(SectionApproach $section_approach)
{
return view('bo.pages.sections_approaches.show',
['sectionApproachesModel' => $section_approach]);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\SectionApproach $section_approach
* @return \Illuminate\Contracts\View\View
*/
public function edit(SectionApproach $section_approach)
{
return view('bo.pages.sections_approaches.edit',
['sectionApproachModel' => $section_approach]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, SectionApproach $section_approach)
{
//
}
/**
* Remove the specified resource from storage.
* @param SectionApproach $section_approach
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(SectionApproach $section_approach)
{
return DB::transaction(function () use ($section_approach) {
$section = SectionApproach::query()
->whereKey($section_approach->id)
->lockForUpdate()
->firstOrFail();
$pageId = (int) $section->page_id;
$categoryId = (int) $section->category_id;
$count = SectionApproach::query()
->where('page_id', $pageId)
->where('category_id', $categoryId)
->lockForUpdate()
->count();
// MIN 2
if ($count <= 2) {
return redirect()
->back()
->withErrors([
'delete' => "Suppression impossible : une catégorie doit contenir au minimum 2 rubriques pour cette page."
]);
}
$section->delete();
return redirect()
->route('bo.sections_approaches.index')
->with('success', "Rubrique supprimée avec succès.");
});
}
}