ldc-legaulois/app/Http/Controllers/Bo/StaticPageController.php
2026-08-10 16:07:46 +02:00

196 lines
6.7 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Enums\StaticPageType;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\StaticPages\UpdateCommonSectionRequest;
use App\Http\Requests\Bo\StaticPages\UpdateStaticPageRequest;
use App\Models\StaticPage;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Throwable;
class StaticPageController extends Controller
{
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(StaticPage::class);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\StaticPage $static_page
* @return \Illuminate\Contracts\View\View
*/
public function edit(StaticPage $static_page): \Illuminate\Contracts\View\View
{
$hasSections = in_array(
$static_page->type,
[...StaticPageType::getCommitmentsPages(), StaticPageType::home],
);
if ($hasSections) {
$static_page->load(['sections.blocks', 'sections.pictos']);
}
return view('bo.pages.static_pages.edit', [
'staticPageModel' => $static_page,
'hasSections' => $hasSections,
]);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\Bo\StaticPages\UpdateStaticPageRequest $request
* @param \App\Models\StaticPage $static_page
* @return \Illuminate\Http\RedirectResponse
*/
public function update(UpdateStaticPageRequest $request, StaticPage $static_page): \Illuminate\Http\RedirectResponse
{
return DB::transaction(function () use ($request, $static_page) {
$validated = $request->validated();
$data = $static_page->data;
foreach ($validated['data'] as $key => $input) {
if (isset($input['value']) && is_array($input['value'])) {
$data[$key]['value'] = $input['value'];
}
}
$static_page->data = $data;
$static_page->fill(array_replace_recursive(
$static_page->getFillableFields($static_page),
$validated
));
if ($static_page->saveOrFail()) {
return redirect()->route('bo.static_pages.edit', $static_page)
->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(\trans_choice('models.classes.static_page', 1))
]));
}
return redirect()->route('bo.static_pages.edit', $static_page)
->with('error', trans('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(\trans_choice('models.classes.static_page', 1))
]));
});
}
/**
* Display the common section edit form for all breeding static pages.
*
* @return \Illuminate\Contracts\View\View
*/
public function getCommonSection(): \Illuminate\Contracts\View\View
{
$commonSection = StaticPage::whereIn('type', StaticPageType::getBreedingValues())
->get()
->map(fn($page) => $page->data['common_section'] ?? null)
->filter()
->first();
return view('bo.pages.static_pages.common_section.edit', [
'commonSection' => $commonSection,
]);
}
/**
* Update the common section for all breeding static pages.
*
* @param \App\Http\Requests\Bo\StaticPages\UpdateCommonSectionRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function updateCommonSection(UpdateCommonSectionRequest $request): RedirectResponse
{
$pages = StaticPage::whereIn('type', StaticPageType::getBreedingValues())->get();
try {
DB::transaction(function () use ($pages, $request) {
foreach ($pages as $page) {
$data = $page->data ?? [];
$data['common_section'] = [
'title' => $request->validated('title'),
'content' => $request->validated('content'),
];
$page->data = $data;
$page->saveOrFail();
}
});
} catch (Throwable $e) {
return redirect()
->back()
->withInput()
->with('error', __('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(__('validation.custom.common_section_part'))]));
}//end try
return redirect()
->route('bo.static_page.sections.edit')
->with('success', __('crud.messages.has_been_updated', [
'model' => Str::ucfirst(__('validation.custom.common_section_part'))
]));
}
public function updateCommitmentsCommonSection(UpdateCommonSectionRequest $request): RedirectResponse
{
$pages = StaticPage::whereIn('type', StaticPageType::getCommitmentsPages())->get();
try {
DB::transaction(function () use ($pages, $request) {
foreach ($pages as $page) {
$data = $page->data ?? [];
$data['common_section'] = [
'title' => $request->validated('title'),
'content' => $request->validated('content'),
];
$page->data = $data;
$page->saveOrFail();
}
});
} catch (Throwable $e) {
return redirect()
->back()
->withInput()
->with('error', __('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(__('validation.custom.common_section_part'))
]));
} //end try
return redirect()
->route('bo.static_page.commitmments.sections.edit')
->with('success', __('crud.messages.has_been_updated', [
'model' => Str::ucfirst(__('validation.custom.common_section_part'))
]));
}
/**
* Display the common section edit form for all breeding static pages.
*
* @return \Illuminate\Contracts\View\View
*/
public function getCommitmentsCommonSection(): \Illuminate\Contracts\View\View
{
$commonSection = StaticPage::whereIn('type', StaticPageType::getCommitmentsPages())
->get()
->map(fn($page) => $page->data['common_section'] ?? null)
->filter()
->first();
return view('bo.pages.static_pages.common_section.commitments.edit', [
'commonSection' => $commonSection,
]);
}
}