mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-30 19:43:49 +00:00
activity: avoid existing entities being added in Users, ThirdParties, Persons
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\ActivityBundle\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
*/
|
||||
class ActivityValidity extends Constraint
|
||||
{
|
||||
public const IS_REQUIRED_MESSAGE = ' is required';
|
||||
|
||||
public const ROOT_MESSAGE = 'For this type of activity, ';
|
||||
|
||||
public $noPersonsMessage = 'For this type of activity, you must add at least one person';
|
||||
|
||||
public $noThirdPartiesMessage = 'For this type of activity, you must add at least one third party';
|
||||
|
||||
public $noUsersMessage = 'For this type of activity, you must add at least one user';
|
||||
|
||||
public $socialActionsMessage = 'For this type of activity, you must add at least one social action';
|
||||
|
||||
public $socialIssuesMessage = 'For this type of activity, you must add at least one social issue';
|
||||
|
||||
public function getTargets()
|
||||
{
|
||||
return self::CLASS_CONSTRAINT;
|
||||
}
|
||||
|
||||
public function makeIsRequiredMessage(string $property)
|
||||
{
|
||||
return self::ROOT_MESSAGE . $property . self::IS_REQUIRED_MESSAGE;
|
||||
}
|
||||
}
|
@@ -0,0 +1,126 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\ActivityBundle\Validator\Constraints;
|
||||
|
||||
use Chill\ActivityBundle\Entity\Activity;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
||||
|
||||
class ActivityValidityValidator extends ConstraintValidator
|
||||
{
|
||||
public function validate($activity, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof ActivityValidity) {
|
||||
throw new UnexpectedTypeException($constraint, ActivityValidity::class);
|
||||
}
|
||||
|
||||
if (!$activity instanceof Activity) {
|
||||
throw new UnexpectedValueException($activity, Activity::class);
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getPersonsVisible() === 2 && count($activity->getPersons()) === 0) {
|
||||
$this->context
|
||||
->buildViolation($constraint->noPersonsMessage)
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getUsersVisible() === 2 && count($activity->getUsers()) === 0) {
|
||||
$this->context
|
||||
->buildViolation($constraint->noUsersMessage)
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getThirdPartiesVisible() === 2 && count($activity->getThirdParties()) === 0) {
|
||||
$this->context
|
||||
->buildViolation($constraint->noThirdPartiesMessage)
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getUserVisible() === 2 && null === $activity->getUser()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('user'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getDateVisible() === 2 && null === $activity->getDate()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('date'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getLocationVisible() === 2 && null === $activity->getLocation()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('location'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getDurationTimeVisible() === 2 && null === $activity->getDurationTime()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('duration time'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getTravelTimeVisible() === 2 && null === $activity->getTravelTime()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('travel time'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getAttendeeVisible() === 2 && null === $activity->getAttendee()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('attendee'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getReasonsVisible() === 2 && null === $activity->getReasons()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('reasons'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getCommentVisible() === 2 && null === $activity->getComment()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('comment'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getSentReceivedVisible() === 2 && null === $activity->getSentReceived()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('sent/received'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getDocumentsVisible() === 2 && null === $activity->getDocuments()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('document'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getEmergencyVisible() === 2 && null === $activity->getEmergency()) {
|
||||
$this->context
|
||||
->buildViolation($constraint->makeIsRequiredMessage('emergency'))
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getSocialIssuesVisible() === 2 && $activity->getSocialIssues()->count() === 0) {
|
||||
$this->context
|
||||
->buildViolation($constraint->socialIssuesMessage)
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if ($activity->getActivityType()->getSocialActionsVisible() === 2 && $activity->getSocialActions()->count() === 0) {
|
||||
$this->context
|
||||
->buildViolation($constraint->socialActionsMessage)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user