106 lines
3.8 KiB
PHP
106 lines
3.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Requests\MacroSchedule;
|
||
|
|
|
||
|
|
use App\Enums\MacroSchedule\Activity as EnumActivity;
|
||
|
|
use App\Models\MacroSchedule\Activity as ModelActivity;
|
||
|
|
use App\Models\Project;
|
||
|
|
use Carbon\Exceptions\InvalidFormatException;
|
||
|
|
use Illuminate\Foundation\Http\FormRequest;
|
||
|
|
use Illuminate\Support\Carbon;
|
||
|
|
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
|
||
|
|
|
||
|
|
class PostActivityRequest extends FormRequest
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Determine if the user is authorized to make this request.
|
||
|
|
*
|
||
|
|
* @return boolean
|
||
|
|
*/
|
||
|
|
public function authorize()
|
||
|
|
{
|
||
|
|
return auth()->check() and $this->ajax();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the validation rules that apply to the request.
|
||
|
|
*
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function rules()
|
||
|
|
{
|
||
|
|
/** @var \App\Models\Project|null $project */
|
||
|
|
$project = Project::whereId($this->project_id)->with('activities')->first();
|
||
|
|
if ($project and $this->id) {
|
||
|
|
$project->setRelation(
|
||
|
|
'activities',
|
||
|
|
$project->activities->filter(function (ModelActivity $activity) {
|
||
|
|
return $activity->id !== $this->id;
|
||
|
|
})
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return [
|
||
|
|
'project_id' => 'required|exists:projects,id',
|
||
|
|
'id' => 'nullable|exists:macro_activities,id',
|
||
|
|
'activity' => ['required', new EnumIsValidRule(EnumActivity::class)],
|
||
|
|
'comment' => 'required|min:0|max:255',
|
||
|
|
'start' => [
|
||
|
|
'required',
|
||
|
|
'date_format:Y-m-d\TH:i:s',
|
||
|
|
function ($attribute, $value, $fail) use ($project) {
|
||
|
|
try {
|
||
|
|
$dateTime = Carbon::parse($value);
|
||
|
|
if (in_array($dateTime->dayOfWeek, [0, 6])) {
|
||
|
|
$fail('La date de début ne peut être un jour de weekend.');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (!in_array($dateTime->format('H:i'), ['00:00', '12:00'])) {
|
||
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
||
|
|
$fail("$attribute : Il est seulement possible de définir une date le matin ou l'après midi.");
|
||
|
|
}
|
||
|
|
} catch (InvalidFormatException $e) {
|
||
|
|
$fail('Format de date invalide');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
],
|
||
|
|
'end' => [
|
||
|
|
'required',
|
||
|
|
'date_format:Y-m-d\TH:i:s',
|
||
|
|
'after:start',
|
||
|
|
function ($attribute, $value, $fail) use ($project) {
|
||
|
|
try {
|
||
|
|
$dateTime = Carbon::parse($value);
|
||
|
|
if (in_array($dateTime->dayOfWeek, [0, 6])) {
|
||
|
|
$fail('La date de fin ne peut être un jour de weekend.');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (!in_array($dateTime->format('H:i'), ['11:59', '23:59'])) {
|
||
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
||
|
|
$fail("$attribute : Il est seulement possible de définir une date le matin ou l'après midi.");
|
||
|
|
}
|
||
|
|
} catch (InvalidFormatException $e) {
|
||
|
|
$fail('Format de date invalide');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
],
|
||
|
|
'load' => 'required|integer|min:1|max:' . config('macroschedule.totalAvailableDevs')
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get custom attributes for validator errors.
|
||
|
|
*
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function attributes(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'start' => 'Date de début',
|
||
|
|
'end' => 'Date de fin',
|
||
|
|
'activity' => 'Activité',
|
||
|
|
'comment' => 'Commentaire',
|
||
|
|
'load' => 'Charge',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|