From 73f5295f40038fb74654e460aa4384e7d8ed3870 Mon Sep 17 00:00:00 2001 From: nobohan Date: Fri, 6 Apr 2018 08:27:13 +0200 Subject: [PATCH] fix deprecations: pass Request in the Action function instead of using getRequest method --- Controller/AccompanyingPeriodController.php | 15 +- Controller/PersonController.php | 183 ++++++++++---------- 2 files changed, 94 insertions(+), 104 deletions(-) diff --git a/Controller/AccompanyingPeriodController.php b/Controller/AccompanyingPeriodController.php index 0eec5cd29..b7d4241ab 100644 --- a/Controller/AccompanyingPeriodController.php +++ b/Controller/AccompanyingPeriodController.php @@ -42,7 +42,7 @@ class AccompanyingPeriodController extends Controller 'person' => $person)); } - public function createAction($person_id) { + public function createAction($person_id, Request $request) { $person = $this->_getPerson($person_id); $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, @@ -57,8 +57,6 @@ class AccompanyingPeriodController extends Controller $form = $this->createForm(AccompanyingPeriodType::class, $accompanyingPeriod, array('period_action' => 'create')); - $request = $this->getRequest(); - if ($request->getMethod() === 'POST') { $form->handleRequest($request); $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(); $accompanyingPeriod = $em->getRepository('ChillPersonBundle:AccompanyingPeriod') @@ -113,7 +111,6 @@ class AccompanyingPeriodController extends Controller $form = $this->createForm(AccompanyingPeriodType::class, $accompanyingPeriod, array('period_action' => 'update')); - $request = $this->getRequest(); if ($request->getMethod() === 'POST') { $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); $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, @@ -171,8 +168,6 @@ class AccompanyingPeriodController extends Controller 'period_action' => 'close' )); - $request = $this->getRequest(); - if ($request->getMethod() === 'POST') { $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); $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, 'You are not allowed to update this person'); - $request = $this->getRequest(); - //in case the person is already open if ($person->isOpen()) { $this->get('session')->getFlashBag() diff --git a/Controller/PersonController.php b/Controller/PersonController.php index ccd64aa6c..137a575ce 100644 --- a/Controller/PersonController.php +++ b/Controller/PersonController.php @@ -3,7 +3,7 @@ /* * Chill is a software for social workers * - * Copyright (C) 2015, Champs Libres Cooperative SCRLFS, + * Copyright (C) 2015, Champs Libres Cooperative SCRLFS, * , * * 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) - { + { $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."); - + return $this->render('ChillPersonBundle:Person:view.html.twig', array("person" => $person, "cFGroup" => $this->getCFGroup())); } - + public function editAction($person_id) { $person = $this->_getPerson($person_id); - + if ($person === null) { return $this->createNotFoundException(); } - - $this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person, + + $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', @@ -83,88 +83,88 @@ class PersonController extends Controller "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())); } - + public function updateAction($person_id, Request $request) { $person = $this->_getPerson($person_id); - + if ($person === null) { return $this->createNotFoundException(); } - - $this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $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())); - + if ($request->getMethod() === 'POST') { $form->handleRequest($request); - if ( ! $form->isValid() ) { + if ( ! $form->isValid() ) { $this->get('session') ->getFlashBag()->add('error', 'Thp person data provided' . ' are not valid'); - - return $this->render('ChillPersonBundle:Person:edit.html.twig', - array('person' => $person, + + return $this->render('ChillPersonBundle:Person:edit.html.twig', + array('person' => $person, 'form' => $form->createView())); } - + $this->get('session')->getFlashBag() - ->add('success', + ->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( + + $url = $this->generateUrl('chill_person_view', array( 'person_id' => $person->getId() )); - + return $this->redirect($url); } } - + public function newAction() - { - // this is a dummy default center. + { + // 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::NAME, + CreationPersonType::NAME, $person, array( 'action' => $this->generateUrl('chill_person_review'), 'form_status' => CreationPersonType::FORM_NOT_REVIEWED )); - - return $this->_renderNewForm($form); + + return $this->_renderNewForm($form); } - + private function _renderNewForm($form) { - return $this->render('ChillPersonBundle:Person:create.html.twig', + return $this->render('ChillPersonBundle:Person:create.html.twig', array( 'form' => $form->createView() )); } - + /** - * + * * @param type $form * @return \Chill\PersonBundle\Entity\Person */ @@ -174,24 +174,24 @@ class PersonController extends Controller * @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; } - + /** - * + * * @param \Chill\PersonBundle\Entity\Person $person * @return \Symfony\Component\Validator\ConstraintViolationListInterface */ @@ -199,78 +199,76 @@ class PersonController extends Controller { $errors = $this->get('validator') ->validate($person, array('creation')); - + //validate accompanying periods $periods = $person->getAccompanyingPeriods(); - + foreach ($periods as $period) { $period_errors = $this->get('validator') ->validate($period); - + //group errors : foreach($period_errors as $error) { $errors->add($error); } } - + return $errors; } - - public function reviewAction() + + public function reviewAction(Request $request) { - $request = $this->getRequest(); - 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, - new Person(), + 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); + + $errors = $this->_validatePersonAndAccompanyingPeriod($person); $this->get('logger')->info(sprintf('Person created with %d errors ', count($errors))); if ($errors->count() > 0) { $this->get('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, new Person(), array( - 'action' => $this->generateUrl('chill_person_review'), + 'action' => $this->generateUrl('chill_person_review'), 'form_status' => CreationPersonType::FORM_NOT_REVIEWED )); - + $form->handleRequest($request); - + return $this->_renderNewForm($form); } else { $this->get('logger')->info('Person created without errors'); } - + $em = $this->getDoctrine()->getManager(); - + $query = $em->createQuery(); - + $dql = 'SELECT p from ChillPersonBundle:Person p WHERE ' . 'LOWER(p.firstName) LIKE LOWER(:firstName)' . ' OR LOWER(p.lastName) LIKE LOWER(:lastName)' @@ -288,10 +286,10 @@ class PersonController extends Controller // add authorized centers $centers = $this->get('chill.main.security.authorization.helper') ->getReachableCenters($this->getUser(), new Role(PersonVoter::SEE)); - + $dql.=' and p.center IN (:centers) '; $query->setParameter('centers', $centers); - + // run query $query->setDql($dql); @@ -300,13 +298,13 @@ class PersonController extends Controller if (count($alternatePersons) === 0) { return $this->forward('ChillPersonBundle:Person:create'); } - - $this->get('session')->getFlashBag()->add('info', + + $this->get('session')->getFlashBag()->add('info', $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))) ); - + return $this->render('ChillPersonBundle:Person:create_review.html.twig', array('alternatePersons' => $alternatePersons, 'firstName' => $form['firstName']->getData(), @@ -316,38 +314,37 @@ class PersonController extends Controller 'creation_date' => $form['creation_date']->getData(), 'form' => $form->createView())); } - - public function createAction() - { - $request = $this->getRequest(); - + + 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::NAME, null, array( 'form_status' => CreationPersonType::FORM_REVIEWED )); - + $form->handleRequest($request); - + $person = $this->_bindCreationForm($form); - + $errors = $this->_validatePersonAndAccompanyingPeriod($person); - - $this->denyAccessUnlessGranted('CHILL_PERSON_CREATE', $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', + + 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"; @@ -359,7 +356,7 @@ class PersonController extends Controller return $r; } } - + /** * easy getting a person by his id * @return \Chill\PersonBundle\Entity\Person @@ -367,10 +364,10 @@ class PersonController extends Controller private function _getPerson($id) { $em = $this->getDoctrine()->getManager(); - + $person = $em->getRepository('ChillPersonBundle:Person') ->find($id); - + return $person; } }