fix deprecations: pass Request in the Action function instead of using getRequest method

This commit is contained in:
nobohan 2018-04-06 08:27:13 +02:00
parent 80bef1f558
commit 73f5295f40
2 changed files with 94 additions and 104 deletions

View File

@ -42,7 +42,7 @@ class AccompanyingPeriodController extends Controller
'person' => $person)); 'person' => $person));
} }
public function createAction($person_id) { public function createAction($person_id, Request $request) {
$person = $this->_getPerson($person_id); $person = $this->_getPerson($person_id);
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person,
@ -57,8 +57,6 @@ class AccompanyingPeriodController extends Controller
$form = $this->createForm(AccompanyingPeriodType::class, $form = $this->createForm(AccompanyingPeriodType::class,
$accompanyingPeriod, array('period_action' => 'create')); $accompanyingPeriod, array('period_action' => 'create'));
$request = $this->getRequest();
if ($request->getMethod() === 'POST') { if ($request->getMethod() === 'POST') {
$form->handleRequest($request); $form->handleRequest($request);
$errors = $this->_validatePerson($person); $errors = $this->_validatePerson($person);
@ -95,7 +93,7 @@ class AccompanyingPeriodController extends Controller
); );
} }
public function updateAction($person_id, $period_id){ public function updateAction($person_id, $period_id, Request $request){
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$accompanyingPeriod = $em->getRepository('ChillPersonBundle:AccompanyingPeriod') $accompanyingPeriod = $em->getRepository('ChillPersonBundle:AccompanyingPeriod')
@ -113,7 +111,6 @@ class AccompanyingPeriodController extends Controller
$form = $this->createForm(AccompanyingPeriodType::class, $form = $this->createForm(AccompanyingPeriodType::class,
$accompanyingPeriod, array('period_action' => 'update')); $accompanyingPeriod, array('period_action' => 'update'));
$request = $this->getRequest();
if ($request->getMethod() === 'POST') { if ($request->getMethod() === 'POST') {
$form->handleRequest($request); $form->handleRequest($request);
@ -148,7 +145,7 @@ class AccompanyingPeriodController extends Controller
) ); ) );
} }
public function closeAction($person_id) { public function closeAction($person_id, Request $request) {
$person = $this->_getPerson($person_id); $person = $this->_getPerson($person_id);
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person,
@ -171,8 +168,6 @@ class AccompanyingPeriodController extends Controller
'period_action' => 'close' 'period_action' => 'close'
)); ));
$request = $this->getRequest();
if ($request->getMethod() === 'POST') { if ($request->getMethod() === 'POST') {
$form->handleRequest($request); $form->handleRequest($request);
@ -244,14 +239,12 @@ class AccompanyingPeriodController extends Controller
} }
public function openAction($person_id) { public function openAction($person_id, Request $request) {
$person = $this->_getPerson($person_id); $person = $this->_getPerson($person_id);
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person,
'You are not allowed to update this person'); 'You are not allowed to update this person');
$request = $this->getRequest();
//in case the person is already open //in case the person is already open
if ($person->isOpen()) { if ($person->isOpen()) {
$this->get('session')->getFlashBag() $this->get('session')->getFlashBag()

View File

@ -3,7 +3,7 @@
/* /*
* Chill is a software for social workers * Chill is a software for social workers
* *
* Copyright (C) 2015, Champs Libres Cooperative SCRLFS, * Copyright (C) 2015, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop> * <http://www.champs-libres.coop>, <info@champs-libres.coop>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -49,33 +49,33 @@ class PersonController extends Controller
} }
public function viewAction($person_id) public function viewAction($person_id)
{ {
$person = $this->_getPerson($person_id); $person = $this->_getPerson($person_id);
if ($person === null) { if ($person === null) {
throw $this->createNotFoundException("Person with id $person_id not" throw $this->createNotFoundException("Person with id $person_id not"
. " found on this server"); . " found on this server");
} }
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person, $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person,
"You are not allowed to see this person."); "You are not allowed to see this person.");
return $this->render('ChillPersonBundle:Person:view.html.twig', return $this->render('ChillPersonBundle:Person:view.html.twig',
array("person" => $person, array("person" => $person,
"cFGroup" => $this->getCFGroup())); "cFGroup" => $this->getCFGroup()));
} }
public function editAction($person_id) public function editAction($person_id)
{ {
$person = $this->_getPerson($person_id); $person = $this->_getPerson($person_id);
if ($person === null) { if ($person === null) {
return $this->createNotFoundException(); return $this->createNotFoundException();
} }
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person, $this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
'You are not allowed to edit this person'); 'You are not allowed to edit this person');
$form = $this->createForm(PersonType::class, $person, $form = $this->createForm(PersonType::class, $person,
array( array(
"action" => $this->generateUrl('chill_person_general_update', "action" => $this->generateUrl('chill_person_general_update',
@ -83,88 +83,88 @@ class PersonController extends Controller
"cFGroup" => $this->getCFGroup() "cFGroup" => $this->getCFGroup()
) )
); );
return $this->render('ChillPersonBundle:Person:edit.html.twig', return $this->render('ChillPersonBundle:Person:edit.html.twig',
array('person' => $person, 'form' => $form->createView())); array('person' => $person, 'form' => $form->createView()));
} }
public function updateAction($person_id, Request $request) public function updateAction($person_id, Request $request)
{ {
$person = $this->_getPerson($person_id); $person = $this->_getPerson($person_id);
if ($person === null) { if ($person === null) {
return $this->createNotFoundException(); return $this->createNotFoundException();
} }
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person, $this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
'You are not allowed to edit this person'); 'You are not allowed to edit this person');
$form = $this->createForm(PersonType::class, $person, $form = $this->createForm(PersonType::class, $person,
array("cFGroup" => $this->getCFGroup())); array("cFGroup" => $this->getCFGroup()));
if ($request->getMethod() === 'POST') { if ($request->getMethod() === 'POST') {
$form->handleRequest($request); $form->handleRequest($request);
if ( ! $form->isValid() ) { if ( ! $form->isValid() ) {
$this->get('session') $this->get('session')
->getFlashBag()->add('error', 'Thp person data provided' ->getFlashBag()->add('error', 'Thp person data provided'
. ' are not valid'); . ' are not valid');
return $this->render('ChillPersonBundle:Person:edit.html.twig', return $this->render('ChillPersonBundle:Person:edit.html.twig',
array('person' => $person, array('person' => $person,
'form' => $form->createView())); 'form' => $form->createView()));
} }
$this->get('session')->getFlashBag() $this->get('session')->getFlashBag()
->add('success', ->add('success',
$this->get('translator') $this->get('translator')
->trans('The person data has been updated') ->trans('The person data has been updated')
); );
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->flush(); $em->flush();
$url = $this->generateUrl('chill_person_view', array( $url = $this->generateUrl('chill_person_view', array(
'person_id' => $person->getId() 'person_id' => $person->getId()
)); ));
return $this->redirect($url); return $this->redirect($url);
} }
} }
public function newAction() public function newAction()
{ {
// this is a dummy default center. // this is a dummy default center.
$defaultCenter = $this->get('security.token_storage') $defaultCenter = $this->get('security.token_storage')
->getToken() ->getToken()
->getUser() ->getUser()
->getGroupCenters()[0] ->getGroupCenters()[0]
->getCenter(); ->getCenter();
$person = (new Person(new \DateTime('now'))) $person = (new Person(new \DateTime('now')))
->setCenter($defaultCenter); ->setCenter($defaultCenter);
$form = $this->createForm( $form = $this->createForm(
CreationPersonType::NAME, CreationPersonType::NAME,
$person, $person,
array( array(
'action' => $this->generateUrl('chill_person_review'), 'action' => $this->generateUrl('chill_person_review'),
'form_status' => CreationPersonType::FORM_NOT_REVIEWED 'form_status' => CreationPersonType::FORM_NOT_REVIEWED
)); ));
return $this->_renderNewForm($form); return $this->_renderNewForm($form);
} }
private function _renderNewForm($form) private function _renderNewForm($form)
{ {
return $this->render('ChillPersonBundle:Person:create.html.twig', return $this->render('ChillPersonBundle:Person:create.html.twig',
array( array(
'form' => $form->createView() 'form' => $form->createView()
)); ));
} }
/** /**
* *
* @param type $form * @param type $form
* @return \Chill\PersonBundle\Entity\Person * @return \Chill\PersonBundle\Entity\Person
*/ */
@ -174,24 +174,24 @@ class PersonController extends Controller
* @var Person * @var Person
*/ */
$person = $form->getData(); $person = $form->getData();
$periods = $person->getAccompanyingPeriodsOrdered(); $periods = $person->getAccompanyingPeriodsOrdered();
$period = $periods[0]; $period = $periods[0];
$period->setOpeningDate($form['creation_date']->getData()); $period->setOpeningDate($form['creation_date']->getData());
// $person = new Person($form['creation_date']->getData()); // $person = new Person($form['creation_date']->getData());
// //
// $person->setFirstName($form['firstName']->getData()) // $person->setFirstName($form['firstName']->getData())
// ->setLastName($form['lastName']->getData()) // ->setLastName($form['lastName']->getData())
// ->setGender($form['gender']->getData()) // ->setGender($form['gender']->getData())
// ->setBirthdate($form['birthdate']->getData()) // ->setBirthdate($form['birthdate']->getData())
// ->setCenter($form['center']->getData()) // ->setCenter($form['center']->getData())
// ; // ;
return $person; return $person;
} }
/** /**
* *
* @param \Chill\PersonBundle\Entity\Person $person * @param \Chill\PersonBundle\Entity\Person $person
* @return \Symfony\Component\Validator\ConstraintViolationListInterface * @return \Symfony\Component\Validator\ConstraintViolationListInterface
*/ */
@ -199,78 +199,76 @@ class PersonController extends Controller
{ {
$errors = $this->get('validator') $errors = $this->get('validator')
->validate($person, array('creation')); ->validate($person, array('creation'));
//validate accompanying periods //validate accompanying periods
$periods = $person->getAccompanyingPeriods(); $periods = $person->getAccompanyingPeriods();
foreach ($periods as $period) { foreach ($periods as $period) {
$period_errors = $this->get('validator') $period_errors = $this->get('validator')
->validate($period); ->validate($period);
//group errors : //group errors :
foreach($period_errors as $error) { foreach($period_errors as $error) {
$errors->add($error); $errors->add($error);
} }
} }
return $errors; return $errors;
} }
public function reviewAction() public function reviewAction(Request $request)
{ {
$request = $this->getRequest();
if ($request->getMethod() !== 'POST') { if ($request->getMethod() !== 'POST') {
$r = new Response("You must send something to review the creation of a new Person"); $r = new Response("You must send something to review the creation of a new Person");
$r->setStatusCode(400); $r->setStatusCode(400);
return $r; return $r;
} }
$form = $this->createForm( $form = $this->createForm(
CreationPersonType::NAME, CreationPersonType::NAME,
new Person(), new Person(),
array( array(
'action' => $this->generateUrl('chill_person_create'), 'action' => $this->generateUrl('chill_person_create'),
'form_status' => CreationPersonType::FORM_BEING_REVIEWED 'form_status' => CreationPersonType::FORM_BEING_REVIEWED
)); ));
$form->handleRequest($request); $form->handleRequest($request);
$person = $this->_bindCreationForm($form); $person = $this->_bindCreationForm($form);
$errors = $this->_validatePersonAndAccompanyingPeriod($person); $errors = $this->_validatePersonAndAccompanyingPeriod($person);
$this->get('logger')->info(sprintf('Person created with %d errors ', count($errors))); $this->get('logger')->info(sprintf('Person created with %d errors ', count($errors)));
if ($errors->count() > 0) { if ($errors->count() > 0) {
$this->get('logger')->info('The created person has errors'); $this->get('logger')->info('The created person has errors');
$flashBag = $this->get('session')->getFlashBag(); $flashBag = $this->get('session')->getFlashBag();
$translator = $this->get('translator'); $translator = $this->get('translator');
$flashBag->add('error', $translator->trans('The person data are not valid')); $flashBag->add('error', $translator->trans('The person data are not valid'));
foreach($errors as $error) { foreach($errors as $error) {
$flashBag->add('info', $error->getMessage()); $flashBag->add('info', $error->getMessage());
} }
$form = $this->createForm( $form = $this->createForm(
CreationPersonType::NAME, CreationPersonType::NAME,
new Person(), new Person(),
array( array(
'action' => $this->generateUrl('chill_person_review'), 'action' => $this->generateUrl('chill_person_review'),
'form_status' => CreationPersonType::FORM_NOT_REVIEWED 'form_status' => CreationPersonType::FORM_NOT_REVIEWED
)); ));
$form->handleRequest($request); $form->handleRequest($request);
return $this->_renderNewForm($form); return $this->_renderNewForm($form);
} else { } else {
$this->get('logger')->info('Person created without errors'); $this->get('logger')->info('Person created without errors');
} }
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$query = $em->createQuery(); $query = $em->createQuery();
$dql = 'SELECT p from ChillPersonBundle:Person p WHERE ' $dql = 'SELECT p from ChillPersonBundle:Person p WHERE '
. 'LOWER(p.firstName) LIKE LOWER(:firstName)' . 'LOWER(p.firstName) LIKE LOWER(:firstName)'
. ' OR LOWER(p.lastName) LIKE LOWER(:lastName)' . ' OR LOWER(p.lastName) LIKE LOWER(:lastName)'
@ -288,10 +286,10 @@ class PersonController extends Controller
// add authorized centers // add authorized centers
$centers = $this->get('chill.main.security.authorization.helper') $centers = $this->get('chill.main.security.authorization.helper')
->getReachableCenters($this->getUser(), new Role(PersonVoter::SEE)); ->getReachableCenters($this->getUser(), new Role(PersonVoter::SEE));
$dql.=' and p.center IN (:centers) '; $dql.=' and p.center IN (:centers) ';
$query->setParameter('centers', $centers); $query->setParameter('centers', $centers);
// run query // run query
$query->setDql($dql); $query->setDql($dql);
@ -300,13 +298,13 @@ class PersonController extends Controller
if (count($alternatePersons) === 0) { if (count($alternatePersons) === 0) {
return $this->forward('ChillPersonBundle:Person:create'); return $this->forward('ChillPersonBundle:Person:create');
} }
$this->get('session')->getFlashBag()->add('info', $this->get('session')->getFlashBag()->add('info',
$this->get('translator')->trans( $this->get('translator')->trans(
'%nb% person with similar name. Please verify that this is a new person', '%nb% person with similar name. Please verify that this is a new person',
array('%nb%' => count($alternatePersons))) array('%nb%' => count($alternatePersons)))
); );
return $this->render('ChillPersonBundle:Person:create_review.html.twig', return $this->render('ChillPersonBundle:Person:create_review.html.twig',
array('alternatePersons' => $alternatePersons, array('alternatePersons' => $alternatePersons,
'firstName' => $form['firstName']->getData(), 'firstName' => $form['firstName']->getData(),
@ -316,38 +314,37 @@ class PersonController extends Controller
'creation_date' => $form['creation_date']->getData(), 'creation_date' => $form['creation_date']->getData(),
'form' => $form->createView())); 'form' => $form->createView()));
} }
public function createAction() public function createAction(Request $request)
{ {
$request = $this->getRequest();
if ($request->getMethod() !== 'POST') { if ($request->getMethod() !== 'POST') {
$r = new Response('You must send something to create a person !'); $r = new Response('You must send something to create a person !');
$r->setStatusCode(400); $r->setStatusCode(400);
return $r; return $r;
} }
$form = $this->createForm(CreationPersonType::NAME, null, array( $form = $this->createForm(CreationPersonType::NAME, null, array(
'form_status' => CreationPersonType::FORM_REVIEWED 'form_status' => CreationPersonType::FORM_REVIEWED
)); ));
$form->handleRequest($request); $form->handleRequest($request);
$person = $this->_bindCreationForm($form); $person = $this->_bindCreationForm($form);
$errors = $this->_validatePersonAndAccompanyingPeriod($person); $errors = $this->_validatePersonAndAccompanyingPeriod($person);
$this->denyAccessUnlessGranted('CHILL_PERSON_CREATE', $person, $this->denyAccessUnlessGranted('CHILL_PERSON_CREATE', $person,
'You are not allowed to create this person'); 'You are not allowed to create this person');
if ($errors->count() === 0) { if ($errors->count() === 0) {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->persist($person); $em->persist($person);
$em->flush(); $em->flush();
return $this->redirect($this->generateUrl('chill_person_general_edit', return $this->redirect($this->generateUrl('chill_person_general_edit',
array('person_id' => $person->getId()))); array('person_id' => $person->getId())));
} else { } else {
$text = "this should not happen if you reviewed your submission\n"; $text = "this should not happen if you reviewed your submission\n";
@ -359,7 +356,7 @@ class PersonController extends Controller
return $r; return $r;
} }
} }
/** /**
* easy getting a person by his id * easy getting a person by his id
* @return \Chill\PersonBundle\Entity\Person * @return \Chill\PersonBundle\Entity\Person
@ -367,10 +364,10 @@ class PersonController extends Controller
private function _getPerson($id) private function _getPerson($id)
{ {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person') $person = $em->getRepository('ChillPersonBundle:Person')
->find($id); ->find($id);
return $person; return $person;
} }
} }