try to fix [wip]

This commit is contained in:
Julien Fastré 2021-12-06 15:37:20 +01:00
parent 96e5e4a7b2
commit afb7d4a160
4 changed files with 26 additions and 7 deletions

View File

@ -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

View File

@ -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()
{

View File

@ -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();
}
}

View File

@ -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
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'