upgrade and refactor personhistoryFile

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

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.']'
)
);
}
}