From 9c249160cc31686dcc66c7e563d18200eedc8a15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 28 Nov 2013 21:51:16 +0100 Subject: [PATCH] add feature: update the date of an opened file --- Controller/HistoryController.php | 130 +++++++++++++++--- Form/PersonHistoryFileType.php | 56 ++++++++ .../person.scssc | Bin 3379 -> 5952 bytes Resources/config/validation.yml | 2 + Resources/translations/messages.fr.yml | 15 +- Resources/views/History/list.html.twig | 8 +- Resources/views/History/update.html.twig | 29 ++++ 7 files changed, 218 insertions(+), 22 deletions(-) create mode 100644 Form/PersonHistoryFileType.php create mode 100644 Resources/views/History/update.html.twig diff --git a/Controller/HistoryController.php b/Controller/HistoryController.php index d89eefc45..f5ffc555b 100644 --- a/Controller/HistoryController.php +++ b/Controller/HistoryController.php @@ -4,6 +4,8 @@ namespace CL\Chill\PersonBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use CL\Chill\PersonBundle\Entity\Person; +use CL\Chill\PersonBundle\Form\PersonHistoryFileType; +use CL\Chill\PersonBundle\Entity\PersonHistoryFile; class HistoryController extends Controller { @@ -21,8 +23,75 @@ class HistoryController extends Controller } - public function updateAction($historyId){ + public function updateAction($id, $historyId){ + $em = $this->getDoctrine()->getManager(); + $history = $em->getRepository('CLChillPersonBundle:PersonHistoryFile') + ->find($historyId); + + if ($history === null) { + return $this->createNotFoundException("history with id ".$historyId. + " is not found"); + } + + if ($history->isOpen()) { + $r = new \Symfony\Component\HttpFoundation\Response("You are not allowed " + . "to edit an opened history"); + $r->setStatusCode(400); + return $r; + } + + $person = $history->getPerson(); + + if ($person->getId() != $id) { + $r = new \Symfony\Component\HttpFoundation\Response("person id does not" + . " match history id"); + $r->setStatusCode(400); + return $r; + } + + $motivesArray = $this->get('service_container') + ->getParameter('person.history.close.motives'); + + $form = $this->createForm(new PersonHistoryFileType($motivesArray), + $history); + + $request = $this->getRequest(); + + if ($request->getMethod() === 'POST') { + + $form->handleRequest($request); + + $errors = $this->_validatePerson($person); + + $flashBag = $this->get('session')->getFlashBag(); + + if ($form->isValid(array('Default', 'closed')) + && count($errors) === 0) { + $em->flush(); + + $flashBag->add('success', + $this->get('translator')->trans( + 'controller.Person.history.update.done')); + + return $this->redirect($this->generateUrl('chill_person_history_list', + array('id' => $person->getId()))); + } else { + + $flashBag->add('danger', $this->get('translator') + ->trans('controller.Person.history.update.error')); + + foreach($errors as $error) { + $flashBag->add('info', $error->getMessage()); + } + } + } + + + + return $this->render('CLChillPersonBundle:History:update.html.twig', + array('form' => $form->createView(), + 'person' => $person ) ); } public function closeAction($id) { @@ -157,7 +226,9 @@ class HistoryController extends Controller return $this->createNotFoundException('Person not found'); } - if ($person->isOpen() === true) { + $request = $this->getRequest(); + + if ($person->isOpen() === true && ! $request->query->has('historyId')) { $this->get('session')->getFlashBag() ->add('danger', $this->get('translator') ->trans('controller.Person.history.open.is_not_closed', @@ -169,32 +240,55 @@ class HistoryController extends Controller ))); } - $current = $person->getCurrentHistory(); + $historyId = $request->query->get('historyId', null); - $container = array( - 'dateOpening' => new \DateTime(), - 'texto' => null); + if ($historyId !== null) { + $history = $this->getDoctrine()->getEntityManager() + ->getRepository('CLChillPersonBundle:PersonHistoryFile') + ->find($historyId); + + } else { + $history = new PersonHistoryFile(new \DateTime()); + } + + //this may happen if we set an historyId and history is not closed + if ($request->query->has('historyId') && ! $history->isOpen()) { + + $r = new \Symfony\Component\HttpFoundation\Response("You are not allowed " + . "to edit a closed history"); + $r->setStatusCode(400); + return $r; + + } + + - $form = $this->createFormBuilder($container) + $form = $this->createFormBuilder() ->add('dateOpening', 'date', array( - 'data' => new \DateTime() + 'data' => $history->getDateOpening() )) ->add('texto', 'textarea', array( - 'required' => false + 'required' => false, + 'data' => $history->getMemo() )) ->getForm(); - $request = $this->getRequest(); + if ($request->getMethod() === 'POST') { $form->handleRequest($request); if ($form->isValid()) { - - $person->open($form['dateOpening']->getData(), + if ($request->query->has('historyId')) { + $history->setDateOpening($form['dateOpening']->getData()) + ->setMemo($form['texto']->getData()); + } else { + $person->open($form['dateOpening']->getData(), $form['texto']->getData()); + } + $errors = $this->_validatePerson($person); @@ -223,11 +317,11 @@ class HistoryController extends Controller } } - } else { // if errors in forms - $this->get('session')->getFlashBag() - ->add('danger', $this->get('translator') - ->trans('controller.Person.history.open.error_in_form')); - } + } else { // if errors in forms + $this->get('session')->getFlashBag() + ->add('danger', $this->get('translator') + ->trans('controller.Person.history.open.error_in_form')); + } } @@ -239,7 +333,7 @@ class HistoryController extends Controller array( 'form' => $form->createView(), 'person' => $person, - 'history' => $current + 'history' => $history )); } diff --git a/Form/PersonHistoryFileType.php b/Form/PersonHistoryFileType.php new file mode 100644 index 000000000..7690a8dd2 --- /dev/null +++ b/Form/PersonHistoryFileType.php @@ -0,0 +1,56 @@ + $params ) { + $this->motives[$key] = $params['label']; + } + } + + /** + * @param FormBuilderInterface $builder + * @param array $options + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('date_opening', 'date', array("required" => true, + 'widget' => 'single_text')) + ->add('date_closing', 'date', array('required' => true, + 'widget' => 'single_text')) + ->add('motive', 'choice', array( + 'choices' => $this->motives, + 'required' => true + )) + ->add('memo', 'textarea') + ; + } + + /** + * @param OptionsResolverInterface $resolver + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'CL\Chill\PersonBundle\Entity\PersonHistoryFile' + )); + } + + /** + * @return string + */ + public function getName() + { + return 'cl_chill_personbundle_personhistoryfile'; + } +} diff --git a/Resources/assets/css/.sass-cache/5423e640e54bbdf4d80063fe9dc2028bb970d84c/person.scssc b/Resources/assets/css/.sass-cache/5423e640e54bbdf4d80063fe9dc2028bb970d84c/person.scssc index c7bf636ca4ac7cc6c1eaed5e749fc4b24879581b..9287bacc26135338cce96335cc5c892b080a4361 100644 GIT binary patch literal 5952 zcmbtY>rNX-6b7qZ%woXA2{adytx0o{f@Pe5;u$GjCCUYzC)j(uh9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j zOr|pd;fAE^U1=xfKRJ8JC?5r5D&mb?EvN>;WwJgHIfd3Q!_2|)f5cOW9MRonVp93Mupc-V(rP|0epH`@9MM`~ zZS}J@P7QMWxBwxXiX8r?==(XmXkW%JDXm2(Vs6XY4djRp!xYfk2n4(Zz9}Hu1LTN~ zKwq0=l&t;>rm0#QIlO$n1dd-okg);uX4R{X!%50qz-44rs>@`a)I1{U3?qP?oYk4n zCIje|p9z*$SrCo9UCcs@EA13raG3*TWpt5=awx4R;c*Cch<4Uk<+F;z)Osv0{1C+r zyv-UoU(gQV|K?TGUu5$rE`2pr@fwYT%O9?3rVTS0^VKmC(`0WhaLv`NmDel7Pl38l1d9p#Gp`}rxQA$#xfr&+Os)m5!*e356RAIWnwE3yvF{a7t;*tPLG8VM} diff --git a/Resources/config/validation.yml b/Resources/config/validation.yml index b0eeb87c2..439b0c4a8 100644 --- a/Resources/config/validation.yml +++ b/Resources/config/validation.yml @@ -52,6 +52,8 @@ CL\Chill\PersonBundle\Entity\PersonHistoryFile: - NotNull: ~ date_closing: - Date: ~ + - NotNull: + groups: [closed] constraints: - Callback: methods: [isDateConsistent] \ No newline at end of file diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index 1e1b2cc04..19ebf09d7 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -36,14 +36,17 @@ controller: history: close: done: Bravo ! Le dossier de %name% a été clotûré. - error: Une erreur est survenue. Le dossier n'a pu être clos. + error: Les informations introduites ne sont pas valides. Le dossier n'a pu être clos. error_in_form: Le formulaire n'est pas valide. is_not_open: Le dossier de %name% n'est pas ouvert. Il ne peut donc être fermé. open: done: Bravo ! Le dossier de %name% est maintenant ouvert. - error: Une erreur est survenue. Le dossier n'a pu être ouvert. - error_in_form: Le formulaire n'est pas valide. + error: Les informations introduites ne sont pas valides. Le dossier n'a pu être ouvert. + error_in_fom: Le formulaire n'est pas valide. is_not_closed: Le dossier de %name% n'est pas fermé. Il ne peut donc être ouvert. + update: + done: Bravo ! La mise à jour de l'historique a réussi ! + error: Les données introduites ne sont pas valides. Veuillez vérifier les informations ci-dessous. views: layout: @@ -100,6 +103,12 @@ views: action: Ouvrir date_of_opening: Date d'ouverture texto: Mémo + hupdate: + dateOpening: Date d'ouverture + dateClosing: Date de fermeture + motive_of_closing: Motif de clôture + texto: Mémo + action: Modifier diff --git a/Resources/views/History/list.html.twig b/Resources/views/History/list.html.twig index b4c20318f..a8ca3cf6a 100644 --- a/Resources/views/History/list.html.twig +++ b/Resources/views/History/list.html.twig @@ -30,7 +30,13 @@ {% endspaceless %} - +
+ {% if history.isOpen %} + {{ 'views.Person.hlist.edit'|trans }} + {% else %} + {{ 'views.Person.hlist.edit'|trans }} + {% endif %} +
diff --git a/Resources/views/History/update.html.twig b/Resources/views/History/update.html.twig new file mode 100644 index 000000000..cc972fdd8 --- /dev/null +++ b/Resources/views/History/update.html.twig @@ -0,0 +1,29 @@ +{% extends "CLChillPersonBundle::layout.html.twig" %} + +{% set activeRouteKey = null %} + +{% block title %}{% endblock title %} + +{% form_theme form 'CLChillMainBundle:Form:fields.html.twig' %} + +{% block personcontent %} + +{{ form_start(form) }} + +{{ form_row(form.date_opening, { 'label' : 'views.Person.hupdate.dateOpening' }) }} + +{{ form_row(form.date_closing, { 'label' : 'views.Person.hupdate.dateClosing' }) }} + +{{ form_row(form.motive, { 'label' : 'views.Person.hupdate.motive_of_closing' }) }} + +{{ form_row(form.memo, { 'label' : 'views.Person.hupdate.texto' }) }} + +{{form_rest(form) }} + +
+ +
+ +{{ form_end(form) }} + +{% endblock personcontent %}