mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
Add address suggestion for a person
This commit is contained in:
@@ -18,12 +18,15 @@
|
||||
namespace Chill\PersonBundle\Controller;
|
||||
|
||||
use Chill\PersonBundle\Security\Authorization\PersonVoter;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\MainBundle\CRUD\Controller\ApiController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
|
||||
class PersonApiController extends ApiController
|
||||
{
|
||||
@@ -54,4 +57,39 @@ class PersonApiController extends ApiController
|
||||
return $this->addRemoveSomething('address', $id, $request, $_format, 'address', Address::class, [ 'groups' => [ 'read' ] ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/api/1.0/person/address/suggest/by-person/{person_id}.{_format}",
|
||||
* name="chill_person_address_suggest_by_person",
|
||||
* requirements={
|
||||
* "_format"="json"
|
||||
* }
|
||||
* )
|
||||
* @ParamConverter("person", options={"id" = "person_id"})
|
||||
*/
|
||||
public function suggestAddress(Person $person, Request $request, string $_format): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person);
|
||||
|
||||
$addresses = [];
|
||||
// collect addresses from location in courses
|
||||
foreach ($person->getAccompanyingPeriodParticipations() as $participation) {
|
||||
if (null !== $participation->getAccompanyingPeriod()->getAddressLocation()) {
|
||||
$a = $participation->getAccompanyingPeriod()->getAddressLocation();
|
||||
$addresses[$a->getId()] = $a;
|
||||
}
|
||||
if (null !== $participation->getAccompanyingPeriod()->getPersonLocation()) {
|
||||
$a = $participation->getAccompanyingPeriod()->getAddressLocation();
|
||||
$addresses[$a->getId()] = $a;
|
||||
}
|
||||
}
|
||||
|
||||
\array_unique($addresses);
|
||||
// remove the actual address
|
||||
$actual = $person->getCurrentHouseholdAddress();
|
||||
if (null !== $actual) {
|
||||
$addresses = \array_filter($addresses, fn($a) => $a !== $actual);
|
||||
}
|
||||
|
||||
return $this->json(\array_values($addresses), Response::HTTP_OK, [], [ 'groups' => [ 'read' ]]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user