mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
195 lines
7.3 KiB
PHP
195 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace Chill\ActivityBundle\Form;
|
|
|
|
use Symfony\Component\Form\AbstractType;
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
|
use Doctrine\Persistence\ObjectManager;
|
|
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
|
use Chill\MainBundle\Entity\User;
|
|
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
|
|
use Symfony\Component\Form\FormEvent;
|
|
use Symfony\Component\Form\FormEvents;
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
|
use Chill\ActivityBundle\Form\Type\TranslatableActivityType;
|
|
use Chill\ActivityBundle\Form\Type\TranslatableActivityReason;
|
|
use Chill\MainBundle\Form\Type\UserPickerType;
|
|
use Chill\MainBundle\Form\Type\ScopePickerType;
|
|
use Chill\MainBundle\Form\Type\ChillDateType;
|
|
|
|
class ActivityType extends AbstractType
|
|
{
|
|
|
|
/**
|
|
* the user running this form
|
|
*
|
|
* @var User
|
|
*/
|
|
protected $user;
|
|
|
|
/**
|
|
*
|
|
* @var AuthorizationHelper
|
|
*/
|
|
protected $authorizationHelper;
|
|
|
|
/**
|
|
*
|
|
* @var ObjectManager
|
|
*/
|
|
protected $om;
|
|
|
|
/**
|
|
*
|
|
* @var TranslatableStringHelper
|
|
*/
|
|
protected $translatableStringHelper;
|
|
|
|
protected $timeChoices;
|
|
|
|
public function __construct(
|
|
TokenStorageInterface $tokenStorage,
|
|
AuthorizationHelper $authorizationHelper, ObjectManager $om,
|
|
TranslatableStringHelper $translatableStringHelper,
|
|
array $timeChoices
|
|
)
|
|
{
|
|
if (!$tokenStorage->getToken()->getUser() instanceof User) {
|
|
throw new \RuntimeException("you should have a valid user");
|
|
}
|
|
$this->user = $tokenStorage->getToken()->getUser();
|
|
$this->authorizationHelper = $authorizationHelper;
|
|
$this->om = $om;
|
|
$this->translatableStringHelper = $translatableStringHelper;
|
|
$this->timeChoices = $timeChoices;
|
|
}
|
|
|
|
/**
|
|
* @param FormBuilderInterface $builder
|
|
* @param array $options
|
|
*/
|
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
|
{
|
|
// handle times choices
|
|
$timeChoices = array();
|
|
|
|
foreach ($this->timeChoices as $e) {
|
|
$timeChoices[$e['label']] = $e['seconds'];
|
|
};
|
|
|
|
$durationTimeTransformer = new DateTimeToTimestampTransformer('GMT', 'GMT');
|
|
$durationTimeOptions = array(
|
|
'choices' => $timeChoices,
|
|
'placeholder' => 'Choose the duration',
|
|
);
|
|
|
|
$builder
|
|
->add('date', ChillDateType::class, array(
|
|
'required' => true
|
|
))
|
|
->add('durationTime', ChoiceType::class, $durationTimeOptions)
|
|
->add('remark', TextareaType::class, array(
|
|
'required' => false,
|
|
'empty_data' => ''
|
|
))
|
|
->add('attendee', ChoiceType::class, array(
|
|
'expanded' => true,
|
|
'required' => false,
|
|
'choices' => array(
|
|
'present' => true,
|
|
'not present' => false
|
|
)
|
|
))
|
|
->add('user', UserPickerType::class, [
|
|
'center' => $options['center'],
|
|
'role' => $options['role']
|
|
])
|
|
->add('scope', ScopePickerType::class, [
|
|
'center' => $options['center'],
|
|
'role' => $options['role']
|
|
])
|
|
->add('reasons', TranslatableActivityReason::class, array(
|
|
'multiple' => true,
|
|
'required' => false,
|
|
))
|
|
->add('type', TranslatableActivityType::class, array(
|
|
'placeholder' => 'Choose a type',
|
|
'active_only' => true
|
|
))
|
|
;
|
|
|
|
$builder->get('durationTime')
|
|
->addModelTransformer($durationTimeTransformer);
|
|
|
|
|
|
$builder->get('durationTime')
|
|
->addEventListener(
|
|
FormEvents::PRE_SET_DATA,
|
|
function(FormEvent $formEvent) use (
|
|
$timeChoices,
|
|
$builder,
|
|
$durationTimeTransformer,
|
|
$durationTimeOptions
|
|
)
|
|
{
|
|
// set the timezone to GMT, and fix the difference between current and GMT
|
|
// the datetimetransformer will then handle timezone as GMT
|
|
$timezoneUTC = new \DateTimeZone('GMT');
|
|
/* @var $data \DateTime */
|
|
$data = $formEvent->getData() === NULL ?
|
|
\DateTime::createFromFormat('U', 300) :
|
|
$formEvent->getData();
|
|
$seconds = $data->getTimezone()->getOffset($data);
|
|
$data->setTimeZone($timezoneUTC);
|
|
$data->add(new \DateInterval('PT'.$seconds.'S'));
|
|
|
|
// test if the timestamp is in the choices.
|
|
// If not, recreate the field with the new timestamp
|
|
if (!in_array($data->getTimestamp(), $timeChoices)) {
|
|
// the data are not in the possible values. add them
|
|
$timeChoices[$data->format('H:i')] = $data->getTimestamp();
|
|
$form = $builder->create(
|
|
'durationTime',
|
|
ChoiceType::class,
|
|
array_merge(
|
|
$durationTimeOptions,
|
|
array(
|
|
'choices' => $timeChoices,
|
|
'auto_initialize' => false
|
|
)
|
|
));
|
|
$form->addModelTransformer($durationTimeTransformer);
|
|
$formEvent->getForm()->getParent()->add($form->getForm());
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param OptionsResolverInterface $resolver
|
|
*/
|
|
public function configureOptions(OptionsResolver $resolver)
|
|
{
|
|
$resolver->setDefaults(array(
|
|
'data_class' => 'Chill\ActivityBundle\Entity\Activity'
|
|
));
|
|
|
|
$resolver
|
|
->setRequired(array('center', 'role'))
|
|
->setAllowedTypes('center', 'Chill\MainBundle\Entity\Center')
|
|
->setAllowedTypes('role', 'Symfony\Component\Security\Core\Role\Role')
|
|
;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getBlockPrefix()
|
|
{
|
|
return 'chill_activitybundle_activity';
|
|
}
|
|
}
|