mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
85 lines
2.9 KiB
PHP
85 lines
2.9 KiB
PHP
<?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\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;
|
|
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
|
use function in_array;
|
|
|
|
class AccompanyingPeriodValidityValidator extends ConstraintValidator
|
|
{
|
|
private ActivityRepository $activityRepository;
|
|
|
|
private SocialIssueRender $socialIssueRender;
|
|
|
|
public function __construct(ActivityRepository $activityRepository, SocialIssueRender $socialIssueRender)
|
|
{
|
|
$this->activityRepository = $activityRepository;
|
|
$this->socialIssueRender = $socialIssueRender;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
$socialIssues = [];
|
|
|
|
$activities = $this->activityRepository->findBy(['accompanyingPeriod' => $period]);
|
|
|
|
foreach ($activities as $activity) {
|
|
$socialIssues = $activity->getSocialIssues()->toArray();
|
|
}
|
|
|
|
foreach ($period->getWorks() as $work) {
|
|
$socialIssues[] = $work->getSocialAction()->getIssue();
|
|
}
|
|
|
|
$socialIssuesByKey = [];
|
|
foreach ($socialIssues as $si) {
|
|
$socialIssuesByKey[$si->getId()] = $si;
|
|
}
|
|
|
|
$periodIssuesWithAncestors = [];
|
|
foreach ($period->getSocialIssues() as $si) {
|
|
/** @var SocialIssue $si */
|
|
$periodIssuesWithAncestors = array_merge($periodIssuesWithAncestors, \array_map(
|
|
function (SocialIssue $si) { return $si->getId(); },
|
|
$si->getAncestors(true))
|
|
);
|
|
}
|
|
|
|
foreach ($socialIssuesByKey as $key => $si) {
|
|
if (!in_array($key, $periodIssuesWithAncestors)) {
|
|
$this->context
|
|
->buildViolation(
|
|
$constraint->messageSocialIssueCannotBeDeleted
|
|
)
|
|
->setParameter('%name%', $this->socialIssueRender->renderString($si, []))
|
|
->addViolation();
|
|
}
|
|
}
|
|
}
|
|
}
|