person: add custom class validator for accompanying period

This commit is contained in:
nobohan 2021-12-03 18:10:52 +01:00
parent 611f0a9ca6
commit 2e4356c3c9
4 changed files with 100 additions and 0 deletions

View File

@ -27,6 +27,7 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ParticipationOverlap;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ResourceDuplicateCheck;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\AccompanyingPeriodValidity;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use DateTime;
use DateTimeImmutable;
@ -58,6 +59,8 @@ use const SORT_REGULAR;
* "this.isConfidential and this.getUser === NULL",
* message="If the accompanying course is confirmed and confidential, a referrer must remain assigned."
* )
*
* @AccompanyingPeriodValidity(groups={AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED})
*/
class AccompanyingPeriod implements
GroupSequenceProviderInterface,

View File

@ -0,0 +1,27 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class AccompanyingPeriodValidity extends Constraint
{
public $messageSocialIssueCannotBeDeleted = 'This social issue cannot be deleted because it is associated with an activity or an action';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}

View File

@ -0,0 +1,69 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Chill\ActivityBundle\Repository\ActivityACLAwareRepository;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class AccompanyingPeriodValidityValidator extends ConstraintValidator
{
private ActivityACLAwareRepository $activityRepository;
public function __construct(ActivityACLAwareRepository $activityRepository)
{
$this->activityRepository = $activityRepository;
}
public function validate($period, Constraint $constraint)
{
if (!$constraint instanceof AccompanyingPeriodValidity) {
throw new UnexpectedTypeException($constraint, AccompanyingPeriodValidity::class);
}
if (!$period instanceof AccompanyingPeriod) {
throw new UnexpectedValueException($period, AccompanyingPeriod::class);
}
dump($period);
$socialIssues = [];
$activities = $this->activityRepository->findByAccompanyingPeriod($period, 'SEE');
dump($activities);
foreach ($activities as $activity) {
$socialIssues[] = $activity->getSocialIssues();
}
foreach ($period->getWorks() as $work) {
$socialIssues[] = $work->getSocialIssues();
}
dump($socialIssues);
foreach ($period->getSocialIssues() as $si) {
dump($si);
if (!in_array($si, $socialIssues)) {
$this->context
->buildViolation(
$constraint->messageSocialIssueCannotBeDeleted
)
->addViolation();
}
}
}
}

View File

@ -46,3 +46,4 @@ household_membership:
'{{ name }} is already associated to this accompanying course.': '{{ name }} est déjà associé à ce parcours.'
A course must contains at least one social issue: 'Un parcours doit être associé à au moins une problématique sociale'
A course must be associated to at least one scope: 'Un parcours doit être associé à au moins un service'
This social issue cannot be deleted because it is associated with an activity or an action: La problématique sociale ne peut pas être supprimée car elle est associée à une activité ou une action