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

286 lines
9.9 KiB
PHP
Raw Normal View History

2026-02-24 11:43:07 +01:00
<?php
namespace App\Http\Controllers\Bo;
use App\Enums\StaticPageType;
2026-02-24 11:43:07 +01:00
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\SectionApproaches\SectionApproachRequest;
2026-02-24 11:43:07 +01:00
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.
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
2026-02-27 17:35:59 +01:00
public function index(Request $request): \Illuminate\Contracts\View\View
2026-02-24 11:43:07 +01:00
{
2026-02-27 17:35:59 +01:00
$countSection = SectionApproach::count();
$sections = SectionApproach::query();
$categories = CategoryApproaches::query()->orderBy('name', 'ASC')->paginate(10);
2026-02-27 17:35:59 +01:00
$pages = StaticPage::whereIn('type', [StaticPageType::ouiCBon,StaticPageType::exitOutside])->get();
/** @var string $search Search field */
$search = $request->search;
if ($search) {
$this->searchQuery(
$sections,
$search,
null,
'title',
);
}
$searchFields = \implode(', ', [
trans_choice('models.classes.sectionApproach', 1),
2026-02-27 17:35:59 +01:00
]);
$this->sortQuery($sections)->paginate(5);
2026-02-24 11:43:07 +01:00
$sections = $this->paginate($sections);
2026-02-27 17:35:59 +01:00
return view('bo.pages.sections_approaches.index',
compact('sections', 'countSection', 'categories', 'pages', 'search','searchFields'));
2026-02-24 11:43:07 +01:00
}
2026-02-24 11:43:07 +01:00
/**
* 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)
{
$categoryApproachModel = CategoryApproaches::query()->orderBy('name', 'ASC')->paginate();
$staticPageModel = StaticPage::whereIn('type', [StaticPageType::ouiCBon,StaticPageType::exitOutside])->paginate();
return view('bo.pages.sections_approaches.create', [
'sectionApproachModel' => $section_approach,
'categoryApproachModel' => $categoryApproachModel,
'staticPageModel' => $staticPageModel
]);
2026-02-24 11:43:07 +01:00
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\Bo\SectionApproaches\SectionApproachRequest $request
2026-02-24 11:43:07 +01:00
* @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'];
$pageTitle = StaticPage::findOrFail($pageId)->title;
$categoryName = CategoryApproaches::findOrFail($categoryId)->name;
$limitReachedCat = "Limite atteinte : ".$categoryName." dans la page : ".$pageTitle." contient déjà ".SectionApproach::MAX_SECTIONS." sections.";
2026-02-24 11:43:07 +01:00
// 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' => $limitReachedCat
2026-02-24 11:43:07 +01:00
]);
}
$section = new SectionApproach();
$section->fill($validated);
$section->save();
return redirect()
->route('bo.sections.approaches.index')
2026-02-24 11:43:07 +01:00
->with('success', trans('crud.messages.has_been_created', [
'model' => Str::ucfirst(trans_choice('models.classes.sectionApproach', 1)),
2026-02-24 11:43:07 +01:00
]));
});
}
/**
* 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',
['sectionApproachModel' => $section_approach]);
2026-02-24 11:43:07 +01:00
}
/**
* 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.
*
* @param \App\Http\Requests\Bo\SectionApproaches\SectionApproachRequest $request
* @param \App\Models\SectionApproach $section_approach
* @return \Illuminate\Http\RedirectResponse
2026-02-24 11:43:07 +01:00
*/
public function update(SectionApproachRequest $request, SectionApproach $section_approach)
2026-02-24 11:43:07 +01:00
{
$validated = $request->validated();
return DB::transaction(function () use ($validated, $section_approach) {
$section = SectionApproach::query()
->whereKey($section_approach->id)
->lockForUpdate()
->firstOrFail();
$oldPageId = (int) $section->page_id;
$oldCategoryId = (int) $section->category_id;
$newPageId = (int) ($validated['page_id'] ?? $oldPageId);
$newCategoryId = (int) ($validated['category_id'] ?? $oldCategoryId);
$isMoving = ($oldPageId !== $newPageId) || ($oldCategoryId !== $newCategoryId);
// check if category has at least 2 section
if ($isMoving) {
$sourceCount = SectionApproach::query()
->where('page_id', $oldPageId)
->where('category_id', $oldCategoryId)
->lockForUpdate()
->count();
if ($sourceCount <= 2) {
return redirect()
->back()
->withInput()
->withErrors([
'category_id' => "Déplacement impossible : la catégorie d'origine doit garder au minimum ".SectionApproach::MIN_SECTIONS." sections."
]);
}
// check if not greater then 6
$targetCount = SectionApproach::query()
->where('page_id', $newPageId)
->where('category_id', $newCategoryId)
->lockForUpdate()
->count();
if ($targetCount >= 6) {
return redirect()
->back()
->withInput()
->withErrors([
'category_id' => "Déplacement impossible : la catégorie cible contient déjà ".SectionApproach::MAX_SECTIONS." sections pour cette page."
]);
}
}
$section->fill($validated);
$section->save();
return redirect()
->route('bo.sections.approaches.index')
->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.sectionApproach', 1)),
]));
});
2026-02-24 11:43:07 +01:00
}
/**
* 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 ".SectionApproach::MIN_SECTIONS." sections."
2026-02-24 11:43:07 +01:00
]);
}
$section->delete();
return redirect()
->route('bo.sections.approaches.index')
->with('success', trans('crud.messages.has_been_deleted', [
'model' => Str::ucfirst(trans_choice('models.classes.sectionApproach', 1)),
]));
2026-02-24 11:43:07 +01:00
});
}
/**
* Remove the specified resource from storage.
* @param SectionApproach $section_approach
* @return \Illuminate\Contracts\View\View
*/
public function duplicate(SectionApproach $section_approach)
{
return $this->create($section_approach->replicate());
}
/**
* Get a paginate resource.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function jsonPaginatePage(Request $request): \Illuminate\Http\JsonResponse
{
$staticPageModel = $request->targetModel::whereIn('type', [StaticPageType::ouiCBon,StaticPageType::exitOutside])->paginate();
return \response()->json($staticPageModel);
}
2026-02-24 11:43:07 +01:00
}