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 c7bf636ca..9287bacc2 100644 Binary files a/Resources/assets/css/.sass-cache/5423e640e54bbdf4d80063fe9dc2028bb970d84c/person.scssc and b/Resources/assets/css/.sass-cache/5423e640e54bbdf4d80063fe9dc2028bb970d84c/person.scssc differ 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 %} -
{{ 'views.Person.hlist.edit'|trans }}
+
+ {% 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 %}