2021-02-03 18:50:34 +01:00
parent 03cafbf4c6
commit c76e401021
6 changed files with 152 additions and 78 deletions

View File

@@ -2,6 +2,7 @@
namespace Chill\PersonBundle\Form;
use Chill\MainBundle\Entity\Center;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -16,6 +17,11 @@ use Chill\MainBundle\Form\Type\UserPickerType;
use Symfony\Component\Security\Core\Role\Role;
use Chill\PersonBundle\Form\Type\ClosingMotivePickerType;
/**
* Class AccompanyingPeriodType
*
* @package Chill\PersonBundle\Form
*/
class AccompanyingPeriodType extends AbstractType
{
/**
@@ -26,7 +32,7 @@ class AccompanyingPeriodType extends AbstractType
*
* @var string[]
*/
protected $config = array();
protected $config = [];
/**
*
@@ -46,35 +52,36 @@ class AccompanyingPeriodType extends AbstractType
//if the period_action is close, date opening should not be seen
if ($options['period_action'] !== 'close') {
$builder
->add('openingDate', DateType::class, array(
->add('openingDate', DateType::class, [
"required" => true,
'widget' => 'single_text',
'format' => 'dd-MM-yyyy'
));
])
;
}
// 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'] === 'create')
OR
($options['period_action'] === 'update' AND !$accompanyingPeriod->isOpen())
) {
$form->add('closingDate', DateType::class, array('required' => true,
'widget' => 'single_text', 'format' => 'dd-MM-yyyy'));
$form->add('closingMotive', ClosingMotivePickerType::class);
}
});
// closingDate should be seen only if
// period_action = close
// OR ( period_action = update AND accompanying period is already closed )
$accompanyingPeriod = $options['data'];
if (
($options['period_action'] === 'close')
OR
($options['period_action'] === 'create')
OR
($options['period_action'] === 'update' AND !$accompanyingPeriod->isOpen())
) {
$builder->add('closingDate', DateType::class, [
'required' => true,
'widget' => 'single_text',
'format' => 'dd-MM-yyyy'
]);
$builder->add('closingMotive', ClosingMotivePickerType::class);
}
if ($this->config['user'] === 'visible') {
$builder->add('user', UserPickerType::class, [
'center' => $options['center'],
@@ -82,10 +89,9 @@ class AccompanyingPeriodType extends AbstractType
]);
}
$builder->add('remark', TextareaType::class, array(
'required' => false
))
;
$builder->add('remark', TextareaType::class, [
'required' => false
]);
}
/**
@@ -93,20 +99,24 @@ class AccompanyingPeriodType extends AbstractType
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
$resolver->setDefaults([
'data_class' => 'Chill\PersonBundle\Entity\AccompanyingPeriod'
));
]);
$resolver
->setRequired(array('period_action'))
->setRequired(['period_action'])
->addAllowedTypes('period_action', 'string')
->addAllowedValues('period_action', array(
'update', 'open', 'close', 'create'))
->addAllowedValues('period_action', ['update', 'open', 'close', 'create'])
->setRequired('center')
->setAllowedTypes('center', \Chill\MainBundle\Entity\Center::class)
->setAllowedTypes('center', Center::class)
;
}
/**
* @param FormView $view
* @param FormInterface $form
* @param array $options
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['action'] = $options['period_action'];