refactor: Update source code based on PHP conventions.

This commit is contained in:
Pol Dellaiera
2021-04-26 15:45:05 +02:00
parent 6b2eda0f94
commit 054a28ecf4
766 changed files with 52425 additions and 53000 deletions

View File

@@ -1,63 +1,42 @@
<?php
/*
* Chill is a software for social workers
/**
* Chill is a software for social workers.
*
* Copyright (C) 2015, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* 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/>.
* @see https://www.champs-libres.coop/
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Controller;
use Chill\PersonBundle\Privacy\PrivacyEvent;
use Psr\Log\LoggerInterface;
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\PersonType;
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\Security\Core\Role\Role;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\PersonBundle\Search\SimilarPersonMatcher;
use Symfony\Component\Translation\TranslatorInterface;
use Chill\MainBundle\Search\SearchProvider;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Class PersonController
*
* @package Chill\PersonBundle\Controller
*/
use function count;
class PersonController extends AbstractController
{
/**
*
* @var SimilarPersonMatcher
* @var ConfigPersonAltNamesHelper
*/
protected $similarPersonMatcher;
/**
*
* @var TranslatorInterface
*/
protected $translator;
protected $configPersonAltNameHelper;
/**
* @var EventDispatcherInterface
@@ -65,28 +44,31 @@ class PersonController extends AbstractController
protected $eventDispatcher;
/**
*
* @var PersonRepository;
*/
protected $personRepository;
/**
*
* @var ConfigPersonAltNamesHelper
* @var SimilarPersonMatcher
*/
protected $configPersonAltNameHelper;
protected $similarPersonMatcher;
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
/**
* @var ValidatorInterface
*/
private $validator;
public function __construct(
public function __construct(
SimilarPersonMatcher $similarPersonMatcher,
TranslatorInterface $translator,
EventDispatcherInterface $eventDispatcher,
@@ -102,147 +84,292 @@ class PersonController extends AbstractController
$this->personRepository = $personRepository;
$this->logger = $logger;
$this->validator = $validator;
}
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 viewAction($person_id)
public function createAction(Request $request)
{
$person = $this->_getPerson($person_id);
if ($request->getMethod() !== 'POST') {
$r = new Response('You must send something to create a person !');
$r->setStatusCode(400);
if ($person === null) {
throw $this->createNotFoundException("Person with id $person_id not"
. " found on this server");
return $r;
}
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person,
"You are not allowed to see this person.");
$form = $this->createForm(CreationPersonType::class, null, [
'form_status' => CreationPersonType::FORM_REVIEWED,
]);
$event = new PrivacyEvent($person);
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
$form->handleRequest($request);
return $this->render('ChillPersonBundle:Person:view.html.twig',
array(
"person" => $person,
"cFGroup" => $this->getCFGroup(),
"alt_names" => $this->configPersonAltNameHelper->getChoices(),
$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 ($person === null) {
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,
array(
"action" => $this->generateUrl('chill_person_general_update',
array("person_id" => $person_id)),
"cFGroup" => $this->getCFGroup()
)
$this->denyAccessUnlessGranted(
'CHILL_PERSON_UPDATE',
$person,
'You are not allowed to edit this person'
);
return $this->render('ChillPersonBundle:Person:edit.html.twig',
array('person' => $person, 'form' => $form->createView()));
$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 updateAction($person_id, Request $request)
public function getCFGroup()
{
$person = $this->_getPerson($person_id);
$cFGroup = null;
if ($person === null) {
return $this->createNotFoundException();
$em = $this->getDoctrine()->getManager();
$cFDefaultGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsDefaultGroup')
->findOneByEntity('Chill\\PersonBundle\\Entity\\Person');
if ($cFDefaultGroup) {
$cFGroup = $cFDefaultGroup->getCustomFieldsGroup();
}
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
'You are not allowed to edit this person');
$form = $this->createForm(PersonType::class, $person,
array("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',
array('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', array(
'person_id' => $person->getId()
));
return $this->redirect($url);
}
return $cFGroup;
}
public function newAction()
{
// this is a dummy default center.
$defaultCenter = $this->get('security.token_storage')
->getToken()
->getUser()
->getGroupCenters()[0]
->getCenter();
->getToken()
->getUser()
->getGroupCenters()[0]
->getCenter();
$person = (new Person(new \DateTime('now')))
->setCenter($defaultCenter);
$person = (new Person(new DateTime('now')))
->setCenter($defaultCenter);
$form = $this->createForm(
CreationPersonType::class,
$person,
array(
'action' => $this->generateUrl('chill_person_review'),
'form_status' => CreationPersonType::FORM_NOT_REVIEWED
));
CreationPersonType::class,
$person,
[
'action' => $this->generateUrl('chill_person_review'),
'form_status' => CreationPersonType::FORM_NOT_REVIEWED,
]
);
return $this->_renderNewForm($form);
}
private function _renderNewForm($form)
public function reviewAction(Request $request)
{
return $this->render('ChillPersonBundle:Person:create.html.twig',
array(
'form' => $form->createView()
));
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)
@@ -268,14 +395,34 @@ class PersonController extends AbstractController
}
/**
* easy getting a person by his id.
*
* @param \Chill\PersonBundle\Entity\Person $person
* @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, array('creation'));
->validate($person, null, ['creation']);
//validate accompanying periods
$periods = $person->getAccompanyingPeriods();
@@ -285,139 +432,11 @@ class PersonController extends AbstractController
->validate($period);
//group errors :
foreach($period_errors as $error) {
foreach ($period_errors as $error) {
$errors->add($error);
}
}
return $errors;
}
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(),
array(
'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,
array(
'action' => $this->generateUrl('chill_person_review'),
'form_status' => CreationPersonType::FORM_NOT_REVIEWED
));
$form->handleRequest($request);
return $this->_renderNewForm($form);
} else {
$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',
array('%nb%' => count($alternatePersons)))
);
return $this->render('ChillPersonBundle:Person:create_review.html.twig',
array(
'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 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, array(
'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',
array('person_id' => $person->getId())));
} else {
$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;
}
}
/**
* easy getting a person by his id
* @return \Chill\PersonBundle\Entity\Person
*/
private function _getPerson($id)
{
$person = $this->personRepository->find($id);
return $person;
}
}