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

183 lines
6.4 KiB
PHP
Raw Normal View History

2024-04-04 13:00:12 +02:00
<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
2024-04-12 12:15:18 +02:00
use App\Http\Requests\Bo\DemarcheSlides\StoreDemarcheSlideRequest;
use App\Http\Requests\Bo\DemarcheSlides\UpdateDemarcheSlideRequest;
use App\Models\DemarcheSlide;
2024-04-04 13:00:12 +02:00
use App\Traits\Controllers\ChangesModelOrder;
use App\Traits\Controllers\UpdateModelPublished;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
2024-04-12 12:15:18 +02:00
class DemarcheSlideController extends Controller
2024-04-04 13:00:12 +02:00
{
use ChangesModelOrder;
use UpdateModelPublished;
/**
* Create the controller instance.
*/
public function __construct()
{
2024-04-12 12:15:18 +02:00
$this->authorizeResource(DemarcheSlide::class);
2024-04-04 13:00:12 +02:00
}
/**
* 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
{
2024-04-12 12:15:18 +02:00
/** @var \Illuminate\Database\Eloquent\Builder $demarcheSlideModels */
$demarcheSlideModels = DemarcheSlide::query();
2024-04-04 13:00:12 +02:00
/** @var string $search Search field */
$search = $request->search;
if ($search) {
$this->searchQuery(
2024-04-12 12:15:18 +02:00
$demarcheSlideModels,
2024-04-04 13:00:12 +02:00
$search,
null,
'title',
'text',
);
}
$searchFields = \implode(', ', [
trans('validation.attributes.title'),
trans('validation.attributes.text'),
]);
/** Sort columns with a query */
2024-04-12 12:15:18 +02:00
$this->sortQuery($demarcheSlideModels);
2024-04-04 13:00:12 +02:00
/** Custom pagination */
2024-04-12 12:15:18 +02:00
$demarcheSlideModels = $this->paginate($demarcheSlideModels);
2024-04-04 13:00:12 +02:00
2024-04-12 12:15:18 +02:00
return view('bo.pages.demarche_slides.index', compact('demarcheSlideModels', 'search', 'searchFields'));
2024-04-04 13:00:12 +02:00
}
/**
* Show the specified resource.
*
2024-04-12 12:15:18 +02:00
* @param \App\Models\DemarcheSlide $demarcheSlide
2024-04-04 13:00:12 +02:00
* @return \Illuminate\Contracts\View\View
*/
2024-04-12 12:15:18 +02:00
public function show(DemarcheSlide $demarcheSlide): \Illuminate\Contracts\View\View
2024-04-04 13:00:12 +02:00
{
2024-04-12 12:15:18 +02:00
return view('bo.pages.demarche_slides.show', ['demarcheSlideModel' => $demarcheSlide]);
2024-04-04 13:00:12 +02:00
}
/**
* Show the form for creating a new resource.
*
2024-04-12 12:15:18 +02:00
* @param \App\Models\DemarcheSlide $demarcheSlide
2024-04-04 13:00:12 +02:00
* @return \Illuminate\Contracts\View\View
*/
2024-04-12 12:15:18 +02:00
public function create(DemarcheSlide $demarcheSlide): \Illuminate\Contracts\View\View
2024-04-04 13:00:12 +02:00
{
2024-04-12 12:15:18 +02:00
return view('bo.pages.demarche_slides.create', ['demarcheSlideModel' => $demarcheSlide]);
2024-04-04 13:00:12 +02:00
}
/**
* Store a newly created resource in storage.
*
2024-04-12 12:15:18 +02:00
* @param \App\Http\Requests\Bo\DemarcheSlides\StoreDemarcheSlideRequest $request
2024-04-04 13:00:12 +02:00
* @return \Illuminate\Http\RedirectResponse
*/
2024-04-12 12:15:18 +02:00
public function store(StoreDemarcheSlideRequest $request): \Illuminate\Http\RedirectResponse
2024-04-04 13:00:12 +02:00
{
return DB::transaction(function () use ($request) {
2024-04-12 12:15:18 +02:00
$demarcheSlide = new DemarcheSlide();
$demarcheSlide->fill($request->validated());
2024-04-04 13:00:12 +02:00
2024-04-12 12:15:18 +02:00
if ($demarcheSlide->saveOrFail()) {
return redirect()->route('bo.demarche_slides.edit', $demarcheSlide)
2024-04-04 13:00:12 +02:00
->with('success', trans('crud.messages.has_been_created', [
2024-04-12 12:15:18 +02:00
'model' => Str::ucfirst(trans('models.classes.demarche_slide'))
2024-04-04 13:00:12 +02:00
]));
}
return redirect()->back()
->with('error', trans('crud.messages.cannot_be_created', [
2024-04-12 12:15:18 +02:00
'model' => Str::ucfirst(trans('models.classes.demarche_slide'))
2024-04-04 13:00:12 +02:00
]));
});
}
/**
* Show the form for editing the specified resource.
*
2024-04-12 12:15:18 +02:00
* @param \App\Models\DemarcheSlide $demarcheSlide
2024-04-04 13:00:12 +02:00
* @return \Illuminate\Contracts\View\View
*/
2024-04-12 12:15:18 +02:00
public function edit(DemarcheSlide $demarcheSlide): \Illuminate\Contracts\View\View
2024-04-04 13:00:12 +02:00
{
2024-04-12 12:15:18 +02:00
return view('bo.pages.demarche_slides.edit', ['demarcheSlideModel' => $demarcheSlide]);
2024-04-04 13:00:12 +02:00
}
/**
* Update the specified resource in storage.
*
2024-04-12 12:15:18 +02:00
* @param \App\Http\Requests\Bo\DemarcheSlides\UpdateDemarcheSlideRequest $request
* @param \App\Models\DemarcheSlide $demarcheSlide
2024-04-04 13:00:12 +02:00
* @return \Illuminate\Http\RedirectResponse
*/
public function update(
2024-04-12 12:15:18 +02:00
UpdateDemarcheSlideRequest $request,
DemarcheSlide $demarcheSlide
2024-04-04 13:00:12 +02:00
): \Illuminate\Http\RedirectResponse {
2024-04-12 12:15:18 +02:00
return DB::transaction(function () use ($request, $demarcheSlide) {
$demarcheSlide->fill($request->validated());
2024-04-04 13:00:12 +02:00
2024-04-12 12:15:18 +02:00
if ($demarcheSlide->saveOrFail()) {
return redirect()->route('bo.demarche_slides.edit', $demarcheSlide)
2024-04-04 13:00:12 +02:00
->with('success', trans('crud.messages.has_been_updated', [
2024-04-12 12:15:18 +02:00
'model' => Str::ucfirst(trans('models.classes.demarche_slide'))
2024-04-04 13:00:12 +02:00
]));
}
2024-04-12 12:15:18 +02:00
return redirect()->route('bo.demarche_slides.edit', $demarcheSlide)
2024-04-04 13:00:12 +02:00
->with('error', trans('crud.messages.cannot_be_updated', [
2024-04-12 12:15:18 +02:00
'model' => Str::ucfirst(trans('models.classes.demarche_slide'))
2024-04-04 13:00:12 +02:00
]));
});
}
/**
* Remove the specified resource from storage.
*
2024-04-12 12:15:18 +02:00
* @param \App\Models\DemarcheSlide $demarcheSlide
2024-04-04 13:00:12 +02:00
* @return \Illuminate\Http\RedirectResponse
*/
2024-04-12 12:15:18 +02:00
public function destroy(DemarcheSlide $demarcheSlide): \Illuminate\Http\RedirectResponse
2024-04-04 13:00:12 +02:00
{
/** @var array<string,string> */
$previousQueries = [];
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
2024-04-12 12:15:18 +02:00
if ($demarcheSlide->deleteOrFail()) {
return redirect()->route('bo.demarche_slides.index', $previousQueries)
2024-04-04 13:00:12 +02:00
->with('success', trans('crud.messages.has_been_deleted', [
2024-04-12 12:15:18 +02:00
'model' => Str::ucfirst(trans('models.classes.demarche_slide'))
2024-04-04 13:00:12 +02:00
]));
}
return redirect()->back()
->with('error', trans('crud.messages.cannot_be_deleted', [
2024-04-12 12:15:18 +02:00
'model' => Str::ucfirst(trans('models.classes.demarche_slide'))
2024-04-04 13:00:12 +02:00
]));
}
/**
* Duplicate the specified resource.
*
2024-04-12 12:15:18 +02:00
* @param \App\Models\DemarcheSlide $demarcheSlide
2024-04-04 13:00:12 +02:00
* @return \Illuminate\Contracts\View\View
*/
2024-04-12 12:15:18 +02:00
public function duplicate(DemarcheSlide $demarcheSlide): \Illuminate\Contracts\View\View
2024-04-04 13:00:12 +02:00
{
2024-04-12 12:15:18 +02:00
return $this->create($demarcheSlide->replicate());
2024-04-04 13:00:12 +02:00
}
}