upgrade and refactor personhistoryFile

This commit is contained in:
Julien Fastré 2014-11-07 23:48:26 +01:00
parent 29af313582
commit e5f67e32c3
15 changed files with 365 additions and 288 deletions

View File

@ -9,9 +9,9 @@ use Chill\PersonBundle\Entity\PersonHistoryFile;
class HistoryController extends Controller
{
public function listAction($id){
public function listAction($person_id){
$person = $this->_getPerson($id);
$person = $this->_getPerson($person_id);
if ($person === null) {
return $this->createNotFoundException('Person not found');
@ -23,21 +23,21 @@ class HistoryController extends Controller
}
public function createAction($personId) {
$person = $this->_getPerson($personId);
public function createAction($person_id) {
$person = $this->_getPerson($person_id);
if ($person === null) {
return $this->createNotFoundException('Person not found');
}
$history = new PersonHistoryFile(new \DateTime());
$history->setPerson($person);
$history->setPerson($person)
->setDateOpening(new \DateTime())
->setDateClosing(new \DateTime());
$motivesArray = $this->get('service_container')
->getParameter('person.history.close.motives');
$form = $this->createForm(new PersonHistoryFileType($motivesArray),
$history);
$form = $this->createForm(new PersonHistoryFileType(),
$history, array('period_action' => 'update'));
$request = $this->getRequest();
@ -62,7 +62,7 @@ class HistoryController extends Controller
'controller.Person.history.create.done'));
return $this->redirect($this->generateUrl('chill_person_history_list',
array('id' => $person->getId())));
array('person_id' => $person->getId())));
} else {
$flashBag->add('danger', $this->get('translator')
@ -76,43 +76,30 @@ class HistoryController extends Controller
return $this->render('ChillPersonBundle:History:update.html.twig',
array('form' => $form->createView(),
'person' => $person ) );
return $this->render('ChillPersonBundle:History:form.html.twig',
array(
'form' => $form->createView(),
'person' => $person,
'history' => $history
)
);
}
public function updateAction($id, $historyId){
public function updateAction($history_id){
$em = $this->getDoctrine()->getManager();
$history = $em->getRepository('ChillPersonBundle:PersonHistoryFile')
->find($historyId);
->find($history_id);
if ($history === null) {
return $this->createNotFoundException("history with id ".$historyId.
return $this->createNotFoundException("history with id ".$history_id.
" 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);
$form = $this->createForm(new PersonHistoryFileType(),
$history, array('period_action' => 'update'));
$request = $this->getRequest();
@ -133,11 +120,11 @@ class HistoryController extends Controller
'controller.Person.history.update.done'));
return $this->redirect($this->generateUrl('chill_person_history_list',
array('id' => $person->getId())));
array('person_id' => $person->getId())));
} else {
$flashBag->add('danger', $this->get('translator')
->trans('controller.Person.history.update.error'));
->trans('controller.Person.history.edit.error'));
foreach($errors as $error) {
$flashBag->add('info', $error->getMessage());
@ -147,13 +134,16 @@ class HistoryController extends Controller
return $this->render('ChillPersonBundle:History:update.html.twig',
array('form' => $form->createView(),
'person' => $person ) );
return $this->render('ChillPersonBundle:History:form.html.twig',
array(
'form' => $form->createView(),
'person' => $person,
'history' => $history
) );
}
public function closeAction($id) {
$person = $this->_getPerson($id);
public function closeAction($person_id) {
$person = $this->_getPerson($person_id);
if ($person === null) {
return $this->createNotFoundException('Person not found');
@ -162,54 +152,29 @@ class HistoryController extends Controller
if ($person->isOpen() === false) {
$this->get('session')->getFlashBag()
->add('danger', $this->get('translator')
->trans('controller.Person.history.close.is_not_open',
->trans('controller.Person.history.close.is_closed',
array('%name%' => $person->__toString())));
return $this->redirect(
$this->generateUrl('chill_person_history_list', array(
'id' => $person->getId()
'person_id' => $person->getId()
)));
}
$current = $person->getCurrentHistory();
$container = array(
'dateClosing' => new \DateTime(),
'motive' => null,
'texto' => $current->getMemo());
$form = $this->createFormBuilder($container)
->add('dateClosing', 'date', array(
'data' => new \DateTime()
))
->add('motive', 'choice', array(
'choices' => $this->_getMotive(),
'empty_value' => 'views.Person.close.select_a_motive'
))
->add('texto', 'textarea', array(
'required' => false
))
->getForm();
$form = $this->createForm(new PersonHistoryFileType(), $current, array(
'period_action' => 'close'
));
$request = $this->getRequest();
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()){
$person->close($form['dateClosing']->getData(),
$form['motive']->getData(),
$form['texto']->getData());
$person->close($current);
$errors = $this->_validatePerson($person);
if (count($errors) === 0) {
$this->get('session')->getFlashBag()
@ -221,7 +186,7 @@ class HistoryController extends Controller
return $this->redirect(
$this->generateUrl('chill_person_history_list', array(
'id' => $person->getId()
'person_id' => $person->getId()
))
);
} else {
@ -250,7 +215,7 @@ class HistoryController extends Controller
return $this->render('ChillPersonBundle:History:close.html.twig',
return $this->render('ChillPersonBundle:History:form.html.twig',
array(
'form' => $form->createView(),
'person' => $person,
@ -277,8 +242,8 @@ class HistoryController extends Controller
}
public function openAction($id) {
$person = $this->_getPerson($id);
public function openAction($person_id) {
$person = $this->_getPerson($person_id);
if ($person === null) {
return $this->createNotFoundException('Person not found');
@ -286,7 +251,8 @@ class HistoryController extends Controller
$request = $this->getRequest();
if ($person->isOpen() === true && ! $request->query->has('historyId')) {
//in case the person is already open
if ($person->isOpen() === true) {
$this->get('session')->getFlashBag()
->add('danger', $this->get('translator')
->trans('controller.Person.history.open.is_not_closed',
@ -294,60 +260,20 @@ class HistoryController extends Controller
return $this->redirect(
$this->generateUrl('chill_person_history_list', array(
'id' => $person->getId()
'person_id' => $person->getId()
)));
}
$historyId = $request->query->get('historyId', null);
if ($historyId !== null) {
$history = $this->getDoctrine()->getEntityManager()
->getRepository('ChillPersonBundle: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;
$history = new PersonHistoryFile(new \DateTime());
}
$form = $this->createFormBuilder()
->add('dateOpening', 'date', array(
'data' => $history->getDateOpening()
))
->add('texto', 'textarea', array(
'required' => false,
'data' => $history->getMemo()
))
->getForm();
$form = $this->createForm(new PersonHistoryFileType(), $history, array(
'period_action' => 'open'));
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
if ($request->query->has('historyId')) {
$history->setDateOpening($form['dateOpening']->getData())
->setMemo($form['texto']->getData());
} else {
$person->open($form['dateOpening']->getData(),
$form['texto']->getData());
}
$person->open($history);
$errors = $this->_validatePerson($person);
@ -361,7 +287,7 @@ class HistoryController extends Controller
return $this->redirect(
$this->generateUrl('chill_person_history_list', array(
'id' => $person->getId()
'person_id' => $person->getId()
))
);
} else {
@ -383,11 +309,7 @@ class HistoryController extends Controller
}
return $this->render('ChillPersonBundle:History:open.html.twig',
return $this->render('ChillPersonBundle:History:form.html.twig',
array(
'form' => $form->createView(),
'person' => $person,
@ -395,20 +317,6 @@ class HistoryController extends Controller
));
}
private function _getMotive() {
$motivesArray = $this->get('service_container')
->getParameter('person.history.close.motives');
$a = array();
foreach ($motivesArray as $key => $params ) {
$a[$key] = $params['label'];
}
return $a;
}
private function _getPerson($id) {
return $this->getDoctrine()->getManager()
->getRepository('ChillPersonBundle:Person')

View File

@ -0,0 +1,69 @@
<?php
namespace Chill\PersonBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
/**
* Load closing motives into database
*
* @author Julien Fastré <julien arobase fastre point info>
*/
class LoadAccompanyingPeriodClosingMotive extends AbstractFixture
implements OrderedFixtureInterface
{
public function getOrder() {
return 9500;
}
public static $closingMotives = array(
'nothing_to_do' => array(
'name' => array(
'fr' => 'Plus rien à faire',
'en' => 'Nothing to do',
'nl' => 'nieks meer te doen'
)
),
'did_not_come_back' => array(
'name' => array(
'fr' => "N'est plus revenu",
'en' => "Did'nt come back",
'nl' => "Niet teruggekomen"
)
),
'no_more_money' => array(
'active' => false,
'name' => array(
'fr' => "Plus d'argent",
'en' => "No more money",
'nl' => "Geen geld"
)
)
);
public static $references = array();
public function load(ObjectManager $manager)
{
foreach (static::$closingMotives as $ref => $new) {
$motive = new ClosingMotive();
$motive->setName($new['name'])
->setActive((isset($new['active']) ? $new['active'] : true))
;
$manager->persist($motive);
$this->addReference($ref, $motive);
echo "Adding ClosingMotive $ref\n";
}
$manager->flush();
}
}

View File

@ -0,0 +1,92 @@
<?php
/*
*
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\PersonBundle\Entity\AccompanyingPeriod;
/**
* ClosingMotive give an explanation why we closed the Accompanying period
*/
class ClosingMotive
{
/**
* @var integer
*/
private $id;
/**
* @var array
*/
private $name;
/**
*
* @var boolean
*/
private $active;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param array $name
*
* @return ClosingMotive
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return array
*/
public function getName()
{
return $this->name;
}
public function isActive()
{
return $this->active;
}
public function setActive($active)
{
$this->active = $active;
return $this;
}
}

View File

@ -110,11 +110,9 @@ class Person {
*
* @param \DateTime $date
*/
public function open(\DateTime $date, $memo = '') {
$history = new PersonHistoryFile($date);
$history->setMemo($memo);
public function open(PersonHistoryFile $accompanyingPeriod) {
$this->proxyHistoryOpenState = true;
$this->addHistoryFile($history);
$this->addHistoryFile($accompanyingPeriod);
}
/**
@ -126,30 +124,12 @@ class Person {
*
* To check if the Person and his history are consistent, use validation.
*
* @param \DateTime $date
* @param string $motive
* @param string $memo
* @param PersonHistoryFile
* @throws \Exception if two lines of history are open.
*/
public function close(\DateTime $date, $motive, $memo = '') {
$histories = $this->history;
$found = false;
foreach ($histories as $history) {
if ($history->isOpen()) {
if ($found === true) {
throw new \Exception('two open line in history were found. This should not happen.');
}
$history->setDateClosing($date);
$history->setMotive($motive);
$history->setMemo($memo);
$this->proxyHistoryOpenState = false;
$found = true;
}
}
public function close(PersonHistoryFile $accompanyingPeriod)
{
$this->proxyHistoryOpenState = false;
}
/**

View File

@ -25,11 +25,6 @@ class PersonHistoryFile
*/
private $date_closing;
/**
* @var string
*/
private $motive = '';
/**
* @var string
*/
@ -40,6 +35,12 @@ class PersonHistoryFile
*/
private $person;
/**
*
* @var AccompanyingPeriod\ClosingMotive
*/
private $closingMotive = null;
/**
*
* @param \DateTime $dateOpening
@ -121,29 +122,6 @@ class PersonHistoryFile
}
}
/**
* Set motive
*
* @param string $motive
* @return PersonHistoryFile
*/
public function setMotive($motive)
{
$this->motive = $motive;
return $this;
}
/**
* Get motive
*
* @return string
*/
public function getMotive()
{
return $this->motive;
}
/**
* Set memo
*
@ -197,6 +175,18 @@ class PersonHistoryFile
return $this->person;
}
public function getClosingMotive()
{
return $this->closingMotive;
}
public function setClosingMotive(AccompanyingPeriod\ClosingMotive $closingMotive)
{
$this->closingMotive = $closingMotive;
return $this;
}
/// VALIDATION function

View File

@ -5,17 +5,12 @@ namespace Chill\PersonBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
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
@ -23,16 +18,35 @@ class PersonHistoryFileType extends AbstractType
*/
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', array(
//if the period_action is close, date opening should not be seen
if ($options['period_action'] !== 'close') {
$builder
->add('date_opening', 'date', array("required" => true,
'widget' => 'single_text'));
}
// the closingDate should be seen only if period_action = close
// or period_action = update AND accopanying period is already closed
$builder->addEventListener(
FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
$accompanyingPeriod = $event->getData();
$form = $event->getForm();
//add date opening
if (
//if the period_action is "close, should not be shown"
($options['period_action'] === 'close')
OR
($options['period_action'] === 'update' AND !$accompanyingPeriod->isOpen())
){
$form->add('date_closing', 'date', array('required' => true,
'widget' => 'single_text'));
$form->add('closingMotive', 'closing_motive');
}
});
$builder->add('memo', 'textarea', array(
'required' => false
))
;
@ -46,6 +60,13 @@ class PersonHistoryFileType extends AbstractType
$resolver->setDefaults(array(
'data_class' => 'Chill\PersonBundle\Entity\PersonHistoryFile'
));
$resolver
->setRequired(array('period_action'))
->addAllowedTypes(array('period_action' => 'string'))
->addAllowedValues(array('period_action' => array(
'update', 'open', 'close')))
;
}
/**

View File

@ -0,0 +1,53 @@
<?php
namespace Chill\PersonBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* A type to add a closing motive
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class ClosingMotiveType extends AbstractType
{
private $locale;
public function __construct(Request $request = NULL)
{
if ($request !== NULL) {
$this->locale = $request->getLocale();
}
}
public function getName()
{
return 'closing_motive';
}
public function getParent()
{
return 'entity';
}
public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
{
if ($this->locale === NULL) {
throw new \LogicException('the locale should be defined and is extracted '
. 'from the \'request\' service. Maybe was this service '
. 'unaccessible ?');
}
$resolver->setDefaults(array(
'class' => 'ChillPersonBundle:AccompanyingPeriod\ClosingMotive',
'empty_data' => null,
'empty_value' => 'Choose a motive',
'property' => 'name['.$this->locale.']'
)
);
}
}

View File

@ -0,0 +1,12 @@
Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive:
type: entity
id:
id:
type: integer
id: true
generator: { strategy: AUTO }
fields:
name:
type: json_array
active:
type: boolean

View File

@ -13,9 +13,6 @@ Chill\PersonBundle\Entity\PersonHistoryFile:
type: date
default: null
nullable: true
motive:
type: string
length: 200
memo:
type: text
manyToOne:
@ -23,3 +20,6 @@ Chill\PersonBundle\Entity\PersonHistoryFile:
targetEntity: Person
inversedBy: history
cascade: [refresh]
closingMotive:
targetEntity: Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive
nullable: true

View File

@ -49,7 +49,7 @@ chill_person_history_create:
chill_person_history_update:
pattern: /person/{person_id}/history/{history_id}/update
pattern: /person/history/{history_id}/update
defaults: { _controller: ChillPersonBundle:History:update }
chill_person_history_close:

View File

@ -6,3 +6,11 @@ services:
class: Chill\PersonBundle\Routing\RoutesLoader
tags:
- { name: routing.loader }
chill.person.accompanying_period_closing_motive:
class: Chill\PersonBundle\Form\Type\ClosingMotiveType
scope: request
arguments:
- "@request"
tags:
- { name: form.type, alias: closing_motive }

View File

@ -4,7 +4,6 @@
{% block title %}{% endblock title %}
{% form_theme form 'CLChillMainBundle:Form:fields.html.twig' %}
{% block personcontent %}
@ -12,12 +11,16 @@
{{ 'views.Person.close.last_opening_since'|trans(
{ '%last_opening%' : history.dateOpening|date(date_format) }) }}
{% if form.dateClosing is defined %}
{{ form_row(form.dateClosing, {'label' : 'views.Person.close.date_of_closing'} ) }}
{% endif %}
{{ form_row(form.dateClosing, {'label' : 'views.Person.close.date_of_closing'} ) }}
{{ form_row(form.motive, {'label' : 'views.Person.close.motive_of_closing'} ) }}
{{ form_row(form.texto, {'label' : 'views.Person.close.texto' } ) }}
{% if form.closingMotive is defined %}
{{ form_row(form.closingMotive, {'label' : 'views.Person.close.motive_of_closing'} ) }}
{% endif %}
{{ form_row(form.memo, {'label' : 'views.Person.close.texto' } ) }}
{{ form_rest(form) }}

View File

@ -32,11 +32,7 @@
{% endspaceless %}</td>
<td>
<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 %}
<a href="{{ path('chill_person_history_update', {'person_id' : person.id, 'history_id' : history.id } ) }}">{{ 'views.Person.hlist.edit'|trans }}</a>
</div>
</td>
</tr>
@ -58,7 +54,7 @@
<div class="form_control">
<div class="btn small warning icon-right entypo icon-plus">
<a href="{{ path ('chill_person_history_create', {'personId' : person.id } ) }}">
<a href="{{ path ('chill_person_history_create', {'person_id' : person.id } ) }}">
{{ 'views.Person.hlist.create'|trans }}
</a>
</div>
@ -72,11 +68,11 @@
{% endif %}{% endspaceless %}">
{% spaceless %}
{% if person.isOpen == true %}
<a href="{{ path('chill_person_history_close', {'id' : person.id}) }}">
<a href="{{ path('chill_person_history_close', {'person_id' : person.id}) }}">
{{'views.Person.hlist.close'|trans }}
</a>
{% else %}
<a href="{{ path('chill_person_history_open', {'id' : person.id} ) }}">
<a href="{{ path('chill_person_history_open', {'person_id' : person.id} ) }}">
{{'views.Person.hlist.open'|trans }}
</a>
{% endif %}

View File

@ -1,26 +0,0 @@
{% extends "ChillPersonBundle::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.dateOpening, {'label' : 'views.Person.open.date_of_opening'} ) }}
{{ form_row(form.texto, {'label' : 'views.Person.open.texto' } ) }}
{{ form_rest(form) }}
<div class="medium btn danger icon-right entypo icon-lock">
<button type="submit">{{ 'views.Person.open.action'|trans }}</button>
</div>
{{ form_end(form) }}
{% endblock personcontent %}

View File

@ -1,29 +0,0 @@
{% extends "ChillPersonBundle::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 %}