chill-bundles/src/Bundle/ChillPersonBundle/Controller/HouseholdApiController.php

136 lines
5.3 KiB
PHP

<?php
namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Entity\AddressReference;
use Chill\MainBundle\Serializer\Model\Collection;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Repository\Household\HouseholdACLAwareRepositoryInterface;
use Chill\PersonBundle\Repository\Household\HouseholdRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
class HouseholdApiController extends ApiController
{
private HouseholdRepository $householdRepository;
private HouseholdACLAwareRepositoryInterface $householdACLAwareRepository;
public function __construct(
HouseholdRepository $householdRepository,
HouseholdACLAwareRepositoryInterface $householdACLAwareRepository
) {
$this->householdRepository = $householdRepository;
$this->householdACLAwareRepository = $householdACLAwareRepository;
}
public function householdAddressApi($id, Request $request, string $_format): Response
{
return $this->addRemoveSomething('address', $id, $request, $_format, 'address', Address::class, [ '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 ($count !== 0) {
$allHouseholds = $this->householdRepository->findByAccompanyingPeriodParticipation($person,
$paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber());
$currentHouseholdPerson = $person->getCurrentHousehold();
foreach ($allHouseholds as $h) {
if ($h !== $currentHouseholdPerson) {
array_push($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"]]);
}
/**
* @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, fn($a) => $a !== $actual);
}
return $this->json(\array_values($addresses), Response::HTTP_OK, [],
[ 'groups' => [ 'read' ] ]);
}
/**
*
* @Route("/api/1.0/person/household/by-address-reference/{id}.json",
* name="chill_api_person_household_by_address_reference")
* @param AddressReference $addressReference
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function getHouseholdByAddressReference(AddressReference $addressReference): Response
{
// TODO ACL
$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']
]);
}
}