2024-04-09 12:31:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
|
|
|
|
|
|
use App\Enums\Settings\SettingsType;
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Http\Requests\Bo\GeneralSettings\UpdateGeneralSettingRequest;
|
2024-04-19 17:03:25 +02:00
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
2024-04-09 12:31:45 +02:00
|
|
|
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) {
|
2024-04-19 17:03:25 +02:00
|
|
|
if ($general_setting->type === SettingsType::hotNews) {
|
2024-05-22 12:02:14 +02:00
|
|
|
collect($request->only(['value->text', 'value->picture', 'value->picture_alt', 'value->picture_title']))
|
2024-05-21 14:37:24 +02:00
|
|
|
->each(fn (string|\Illuminate\Http\UploadedFile|null $value, string $attribute) =>
|
2024-04-19 17:03:25 +02:00
|
|
|
$general_setting->setAttribute(
|
|
|
|
|
$attribute,
|
|
|
|
|
$value instanceof \Illuminate\Http\UploadedFile ?
|
2024-04-25 16:58:41 +02:00
|
|
|
FileStorageHelper::storeFile($general_setting, $value, true) :
|
2024-05-21 14:37:24 +02:00
|
|
|
$value ?? json_decode(collect($general_setting->value))->picture
|
2024-04-25 16:58:41 +02:00
|
|
|
));
|
2024-06-10 17:13:34 +02:00
|
|
|
} elseif (
|
|
|
|
|
$general_setting->type === SettingsType::products
|
|
|
|
|
|| $general_setting->type === SettingsType::recipes
|
|
|
|
|
) {
|
2024-05-22 12:02:14 +02:00
|
|
|
collect($request->only(['value->subtitle', 'value->text']))
|
|
|
|
|
->each(fn (string $value, string $attribute) => $general_setting->setAttribute($attribute, $value));
|
2024-06-10 17:13:34 +02:00
|
|
|
} elseif ($general_setting->type === SettingsType::product) {
|
|
|
|
|
collect($request->only([
|
|
|
|
|
'value->claim_title',
|
|
|
|
|
'value->recipes_title', 'value->recipes_text',
|
|
|
|
|
'value->products_title', 'value->products_text'
|
|
|
|
|
]))->each(fn (string $value, string $attribute) => $general_setting->setAttribute($attribute, $value));
|
2024-05-15 12:40:31 +02:00
|
|
|
} elseif ($general_setting->type === SettingsType::recipe) {
|
|
|
|
|
collect($request->only(['value->tips_title', 'value->recipes_title', 'value->text']))
|
2024-05-22 12:02:14 +02:00
|
|
|
->each(fn (string $value, string $attribute) => $general_setting->setAttribute($attribute, $value));
|
2024-04-19 17:03:25 +02:00
|
|
|
} else {
|
|
|
|
|
$general_setting->fill($request->only('value'));
|
2024-05-15 12:40:31 +02:00
|
|
|
} //end if
|
2024-04-19 17:03:25 +02:00
|
|
|
|
2024-04-09 12:31:45 +02:00
|
|
|
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))
|
|
|
|
|
]));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|