mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-11-24 12:48:30 +00:00
443 lines
13 KiB
PHP
443 lines
13 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.
|
|
*
|
|
* @see https://www.champs-libres.coop/
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\Controller;
|
|
|
|
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Chill\PersonBundle\Form\CreationPersonType;
|
|
use Chill\PersonBundle\Form\PersonType;
|
|
use Chill\PersonBundle\Privacy\PrivacyEvent;
|
|
use Chill\PersonBundle\Repository\PersonRepository;
|
|
use Chill\PersonBundle\Search\SimilarPersonMatcher;
|
|
use DateTime;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Translation\TranslatorInterface;
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
use function count;
|
|
|
|
class PersonController extends AbstractController
|
|
{
|
|
/**
|
|
* @var ConfigPersonAltNamesHelper
|
|
*/
|
|
protected $configPersonAltNameHelper;
|
|
|
|
/**
|
|
* @var EventDispatcherInterface
|
|
*/
|
|
protected $eventDispatcher;
|
|
|
|
/**
|
|
* @var PersonRepository;
|
|
*/
|
|
protected $personRepository;
|
|
|
|
/**
|
|
* @var SimilarPersonMatcher
|
|
*/
|
|
protected $similarPersonMatcher;
|
|
|
|
/**
|
|
* @var TranslatorInterface
|
|
*/
|
|
protected $translator;
|
|
|
|
/**
|
|
* @var \Psr\Log\LoggerInterface
|
|
*/
|
|
private $logger;
|
|
|
|
/**
|
|
* @var ValidatorInterface
|
|
*/
|
|
private $validator;
|
|
|
|
public function __construct(
|
|
SimilarPersonMatcher $similarPersonMatcher,
|
|
TranslatorInterface $translator,
|
|
EventDispatcherInterface $eventDispatcher,
|
|
PersonRepository $personRepository,
|
|
ConfigPersonAltNamesHelper $configPersonAltNameHelper,
|
|
LoggerInterface $logger,
|
|
ValidatorInterface $validator
|
|
) {
|
|
$this->similarPersonMatcher = $similarPersonMatcher;
|
|
$this->translator = $translator;
|
|
$this->eventDispatcher = $eventDispatcher;
|
|
$this->configPersonAltNameHelper = $configPersonAltNameHelper;
|
|
$this->personRepository = $personRepository;
|
|
$this->logger = $logger;
|
|
$this->validator = $validator;
|
|
}
|
|
|
|
public function createAction(Request $request)
|
|
{
|
|
if ($request->getMethod() !== 'POST') {
|
|
$r = new Response('You must send something to create a person !');
|
|
$r->setStatusCode(400);
|
|
|
|
return $r;
|
|
}
|
|
|
|
$form = $this->createForm(CreationPersonType::class, null, [
|
|
'form_status' => CreationPersonType::FORM_REVIEWED,
|
|
]);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
$person = $this->_bindCreationForm($form);
|
|
|
|
$errors = $this->_validatePersonAndAccompanyingPeriod($person);
|
|
|
|
$this->denyAccessUnlessGranted(
|
|
'CHILL_PERSON_CREATE',
|
|
$person,
|
|
'You are not allowed to create this person'
|
|
);
|
|
|
|
if ($errors->count() === 0) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$em->persist($person);
|
|
|
|
$em->flush();
|
|
|
|
return $this->redirect($this->generateUrl(
|
|
'chill_person_general_edit',
|
|
['person_id' => $person->getId()]
|
|
));
|
|
}
|
|
$text = "this should not happen if you reviewed your submission\n";
|
|
|
|
foreach ($errors as $error) {
|
|
$text .= $error->getMessage() . "\n";
|
|
}
|
|
$r = new Response($text);
|
|
$r->setStatusCode(400);
|
|
|
|
return $r;
|
|
}
|
|
|
|
public function editAction($person_id)
|
|
{
|
|
$person = $this->_getPerson($person_id);
|
|
|
|
if (null === $person) {
|
|
return $this->createNotFoundException();
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted(
|
|
'CHILL_PERSON_UPDATE',
|
|
$person,
|
|
'You are not allowed to edit this person'
|
|
);
|
|
|
|
$form = $this->createForm(
|
|
PersonType::class,
|
|
$person,
|
|
[
|
|
'action' => $this->generateUrl(
|
|
'chill_person_general_update',
|
|
['person_id' => $person_id]
|
|
),
|
|
'cFGroup' => $this->getCFGroup(),
|
|
]
|
|
);
|
|
|
|
return $this->render(
|
|
'ChillPersonBundle:Person:edit.html.twig',
|
|
['person' => $person, 'form' => $form->createView()]
|
|
);
|
|
}
|
|
|
|
public function getCFGroup()
|
|
{
|
|
$cFGroup = null;
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
$cFDefaultGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsDefaultGroup')
|
|
->findOneByEntity('Chill\\PersonBundle\\Entity\\Person');
|
|
|
|
if ($cFDefaultGroup) {
|
|
$cFGroup = $cFDefaultGroup->getCustomFieldsGroup();
|
|
}
|
|
|
|
return $cFGroup;
|
|
}
|
|
|
|
public function newAction()
|
|
{
|
|
// this is a dummy default center.
|
|
$defaultCenter = $this->get('security.token_storage')
|
|
->getToken()
|
|
->getUser()
|
|
->getGroupCenters()[0]
|
|
->getCenter();
|
|
|
|
$person = (new Person(new DateTime('now')))
|
|
->setCenter($defaultCenter);
|
|
|
|
$form = $this->createForm(
|
|
CreationPersonType::class,
|
|
$person,
|
|
[
|
|
'action' => $this->generateUrl('chill_person_review'),
|
|
'form_status' => CreationPersonType::FORM_NOT_REVIEWED,
|
|
]
|
|
);
|
|
|
|
return $this->_renderNewForm($form);
|
|
}
|
|
|
|
public function reviewAction(Request $request)
|
|
{
|
|
if ($request->getMethod() !== 'POST') {
|
|
$r = new Response('You must send something to review the creation of a new Person');
|
|
$r->setStatusCode(400);
|
|
|
|
return $r;
|
|
}
|
|
|
|
$form = $this->createForm(
|
|
//CreationPersonType::NAME,
|
|
CreationPersonType::class,
|
|
new Person(),
|
|
[
|
|
'action' => $this->generateUrl('chill_person_create'),
|
|
'form_status' => CreationPersonType::FORM_BEING_REVIEWED,
|
|
]
|
|
);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
$person = $this->_bindCreationForm($form);
|
|
|
|
$errors = $this->_validatePersonAndAccompanyingPeriod($person);
|
|
$this->logger->info(sprintf('Person created with %d errors ', count($errors)));
|
|
|
|
if ($errors->count() > 0) {
|
|
$this->logger->info('The created person has errors');
|
|
$flashBag = $this->get('session')->getFlashBag();
|
|
$translator = $this->get('translator');
|
|
|
|
$flashBag->add('error', $translator->trans('The person data are not valid'));
|
|
|
|
foreach ($errors as $error) {
|
|
$flashBag->add('info', $error->getMessage());
|
|
}
|
|
|
|
$form = $this->createForm(
|
|
CreationPersonType::NAME,
|
|
$person,
|
|
[
|
|
'action' => $this->generateUrl('chill_person_review'),
|
|
'form_status' => CreationPersonType::FORM_NOT_REVIEWED,
|
|
]
|
|
);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
return $this->_renderNewForm($form);
|
|
}
|
|
$this->logger->info('Person created without errors');
|
|
|
|
$alternatePersons = $this->similarPersonMatcher
|
|
->matchPerson($person);
|
|
|
|
if (count($alternatePersons) === 0) {
|
|
return $this->forward('ChillPersonBundle:Person:create');
|
|
}
|
|
|
|
$this->get('session')->getFlashBag()->add(
|
|
'info',
|
|
$this->get('translator')->trans(
|
|
'%nb% person with similar name. Please verify that this is a new person',
|
|
['%nb%' => count($alternatePersons)]
|
|
)
|
|
);
|
|
|
|
return $this->render(
|
|
'ChillPersonBundle:Person:create_review.html.twig',
|
|
[
|
|
'person' => $person,
|
|
'alternatePersons' => $alternatePersons,
|
|
'firstName' => $form['firstName']->getData(),
|
|
'lastName' => $form['lastName']->getData(),
|
|
'birthdate' => $form['birthdate']->getData(),
|
|
'gender' => $form['gender']->getData(),
|
|
'creation_date' => $form['creation_date']->getData(),
|
|
'form' => $form->createView(), ]
|
|
);
|
|
}
|
|
|
|
public function updateAction($person_id, Request $request)
|
|
{
|
|
$person = $this->_getPerson($person_id);
|
|
|
|
if (null === $person) {
|
|
return $this->createNotFoundException();
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted(
|
|
'CHILL_PERSON_UPDATE',
|
|
$person,
|
|
'You are not allowed to edit this person'
|
|
);
|
|
|
|
$form = $this->createForm(
|
|
PersonType::class,
|
|
$person,
|
|
['cFGroup' => $this->getCFGroup()]
|
|
);
|
|
|
|
if ($request->getMethod() === 'POST') {
|
|
$form->handleRequest($request);
|
|
|
|
if (!$form->isValid()) {
|
|
$this->get('session')
|
|
->getFlashBag()->add('error', $this->translator
|
|
->trans('This form contains errors'));
|
|
|
|
return $this->render(
|
|
'ChillPersonBundle:Person:edit.html.twig',
|
|
['person' => $person,
|
|
'form' => $form->createView(), ]
|
|
);
|
|
}
|
|
|
|
$this->get('session')->getFlashBag()
|
|
->add(
|
|
'success',
|
|
$this->get('translator')
|
|
->trans('The person data has been updated')
|
|
);
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
$em->flush();
|
|
|
|
$url = $this->generateUrl('chill_person_view', [
|
|
'person_id' => $person->getId(),
|
|
]);
|
|
|
|
return $this->redirect($url);
|
|
}
|
|
}
|
|
|
|
public function viewAction($person_id)
|
|
{
|
|
$person = $this->_getPerson($person_id);
|
|
|
|
if (null === $person) {
|
|
throw $this->createNotFoundException("Person with id {$person_id} not"
|
|
. ' found on this server');
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted(
|
|
'CHILL_PERSON_SEE',
|
|
$person,
|
|
'You are not allowed to see this person.'
|
|
);
|
|
|
|
$event = new PrivacyEvent($person);
|
|
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
|
|
|
|
return $this->render(
|
|
'ChillPersonBundle:Person:view.html.twig',
|
|
[
|
|
'person' => $person,
|
|
'cFGroup' => $this->getCFGroup(),
|
|
'alt_names' => $this->configPersonAltNameHelper->getChoices(),
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param type $form
|
|
*
|
|
* @return \Chill\PersonBundle\Entity\Person
|
|
*/
|
|
private function _bindCreationForm($form)
|
|
{
|
|
/**
|
|
* @var Person
|
|
*/
|
|
$person = $form->getData();
|
|
|
|
$periods = $person->getAccompanyingPeriodsOrdered();
|
|
$period = $periods[0];
|
|
$period->setOpeningDate($form['creation_date']->getData());
|
|
// $person = new Person($form['creation_date']->getData());
|
|
//
|
|
// $person->setFirstName($form['firstName']->getData())
|
|
// ->setLastName($form['lastName']->getData())
|
|
// ->setGender($form['gender']->getData())
|
|
// ->setBirthdate($form['birthdate']->getData())
|
|
// ->setCenter($form['center']->getData())
|
|
// ;
|
|
|
|
return $person;
|
|
}
|
|
|
|
/**
|
|
* easy getting a person by his id.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return \Chill\PersonBundle\Entity\Person
|
|
*/
|
|
private function _getPerson($id)
|
|
{
|
|
return $this->personRepository->find($id);
|
|
}
|
|
|
|
private function _renderNewForm($form)
|
|
{
|
|
return $this->render(
|
|
'ChillPersonBundle:Person:create.html.twig',
|
|
[
|
|
'form' => $form->createView(),
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return \Symfony\Component\Validator\ConstraintViolationListInterface
|
|
*/
|
|
private function _validatePersonAndAccompanyingPeriod(Person $person)
|
|
{
|
|
$errors = $this->validator
|
|
->validate($person, null, ['creation']);
|
|
|
|
//validate accompanying periods
|
|
$periods = $person->getAccompanyingPeriods();
|
|
|
|
foreach ($periods as $period) {
|
|
$period_errors = $this->validator
|
|
->validate($period);
|
|
|
|
//group errors :
|
|
foreach ($period_errors as $error) {
|
|
$errors->add($error);
|
|
}
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
}
|