ldc-legaulois/app/Http/Requests/Bo/GeneralSettings/UpdateGeneralSettingRequest.php

246 lines
9.7 KiB
PHP
Raw Permalink Normal View History

2024-04-09 12:31:45 +02:00
<?php
namespace App\Http\Requests\Bo\GeneralSettings;
use App\Enums\Settings\MenuNodeType;
use App\Enums\Settings\SettingsType;
2024-11-07 13:01:54 +01:00
use App\Lib\Link;
2024-05-15 12:40:31 +02:00
use App\Rules\WysiwygLength;
use App\Traits\Requests\HasPicture;
2024-04-09 12:31:45 +02:00
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\Rules\Enum;
2024-04-09 12:31:45 +02:00
class UpdateGeneralSettingRequest extends FormRequest
{
use HasPicture;
2024-04-09 12:31:45 +02:00
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Gate::check('update', $this->route('general_setting'));
}
/**
* Prepare the data for validation.
*/
public function prepareForValidation(): void
{
2024-04-19 10:08:36 +02:00
/** @var \App\Models\GeneralSetting $generalSettingModel */
$generalSettingModel = $this->route('general_setting');
switch ($generalSettingModel->type) {
case SettingsType::hotNews:
$this->prepareHotNewsDatas();
break;
case SettingsType::breederMap:
$this->prepareBreederMapDatas();
break;
case SettingsType::mainMenu:
$this->prepareMainMenuDatas();
break;
}
2024-04-09 12:31:45 +02:00
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
2024-04-19 10:08:36 +02:00
/** @var \App\Models\GeneralSetting $generalSettingModel */
$generalSettingModel = $this->route('general_setting');
$rules = [];
switch ($generalSettingModel->type) {
case SettingsType::hotNews:
$rules = $this->getRulesHotNews();
break;
case SettingsType::breederMap:
$rules = $this->getRulesBreederMap();
break;
case SettingsType::mainMenu:
$rules = $this->getRulesMainMenu();
break;
}
2026-09-25 16:25:31 +02:00
return $rules;
2024-04-09 12:31:45 +02:00
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'text' => trans('validation.attributes.text'),
'picture' => trans('validation.attributes.image'),
'picture_alt' => trans('validation.custom.attribute_alt'),
'picture_title' => trans('validation.custom.attribute_title'),
2024-05-15 12:40:31 +02:00
'tips_title' => trans('validation.attributes.title'),
'recipes_title' => trans('validation.attributes.title'),
2024-08-30 15:14:57 +02:00
'picto_text' => trans('validation.custom.breeder_map_component.picto_text'),
'main_text' => trans('validation.custom.breeder_map_component.main_text'),
'subtitle' => trans('validation.custom.subtitle'),
'value' => trans('validation.custom.value'),
'value.*.type' => trans(':field :internoeud', [
'field' => trans('validation.custom.type'),
'inter' => trans('validation.custom.of_the.male'),
]),
'value.*.published' => trans(':field :internoeud', [
'field' => trans('validation.custom.published'),
'inter' => trans('validation.custom.of_the.male'),
]),
'value.*.title' => trans(':field :internoeud', [
'field' => trans('validation.attributes.title'),
'inter' => trans('validation.custom.of_the.male'),
]),
'value.*.link.url' => trans(':field :internoeud', [
'field' => trans('validation.custom.url'),
'inter' => trans('validation.custom.of_the.male'),
]),
'value.*.link.routeName' => trans(':field :internoeud', [
'field' => trans('validation.custom.route_name'),
'inter' => trans('validation.custom.of_the.male'),
]),
2024-04-09 12:31:45 +02:00
];
}
2024-08-30 15:14:57 +02:00
/**
* Prepare validation for Hot News.
2024-08-30 15:14:57 +02:00
*/
private function prepareHotNewsDatas(): void
{
$this->merge([
'value->text' => $this->input('text'),
'value->picture' => $this->file('picture'),
'value->picture_alt' => $this->input('picture_alt'),
'value->picture_title' => $this->input('picture_title'),
]);
$this->mergePicture('picture');
2024-08-30 15:14:57 +02:00
}
/**
* Prepare validation for Breeder Map.
2024-08-30 15:14:57 +02:00
*/
private function prepareBreederMapDatas(): void
2024-08-30 15:14:57 +02:00
{
$this->merge([
'value->title' => $this->input('title'),
'value->main_text' => $this->input('main_text'),
'value->picto_text' => $this->input('picto_text'),
]);
}
/**
* Prepare validation for Breeder Map.
2024-08-30 15:14:57 +02:00
*/
private function prepareMainMenuDatas(): void
{
$this->merge([
'value' => collect(json_decode($this->input('jsonValue', '[]'), true))->map(function ($element) {
$element['published'] = (array_key_exists('published', $element)) ? $element['published'] : false;
2024-11-07 13:01:54 +01:00
if (MenuNodeType::make($element['type'])->equals(MenuNodeType::ourCommitments)) {
$element['link'] = (new Link(routeName: MenuNodeType::ourCommitments->routeName()))->toArray();
} elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::siteLogo)) {
$element['link'] = (new Link(routeName: MenuNodeType::siteLogo->routeName()))->toArray();
} elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::breedings)) {
$element['link'] = (new Link(routeName: MenuNodeType::breedings->routeName()))->toArray();
} elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::ourBrand)) {
$element['link'] = (new Link(routeName: MenuNodeType::ourBrand->routeName()))->toArray();
} elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::news)) {
$element['link'] = (new Link(routeName: MenuNodeType::news->routeName()))->toArray();
} elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::ourProducts)) {
$element['link'] = (new Link(routeName: MenuNodeType::ourProducts->routeName()))->toArray();
} elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::ourRecipes)) {
$element['link'] = (new Link(routeName: MenuNodeType::ourRecipes->routeName()))->toArray();
} elseif (MenuNodeType::make($element['type'])->equals(MenuNodeType::recruitment)) {
$element['link'] = (new Link(routeName: MenuNodeType::recruitment->routeName()))->toArray();
}
2026-09-25 16:25:31 +02:00
2024-08-30 15:14:57 +02:00
return $element;
2026-09-25 16:25:31 +02:00
})->toArray(),
2024-08-30 15:14:57 +02:00
]);
}
/**
* Get tha array of rules for the Main Menu.
*/
private function getRulesMainMenu(): array
{
/** @var array<array-key, mixed> $routesName Get all front routes name */
$routesName = collect(Route::getRoutes())->map(function ($route) {
if (preg_match('/fo.([a-zA-Z])+.([a-zA-Z])+/', $route->getName())) {
return $route->getName();
}
})->filter()->toArray();
/** @var int $count Count all node published */
$count = collect($this->value)->filter(function ($element) {
if ($element['published']) {
return $element;
}
})->count();
return [
'jsonValue' => 'required|json',
'value' => [
'present',
'array',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
2024-08-30 15:14:57 +02:00
function ($attribute, array $value, $fail) use ($count) {
if ($count > 8) {
$fail(\trans('Maximum 8 liens doivent être présents dans le menu'));
}
2026-09-25 16:25:31 +02:00
},
2024-08-30 15:14:57 +02:00
],
'value.*.type' => ['required', new Enum(MenuNodeType::class)],
'value.*.published' => 'required|boolean',
'value.*.title' => 'required|string|between:3,100',
'value.*.link.url' => 'nullable|string|url:https',
'value.*.link.routeName' => [
'nullable',
'string',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
2024-08-30 15:14:57 +02:00
function ($attribute, string $value, $fail) use ($routesName) {
2026-09-25 16:25:31 +02:00
if (! in_array($value, $routesName)) {
2024-08-30 15:14:57 +02:00
$fail(\trans('Doît être un lien front existant'));
}
2026-09-25 16:25:31 +02:00
},
2024-08-30 15:14:57 +02:00
],
];
}
/**
* Get tha array of rules for the Hot News.
*/
private function getRulesHotNews(): array
{
$rules = [
'text' => ['required', 'string', new WysiwygLength(3)],
'picture_alt' => 'required|string|between:3,255',
'picture_title' => 'required|string|between:3,255',
];
2026-09-25 16:25:31 +02:00
2024-08-30 15:14:57 +02:00
return \array_merge(
$rules,
$this->pictureRules('picture', 500, 281, 500, 281),
2024-08-30 15:14:57 +02:00
);
}
/**
* Get tha array of rules for the Breeder Map.
2024-08-30 15:14:57 +02:00
*/
private function getRulesBreederMap(): array
2024-08-30 15:14:57 +02:00
{
return [
'title' => 'required|string|between:3,255',
'main_text' => ['required', 'string', new WysiwygLength(3)],
'picto_text' => 'required|string|between:3,255',
];
}
2024-04-09 12:31:45 +02:00
}