From afb7d4a160e512d86229c86e6fc049e48faab3cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 6 Dec 2021 15:37:20 +0100 Subject: [PATCH] try to fix [wip] --- .../Repository/ActivityRepository.php | 1 + .../AccompanyingPeriodValidity.php | 2 +- .../AccompanyingPeriodValidityValidator.php | 28 +++++++++++++++---- .../translations/validators.fr.yml | 2 +- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php b/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php index 1493a45ae..ebdc0a3c8 100644 --- a/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php +++ b/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php @@ -31,6 +31,7 @@ class ActivityRepository extends ServiceEntityRepository } /** + * @deprecated use @link{ActivityACLAwareRepositoryInterface::findByAccompanyingPeriod} * @return Activity[] */ public function findByAccompanyingPeriod(AccompanyingPeriod $period, array $scopes, ?bool $allowNullScope = false, ?int $limit = 100, ?int $offset = 0, array $orderBy = ['date' => 'desc']): array diff --git a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidity.php b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidity.php index 536000beb..c4bd30ed3 100644 --- a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidity.php +++ b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidity.php @@ -18,7 +18,7 @@ use Symfony\Component\Validator\Constraint; */ class AccompanyingPeriodValidity extends Constraint { - public $messageSocialIssueCannotBeDeleted = 'This social issue cannot be deleted because it is associated with an activity or an action'; + public $messageSocialIssueCannotBeDeleted = 'The social %name% issue cannot be deleted because it is associated with an activity or an action'; public function getTargets() { diff --git a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidityValidator.php b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidityValidator.php index 50b439556..8fed065ce 100644 --- a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidityValidator.php +++ b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidityValidator.php @@ -12,7 +12,10 @@ declare(strict_types=1); namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod; use Chill\ActivityBundle\Repository\ActivityACLAwareRepository; +use Chill\ActivityBundle\Repository\ActivityRepository; use Chill\PersonBundle\Entity\AccompanyingPeriod; +use Chill\PersonBundle\Entity\SocialWork\SocialIssue; +use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; @@ -21,11 +24,14 @@ use function in_array; class AccompanyingPeriodValidityValidator extends ConstraintValidator { - private ActivityACLAwareRepository $activityRepository; + private ActivityRepository $activityRepository; - public function __construct(ActivityACLAwareRepository $activityRepository) + private SocialIssueRender $socialIssueRender; + + public function __construct(ActivityRepository $activityRepository, SocialIssueRender $socialIssueRender) { $this->activityRepository = $activityRepository; + $this->socialIssueRender = $socialIssueRender; } public function validate($period, Constraint $constraint) @@ -40,22 +46,34 @@ class AccompanyingPeriodValidityValidator extends ConstraintValidator $socialIssues = []; - $activities = $this->activityRepository->findByAccompanyingPeriod($period, 'SEE'); + $activities = $this->activityRepository->findBy(['accompanyingPeriod' => $period]); foreach ($activities as $activity) { - $socialIssues = $activity->getSocialIssues()->getValues(); + $socialIssues = $activity->getSocialIssues()->toArray(); } foreach ($period->getWorks() as $work) { $socialIssues[] = $work->getSocialAction()->getIssue(); } + $socialIssuesByKey = []; foreach ($socialIssues as $si) { - if (!in_array($si, $period->getSocialIssues()->getValues(), true)) { + $socialIssuesByKey[$si->getId()] = $si; + } + + $periodIssuesKeys = $period->getSocialIssues()->map(function (SocialIssue $si) { return $si->getId();}) + ->toArray(); + + dump($socialIssuesByKey); + dump($periodIssuesKeys); + + foreach ($socialIssuesByKey as $key => $si) { + if (!in_array($key, $periodIssuesKeys)) { $this->context ->buildViolation( $constraint->messageSocialIssueCannotBeDeleted ) + ->setParameter('%name%', $this->socialIssueRender->renderString($si, [])) ->addViolation(); } } diff --git a/src/Bundle/ChillPersonBundle/translations/validators.fr.yml b/src/Bundle/ChillPersonBundle/translations/validators.fr.yml index c1dda8bf9..b421e9f43 100644 --- a/src/Bundle/ChillPersonBundle/translations/validators.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/validators.fr.yml @@ -46,4 +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 \ No newline at end of file +The social %name% issue cannot be deleted because it is associated with an activity or an action: 'La problématique sociale "%name%" ne peut pas être supprimée car elle est associée à une activité ou une action'