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