add feature: update the date of an opened file

This commit is contained in:
Julien Fastré 2013-11-28 21:51:16 +01:00
parent af984962a7
commit 9c249160cc
7 changed files with 218 additions and 22 deletions

View File

@ -4,6 +4,8 @@ namespace CL\Chill\PersonBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use CL\Chill\PersonBundle\Entity\Person; use CL\Chill\PersonBundle\Entity\Person;
use CL\Chill\PersonBundle\Form\PersonHistoryFileType;
use CL\Chill\PersonBundle\Entity\PersonHistoryFile;
class HistoryController extends Controller 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) { public function closeAction($id) {
@ -157,7 +226,9 @@ class HistoryController extends Controller
return $this->createNotFoundException('Person not found'); 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() $this->get('session')->getFlashBag()
->add('danger', $this->get('translator') ->add('danger', $this->get('translator')
->trans('controller.Person.history.open.is_not_closed', ->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( if ($historyId !== null) {
'dateOpening' => new \DateTime(), $history = $this->getDoctrine()->getEntityManager()
'texto' => null); ->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( ->add('dateOpening', 'date', array(
'data' => new \DateTime() 'data' => $history->getDateOpening()
)) ))
->add('texto', 'textarea', array( ->add('texto', 'textarea', array(
'required' => false 'required' => false,
'data' => $history->getMemo()
)) ))
->getForm(); ->getForm();
$request = $this->getRequest();
if ($request->getMethod() === 'POST') { if ($request->getMethod() === 'POST') {
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isValid()) { if ($form->isValid()) {
if ($request->query->has('historyId')) {
$history->setDateOpening($form['dateOpening']->getData())
->setMemo($form['texto']->getData());
} else {
$person->open($form['dateOpening']->getData(), $person->open($form['dateOpening']->getData(),
$form['texto']->getData()); $form['texto']->getData());
}
$errors = $this->_validatePerson($person); $errors = $this->_validatePerson($person);
@ -239,7 +333,7 @@ class HistoryController extends Controller
array( array(
'form' => $form->createView(), 'form' => $form->createView(),
'person' => $person, 'person' => $person,
'history' => $current 'history' => $history
)); ));
} }

View File

@ -0,0 +1,56 @@
<?php
namespace CL\Chill\PersonBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class PersonHistoryFileType extends AbstractType
{
private $motives = array();
public function __construct(array $motivesArray) {
foreach ($motivesArray as $key => $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';
}
}

View File

@ -52,6 +52,8 @@ CL\Chill\PersonBundle\Entity\PersonHistoryFile:
- NotNull: ~ - NotNull: ~
date_closing: date_closing:
- Date: ~ - Date: ~
- NotNull:
groups: [closed]
constraints: constraints:
- Callback: - Callback:
methods: [isDateConsistent] methods: [isDateConsistent]

View File

@ -36,14 +36,17 @@ controller:
history: history:
close: close:
done: Bravo ! Le dossier de <em>%name%</em> a été clotûré. done: Bravo ! Le dossier de <em>%name%</em> 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. error_in_form: Le formulaire n'est pas valide.
is_not_open: Le dossier de </em>%name%</em> n'est pas ouvert. Il ne peut donc être fermé. is_not_open: Le dossier de </em>%name%</em> n'est pas ouvert. Il ne peut donc être fermé.
open: open:
done: Bravo ! Le dossier de <em>%name%</em> est maintenant ouvert. done: Bravo ! Le dossier de <em>%name%</em> est maintenant ouvert.
error: Une erreur est survenue. Le dossier n'a pu être ouvert. error: Les informations introduites ne sont pas valides. Le dossier n'a pu être ouvert.
error_in_form: Le formulaire n'est pas valide. error_in_fom: Le formulaire n'est pas valide.
is_not_closed: Le dossier de <em>%name%</em> n'est pas fermé. Il ne peut donc être ouvert. is_not_closed: Le dossier de <em>%name%</em> 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: views:
layout: layout:
@ -100,6 +103,12 @@ views:
action: Ouvrir action: Ouvrir
date_of_opening: Date d'ouverture date_of_opening: Date d'ouverture
texto: Mémo texto: Mémo
hupdate:
dateOpening: Date d'ouverture
dateClosing: Date de fermeture
motive_of_closing: Motif de clôture
texto: Mémo
action: Modifier

View File

@ -30,7 +30,13 @@
{% endspaceless %}</td> {% endspaceless %}</td>
<td> <td>
<div class="small warning btn icon-right entypo icon-pencil"><a href="{{ path('chill_person_history_update', {'id' : person.id, 'historyId' : history.id } ) }}">{{ 'views.Person.hlist.edit'|trans }}</a></div> <div class="small warning btn icon-right entypo icon-pencil">
{% if history.isOpen %}
<a href="{{ path('chill_person_history_open', {'id' : person.id, 'historyId' : history.id } ) }}">{{ 'views.Person.hlist.edit'|trans }}</a>
{% else %}
<a href="{{ path('chill_person_history_update', {'id' : person.id, 'historyId' : history.id } ) }}">{{ 'views.Person.hlist.edit'|trans }}</a>
{% endif %}
</div>
</td> </td>
</tr> </tr>

View File

@ -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) }}
<div class="medium btn danger icon-right entypo icon-lock">
<button type="submit">{{ 'views.Person.hupdate.action'|trans }}</button>
</div>
{{ form_end(form) }}
{% endblock personcontent %}