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\Enums\UserRolesEnum;
|
|
|
|
|
use App\Models\Setting;
|
2023-04-17 17:46:20 +02:00
|
|
|
use App\Traits\Requests\HasPicture;
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2023-04-18 12:43:39 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
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()
|
|
|
|
|
{
|
2023-04-18 12:43:39 +02:00
|
|
|
if (
|
|
|
|
|
$this->key === "Button1" ||
|
|
|
|
|
$this->key === "Button2" ||
|
|
|
|
|
$this->key === "Button3" ||
|
|
|
|
|
$this->key === "ShowIsActive"
|
|
|
|
|
) {
|
|
|
|
|
return auth('backend')->check();
|
|
|
|
|
} else {
|
2023-07-13 08:34:54 +00:00
|
|
|
return (auth('backend')->check() && Auth::user()->role === UserRolesEnum::admin()->value);
|
2023-04-18 12:43:39 +02:00
|
|
|
}
|
2023-04-17 17:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function prepareForValidation()
|
|
|
|
|
{
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
foreach ($buttons as $button) {
|
|
|
|
|
if ($button->value === null) {
|
|
|
|
|
if ($button->key !== $this->key) {
|
|
|
|
|
$buttonsNullable++;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'value' => [
|
|
|
|
|
//phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
|
|
|
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"
|
|
|
|
|
) {
|
2023-07-13 08:34:54 +00:00
|
|
|
return $this->pictureRules('value', true, 100, 100, 1920, 1200);
|
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()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'key' => \trans('clé'),
|
|
|
|
|
'value' => \trans('valeur'),
|
2023-07-13 08:34:54 +00:00
|
|
|
'type' => \trans('type'),
|
2023-04-17 17:46:20 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|