mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-09 00:04:59 +00:00
accompanying period validation - on social issues suppression
This commit is contained in:
@@ -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 = 'The social %name% issue cannot be deleted because it is associated with an activity or an action';
|
||||
|
||||
public function getTargets()
|
||||
{
|
||||
return self::CLASS_CONSTRAINT;
|
||||
}
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user