Allow encoding of an activity with configuration preset durations

fix #11

The activity form allow to receive pre-configured activity durations.

Those duration can be configured through the option
chill_activity.form.time_duration :

```yaml

chill_activity:
   form:
      time_duration:
         - { label: '12 minutes', seconds: 720 }
         # - ...

```

If a pre-existing activity receives a different time, the time is added
to the list of pre-configured duration time.
This commit is contained in:
2016-03-09 13:38:05 +01:00
parent f28a6e9fa0
commit 77b7333fea
9 changed files with 374 additions and 21 deletions

View File

@@ -11,6 +11,13 @@ use Doctrine\Common\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\DateTimeType;
use Chill\ActivityBundle\Form\Type\TranslatableActivityType;
use Chill\ActivityBundle\Form\Type\TranslatableActivityReason;
class ActivityType extends AbstractType
{
@@ -42,9 +49,14 @@ class ActivityType extends AbstractType
*/
protected $translatableStringHelper;
public function __construct(TokenStorageInterface $tokenStorage,
AuthorizationHelper $authorizationHelper, ObjectManager $om,
TranslatableStringHelper $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");
@@ -53,6 +65,7 @@ class ActivityType extends AbstractType
$this->authorizationHelper = $authorizationHelper;
$this->om = $om;
$this->translatableStringHelper = $translatableStringHelper;
$this->timeChoices = $timeChoices;
}
/**
@@ -61,21 +74,32 @@ class ActivityType extends AbstractType
*/
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,
'choices_as_values' => true,
'placeholder' => 'Choose the duration',
);
$builder
->add('date', 'date', array(
'required' => true,
'widget' => 'single_text',
'format' => 'dd-MM-yyyy')
)
->add('durationTime', 'time', array(
'widget' => 'text',
'hours' => array(0, 1, 2, 3, 4, 5, 6, 7, 8)
))
->add('durationTime', ChoiceType::class, $durationTimeOptions)
->add('remark', 'textarea', array(
'required' => false,
'empty_data' => ''
))
->add('attendee', 'choice', array(
->add('attendee', ChoiceType::class, array(
'expanded' => true,
'required' => false,
'choices' => array(
@@ -85,17 +109,61 @@ class ActivityType extends AbstractType
))
->add('user')
->add('scope')
->add('reasons', 'translatable_activity_reason', array(
->add('reasons', TranslatableActivityReason::class, array(
'multiple' => true,
'required' => false
))
->add('type', 'translatable_activity_type')
//->add('person')
->add('type', TranslatableActivityType::class)
;
$this->appendScopeChoices($builder, $options['role'],
$options['center'], $this->user, $this->authorizationHelper,
$this->translatableStringHelper, $this->om);
$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());
}
});
}
/**