ldc-volena/app/Http/Controllers/Bo/SectionController.php

152 lines
4.9 KiB
PHP
Raw Normal View History

2023-11-17 16:07:28 +01:00
<?php
namespace App\Http\Controllers\Bo;
use App\Enums\StaticPageType;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\Sections\UpdateSectionRequest;
use App\Models\Section;
use App\Models\StaticPage;
2024-06-21 10:01:50 +02:00
use Illuminate\Http\RedirectResponse;
2023-11-17 16:07:28 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class SectionController extends Controller
{
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(Section::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
{
/** @var \Illuminate\Database\Eloquent\Builder<\App\Models\Section> $query */
2023-11-17 16:07:28 +01:00
$query = Section::query()->with('page');
// Filter by related page if requested.
if (
$request->related_page &&
null !== StaticPage::where('type', \collect(StaticPageType::all())
->where('name', $request->related_page)->first())->first()
) {
$query = $query->whereBelongsTo(
StaticPage::where(
'type',
\collect(StaticPageType::all())->where('name', $request->related_page)->first()
)->first(),
'page'
);
}
/** @var string $search Search field */
2025-07-21 15:27:01 +02:00
$search = $request->input('search');
2023-11-17 16:07:28 +01:00
if ($search) {
$this->searchQuery(
$query,
$search,
null,
'h2_title',
);
}
$searchFields = \implode(', ', [
Str::ucfirst(__('validation.custom.h2_title')),
]);
/** Sort columns with a query */
$this->sortQuery($query);
/** Custom pagination */
$sectionModelList = $this->paginate($query);
return view('bo.pages.sections.index', compact('sectionModelList', 'search', 'searchFields'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Section $section
2023-11-17 16:07:28 +01:00
* @return \Illuminate\Contracts\View\View
*/
public function edit(Section $section): \Illuminate\Contracts\View\View
{
return view('bo.pages.sections.edit', ['sectionModel' => $section]);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\Bo\Sections\UpdateSectionRequest $request
* @param \App\Models\Section $section
* @return \Illuminate\Http\RedirectResponse
*/
2024-06-21 10:01:50 +02:00
public function update(UpdateSectionRequest $request, Section $section): RedirectResponse
2023-11-17 16:07:28 +01:00
{
return DB::transaction(function () use ($request, $section) {
$encoded = json_encode($section->toArray());
if ($encoded === false) {
return redirect()->back()->with('error', trans('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(trans('models.classes.section'))
]));
}
/** @var array<string, mixed> $data */
$data = json_decode($encoded, true);
$section->fill(array_replace_recursive($data, $request->validated()));
2023-11-17 16:07:28 +01:00
if ($section->saveOrFail()) {
return redirect()->route('bo.sections.edit', $section)
2024-03-20 17:39:24 +01:00
->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans('models.classes.section'))
2023-11-17 16:07:28 +01:00
]));
}
return redirect()->route('bo.sections.edit', $section)
2024-03-20 17:39:24 +01:00
->with('error', trans('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(trans('models.classes.section'))
2023-11-17 16:07:28 +01:00
]));
});
}
2024-06-21 10:01:50 +02:00
/**
* Section add PartPiece
*
* @param \App\Models\Section $section
* @return \Illuminate\Http\RedirectResponse
*/
public function addPartPiece(Section $section): RedirectResponse
{
$section->addPartPiece();
$section->saveOrFail();
return response()->redirectToRoute('bo.sections.edit', $section)
->with('success', trans('Partie ajouté'));
}
/**
* Section remove PartPiece
*
* @param \App\Models\Section $section
* @param integer $index
* @return \Illuminate\Http\RedirectResponse
*/
public function removePartPiece(Section $section, int $index): RedirectResponse
{
$count = $section->removePartPiece($index);
$section->saveOrFail();
$response = response()->redirectToRoute('bo.sections.edit', $section);
if ($count) {
$response->with('error', trans('Partie enlevé'));
} else {
$response->with('info', trans('Aucune partie enlevé'));
}
return $response;
}
2023-11-17 16:07:28 +01:00
}