ldc-legaulois/app/Http/Controllers/Bo/SectionApproachController.php
2026-03-31 11:49:27 +02:00

202 lines
6.8 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Enums\StaticPageType;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\SectionApproaches\SectionApproachRequest;
use App\Models\SectionApproach;
use App\Services\SectionApproach\SectionApproachService;
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
) {
$this->authorizeResource(SectionApproach::class);
}
/**
* 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
{
$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);
$sections = $this->paginate($sections);
return view(
'bo.pages.sections_approaches.index',
compact('sections', 'search', 'searchFields') + $formData
);
}
/**
* 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
{
$formData = $this->sectionApproachService->getFormData();
return view('bo.pages.sections_approaches.create', [
'sectionApproachModel' => $section_approach,
'categoryApproachModel' => $formData['categories'],
'staticPageModel' => $formData['pages'],
]);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\Bo\SectionApproaches\SectionApproachRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(SectionApproachRequest $request): \Illuminate\Http\RedirectResponse
{
return DB::transaction(function () use ($request) {
try {
$section = $this->sectionApproachService->store($request->validated());
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()]);
}
});
}
/**
* 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
{
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): \Illuminate\Contracts\View\View
{
$formData = $this->sectionApproachService->getFormData();
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): \Illuminate\Http\RedirectResponse
{
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()]);
}
});
}
/**
* Remove the specified resource from storage.
*
* @param SectionApproach $section_approach
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(SectionApproach $section_approach): \Illuminate\Http\RedirectResponse
{
return DB::transaction(function () use ($section_approach) {
try {
$this->sectionApproachService->destroy($section_approach);
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()]);
}
});
}
/**
* Duplicate the specified section approach by pre-filling the create form.
*
* @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());
}
/**
* 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);
}
}