ldc-com-app-bo/app/Http/Requests/Bo/Settings/UpdateSettingRequest.php

121 lines
3.5 KiB
PHP
Raw Normal View History

2023-04-17 17:46:20 +02:00
<?php
namespace App\Http\Requests\Bo\Settings;
2023-07-13 08:34:54 +00:00
use App\Models\Setting;
2023-04-17 17:46:20 +02:00
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
2023-04-17 17:46:20 +02:00
class UpdateSettingRequest extends FormRequest
{
use HasPicture;
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
2023-04-17 17:46:20 +02:00
{
if (
$this->key === "Button1" ||
$this->key === "Button2" ||
$this->key === "Button3" ||
$this->key === "ShowIsActive"
) {
return Gate::check('update', $this->route('setting'));
} else {
/** @var \App\Models\User $user */
$user = Auth::guard('backend')->user();
return $user->can('isAdmin');
}
2023-04-17 17:46:20 +02:00
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
2023-04-17 17:46:20 +02:00
{
if ($this->key === "ShowIsActive") {
2023-07-13 08:34:54 +00:00
$this->merge(['value' => intval($this->value) ? "true" : "false"]);
} elseif (
$this->key === "Button1" ||
$this->key === "Button2" ||
$this->key === "Button3"
) {
$this->merge(['value' => $this->value === "0" ? null : $this->value]);
} elseif (
2023-04-17 17:46:20 +02:00
$this->key === "SquaredLogo" ||
$this->key === "RectangleLogo" ||
$this->key === "BackgroundImage" ||
$this->key === "HomeButtonImage"
) {
$this->mergePicture('value');
}
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
2023-04-17 17:46:20 +02:00
{
if (
2023-07-13 08:34:54 +00:00
$this->key === "Button1" ||
$this->key === "Button2" ||
$this->key === "Button3"
) {
$buttonsNullable = 0;
$buttons = Setting::where('key', 'Button1')
->orWhere('key', 'Button2')
->orWhere('key', 'Button3')
->get();
2023-07-13 08:34:54 +00:00
foreach ($buttons as $button) {
if ($button->value === null) {
if ($button->key !== $this->key) {
$buttonsNullable++;
}
};
}
return [
'value' => [
//phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
2023-07-13 08:34:54 +00:00
function ($attribute, $value, $fail) use ($buttonsNullable) {
if (!isset($value) && $buttonsNullable >= 2) {
$fail(\trans('Au moins 1 bouton doît être défini.'));
}
}
]
];
} elseif (
2023-04-17 17:46:20 +02:00
$this->key === "SquaredLogo" ||
$this->key === "RectangleLogo" ||
$this->key === "BackgroundImage" ||
$this->key === "HomeButtonImage"
) {
return $this->pictureRules('value', 100, 100, 1920, 1200, true);
2023-04-17 17:46:20 +02:00
} else {
2023-07-13 08:34:54 +00:00
return ['value' => 'required|string|min:1'];
} //end if
2023-04-17 17:46:20 +02:00
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
2023-04-17 17:46:20 +02:00
{
return [
'key' => \trans('validation.custom.key'),
'value' => \trans('validation.custom.value'),
'type' => \trans('validation.custom.type'),
2023-04-17 17:46:20 +02:00
];
}
}