diff --git a/Controller/PersonController.php b/Controller/PersonController.php index 5f003e9e4..f04584a65 100644 --- a/Controller/PersonController.php +++ b/Controller/PersonController.php @@ -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 diff --git a/Form/CreationPersonType.php b/Form/CreationPersonType.php new file mode 100644 index 000000000..e3c9a2458 --- /dev/null +++ b/Form/CreationPersonType.php @@ -0,0 +1,77 @@ +setStatus($form_status); + } + + /** + * @param FormBuilderInterface $builder + * @param array $options + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + if ($this->form_status === self::FORM_BEING_REVIEWED) { + $builder->add('name', 'hidden') + ->add('surname', 'hidden') + ->add('dateOfBirth', 'hidden') + ->add('genre', 'hidden') + ->add('creation_date', 'hidden') + ->add('form_status', 'hidden') + ; + } else { + $builder + ->add('name') + ->add('surname') + ->add('dateOfBirth', 'date', array('required' => false, 'widget' => 'single_text')) + ->add('genre', new GenderType(), array( + 'required' => false + )) + ->add('creation_date', 'date', array('required' => true, + 'widget' => 'single_text', 'data' => new \DateTime())) + ->add('form_status', 'hidden', array('data' => $this->form_status)) + ; + } + } + + private function setStatus($status) { + $this->form_status = $status; + } + + /** + * @param OptionsResolverInterface $resolver + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { +// $resolver->setDefaults(array( +// 'data_class' => 'CL\Chill\PersonBundle\Entity\Person' +// )); + } + + /** + * @return string + */ + public function getName() + { + return self::NAME; + } +} diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index 710fb17fb..61a23746c 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -16,6 +16,18 @@ chill_person_general_update: pattern: /view/{id}/update defaults: {_controller: CLChillPersonBundle:Person:update } +chill_person_new: + pattern: /new + defaults: {_controller: CLChillPersonBundle:Person:new } + +chill_person_review: + pattern: /review + defaults: {_controller: CLChillPersonBundle:Person:review } + +chill_person_create: + pattern: /create + defaults: {_controller: CLChillPersonBundle:Person:create } + chill_person_search: pattern: /search diff --git a/Resources/config/validation.yml b/Resources/config/validation.yml index 439b0c4a8..32c788e54 100644 --- a/Resources/config/validation.yml +++ b/Resources/config/validation.yml @@ -7,17 +7,17 @@ CL\Chill\PersonBundle\Entity\Person: groups: [general] name: - NotBlank: - groups: [general] + groups: [general, creation] - Length: min: 2 max: 255 minMessage: validation.Person.constraint.name_min maxMessage: validation.Person.constraint.name_max - groups: [general] + groups: [general, creation] surname: - NotBlank: - groups: [general] + groups: [general, creation] - Length: min: 2 max: 255 @@ -26,7 +26,8 @@ CL\Chill\PersonBundle\Entity\Person: groups: [general] dateOfBirth: - Date: - groups: [general] + message: validation.Person.constraint.dateOfBirth.not_valid + groups: [general, creation] nbOfChild: - Range: min: 0 @@ -48,11 +49,15 @@ CL\Chill\PersonBundle\Entity\Person: CL\Chill\PersonBundle\Entity\PersonHistoryFile: properties: date_opening: - - Date: ~ - - NotNull: ~ + - Date: + message: validation.history.date_opening.not_valid + - NotNull: + message: validation.history.date_opening.not_null date_closing: - - Date: ~ + - Date: + message: validation.history.date_closing.not_valid - NotNull: + message: validation.history.date_closing.not_null groups: [closed] constraints: - Callback: diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index 6da41a869..0a7aa9756 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -2,7 +2,9 @@ person: name: Nom surname: Prénom dateOfBirth: Date de naissance + without_date_of_birth: Date de naissance inconnue nationality: Nationalité + without_nationality: Sans nationalité civil_union: divorced: Divorcé separated: Séparé @@ -33,6 +35,9 @@ validation: error: '{1} Le champs %field% est incorrect. Veuillez le corriger. | ]1, Inf] Plusieurs champs sont incorrects. Veuillez les vérifier.' controller: Person: + review: + people_with_similar_name: %nb% personnes ont un nom similaire. Vérifiez qu'il ne s'agit pas de l'une d'elles. + problem_with_data: Les données de votre formulaire sont invalides. history: close: done: Bravo ! Le dossier de %name% a été clotûré. @@ -113,6 +118,17 @@ views: motive_of_closing: Motif de clôture texto: Mémo action: Modifier + creation: + add: Ajouter + open: Ouverture d'un dossier + review: + altName: Nom + altDateOfBirth: Prénom + altNationality: Nationalité + confirm_creation: Confirmer la création + creation_date: Date d'ouverture + you_will_create_this_person: Vous allez créer le dossier suivant + diff --git a/Resources/translations/validators.fr.yml b/Resources/translations/validators.fr.yml index 3f3de3647..584dca9d5 100644 --- a/Resources/translations/validators.fr.yml +++ b/Resources/translations/validators.fr.yml @@ -6,6 +6,8 @@ validation: nbOfChild_min: Ce nombre est négatif. Il ne peut y avoir moins de 0 enfants. nbOfChild_max: Ce nombre est trop élevé. Il ne peut être supérieur à {{ limit }}. nbOfChild_invalid: La valeur introduite n'est pas un nombre. + dateOfBirth: + not_valid: La date de naissance n'est pas valide. history: opening_is_before_closing: L'historique des ouvertures et fermetures de dossier n'est pas cohérent. Des historiques d'ouverture et de fermeture se chevauchent. Vérifiez la liste des ouvertures et fermetures. open_history_without_closing: Vous tentez d'ouvrir un dossier à une date passée alors qu'il est réouvert plus tard. Veuillez insérer une nouvelle ligne d'ouverture avant de la clotûrer avant la suivante.