Refactoring

This commit is contained in:
Marc Ducobu 2014-11-10 14:24:29 +01:00
parent b50eb2ec81
commit 52e0282549

View File

@ -1,5 +1,24 @@
<?php <?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.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/>.
*/
namespace Chill\PersonBundle\Controller; namespace Chill\PersonBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
@ -9,10 +28,10 @@ use Chill\PersonBundle\Form\CreationPersonType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
class PersonController extends Controller { 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) {
@ -20,32 +39,30 @@ class PersonController extends Controller {
} }
return $this->render('ChillPersonBundle:Person:view.html.twig', return $this->render('ChillPersonBundle:Person:view.html.twig',
array("person" => $person) array("person" => $person));
);
} }
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();
} }
$form = $this->createForm(new PersonType(), $person, array( $form = $this->createForm(new PersonType(), $person,
'action' => $this->generateUrl('chill_person_general_update', array( array('action' => $this->generateUrl('chill_person_general_update',
'person_id' => $person_id array('person_id' => $person_id)
)) )
)); )
);
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()));
} }
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) {
@ -86,7 +103,8 @@ class PersonController extends Controller {
} }
} }
public function searchAction() { public function searchAction()
{
$q = $this->getRequest()->query->getAlnum('q', ''); $q = $this->getRequest()->query->getAlnum('q', '');
$q = trim($q); $q = trim($q);
@ -95,7 +113,8 @@ class PersonController extends Controller {
->getFlashBag() ->getFlashBag()
->add('info', ->add('info',
$this->get('translator') $this->get('translator')
->trans('search.q_is_empty') ); ->trans('search.q_is_empty')
);
} }
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
@ -142,18 +161,17 @@ class PersonController extends Controller {
)); ));
} }
public function newAction() { public function newAction()
{
$form = $this->createForm( $form = $this->createForm(
new CreationPersonType(CreationPersonType::FORM_NOT_REVIEWED), new CreationPersonType(CreationPersonType::FORM_NOT_REVIEWED),
null, array('action' => $this->generateUrl('chill_person_review'))); null, array('action' => $this->generateUrl('chill_person_review')));
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()
@ -165,7 +183,8 @@ class PersonController extends Controller {
* @param type $form * @param type $form
* @return \Chill\PersonBundle\Entity\Person * @return \Chill\PersonBundle\Entity\Person
*/ */
private function _bindCreationForm($form) { private function _bindCreationForm($form)
{
$date = new \DateTime(); $date = new \DateTime();
$person = new Person($form['creation_date']->getData()); $person = new Person($form['creation_date']->getData());
@ -186,7 +205,8 @@ class PersonController extends Controller {
* @param \Chill\PersonBundle\Entity\Person $person * @param \Chill\PersonBundle\Entity\Person $person
* @return \Symfony\Component\Validator\ConstraintViolationListInterface * @return \Symfony\Component\Validator\ConstraintViolationListInterface
*/ */
private function _validatePersonAndHistory(Person $person) { private function _validatePersonAndHistory(Person $person)
{
$errors = $this->get('validator') $errors = $this->get('validator')
->validate($person, array('creation')); ->validate($person, array('creation'));
@ -206,7 +226,8 @@ class PersonController extends Controller {
return $errors; return $errors;
} }
public function reviewAction() { public function reviewAction()
{
$request = $this->getRequest(); $request = $this->getRequest();
if ($request->getMethod() !== 'POST') { if ($request->getMethod() !== 'POST') {
@ -225,10 +246,7 @@ class PersonController extends Controller {
$errors = $this->_validatePersonAndHistory($person); $errors = $this->_validatePersonAndHistory($person);
if ( count($errors) > 0) { if ( count($errors) > 0) {
$flashBag = $this->get('session')->getFlashBag(); $flashBag = $this->get('session')->getFlashBag();
$translator = $this->get('translator'); $translator = $this->get('translator');
@ -247,8 +265,6 @@ class PersonController extends Controller {
return $this->_renderNewForm($form); return $this->_renderNewForm($form);
} }
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$query = $em->createQuery(); $query = $em->createQuery();
@ -287,11 +303,10 @@ class PersonController extends Controller {
'genre' => $form['genre']->getData(), 'genre' => $form['genre']->getData(),
'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 = $this->getRequest(); $request = $this->getRequest();
if ($request->getMethod() !== 'POST') { if ($request->getMethod() !== 'POST') {
@ -322,15 +337,14 @@ class PersonController extends Controller {
$r->setStatusCode(400); $r->setStatusCode(400);
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
*/ */
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')
@ -338,5 +352,4 @@ class PersonController extends Controller {
return $person; return $person;
} }
} }