mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
add validation to accompanying periods
This commit is contained in:
@@ -11,7 +11,7 @@ class LocationValidity extends Constraint
|
||||
{
|
||||
public $messagePersonLocatedMustBeAssociated = "The person where the course is located must be associated to the course. Change course's location before removing the person.";
|
||||
|
||||
public $messagePeriodMustRemainsLocated = "The period must remains located";
|
||||
public $messagePeriodMustRemainsLocated = "The period must remain located";
|
||||
|
||||
public function getTargets()
|
||||
{
|
||||
|
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
*/
|
||||
class ParticipationOverlap extends Constraint
|
||||
{
|
||||
public $message = 'This participation already exists.';
|
||||
}
|
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
|
||||
|
||||
use Chill\MainBundle\Util\DateRangeCovering;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
|
||||
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ParticipationOverlap;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
class ParticipationOverlapValidator extends ConstraintValidator
|
||||
{
|
||||
private const MAX_PARTICIPATION = 1;
|
||||
|
||||
public function validate($participations, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof ParticipationOverlap) {
|
||||
throw new UnexpectedTypeException($constraint, ParticipationOverlap::class);
|
||||
}
|
||||
|
||||
if (!$participations instanceof Collection) {
|
||||
throw new UnexpectedTypeException($participations, 'This should be a collection');
|
||||
}
|
||||
|
||||
if (count($participations) <= self::MAX_PARTICIPATION) {
|
||||
return;
|
||||
}
|
||||
|
||||
$overlaps = new DateRangeCovering(self::MAX_PARTICIPATION, $participations[0]->getStartDate()->getTimezone());
|
||||
|
||||
foreach ($participations as $participation) {
|
||||
|
||||
if (!$participation instanceof AccompanyingPeriodParticipation) {
|
||||
throw new UnexpectedTypeException($participation, AccompanyingPeriodParticipation::class);
|
||||
}
|
||||
|
||||
$personId = $participation->getPerson()->getId();
|
||||
|
||||
$particpationList[$personId][] = $participation;
|
||||
|
||||
}
|
||||
|
||||
foreach ($particpationList as $group) {
|
||||
if (count($group) > 1) {
|
||||
foreach ($group as $p) {
|
||||
$overlaps->add($p->getStartDate(), $p->getEndDate(), $p->getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$overlaps->compute();
|
||||
|
||||
if ($overlaps->hasIntersections()) {
|
||||
foreach ($overlaps->getIntersections() as list($start, $end, $ids)) {
|
||||
$msg = $end === null ? $constraint->message :
|
||||
$constraint->message;
|
||||
|
||||
$this->context->buildViolation($msg)
|
||||
->setParameters([
|
||||
'{{ start }}' => $start->format('d-m-Y'),
|
||||
'{{ end }}' => $end === null ? null : $end->format('d-m-Y'),
|
||||
'{{ ids }}' => $ids,
|
||||
])
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
*/
|
||||
class ResourceDuplicateCheck extends Constraint
|
||||
{
|
||||
public $message = '{{ name }} is already associated to this accompanying course.';
|
||||
}
|
||||
|
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
|
||||
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Templating\Entity\PersonRender;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ResourceDuplicateCheck;
|
||||
use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender;
|
||||
|
||||
class ResourceDuplicateCheckValidator extends ConstraintValidator
|
||||
{
|
||||
|
||||
private PersonRender $personRender;
|
||||
private ThirdPartyRender $thirdpartyRender;
|
||||
|
||||
public function __construct(PersonRender $personRender, ThirdPartyRender $thirdPartyRender)
|
||||
{
|
||||
$this->personRender = $personRender;
|
||||
$this->thirdpartyRender = $thirdPartyRender;
|
||||
}
|
||||
|
||||
public function validate($resources, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof ResourceDuplicateCheck) {
|
||||
throw new UnexpectedTypeException($constraint, ParticipationOverlap::class);
|
||||
}
|
||||
|
||||
if (!$resources instanceof Collection) {
|
||||
throw new UnexpectedTypeException($resources, Collection::class);
|
||||
}
|
||||
|
||||
$resourceList = [];
|
||||
|
||||
foreach ($resources as $resource) {
|
||||
$id = ($resource->getResource() instanceof Person ? 'p' :
|
||||
't').$resource->getResource()->getId();
|
||||
|
||||
if (\in_array($id, $resourceList, true)) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->setParameter('{{ name }}', $resource->getResource() instanceof Person ? $this->personRender->renderString($resource->getResource(), []) :
|
||||
$this->thirdpartyRender->renderString($resource->getResource(), []))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
$resourceList[] = $id;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user