130 lines
4.5 KiB
PHP
130 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo;
|
|
|
|
use App\Enums\Tickets\TicketStatusEnum;
|
|
use App\Enums\Tickets\TicketTypeEnum;
|
|
use App\Models\Ticket;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class StoreTicket extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize()
|
|
{
|
|
return auth()->check();
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function prepareForValidation()
|
|
{
|
|
/** @var \App\Models\User|null */
|
|
$user = auth()->user();
|
|
if ($user) {
|
|
if (!$user->isAdmin()) {
|
|
$this->merge([
|
|
'status' => TicketStatusEnum::new->value,
|
|
'is_internal' => false,
|
|
]);
|
|
} else {
|
|
$this->merge([
|
|
'is_internal' => $this->is_internal ? true : false,
|
|
]);
|
|
}
|
|
}
|
|
$this->merge([
|
|
'labels' => $this->labels ?? []
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
return [
|
|
'site_id' => [
|
|
'required',
|
|
'exists:sites,id',
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
function ($attribute, $value, $fail) {
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
if ($user->isAdmin()) {
|
|
return;
|
|
}
|
|
if (!in_array($value, $user->sites()->pluck('id')->all())) {
|
|
$fail(__('Veuillez choisir entre :values', [
|
|
'values' => $user->sites()->pluck('name')->implode(',')
|
|
]));
|
|
}
|
|
}
|
|
],
|
|
'type' => [
|
|
'required',
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
function ($attribute, $value, $fail) {
|
|
if (!in_array($value, TicketTypeEnum::toValues())) {
|
|
$fail(__('Veuillez choisir entre :values', [
|
|
'value' => implode(',', TicketTypeEnum::toLabels())
|
|
]));
|
|
}
|
|
}
|
|
],
|
|
'status' => [
|
|
'required',
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
function ($attribute, $value, $fail) {
|
|
if (!in_array($value, TicketStatusEnum::toValues())) {
|
|
$fail(__('Veuillez choisir entre :values', [
|
|
'value' => implode(',', TicketStatusEnum::toLabels())
|
|
]));
|
|
}
|
|
}
|
|
],
|
|
'date_mep' => 'nullable|date',
|
|
'text' => [
|
|
'required',
|
|
'string',
|
|
'min:40',
|
|
function ($attribute, $value, $fail) {
|
|
$forbidden = ['merci de', 'merci de'];
|
|
foreach ($forbidden as $word) {
|
|
if (stripos(strtolower($value), $word) !== false) {
|
|
$fail(
|
|
"Le champs 'Description du ticket' contient la chaine de caractère non autorisée suivante : $word"
|
|
);
|
|
}
|
|
}
|
|
},
|
|
],
|
|
'title' => 'required|string|between:3,255',
|
|
'file_tickets' => 'sometimes|array',
|
|
'file_tickets.*' => 'mimes:pptx,docx,xlsx,csv,txt,jpeg,jpg,png,pdf,svg|max:2048',
|
|
'is_internal' => 'required|boolean',
|
|
'labels' => 'sometimes|array',
|
|
'labels.*' => [
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
'required', 'exists:labels,id', function ($attribute, $value, $fail) use ($user) {
|
|
if (!$user->can('assignLabels', Ticket::class)) {
|
|
$fail(trans('Vous n\'êtes pas autorisé à modifier cette valeur'));
|
|
}
|
|
}
|
|
]
|
|
];
|
|
}
|
|
}
|