Files
chill-bundles/src/Bundle/ChillPersonBundle/Controller/PersonApiController.php
2021-11-30 13:54:58 +01:00

82 lines
2.8 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\Security\Authorization\AuthorizationHelper;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
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\Security\Core\Role\Role;
use function array_filter;
use function array_values;
class PersonApiController extends ApiController
{
private AuthorizationHelper $authorizationHelper;
public function __construct(AuthorizationHelper $authorizationHelper)
{
$this->authorizationHelper = $authorizationHelper;
}
public function personAddressApi($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-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;
}
}
// remove the actual address
$actual = $person->getCurrentHouseholdAddress();
if (null !== $actual) {
$addresses = array_filter($addresses, static fn ($a) => $a !== $actual);
}
return $this->json(array_values($addresses), Response::HTTP_OK, [], ['groups' => ['read']]);
}
protected function createEntity(string $action, Request $request): object
{
return parent::createEntity($action, $request);
// TODO temporary hack to allow creation of person with fake center
/* $centers = $this->authorizationHelper->getReachableCenters($this->getUser(),
new Role(PersonVoter::CREATE));
$person->setCenter($centers[0]); */
}
}