cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,65 +1,46 @@
<?php
/*
/**
* Chill is a software for social workers
*
* Copyright (C) 2015, 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\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 Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Role\Role;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\PersonBundle\Search\SimilarPersonMatcher;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Translation\TranslatorInterface;
use Chill\MainBundle\Search\SearchProvider;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
use Chill\PersonBundle\Repository\PersonNotDuplicateRepository;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use function hash;
use function implode;
use function in_array;
use function is_array;
final class PersonController extends AbstractController
{
/**
* @var SimilarPersonMatcher
* @var ConfigPersonAltNamesHelper
*/
protected $similarPersonMatcher;
/**
* @var TranslatorInterface
*/
protected $translator;
protected $configPersonAltNameHelper;
/**
* @var EventDispatcherInterface
@@ -72,9 +53,14 @@ final class PersonController extends AbstractController
protected $personRepository;
/**
* @var ConfigPersonAltNamesHelper
* @var SimilarPersonMatcher
*/
protected $configPersonAltNameHelper;
protected $similarPersonMatcher;
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* @var EntityManagerInterface
@@ -111,60 +97,28 @@ final class PersonController extends AbstractController
$this->validator = $validator;
$this->em = $em;
$this->security = $security;
}
public function getCFGroup()
{
$cFGroup = null;
$cFDefaultGroup = $this->em->getRepository("ChillCustomFieldsBundle:CustomFieldsDefaultGroup")
->findOneByEntity("Chill\PersonBundle\Entity\Person");
if($cFDefaultGroup) {
$cFGroup = $cFDefaultGroup->getCustomFieldsGroup();
}
return $cFGroup;
}
public function viewAction($person_id)
{
$person = $this->_getPerson($person_id);
if ($person === null) {
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',
array(
"person" => $person,
"cFGroup" => $this->getCFGroup(),
"alt_names" => $this->configPersonAltNameHelper->getChoices(),
));
}
public function editAction($person_id, Request $request)
{
$person = $this->_getPerson($person_id);
if ($person === null) {
if (null === $person) {
throw $this->createNotFoundException();
}
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
'You are not allowed to edit this person');
$this->denyAccessUnlessGranted(
'CHILL_PERSON_UPDATE',
$person,
'You are not allowed to edit this person'
);
$form = $this->createForm(PersonType::class, $person,
array(
"cFGroup" => $this->getCFGroup()
)
$form = $this->createForm(
PersonType::class,
$person,
[
'cFGroup' => $this->getCFGroup(),
]
);
$form->handleRequest($request);
@@ -172,28 +126,71 @@ final class PersonController extends AbstractController
if ($form->isSubmitted() && !$form->isValid()) {
$this->get('session')
->getFlashBag()->add('error', $this->translator
->trans('This form contains errors'));
->trans('This form contains errors'));
} elseif ($form->isSubmitted() && $form->isValid()) {
$this->get('session')->getFlashBag()
->add('success',
->add(
'success',
$this->get('translator')
->trans('The person data has been updated')
);
$this->em->flush();
return $this->redirectToRoute('chill_person_view', [
'person_id' => $person->getId()
return $this->redirectToRoute('chill_person_view', [
'person_id' => $person->getId(),
]);
}
return $this->render(
'ChillPersonBundle:Person:edit.html.twig',
['person' => $person, 'form' => $form->createView()]
);
}
return $this->render('ChillPersonBundle:Person:edit.html.twig',
array('person' => $person, 'form' => $form->createView()));
public function getCFGroup()
{
$cFGroup = null;
$cFDefaultGroup = $this->em->getRepository('ChillCustomFieldsBundle:CustomFieldsDefaultGroup')
->findOneByEntity('Chill\\PersonBundle\\Entity\\Person');
if ($cFDefaultGroup) {
$cFGroup = $cFDefaultGroup->getCustomFieldsGroup();
}
return $cFGroup;
}
/**
* Method for creating a new person
* @Route(
* "/{_locale}/person/household/{person_id}/history",
* name="chill_person_household_person_history",
* methods={"GET", "POST"}
* )
* @ParamConverter("person", options={"id": "person_id"})
*/
public function householdHistoryByPerson(Request $request, Person $person): Response
{
$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(
'@ChillPerson/Person/household_history.html.twig',
[
'person' => $person,
]
);
}
/**
* Method for creating a new person.
*
*The controller register data from a previous post on the form, and
* register it in the session.
@@ -201,15 +198,14 @@ final class PersonController extends AbstractController
* The next post compare the data with previous one and, if yes, show a
* review page if there are "alternate persons".
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
* @return Response|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function newAction(Request $request)
{
$person = new Person();
if (1 === count($this->security->getUser()
->getGroupCenters())) {
->getGroupCenters())) {
$person->setCenter(
$this->security->getUser()
->getGroupCenters()[0]
@@ -218,13 +214,13 @@ final class PersonController extends AbstractController
}
$form = $this->createForm(CreationPersonType::class, $person, [
'validation_groups' => ['create']
'validation_groups' => ['create'],
])->add('editPerson', SubmitType::class, [
'label' => 'Add the person'
'label' => 'Add the person',
])->add('createPeriod', SubmitType::class, [
'label' => 'Add the person and create an accompanying period'
'label' => 'Add the person and create an accompanying period',
])->add('createHousehold', SubmitType::class, [
'label' => 'Add the person and create an household'
'label' => 'Add the person and create an household',
]); // TODO createHousehold form action
$form->handleRequest($request);
@@ -233,14 +229,12 @@ final class PersonController extends AbstractController
$this->lastPostDataReset();
} elseif ($request->getMethod() === Request::METHOD_POST
&& $form->isValid()) {
$alternatePersons = $this->similarPersonMatcher
->matchPerson($person);
if (
FALSE === $this->isLastPostDataChanges($form, $request, true)
||
count($alternatePersons) === 0
false === $this->isLastPostDataChanges($form, $request, true)
|| count($alternatePersons) === 0
) {
$this->em->persist($person);
@@ -249,84 +243,75 @@ final class PersonController extends AbstractController
if ($form->get('createPeriod')->isClicked()) {
return $this->redirectToRoute('chill_person_accompanying_course_new', [
'person_id' => [ $person->getId() ]
'person_id' => [$person->getId()],
]);
}
return $this->redirectToRoute('chill_person_general_edit',
['person_id' => $person->getId()]);
return $this->redirectToRoute(
'chill_person_general_edit',
['person_id' => $person->getId()]
);
}
} elseif ($request->getMethod() === Request::METHOD_POST && !$form->isValid()) {
$this->addFlash('error', $this->translator->trans('This form contains errors'));
}
return $this->render('@ChillPerson/Person/create.html.twig',
return $this->render(
'@ChillPerson/Person/create.html.twig',
[
'form' => $form->createView(),
'alternatePersons' => $alternatePersons ?? []
'alternatePersons' => $alternatePersons ?? [],
]
);
}
private function isLastPostDataChanges(Form $form, Request $request, bool $replace = false): bool
public function viewAction($person_id)
{
/** @var SessionInterface $session */
$session = $this->get('session');
if (!$session->has('last_person_data')) {
return true;
$person = $this->_getPerson($person_id);
if (null === $person) {
throw $this->createNotFoundException("Person with id {$person_id} not"
. ' found on this server');
}
$newPost = $this->lastPostDataBuildHash($form, $request);
$this->denyAccessUnlessGranted(
'CHILL_PERSON_SEE',
$person,
'You are not allowed to see this person.'
);
$isChanged = $newPost !== $session->get('last_person_data');
$event = new PrivacyEvent($person);
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
if ($replace) {
$session->set('last_person_data', $newPost);
}
return $isChanged ;
}
private function lastPostDataReset(): void
{
$this->get('session')->set('last_person_data', "");
return $this->render(
'ChillPersonBundle:Person:view.html.twig',
[
'person' => $person,
'cFGroup' => $this->getCFGroup(),
'alt_names' => $this->configPersonAltNameHelper->getChoices(),
]
);
}
/**
* build the hash for posted data
* easy getting a person by his id.
*
* For privacy reasons, the data are hashed using sha512
* @param mixed $id
*
* @param Form $form
* @param Request $request
* @return string
* @return \Chill\PersonBundle\Entity\Person
*/
private function lastPostDataBuildHash(Form $form, Request $request): string
private function _getPerson($id)
{
$fields = [];
$ignoredFields = ['form_status', '_token'];
foreach ($request->request->all()[$form->getName()] as $field => $value) {
if (\in_array($field, $ignoredFields)) {
continue;
}
$fields[$field] = \is_array($value) ?
\implode(",", $value) : $value;
}
ksort($fields);
return \hash('sha512', \implode("&", $fields));
return $this->personRepository->find($id);
}
/**
*
* @param \Chill\PersonBundle\Entity\Person $person
* @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();
@@ -336,7 +321,7 @@ final class PersonController extends AbstractController
->validate($period);
//group errors :
foreach($period_errors as $error) {
foreach ($period_errors as $error) {
$errors->add($error);
}
}
@@ -344,39 +329,50 @@ final class PersonController extends AbstractController
return $errors;
}
/**
* easy getting a person by his id
* @return \Chill\PersonBundle\Entity\Person
*/
private function _getPerson($id)
private function isLastPostDataChanges(Form $form, Request $request, bool $replace = false): bool
{
$person = $this->personRepository->find($id);
/** @var SessionInterface $session */
$session = $this->get('session');
return $person;
if (!$session->has('last_person_data')) {
return true;
}
$newPost = $this->lastPostDataBuildHash($form, $request);
$isChanged = $session->get('last_person_data') !== $newPost;
if ($replace) {
$session->set('last_person_data', $newPost);
}
return $isChanged;
}
/**
* build the hash for posted data.
*
* @Route(
* "/{_locale}/person/household/{person_id}/history",
* name="chill_person_household_person_history",
* methods={"GET", "POST"}
* )
* @ParamConverter("person", options={"id" = "person_id"})
* For privacy reasons, the data are hashed using sha512
*/
public function householdHistoryByPerson(Request $request, Person $person): Response
private function lastPostDataBuildHash(Form $form, Request $request): string
{
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person,
"You are not allowed to see this person.");
$fields = [];
$ignoredFields = ['form_status', '_token'];
$event = new PrivacyEvent($person);
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
foreach ($request->request->all()[$form->getName()] as $field => $value) {
if (in_array($field, $ignoredFields)) {
continue;
}
$fields[$field] = is_array($value) ?
implode(',', $value) : $value;
}
ksort($fields);
return $this->render(
'@ChillPerson/Person/household_history.html.twig',
[
'person' => $person
]
);
return hash('sha512', implode('&', $fields));
}
private function lastPostDataReset(): void
{
$this->get('session')->set('last_person_data', '');
}
}