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

202 lines
6.8 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\SectionApproach;
use App\Services\SectionApproach\SectionApproachService;
2026-02-24 11:43:07 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class SectionApproachController extends Controller
{
/**
* Create the controller instance.
*/
public function __construct(
private readonly SectionApproachService $sectionApproachService
) {
2026-03-31 11:49:27 +02:00
$this->authorizeResource(SectionApproach::class);
2026-02-24 11:43:07 +01:00
}
/**
* Display a listing of the resource.
2026-03-03 17:22:33 +01:00
*
* @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
{
$sections = SectionApproach::with(['category', 'page']);
$formData = $this->sectionApproachService->getFormData();
/** @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);
2026-02-24 11:43:07 +01:00
$sections = $this->paginate($sections);
return view(
'bo.pages.sections_approaches.index',
compact('sections', 'search', 'searchFields') + $formData
);
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): \Illuminate\Contracts\View\View
2026-02-24 11:43:07 +01:00
{
$formData = $this->sectionApproachService->getFormData();
return view('bo.pages.sections_approaches.create', [
'sectionApproachModel' => $section_approach,
'categoryApproachModel' => $formData['categories'],
'staticPageModel' => $formData['pages'],
]);
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): \Illuminate\Http\RedirectResponse
2026-02-24 11:43:07 +01:00
{
return DB::transaction(function () use ($request) {
try {
$section = $this->sectionApproachService->store($request->validated());
2026-02-24 11:43:07 +01:00
return redirect()
->route('bo.sections.approaches.edit', $section)
->with('success', trans('crud.messages.has_been_created', [
'model' => Str::ucfirst(trans_choice('models.classes.sectionApproach', 1)),
]));
} catch (\Exception $e) {
return redirect()->back()->withInput()->withErrors(['category_id' => $e->getMessage()]);
2026-02-24 11:43:07 +01:00
}
});
}
/**
2026-03-03 17:22:33 +01:00
* Show the specified resource.
*
* @param \App\Models\SectionApproach $section_approach
* @return \Illuminate\Contracts\View\View
*/
public function show(SectionApproach $section_approach): \Illuminate\Contracts\View\View
2026-02-24 11:43:07 +01:00
{
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): \Illuminate\Contracts\View\View
2026-02-24 11:43:07 +01:00
{
$formData = $this->sectionApproachService->getFormData();
return view(
'bo.pages.sections_approaches.edit',
['sectionApproachModel' => $section_approach]
);
2026-02-24 11:43:07 +01:00
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\Bo\SectionApproaches\SectionApproachRequest $request
2026-03-03 17:22:33 +01:00
* @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): \Illuminate\Http\RedirectResponse
2026-02-24 11:43:07 +01:00
{
return DB::transaction(function () use ($request, $section_approach) {
try {
$this->sectionApproachService->update($section_approach, $request->validated());
return redirect()
->route('bo.sections.approaches.edit', $section_approach)
->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.sectionApproach', 1)),
]));
} catch (\Exception $e) {
return redirect()->back()->withInput()->withErrors(['category_id' => $e->getMessage()]);
}
});
2026-02-24 11:43:07 +01:00
}
/**
2026-03-03 17:22:33 +01:00
* Remove the specified resource from storage.
*
* @param SectionApproach $section_approach
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(SectionApproach $section_approach): \Illuminate\Http\RedirectResponse
2026-02-24 11:43:07 +01:00
{
return DB::transaction(function () use ($section_approach) {
try {
$this->sectionApproachService->destroy($section_approach);
2026-02-24 11:43:07 +01:00
return redirect()
->route('bo.sections.approaches.index')
->with('success', trans('crud.messages.has_been_deleted', [
'model' => Str::ucfirst(trans_choice('models.classes.sectionApproach', 1)),
]));
} catch (\Exception $e) {
return redirect()->back()->withErrors(['delete' => $e->getMessage()]);
2026-02-24 11:43:07 +01:00
}
});
}
/**
* Duplicate the specified section approach by pre-filling the create form.
2026-03-03 17:22:33 +01:00
*
* @param SectionApproach $section_approach
* @return \Illuminate\Contracts\View\View
*/
public function duplicate(SectionApproach $section_approach): \Illuminate\Contracts\View\View
{
return $this->create($section_approach->replicate());
}
/**
2026-03-03 17:22:33 +01:00
* 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
}