mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,51 +1,39 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2021, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Controller;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Form\AccompanyingPeriodType;
|
||||
use Chill\PersonBundle\Privacy\PrivacyEvent;
|
||||
use Chill\PersonBundle\Repository\AccompanyingPeriodACLAwareRepositoryInterface;
|
||||
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
|
||||
use Chill\PersonBundle\Security\Authorization\PersonVoter;
|
||||
use DateTime;
|
||||
use Doctrine\DBAL\Exception;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Form\AccompanyingPeriodType;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Chill\PersonBundle\Security\Authorization\PersonVoter;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use function array_filter;
|
||||
|
||||
/**
|
||||
* Class AccompanyingPeriodController
|
||||
*
|
||||
* @package Chill\PersonBundle\Controller
|
||||
* Class AccompanyingPeriodController.
|
||||
*/
|
||||
class AccompanyingPeriodController extends AbstractController
|
||||
{
|
||||
protected AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository;
|
||||
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
@@ -56,8 +44,6 @@ class AccompanyingPeriodController extends AbstractController
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
protected AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository;
|
||||
|
||||
public function __construct(
|
||||
AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository,
|
||||
EventDispatcherInterface $eventDispatcher,
|
||||
@@ -68,218 +54,71 @@ class AccompanyingPeriodController extends AbstractController
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ParamConverter("person", options={"id"="person_id"})
|
||||
*/
|
||||
public function listAction(Person $person): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted(AccompanyingPeriodVoter::SEE, $person);
|
||||
|
||||
$event = new PrivacyEvent($person, [
|
||||
'element_class' => AccompanyingPeriod::class,
|
||||
'action' => 'list'
|
||||
]);
|
||||
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
|
||||
|
||||
$accompanyingPeriods = $this->accompanyingPeriodACLAwareRepository
|
||||
->findByPerson($person, AccompanyingPeriodVoter::SEE);
|
||||
|
||||
return $this->render('@ChillPerson/AccompanyingPeriod/list.html.twig', [
|
||||
'accompanying_periods' => $accompanyingPeriods,
|
||||
'person' => $person
|
||||
]);
|
||||
}
|
||||
|
||||
public function createAction(int $person_id, Request $request): Response
|
||||
{
|
||||
$person = $this->_getPerson($person_id);
|
||||
|
||||
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person,
|
||||
'You are not allowed to update this person');
|
||||
|
||||
$accompanyingPeriod = new AccompanyingPeriod(new \DateTime('now'));
|
||||
$accompanyingPeriod->setClosingDate(new \DateTime('now'));
|
||||
|
||||
$accompanyingPeriod->addPerson($person);
|
||||
//or $person->addAccompanyingPeriod($accompanyingPeriod);
|
||||
|
||||
$form = $this->createForm(
|
||||
AccompanyingPeriodType::class,
|
||||
$accompanyingPeriod, [
|
||||
'period_action' => 'create',
|
||||
'center' => $person->getCenter()
|
||||
]);
|
||||
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$form->handleRequest($request);
|
||||
$errors = $this->_validatePerson($person);
|
||||
$flashBag = $this->get('session')->getFlashBag();
|
||||
|
||||
if ($form->isValid(['Default', 'closed'])
|
||||
&& count($errors) === 0) {
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->persist($accompanyingPeriod);
|
||||
$em->flush();
|
||||
$flashBag->add('success',
|
||||
$this->get('translator')->trans(
|
||||
'A period has been created.'));
|
||||
|
||||
return $this->redirect(
|
||||
$this->generateUrl('chill_person_accompanying_period_list', [
|
||||
'person_id' => $person->getId()
|
||||
]));
|
||||
|
||||
} else {
|
||||
$flashBag->add('error', $this->get('translator')
|
||||
->trans('Error! Period not created!'));
|
||||
|
||||
foreach($errors as $error) {
|
||||
$flashBag->add('info', $error->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
'person' => $person,
|
||||
'accompanying_period' => $accompanyingPeriod
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function updateAction(int $person_id, int $period_id, Request $request): Response
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
/** @var AccompanyingPeriod $accompanyingPeriod */
|
||||
$accompanyingPeriod = $em->getRepository(AccompanyingPeriod::class)->find($period_id);
|
||||
|
||||
if ($accompanyingPeriod === null) {
|
||||
throw $this->createNotFoundException("Period with id " . $period_id . " is not found");
|
||||
}
|
||||
|
||||
/** @var Person $person */
|
||||
$person = $this->_getPerson($person_id);
|
||||
|
||||
// CHECK
|
||||
if (! $accompanyingPeriod->containsPerson($person)) {
|
||||
throw new Exception("Accompanying period " . $period_id . " does not contain person " . $person_id);
|
||||
}
|
||||
|
||||
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person,
|
||||
'You are not allowed to update this person');
|
||||
|
||||
$form = $this->createForm(AccompanyingPeriodType::class,
|
||||
$accompanyingPeriod, [
|
||||
'period_action' => 'update',
|
||||
'center' => $person->getCenter()
|
||||
]);
|
||||
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$form->handleRequest($request);
|
||||
$errors = $this->_validatePerson($person);
|
||||
$flashBag = $this->get('session')->getFlashBag();
|
||||
|
||||
if ($form->isValid(['Default', 'closed'])
|
||||
&& count($errors) === 0) {
|
||||
|
||||
$em->flush();
|
||||
|
||||
$flashBag->add('success',
|
||||
$this->get('translator')->trans('An accompanying period has been updated.'));
|
||||
|
||||
return $this->redirect(
|
||||
$this->generateUrl('chill_person_accompanying_period_list', [
|
||||
'person_id' => $person->getId()
|
||||
]));
|
||||
|
||||
} else {
|
||||
|
||||
$flashBag->add('error', $this->get('translator')
|
||||
->trans('Error when updating the period'));
|
||||
|
||||
foreach($errors as $error) {
|
||||
$flashBag->add('info', $error->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
'person' => $person,
|
||||
'accompanying_period' => $accompanyingPeriod
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function closeAction(int $person_id, Request $request): Response
|
||||
{
|
||||
|
||||
$person = $this->_getPerson($person_id);
|
||||
|
||||
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, 'You are not allowed to update this person');
|
||||
|
||||
if ($person->isOpen() === false) {
|
||||
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('error', $this->get('translator')
|
||||
->trans('Beware period is closed', ['%name%' => $person->__toString()]
|
||||
));
|
||||
->trans(
|
||||
'Beware period is closed',
|
||||
['%name%' => $person->__toString()]
|
||||
));
|
||||
|
||||
return $this->redirect(
|
||||
$this->generateUrl('chill_person_accompanying_period_list', [
|
||||
'person_id' => $person->getId()
|
||||
]));
|
||||
'person_id' => $person->getId(),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
$current = $person->getCurrentAccompanyingPeriod();
|
||||
|
||||
$form = $this->createForm(AccompanyingPeriodType::class, $current, [
|
||||
'period_action' => 'close',
|
||||
'center' => $person->getCenter()
|
||||
'center' => $person->getCenter(),
|
||||
]);
|
||||
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isValid()){
|
||||
if ($form->isValid()) {
|
||||
$person->close($current);
|
||||
$errors = $this->_validatePerson($person);
|
||||
|
||||
if (count($errors) === 0) {
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('success', $this->get('translator')
|
||||
->trans('An accompanying period has been closed.', [
|
||||
'%name%' => $person->__toString()
|
||||
]));
|
||||
->trans('An accompanying period has been closed.', [
|
||||
'%name%' => $person->__toString(),
|
||||
]));
|
||||
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
|
||||
return $this->redirect(
|
||||
$this->generateUrl('chill_person_accompanying_period_list', [
|
||||
'person_id' => $person->getId()
|
||||
'person_id' => $person->getId(),
|
||||
])
|
||||
);
|
||||
|
||||
} else {
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('error', $this->get('translator')
|
||||
->trans('Error! Period not closed!'));
|
||||
|
||||
foreach ($errors as $error) {
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('info', $error->getMessage());
|
||||
}
|
||||
}
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('error', $this->get('translator')
|
||||
->trans('Error! Period not closed!'));
|
||||
|
||||
foreach ($errors as $error) {
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('info', $error->getMessage());
|
||||
}
|
||||
} else { //if form is not valid
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('error',
|
||||
->add(
|
||||
'error',
|
||||
$this->get('translator')
|
||||
->trans('Pediod closing form is not valid')
|
||||
);
|
||||
@@ -294,57 +133,131 @@ class AccompanyingPeriodController extends AbstractController
|
||||
return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
'person' => $person,
|
||||
'accompanying_period' => $current
|
||||
'accompanying_period' => $current,
|
||||
]);
|
||||
}
|
||||
|
||||
private function _validatePerson(Person $person): ConstraintViolationListInterface
|
||||
public function createAction(int $person_id, Request $request): Response
|
||||
{
|
||||
$errors = $this->validator->validate($person, null,
|
||||
['Default']);
|
||||
$person = $this->_getPerson($person_id);
|
||||
|
||||
// Can be disabled with config
|
||||
if (false === $this->container->getParameter('chill_person.allow_multiple_simultaneous_accompanying_periods')) {
|
||||
$this->denyAccessUnlessGranted(
|
||||
PersonVoter::UPDATE,
|
||||
$person,
|
||||
'You are not allowed to update this person'
|
||||
);
|
||||
|
||||
$errors_accompanying_period = $this->validator->validate($person, null,
|
||||
['accompanying_period_consistent']);
|
||||
$accompanyingPeriod = new AccompanyingPeriod(new DateTime('now'));
|
||||
$accompanyingPeriod->setClosingDate(new DateTime('now'));
|
||||
|
||||
foreach($errors_accompanying_period as $error ) {
|
||||
$errors->add($error);
|
||||
$accompanyingPeriod->addPerson($person);
|
||||
//or $person->addAccompanyingPeriod($accompanyingPeriod);
|
||||
|
||||
$form = $this->createForm(
|
||||
AccompanyingPeriodType::class,
|
||||
$accompanyingPeriod,
|
||||
[
|
||||
'period_action' => 'create',
|
||||
'center' => $person->getCenter(),
|
||||
]
|
||||
);
|
||||
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$form->handleRequest($request);
|
||||
$errors = $this->_validatePerson($person);
|
||||
$flashBag = $this->get('session')->getFlashBag();
|
||||
|
||||
if ($form->isValid(['Default', 'closed'])
|
||||
&& count($errors) === 0) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->persist($accompanyingPeriod);
|
||||
$em->flush();
|
||||
$flashBag->add(
|
||||
'success',
|
||||
$this->get('translator')->trans(
|
||||
'A period has been created.'
|
||||
)
|
||||
);
|
||||
|
||||
return $this->redirect(
|
||||
$this->generateUrl('chill_person_accompanying_period_list', [
|
||||
'person_id' => $person->getId(),
|
||||
])
|
||||
);
|
||||
}
|
||||
$flashBag->add('error', $this->get('translator')
|
||||
->trans('Error! Period not created!'));
|
||||
|
||||
foreach ($errors as $error) {
|
||||
$flashBag->add('info', $error->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
'person' => $person,
|
||||
'accompanying_period' => $accompanyingPeriod,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ParamConverter("person", options={"id": "person_id"})
|
||||
*/
|
||||
public function listAction(Person $person): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted(AccompanyingPeriodVoter::SEE, $person);
|
||||
|
||||
$event = new PrivacyEvent($person, [
|
||||
'element_class' => AccompanyingPeriod::class,
|
||||
'action' => 'list',
|
||||
]);
|
||||
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
|
||||
|
||||
$accompanyingPeriods = $this->accompanyingPeriodACLAwareRepository
|
||||
->findByPerson($person, AccompanyingPeriodVoter::SEE);
|
||||
|
||||
return $this->render('@ChillPerson/AccompanyingPeriod/list.html.twig', [
|
||||
'accompanying_periods' => $accompanyingPeriods,
|
||||
'person' => $person,
|
||||
]);
|
||||
}
|
||||
|
||||
public function openAction(int $person_id, Request $request): Response
|
||||
{
|
||||
$person = $this->_getPerson($person_id);
|
||||
|
||||
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person,
|
||||
'You are not allowed to update this person');
|
||||
$this->denyAccessUnlessGranted(
|
||||
PersonVoter::UPDATE,
|
||||
$person,
|
||||
'You are not allowed to update this person'
|
||||
);
|
||||
|
||||
//in case the person is already open
|
||||
if ($person->isOpen()) {
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('error', $this->get('translator')
|
||||
->trans('Error! Period %name% is not closed ; it can be open',
|
||||
['%name%' => $person->__toString()]
|
||||
));
|
||||
->trans(
|
||||
'Error! Period %name% is not closed ; it can be open',
|
||||
['%name%' => $person->__toString()]
|
||||
));
|
||||
|
||||
return $this->redirect(
|
||||
$this->generateUrl('chill_person_accompanying_period_list', [
|
||||
'person_id' => $person->getId()
|
||||
]));
|
||||
'person_id' => $person->getId(),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
$accompanyingPeriod = new AccompanyingPeriod(new \DateTime());
|
||||
$accompanyingPeriod = new AccompanyingPeriod(new DateTime());
|
||||
|
||||
$form = $this->createForm(AccompanyingPeriodType::class,
|
||||
$accompanyingPeriod, [
|
||||
$form = $this->createForm(
|
||||
AccompanyingPeriodType::class,
|
||||
$accompanyingPeriod,
|
||||
[
|
||||
'period_action' => 'open',
|
||||
'center' => $person->getCenter()
|
||||
]);
|
||||
'center' => $person->getCenter(),
|
||||
]
|
||||
);
|
||||
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$form->handleRequest($request);
|
||||
@@ -357,40 +270,41 @@ class AccompanyingPeriodController extends AbstractController
|
||||
if (count($errors) <= 0) {
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('success', $this->get('translator')
|
||||
->trans('An accompanying period has been opened.',
|
||||
->trans(
|
||||
'An accompanying period has been opened.',
|
||||
['%name%' => $person->__toString()]
|
||||
));
|
||||
));
|
||||
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
|
||||
return $this->redirect(
|
||||
$this->generateUrl('chill_person_accompanying_period_list', [
|
||||
'person_id' => $person->getId()
|
||||
]));
|
||||
|
||||
} else {
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('error', $this->get('translator')
|
||||
->trans('Period not opened'));
|
||||
|
||||
foreach ($errors as $error) {
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('info', $error->getMessage());
|
||||
}
|
||||
'person_id' => $person->getId(),
|
||||
])
|
||||
);
|
||||
}
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('error', $this->get('translator')
|
||||
->trans('Period not opened'));
|
||||
|
||||
foreach ($errors as $error) {
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('info', $error->getMessage());
|
||||
}
|
||||
} else { // if errors in forms
|
||||
$this->get('session')->getFlashBag()
|
||||
->add('error', $this->get('translator')
|
||||
->trans('Period not opened : form is invalid')
|
||||
);
|
||||
->add(
|
||||
'error',
|
||||
$this->get('translator')
|
||||
->trans('Period not opened : form is invalid')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
'person' => $person,
|
||||
'accompanying_period' => $accompanyingPeriod
|
||||
'accompanying_period' => $accompanyingPeriod,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -400,20 +314,20 @@ class AccompanyingPeriodController extends AbstractController
|
||||
$person = $this->_getPerson($person_id);
|
||||
|
||||
/* @var $period AccompanyingPeriod */
|
||||
$period = \array_filter(
|
||||
$period = array_filter(
|
||||
$person->getAccompanyingPeriods(),
|
||||
function (AccompanyingPeriod $p) use ($period_id) {
|
||||
return $p->getId() === ($period_id);
|
||||
}
|
||||
)[0] ?? NULL;
|
||||
)[0] ?? null;
|
||||
|
||||
if ($period === NULL) {
|
||||
if (null === $period) {
|
||||
throw $this->createNotFoundException('period not found');
|
||||
}
|
||||
|
||||
$confirm = $request->query->getBoolean('confirm', false);
|
||||
|
||||
if ($confirm === true && $period->canBeReOpened($person)) {
|
||||
if (true === $confirm && $period->canBeReOpened($person)) {
|
||||
$period->reOpen();
|
||||
|
||||
$this->_validatePerson($person);
|
||||
@@ -421,40 +335,141 @@ class AccompanyingPeriodController extends AbstractController
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')->trans(
|
||||
'The period has been re-opened'));
|
||||
'The period has been re-opened'
|
||||
));
|
||||
|
||||
return $this->redirectToRoute('chill_person_accompanying_period_list', [
|
||||
'person_id' => $person->getId()
|
||||
'person_id' => $person->getId(),
|
||||
]);
|
||||
}
|
||||
|
||||
} elseif ($confirm === false && $period->canBeReOpened($person)) {
|
||||
if (false === $confirm && $period->canBeReOpened($person)) {
|
||||
return $this->render('ChillPersonBundle:AccompanyingPeriod:re_open.html.twig', [
|
||||
'period' => $period,
|
||||
'person' => $person
|
||||
'person' => $person,
|
||||
]);
|
||||
|
||||
} else {
|
||||
return (new Response())
|
||||
->setStatusCode(Response::HTTP_BAD_REQUEST)
|
||||
->setContent("You cannot re-open this period");
|
||||
}
|
||||
|
||||
return (new Response())
|
||||
->setStatusCode(Response::HTTP_BAD_REQUEST)
|
||||
->setContent('You cannot re-open this period');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function updateAction(int $person_id, int $period_id, Request $request): Response
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
/** @var AccompanyingPeriod $accompanyingPeriod */
|
||||
$accompanyingPeriod = $em->getRepository(AccompanyingPeriod::class)->find($period_id);
|
||||
|
||||
if (null === $accompanyingPeriod) {
|
||||
throw $this->createNotFoundException('Period with id ' . $period_id . ' is not found');
|
||||
}
|
||||
|
||||
/** @var Person $person */
|
||||
$person = $this->_getPerson($person_id);
|
||||
|
||||
// CHECK
|
||||
if (!$accompanyingPeriod->containsPerson($person)) {
|
||||
throw new Exception('Accompanying period ' . $period_id . ' does not contain person ' . $person_id);
|
||||
}
|
||||
|
||||
$this->denyAccessUnlessGranted(
|
||||
PersonVoter::UPDATE,
|
||||
$person,
|
||||
'You are not allowed to update this person'
|
||||
);
|
||||
|
||||
$form = $this->createForm(
|
||||
AccompanyingPeriodType::class,
|
||||
$accompanyingPeriod,
|
||||
[
|
||||
'period_action' => 'update',
|
||||
'center' => $person->getCenter(),
|
||||
]
|
||||
);
|
||||
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$form->handleRequest($request);
|
||||
$errors = $this->_validatePerson($person);
|
||||
$flashBag = $this->get('session')->getFlashBag();
|
||||
|
||||
if ($form->isValid(['Default', 'closed'])
|
||||
&& count($errors) === 0) {
|
||||
$em->flush();
|
||||
|
||||
$flashBag->add(
|
||||
'success',
|
||||
$this->get('translator')->trans('An accompanying period has been updated.')
|
||||
);
|
||||
|
||||
return $this->redirect(
|
||||
$this->generateUrl('chill_person_accompanying_period_list', [
|
||||
'person_id' => $person->getId(),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
$flashBag->add('error', $this->get('translator')
|
||||
->trans('Error when updating the period'));
|
||||
|
||||
foreach ($errors as $error) {
|
||||
$flashBag->add('info', $error->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
'person' => $person,
|
||||
'accompanying_period' => $accompanyingPeriod,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||
*/
|
||||
private function _getPerson(int $id) : Person
|
||||
private function _getPerson(int $id): Person
|
||||
{
|
||||
$person = $this->getDoctrine()->getManager()
|
||||
->getRepository('ChillPersonBundle:Person')->find($id);
|
||||
|
||||
if ($person === null) {
|
||||
if (null === $person) {
|
||||
throw $this->createNotFoundException('Person not found');
|
||||
}
|
||||
|
||||
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person,
|
||||
"You are not allowed to see this person");
|
||||
$this->denyAccessUnlessGranted(
|
||||
PersonVoter::SEE,
|
||||
$person,
|
||||
'You are not allowed to see this person'
|
||||
);
|
||||
|
||||
return $person;
|
||||
}
|
||||
|
||||
private function _validatePerson(Person $person): ConstraintViolationListInterface
|
||||
{
|
||||
$errors = $this->validator->validate(
|
||||
$person,
|
||||
null,
|
||||
['Default']
|
||||
);
|
||||
|
||||
// Can be disabled with config
|
||||
if (false === $this->container->getParameter('chill_person.allow_multiple_simultaneous_accompanying_periods')) {
|
||||
$errors_accompanying_period = $this->validator->validate(
|
||||
$person,
|
||||
null,
|
||||
['accompanying_period_consistent']
|
||||
);
|
||||
|
||||
foreach ($errors_accompanying_period as $error) {
|
||||
$errors->add($error);
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user