diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/ResidentialAddress.php b/src/Bundle/ChillPersonBundle/Entity/Person/ResidentialAddress.php index 33cc41dc1..cb82f766a 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person/ResidentialAddress.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person/ResidentialAddress.php @@ -19,6 +19,7 @@ use Chill\ThirdPartyBundle\Entity\ThirdParty; use DateTimeImmutable; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; +use Symfony\Component\Serializer\Annotation\Context; /** * @ORM\Entity(repositoryClass=ResidentialAddressRepository::class) @@ -48,6 +49,7 @@ class ResidentialAddress * @ORM\ManyToOne(targetEntity=Person::class) * @ORM\JoinColumn(nullable=true) * @Groups({"read"}) + * @Context(normalizationContext={"groups"={"minimal"}}) */ private ?Person $hostPerson = null; diff --git a/src/Bundle/ChillPersonBundle/Repository/ResidentialAddressRepository.php b/src/Bundle/ChillPersonBundle/Repository/ResidentialAddressRepository.php index 11ea823fd..743a7d9f4 100644 --- a/src/Bundle/ChillPersonBundle/Repository/ResidentialAddressRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/ResidentialAddressRepository.php @@ -15,6 +15,8 @@ use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person\ResidentialAddress; use DateTimeImmutable; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; /** @@ -30,18 +32,37 @@ class ResidentialAddressRepository extends ServiceEntityRepository parent::__construct($registry, ResidentialAddress::class); } - public function findCurrentResidentialAddressByPerson(Person $person, ?DateTimeImmutable $at = null): ?ResidentialAddress + /** + * @param Person $person + * @param DateTimeImmutable|null $at + * @return array|ResidentialAddress[]|null + */ + public function findCurrentResidentialAddressByPerson(Person $person, ?DateTimeImmutable $at = null): array + { + return $this->buildQueryFindCurrentResidentialAddresses($person, $at) + ->select('ra') + ->getQuery() + ->getResult(); + } + + public function buildQueryFindCurrentResidentialAddresses(Person $person, ?DateTimeImmutable $at = null): QueryBuilder { - $addresses = $this->findBy(['person' => $person], ['startDate' => 'DESC']); $date = null === $at ? new DateTimeImmutable('today') : $at; + $qb = $this->createQueryBuilder('ra'); - foreach ($addresses as $a) { - if ($a->getStartDate() < $date && $a->getEndDate() > $date) { - return $a; - } - } + $dateFilter = $qb->expr()->andX( + $qb->expr()->lte('ra.startDate', ':dateIn'), + $qb->expr()->orX( + $qb->expr()->isNull('ra.endDate'), + $qb->expr()->gte('ra.endDate', ':dateIn') + ) + ); - return null; + $qb + ->where($dateFilter) + ->setParameter('dateIn', $date, Types::DATE_IMMUTABLE); + + return $qb; } // /** diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/Entity/PersonRenderBox.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/Entity/PersonRenderBox.vue index 2f3c191b3..2ded0840b 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/Entity/PersonRenderBox.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/Entity/PersonRenderBox.vue @@ -128,42 +128,42 @@ -
+
-
+
    -
  • +
  • -
    +

    ({{ $t('renderbox.residential_address') }})

    -
    +

    {{ $t('renderbox.located_at') }}:

    -
    -
    +

    {{ $t('renderbox.located_at') }}:

    -
    @@ -216,7 +216,25 @@ export default { PersonText, ThirdPartyText }, - props: ['person', 'options', 'render', 'returnPath'], + props: { + person: { + required: true, + }, + options: { + type: Object, + required: false, + }, + render: { + type: String, + }, + returnPath: { + type: String, + }, + showResidentialAddresses: { + type: Boolean, + default: false, + } + }, computed: { isMultiline: function() { if(this.options.isMultiline){ diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue index e007ee94d..366ecd6b7 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue @@ -15,6 +15,7 @@ addNoData: true, isMultiline: true }" + :show-residential-addresses="true" >
    diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonJsonNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonJsonNormalizer.php index 4026518c9..a79a2f6cd 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonJsonNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonJsonNormalizer.php @@ -22,6 +22,7 @@ use DateTime; use DateTimeImmutable; use Doctrine\Common\Collections\Collection; use Symfony\Component\Serializer\Exception\UnexpectedValueException; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; @@ -35,7 +36,9 @@ use function array_key_exists; class PersonJsonNormalizer implements DenormalizerAwareInterface, NormalizerAwareInterface, PersonJsonNormalizerInterface { use DenormalizerAwareTrait; + use NormalizerAwareTrait; + use ObjectToPopulateTrait; private CenterResolverManagerInterface $centerResolverManager; @@ -177,29 +180,37 @@ class PersonJsonNormalizer implements DenormalizerAwareInterface, NormalizerAwar */ public function normalize($person, $format = null, array $context = []) { + $groups = $context[AbstractNormalizer::GROUPS] ?? []; $household = $person->getCurrentHousehold(); - $currentResidentialAddress = $this->residentialAddressRepository->findCurrentResidentialAddressByPerson($person); + $currentResidentialAddresses = $this->residentialAddressRepository->findCurrentResidentialAddressByPerson($person); - return [ + $data = [ 'type' => 'person', 'id' => $person->getId(), 'text' => $this->render->renderString($person, ['addAge' => false]), 'textAge' => $this->render->renderString($person, ['addAge' => true]), 'firstName' => $person->getFirstName(), 'lastName' => $person->getLastName(), + 'current_household_address' => $this->normalizer->normalize($person->getCurrentHouseholdAddress(), $format, $context), 'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $context), 'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $context), 'age' => $this->normalizer->normalize($person->getAge(), $format, $context), + ]; + + if (in_array("minimal", $groups) && 1 === count($context)) { + return $data; + } + + return array_merge($data, [ 'centers' => $this->normalizer->normalize($this->centerResolverManager->resolveCenters($person), $format, $context), 'phonenumber' => $person->getPhonenumber(), 'mobilenumber' => $person->getMobilenumber(), 'email' => $person->getEmail(), 'altNames' => $this->normalizeAltNames($person->getAltNames()), 'gender' => $person->getGender(), - 'current_household_address' => $this->normalizer->normalize($person->getCurrentHouseholdAddress(), $format, $context), 'current_household_id' => $household ? $this->normalizer->normalize($household->getId(), $format, $context) : null, - 'current_residential_address' => $currentResidentialAddress ? $this->normalizer->normalize($currentResidentialAddress, $format, $context) : null, - ]; + 'current_residential_addresses' => $currentResidentialAddresses ? $this->normalizer->normalize($currentResidentialAddresses, $format, $context) : null, + ]); } public function supportsDenormalization($data, $type, $format = null)