authorizeResource(SectionApproach::class, 'section_approach'); } /** * Display a listing of the resource. * @param \Illuminate\Http\Request $request * @return \Illuminate\Contracts\View\View */ public function index(Request $request): \Illuminate\Contracts\View\View { $countSection = SectionApproach::count(); $sections = SectionApproach::query(); $categories = CategoryApproaches::query()->orderBy('name', 'ASC')->paginate(10); $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), ]); $this->sortQuery($sections)->paginate(5); $sections = $this->paginate($sections); return view('bo.pages.sections_approaches.index', compact('sections', 'countSection', 'categories', 'pages', 'search','searchFields')); } /** * 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 ]); } /** * Store a newly created resource in storage. * * @param \App\Http\Requests\Bo\SectionApproaches\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']; $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."; // 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 ]); } $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_choice('models.classes.sectionApproach', 1)), ])); }); } /** * 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]); } /** * 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 */ public function update(SectionApproachRequest $request, SectionApproach $section_approach) { $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)), ])); }); } /** * 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." ]); } $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)), ])); }); } /** * 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); } }