ldc-legaulois/app/Http/Requests/Bo/NewsClubs/StoreNewsClubRequest.php
2026-09-25 16:25:31 +02:00

100 lines
3.9 KiB
PHP

<?php
namespace App\Http\Requests\Bo\NewsClubs;
use App\Enums\News\NewsClubTypeEnum;
use App\Models\NewsClub;
use App\Rules\WysiwygLength;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\Rules\Enum;
class StoreNewsClubRequest extends FormRequest
{
use HasPicture;
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Gate::check('create', NewsClub::class);
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$this->merge([
'type' => intval($this->route()->parameter('news_club_type')),
'content' => ($this->type === NewsClubTypeEnum::news->value()) ? $this->input('content') : null,
'multiple_answers' => $this->boolean('multiple_answers'),
'published' => $this->boolean('published'),
]);
$this->mergePicture('picture');
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
$rules = [
'type' => ['required', new Enum(NewsClubTypeEnum::class)],
'title' => 'required|string|between:3,255',
'picture_alt' => 'required|string|between:3,255',
'picture_title' => 'required|string|between:3,255',
'content' => ['nullable', 'string', new WysiwygLength()],
'multiple_answers' => 'required|boolean',
'published' => [
'required',
'boolean',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function ($attribute, string $value, $fail) {
if (
($publishedModels = NewsClub::query()
->where('published', true)
->orderBy('publish_at', 'asc')
->get())->count() >= 3
&& ! $publishedModels->contains(request()->route('news_club'))
&& $this->published
) {
$publishedModels->first()->update(['published' => false]);
// phpcs:ignore Generic.Files.LineLength.TooLong
Session::flash('warning', \trans('Maximum 3 :model doivent être publiées en même temps, la plus ancienne est donc dépubliée.', [
'model' => trans_choice('models.classes.news_club', \INF),
]));
}
},
],
'publish_at' => 'sometimes|nullable|date',
'unpublish_at' => 'sometimes|nullable|date|after:publish_at',
];
return \array_merge(
$rules,
$this->pictureRules('picture', 400, 225, 400, 225),
);
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'type' => trans('validation.custom.type'),
'title' => trans('validation.attributes.title'),
'picture' => trans('validation.attributes.image'),
'picture_alt' => trans('validation.custom.attribute_alt'),
'picture_title' => trans('validation.custom.attribute_title'),
'multiple_answers' => trans('validation.custom.multiple_answers'),
'published' => trans('validation.attributes.published'),
'publish_at' => trans('validation.custom.publish_at'),
'unpublish_at' => trans('validation.custom.unpublish_at'),
];
}
}