create and review the creation of a person

This commit is contained in:
2013-12-03 17:12:56 +01:00
parent 44d8236f21
commit 5750c73574
6 changed files with 258 additions and 7 deletions

View File

@@ -5,7 +5,9 @@ namespace CL\Chill\PersonBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use CL\Chill\PersonBundle\Entity\Person;
use CL\Chill\PersonBundle\Form\PersonType;
use CL\Chill\PersonBundle\Form\CreationPersonType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class PersonController extends Controller {
@@ -140,6 +142,143 @@ class PersonController extends Controller {
));
}
public function newAction() {
$form = $this->createForm(
new CreationPersonType(CreationPersonType::FORM_NOT_REVIEWED),
null, array('action' => $this->generateUrl('chill_person_review')));
return $this->_renderNewForm($form);
}
private function _renderNewForm($form) {
return $this->render('CLChillPersonBundle:Person:create.html.twig',
array(
'form' => $form->createView()
));
}
/**
*
* @param type $form
* @return \CL\Chill\PersonBundle\Entity\Person
*/
private function _bindCreationForm($form) {
$date = new \DateTime($form['creation_date']->getData());
$person = new Person($date);
$date_of_birth = new \DateTime($form['dateOfBirth']->getData());
$person->setName($form['name']->getData())
->setSurname($form['surname']->getData())
->setGenre($form['genre']->getData())
->setDateOfBirth($date_of_birth)
;
return $person;
}
public function reviewAction() {
$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(
new CreationPersonType(CreationPersonType::FORM_BEING_REVIEWED),
null, array('action' => $this->generateUrl('chill_person_create')));
$form->handleRequest($request);
$person = $this->_bindCreationForm($form);
$errors = $this->get('validator')
->validate($person, array('creation'));
//validate history
$histories = $person->getHistories();
foreach ($histories as $history) {
$errors_history = $this->get('validator')
->validate($history);
//group errors :
foreach($errors_history as $error) {
$errors->add($error);
}
}
if ( count($errors) > 0) {
$flashBag = $this->get('session')->getFlashBag();
$translator = $this->get('translator');
$flashBag->add('danger', $translator->trans('controller.Person.review.problem_with_data'));
foreach($errors as $error) {
$flashBag->add('info', $error->getMessage());
}
$form = $this->createForm(
new CreationPersonType(CreationPersonType::FORM_NOT_REVIEWED),
array('action' => $this->generateUrl('chill_person_create')));
$form->handleRequest($request);
return $this->_renderNewForm($form);
}
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery();
$dql = 'SELECT p from CLChillPersonBundle:Person p WHERE '
. 'LOWER(p.name) LIKE LOWER(:name)'
. ' OR LOWER(p.surname) LIKE LOWER(:surname)';
$query->setParameter('name', $form['name']->getData())
->setParameter('surname', $form['surname']->getData());
if ($this->container
->getParameter('cl_chill_person.search.use_double_metaphone')) {
$dql .= ' OR DOUBLEMETAPHONE(p.name) LIKE DOUBLEMETAPHONE(:name)';
}
$query->setDql($dql);
$alternatePersons = $query->getResult();
if (count($alternatePersons) === 0) {
return $this->forward('CLChillPersonBundle:Person:create');
}
$this->get('session')->getFlashBag()->add('info',
$this->get('translator')->trans(
'controller.Person.review.people_with_similar_name',
array('%nb%' => count($alternatePersons)))
);
return $this->render('CLChillPersonBundle:Person:create_review.html.twig',
array('alternatePersons' => $alternatePersons,
'name' => $form['name']->getData(),
'surname' => $form['surname']->getData(),
'dateOfBirth' => $form['dateOfBirth']->getData(),
'genre' => $form['genre']->getData(),
'creation_date' => $form['creation_date']->getData(),
'form' => $form->createView()));
}
/**
* easy getting a person by his id
* @return \CL\Chill\PersonBundle\Entity\Person