152 lines
5.5 KiB
PHP
152 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Requests\Bo\BrigadeSections\StoreBrigadeSectionsRequest;
|
|
use App\Http\Requests\Bo\BrigadeSections\UpdateBrigadeSectionsRequest;
|
|
use App\Models\BrigadeSection;
|
|
use App\Traits\Controllers\ChangesModelOrder;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Uwil\Laravel\Common\Controllers\Controller;
|
|
|
|
class BrigadeSectionController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
use ChangesModelOrder;
|
|
|
|
/**
|
|
* 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
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder $brigadeSectionModels */
|
|
$brigadeSectionModels = BrigadeSection::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$brigadeSectionModels,
|
|
$search,
|
|
null,
|
|
'title',
|
|
'content',
|
|
'button_text',
|
|
'button_url'
|
|
);
|
|
}
|
|
$searchFields = trans('Titre, Texte, CTA, Lien');
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($brigadeSectionModels);
|
|
|
|
/** Custom pagination */
|
|
$brigadeSectionModels = $this->paginate($brigadeSectionModels);
|
|
|
|
return view('bo.pages.brigade_sections.index', compact('brigadeSectionModels', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\BrigadeSection $brigadeSection
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(BrigadeSection $brigadeSection): \Illuminate\Contracts\View\View
|
|
{
|
|
$brigadeSection = $brigadeSection->exists() ? $brigadeSection : new BrigadeSection();
|
|
|
|
return view('bo.pages.brigade_sections.create', [
|
|
'brigadeSectionModel' => $brigadeSection,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\BrigadeSections\StoreBrigadeSectionsRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreBrigadeSectionsRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$brigadeSection = new BrigadeSection();
|
|
$brigadeSection->fill($request->validated());
|
|
|
|
if ($brigadeSection->saveOrFail()) {
|
|
return redirect()->route('bo.brigade_sections.edit', $brigadeSection->id)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => trans_choice('models.classes.brigade_section', 1)
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => trans_choice('models.classes.brigade_section', 1)
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\BrigadeSection $brigadeSection
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(BrigadeSection $brigadeSection): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.brigade_sections.edit', [
|
|
'brigadeSectionModel' => $brigadeSection,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\BrigadeSections\UpdateBrigadeSectionsRequest $request
|
|
* @param \App\Models\BrigadeSection $brigadeSection
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateBrigadeSectionsRequest $request, BrigadeSection $brigadeSection): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $brigadeSection) {
|
|
$brigadeSection->fill($request->validated());
|
|
|
|
if ($brigadeSection->saveOrFail()) {
|
|
return redirect()->route('bo.brigade_sections.edit', $brigadeSection->id)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => trans_choice('models.classes.brigade_section', 1)
|
|
]));
|
|
}
|
|
return redirect()->route('bo.brigade_sections.edit', $brigadeSection->id)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => trans_choice('models.classes.brigade_section', 1)
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\BrigadeSection $brigadeSection
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(BrigadeSection $brigadeSection): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if ($brigadeSection->deleteOrFail()) {
|
|
return redirect()->route('bo.brigade_sections.index')
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => trans_choice('models.classes.brigade_section', 1)
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => trans_choice('models.classes.brigade_section', 1)
|
|
]));
|
|
}
|
|
}
|