mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
fix double person creation + button for creating accompanying course on creation + simplification person create
The controller now register data from a previous post on the form, and register it in the session. The next post compare the data with previous one and, if yes, show a review page if there are "alternate persons.
This commit is contained in:
@@ -29,11 +29,16 @@ use Chill\PersonBundle\Form\PersonType;
|
||||
use Chill\PersonBundle\Form\CreationPersonType;
|
||||
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\Security\Core\Security;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Chill\MainBundle\Search\SearchProvider;
|
||||
use Chill\PersonBundle\Repository\PersonRepository;
|
||||
@@ -94,7 +99,8 @@ final class PersonController extends AbstractController
|
||||
ConfigPersonAltNamesHelper $configPersonAltNameHelper,
|
||||
LoggerInterface $logger,
|
||||
ValidatorInterface $validator,
|
||||
EntityManagerInterface $em
|
||||
EntityManagerInterface $em,
|
||||
Security $security
|
||||
) {
|
||||
$this->similarPersonMatcher = $similarPersonMatcher;
|
||||
$this->translator = $translator;
|
||||
@@ -104,6 +110,7 @@ final class PersonController extends AbstractController
|
||||
$this->logger = $logger;
|
||||
$this->validator = $validator;
|
||||
$this->em = $em;
|
||||
$this->security = $security;
|
||||
}
|
||||
|
||||
public function getCFGroup()
|
||||
@@ -209,11 +216,21 @@ final class PersonController extends AbstractController
|
||||
}
|
||||
}
|
||||
|
||||
public function newAction()
|
||||
/**
|
||||
* Method for creating a new person
|
||||
*
|
||||
*The controller register data from a previous post on the form, and
|
||||
* register it in the session.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
public function newAction(Request $request)
|
||||
{
|
||||
// this is a dummy default center.
|
||||
$defaultCenter = $this->get('security.token_storage')
|
||||
->getToken()
|
||||
$defaultCenter = $this->security
|
||||
->getUser()
|
||||
->getGroupCenters()[0]
|
||||
->getCenter();
|
||||
@@ -221,38 +238,103 @@ final class PersonController extends AbstractController
|
||||
$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
|
||||
));
|
||||
$form = $this->createForm(CreationPersonType::class, $person, [
|
||||
'validation_groups' => ['create']
|
||||
])->add('editPerson', SubmitType::class, [
|
||||
'label' => 'Add the person'
|
||||
])->add('createPeriod', SubmitType::class, [
|
||||
'label' => 'Add the person and create an accompanying period'
|
||||
]);
|
||||
|
||||
return $this->_renderNewForm($form);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($request->getMethod() === Request::METHOD_GET) {
|
||||
$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
|
||||
) {
|
||||
$this->em->persist($person);
|
||||
|
||||
$this->em->flush();
|
||||
$this->lastPostDataReset();
|
||||
|
||||
if ($form->get('createPeriod')->isClicked()) {
|
||||
return $this->redirectToRoute('chill_person_accompanying_course_new', [
|
||||
'person_id' => [ $person->getId() ]
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('chill_person_general_edit',
|
||||
['person_id' => $person->getId()]);
|
||||
}
|
||||
} elseif ($request->getMethod() === Request::METHOD_POST) {
|
||||
$this->addFlash('error', $this->translator->trans('This form contains errors'));
|
||||
}
|
||||
|
||||
return $this->render('@ChillPerson/Person/create.html.twig',
|
||||
[
|
||||
'form' => $form->createView(),
|
||||
'alternatePersons' => $alternatePersons ?? []
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function _renderNewForm($form)
|
||||
private function isLastPostDataChanges(Form $form, Request $request, bool $replace = false): bool
|
||||
{
|
||||
return $this->render('ChillPersonBundle:Person:create.html.twig',
|
||||
array(
|
||||
'form' => $form->createView()
|
||||
));
|
||||
/** @var SessionInterface $session */
|
||||
$session = $this->get('session');
|
||||
if (!$session->has('last_person_data')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$newPost = $this->lastPostDataBuildHash($form, $request);
|
||||
|
||||
$isChanged = $newPost !== $session->get('last_person_data');
|
||||
|
||||
if ($replace) {
|
||||
$session->set('last_person_data', $newPost);
|
||||
}
|
||||
|
||||
return $isChanged ;
|
||||
}
|
||||
|
||||
private function lastPostDataReset(): void
|
||||
{
|
||||
$this->get('session')->set('last_person_data', "");
|
||||
}
|
||||
|
||||
/**
|
||||
* build the hash for posted data
|
||||
*
|
||||
* @param type $form
|
||||
* @return \Chill\PersonBundle\Entity\Person
|
||||
* For privacy reasons, the data are hashed using sha512
|
||||
*
|
||||
* @param Form $form
|
||||
* @param Request $request
|
||||
* @return string
|
||||
*/
|
||||
private function _bindCreationForm($form)
|
||||
private function lastPostDataBuildHash(Form $form, Request $request): string
|
||||
{
|
||||
/**
|
||||
* @var Person
|
||||
*/
|
||||
$person = $form->getData();
|
||||
$fields = [];
|
||||
$ignoredFields = ['form_status', '_token'];
|
||||
|
||||
return $person;
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -281,120 +363,6 @@ final class PersonController extends AbstractController
|
||||
return $errors;
|
||||
}
|
||||
|
||||
public function reviewAction(Request $request, PersonNotDuplicateRepository $personNotDuplicateRepository)
|
||||
{
|
||||
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::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::class,
|
||||
$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');
|
||||
}
|
||||
|
||||
$this->em->persist($person);
|
||||
|
||||
$alternatePersons = $this->similarPersonMatcher->matchPerson($person, $personNotDuplicateRepository);
|
||||
|
||||
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(),
|
||||
'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) {
|
||||
$this->em->persist($person);
|
||||
|
||||
$this->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
|
||||
|
Reference in New Issue
Block a user