mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
167 lines
5.7 KiB
PHP
167 lines
5.7 KiB
PHP
<?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\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\Household\Household;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Chill\PersonBundle\Repository\Household\HouseholdACLAwareRepositoryInterface;
|
|
use Chill\PersonBundle\Repository\Household\HouseholdRepository;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
|
|
|
use function array_filter;
|
|
use function array_values;
|
|
|
|
class HouseholdApiController extends ApiController
|
|
{
|
|
private HouseholdACLAwareRepositoryInterface $householdACLAwareRepository;
|
|
|
|
private HouseholdRepository $householdRepository;
|
|
|
|
public function __construct(
|
|
HouseholdRepository $householdRepository,
|
|
HouseholdACLAwareRepositoryInterface $householdACLAwareRepository
|
|
) {
|
|
$this->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']]
|
|
);
|
|
}
|
|
}
|