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

71 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Enums\Settings\SettingsType;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\GeneralSettings\UpdateGeneralSettingRequest;
use App\Lib\Helpers\FileStorageHelper;
use App\Models\GeneralSetting;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class GeneralSettingController extends Controller
{
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(GeneralSetting::class);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\GeneralSetting $general_setting
* @return \Illuminate\Contracts\View\View
*/
public function edit(GeneralSetting $general_setting): \Illuminate\Contracts\View\View
{
return view('bo.pages.general_settings.edit', ['generalSettingModel' => $general_setting]);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\Bo\GeneralSettings\UpdateGeneralSettingRequest $request
* @param \App\Models\GeneralSetting $general_setting
* @return \Illuminate\Http\RedirectResponse
*/
public function update(
UpdateGeneralSettingRequest $request,
GeneralSetting $general_setting
): \Illuminate\Http\RedirectResponse {
return DB::transaction(function () use ($request, $general_setting) {
if ($general_setting->type === SettingsType::hotNews) {
collect($request->only(['value->text', 'value->picture', 'value->picture_alt', 'value->picture_title']))
->each(fn (string|\Illuminate\Http\UploadedFile|null $value, string $attribute) =>
$general_setting->setAttribute(
$attribute,
$value instanceof \Illuminate\Http\UploadedFile ?
FileStorageHelper::storeFile($general_setting, $value, true) :
$value ?? json_decode(collect($general_setting->value))->picture
));
} else {
$general_setting->fill($request->only('value'));
} //end if
if ($general_setting->saveOrFail()) {
return redirect()->route('bo.general_settings.edit', $general_setting)
->with('success', __('crud.messages.has_been_updated', [
'model' => Str::ucfirst(\trans_choice('models.classes.general_setting', 1))
]));
}
return redirect()->route('bo.general_settings.edit', $general_setting)
->with('error', __('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(\trans_choice('models.classes.general_setting', 1))
]));
});
}
}