2021-04-18 20:31:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
2023-08-14 09:23:39 +00:00
|
|
|
namespace App\Http\Requests\Bo;
|
2021-04-18 20:31:07 +02:00
|
|
|
|
2023-08-14 09:23:39 +00:00
|
|
|
use App\Enums\Tickets\TicketStatusEnum;
|
|
|
|
|
use App\Enums\Tickets\TicketTypeEnum;
|
2022-05-26 13:22:50 +02:00
|
|
|
use App\Models\Ticket;
|
2021-04-18 20:31:07 +02:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
|
|
|
|
|
class StoreTicket extends FormRequest
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
|
*
|
2022-03-29 16:57:54 +02:00
|
|
|
* @return boolean
|
2021-04-18 20:31:07 +02:00
|
|
|
*/
|
|
|
|
|
public function authorize()
|
|
|
|
|
{
|
2022-05-24 12:38:30 +02:00
|
|
|
return auth()->check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function prepareForValidation()
|
|
|
|
|
{
|
|
|
|
|
/** @var \App\Models\User|null */
|
2021-07-23 11:04:09 +02:00
|
|
|
$user = auth()->user();
|
2022-05-24 12:38:30 +02:00
|
|
|
if ($user) {
|
|
|
|
|
if (!$user->isAdmin()) {
|
|
|
|
|
$this->merge([
|
2023-08-14 09:23:39 +00:00
|
|
|
'status' => TicketStatusEnum::new->value,
|
2022-05-24 12:38:30 +02:00
|
|
|
'is_internal' => false,
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$this->merge([
|
|
|
|
|
'is_internal' => $this->is_internal ? true : false,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2021-06-28 17:22:52 +02:00
|
|
|
}
|
2022-09-29 13:02:18 +02:00
|
|
|
$this->merge([
|
|
|
|
|
'labels' => $this->labels ?? []
|
|
|
|
|
]);
|
2021-04-18 20:31:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
|
|
|
|
|
|
2021-04-18 20:31:07 +02:00
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function rules()
|
|
|
|
|
{
|
2022-05-26 13:22:50 +02:00
|
|
|
/** @var \App\Models\User */
|
|
|
|
|
$user = auth()->user();
|
2021-04-18 20:31:07 +02:00
|
|
|
return [
|
2021-07-23 11:34:16 +02:00
|
|
|
'site_id' => [
|
2021-06-28 17:22:52 +02:00
|
|
|
'required',
|
2021-07-23 11:34:16 +02:00
|
|
|
'exists:sites,id',
|
2024-06-11 08:03:40 +00:00
|
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
2021-06-28 17:22:52 +02:00
|
|
|
function ($attribute, $value, $fail) {
|
2021-07-23 11:04:09 +02:00
|
|
|
/** @var \App\Models\User */
|
|
|
|
|
$user = auth()->user();
|
|
|
|
|
if ($user->isAdmin()) {
|
2021-06-29 10:17:43 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2021-07-23 11:34:16 +02:00
|
|
|
if (!in_array($value, $user->sites()->pluck('id')->all())) {
|
2021-06-28 17:22:52 +02:00
|
|
|
$fail(__('Veuillez choisir entre :values', [
|
2021-07-23 11:34:16 +02:00
|
|
|
'values' => $user->sites()->pluck('name')->implode(',')
|
2021-06-28 17:22:52 +02:00
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
],
|
2021-04-18 20:31:07 +02:00
|
|
|
'type' => [
|
|
|
|
|
'required',
|
2024-06-11 08:03:40 +00:00
|
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
2021-04-18 20:31:07 +02:00
|
|
|
function ($attribute, $value, $fail) {
|
2023-04-25 09:17:47 +00:00
|
|
|
if (!in_array($value, TicketTypeEnum::toValues())) {
|
2021-04-18 20:31:07 +02:00
|
|
|
$fail(__('Veuillez choisir entre :values', [
|
2023-04-25 09:17:47 +00:00
|
|
|
'value' => implode(',', TicketTypeEnum::toLabels())
|
2021-04-18 20:31:07 +02:00
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
'status' => [
|
|
|
|
|
'required',
|
2024-06-11 08:03:40 +00:00
|
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
2021-04-18 20:31:07 +02:00
|
|
|
function ($attribute, $value, $fail) {
|
2023-08-14 09:23:39 +00:00
|
|
|
if (!in_array($value, TicketStatusEnum::toValues())) {
|
2021-04-18 20:31:07 +02:00
|
|
|
$fail(__('Veuillez choisir entre :values', [
|
2023-08-14 09:23:39 +00:00
|
|
|
'value' => implode(',', TicketStatusEnum::toLabels())
|
2021-04-18 20:31:07 +02:00
|
|
|
]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
],
|
2024-07-23 13:44:51 +02:00
|
|
|
'text' => 'required|string|min:40',
|
|
|
|
|
'title' => 'required|string|between:3,255',
|
|
|
|
|
'file_tickets' => 'sometimes|array',
|
2024-07-29 08:47:28 +02:00
|
|
|
'file_tickets.*' => 'mimes:pptx,docx,xlsx,csv,txt,jpeg,jpg,png,pdf|max:2048',
|
2024-07-23 13:44:51 +02:00
|
|
|
'is_internal' => 'required|boolean',
|
|
|
|
|
'labels' => 'sometimes|array',
|
|
|
|
|
'labels.*' => [
|
2024-06-11 08:03:40 +00:00
|
|
|
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
2022-10-12 14:19:37 +02:00
|
|
|
'required', 'exists:labels,id', function ($attribute, $value, $fail) use ($user) {
|
2022-05-26 13:22:50 +02:00
|
|
|
if (!$user->can('assignLabels', Ticket::class)) {
|
|
|
|
|
$fail(trans('Vous n\'êtes pas autorisé à modifier cette valeur'));
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-12 14:19:37 +02:00
|
|
|
]
|
2021-04-18 20:31:07 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|