311 lines
10 KiB
PHP
311 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Enums\StaticPageType;
|
|
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.
|
|
// * @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::all();
|
|
// $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('section'),
|
|
// ]);
|
|
// $this->sortQuery($sections);
|
|
|
|
// $sections = $sections->getModels();
|
|
|
|
// return view('bo.pages.sections_approaches.index',
|
|
// compact('sections', 'countSection', 'categories', 'pages', 'search','searchFields'));
|
|
// }
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index()
|
|
{
|
|
$pages = StaticPage::query()
|
|
->whereHas('sectionsApproaches')
|
|
->select(['id', 'title'])
|
|
->with([
|
|
'sectionsApproaches' => function ($q) {
|
|
$q->select(['id','title','content','picture','category_id','page_id'])
|
|
->with(['category:id,name'])
|
|
->orderBy('category_id')
|
|
->orderBy('id');
|
|
}
|
|
])
|
|
->orderBy('title')
|
|
->get();
|
|
|
|
// Page -> Catégories -> Sections
|
|
$tree = $pages->map(function ($page) {
|
|
$categories = $page->sectionsApproaches
|
|
->groupBy('category_id')
|
|
->map(function ($sections) {
|
|
return [
|
|
'category' => $sections->first()->category,
|
|
'sections' => $sections->values(),
|
|
];
|
|
})
|
|
->values();
|
|
|
|
return [
|
|
'page' => $page,
|
|
'categories' => $categories,
|
|
];
|
|
});
|
|
|
|
return view('bo.pages.sections_approaches.index-list', [
|
|
'tree' => $tree,
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* 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." section."
|
|
]);
|
|
}
|
|
|
|
$section = new SectionApproach();
|
|
$section->fill($validated);
|
|
$section->save();
|
|
|
|
return redirect()
|
|
->route('bo.sections.approaches.index', [
|
|
'page' => $section->page_id,
|
|
'cat' => $section->category_id,
|
|
])
|
|
->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',
|
|
['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.
|
|
*/
|
|
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', [
|
|
'page' => $section->page_id,
|
|
'cat' => $section->category_id,
|
|
])
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans('section')),
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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', [
|
|
'page' => $pageId,
|
|
'cat' => $categoryId,
|
|
])
|
|
->with('success', trans('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans('section')),
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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());
|
|
}
|
|
}
|