householdRepository = $householdRepository; $this->householdACLAwareRepository = $householdACLAwareRepository; } /** * @Route("/api/1.0/person/household/by-address-reference/{id}.json", * name="chill_api_person_household_by_address_reference") * * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function getHouseholdByAddressReference(AddressReference $addressReference): Response { $this->denyAccessUnlessGranted('ROLE_USER'); $total = $this->householdACLAwareRepository->countByAddressReference($addressReference); $paginator = $this->getPaginatorFactory()->create($total); $households = $this->householdACLAwareRepository->findByAddressReference( $addressReference, $paginator->getCurrentPageFirstItemNumber(), $paginator->getItemsPerPage() ); $collection = new Collection($households, $paginator); return $this->json($collection, Response::HTTP_OK, [], [ AbstractNormalizer::GROUPS => ['read'], ]); } public function householdAddressApi($id, Request $request, string $_format): Response { return $this->addRemoveSomething('address', $id, $request, $_format, 'address', Address::class, ['groups' => ['read']]); } /** * @Route("/api/1.0/person/address/suggest/by-household/{household_id}.{_format}", * name="chill_person_address_suggest_by_household", * requirements={ * "_format": "json" * } * ) * @ParamConverter("household", options={"id": "household_id"}) */ public function suggestAddressByHousehold(Household $household, string $_format) { // TODO add acl $addresses = []; // collect addresses from location in courses foreach ($household->getCurrentPersons() as $person) { foreach ($person->getAccompanyingPeriodParticipations() as $participation) { if (null !== $participation->getAccompanyingPeriod()->getAddressLocation()) { $a = $participation->getAccompanyingPeriod()->getAddressLocation(); $addresses[$a->getId()] = $a; } if ( null !== $personLocation = $participation ->getAccompanyingPeriod()->getPersonLocation() ) { $a = $personLocation->getCurrentHouseholdAddress(); if (null !== $a) { $addresses[$a->getId()] = $a; } } } } // remove the actual address $actual = $household->getCurrentAddress(); if (null !== $actual) { $addresses = array_filter($addresses, static fn ($a) => $a !== $actual); } return $this->json( array_values($addresses), Response::HTTP_OK, [], ['groups' => ['read']] ); } /** * Find Household of people participating to the same AccompanyingPeriod. * * @ParamConverter("person", options={"id": "person_id"}) */ public function suggestHouseholdByAccompanyingPeriodParticipationApi(Person $person, string $_format) { // TODO add acl $count = $this->householdRepository->countByAccompanyingPeriodParticipation($person); $paginator = $this->getPaginatorFactory()->create($count); $households = []; if (0 !== $count) { $allHouseholds = $this->householdRepository->findByAccompanyingPeriodParticipation( $person, $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber() ); $currentHouseholdPerson = $person->getCurrentHousehold(); foreach ($allHouseholds as $h) { if ($h !== $currentHouseholdPerson) { $households[] = $h; } } if (null !== $currentHouseholdPerson) { $count = $count - 1; $paginator = $this->getPaginatorFactory()->create($count); } } $collection = new Collection($households, $paginator); return $this->json( $collection, Response::HTTP_OK, [], ['groups' => ['read']] ); } }