fix deprecations: use choices_as_values=true and flip content of the choice options

This commit is contained in:
nobohan 2018-04-04 17:54:17 +02:00
parent 95f32a8fcb
commit 4143e773b6
2 changed files with 72 additions and 70 deletions

View File

@ -44,39 +44,39 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
*/
class ListActivity implements ListInterface
{
/**
*
* @var EntityManagerInterface
*/
protected $entityManager;
/**
*
* @var TranslatorInterface
*/
protected $translator;
/**
*
* @var TranslatableStringHelper
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
protected $fields = array(
'id',
'date',
'durationTime',
'attendee',
'list_reasons',
'user_username',
'circle_name',
'type_name',
'id',
'date',
'durationTime',
'attendee',
'list_reasons',
'user_username',
'circle_name',
'type_name',
'person_firstname',
'person_lastname',
'person_id'
);
public function __construct(
EntityManagerInterface $em,
TranslatorInterface $translator,
@ -87,10 +87,10 @@ class ListActivity implements ListInterface
$this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
}
/**
* {@inheritDoc}
*
*
* @param FormBuilderInterface $builder
*/
public function buildForm(FormBuilderInterface $builder)
@ -99,6 +99,7 @@ class ListActivity implements ListInterface
'multiple' => true,
'expanded' => true,
'choices' => array_combine($this->fields, $this->fields),
'choices_as_values' => true,
'label' => 'Fields to include in export',
'constraints' => [new Callback(array(
'callback' => function($selected, ExecutionContextInterface $context) {
@ -115,7 +116,7 @@ class ListActivity implements ListInterface
/**
* {@inheritDoc}
*
*
* @return type
*/
public function getAllowedFormattersTypes()
@ -130,35 +131,35 @@ class ListActivity implements ListInterface
public function getLabels($key, array $values, $data)
{
switch ($key)
switch ($key)
{
case 'date' :
return function($value) {
if ($value === '_header') return 'date';
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $value);
return $date->format('d-m-Y');
};
case 'attendee':
return function($value) {
if ($value === '_header') return 'attendee';
return $value ? 1 : 0;
};
case 'list_reasons' :
/* @var $activityReasonsRepository EntityRepository */
$activityRepository = $this->entityManager
->getRepository('ChillActivityBundle:Activity');
return function($value) use ($activityRepository) {
if ($value === '_header') return 'activity reasons';
$activity = $activityRepository
->find($value);
return implode(", ", array_map(function(ActivityReason $r) {
return '"'.
$this->translatableStringHelper->localize($r->getCategory()->getName())
.' > '.
@ -169,21 +170,21 @@ class ListActivity implements ListInterface
case 'circle_name' :
return function($value) {
if ($value === '_header') return 'circle';
return $this->translatableStringHelper
->localize(json_decode($value, true));
};
case 'type_name' :
return function($value) {
if ($value === '_header') return 'activity type';
return $this->translatableStringHelper
->localize(json_decode($value, true));
};
default:
return function($value) use ($key) {
if ($value === '_header') return $key;
return $value;
};
}
@ -212,15 +213,15 @@ class ListActivity implements ListInterface
public function initiateQuery(array $requiredModifiers, array $acl, array $data = array())
{
$centers = array_map(function($el) { return $el['center']; }, $acl);
// throw an error if any fields are present
if (!\array_key_exists('fields', $data)) {
throw new \Doctrine\DBAL\Exception\InvalidArgumentException("any fields "
. "have been checked");
}
$qb = $this->entityManager->createQueryBuilder();
$qb
->from('ChillActivityBundle:Activity', 'activity')
->join('activity.person', 'person')
@ -228,7 +229,7 @@ class ListActivity implements ListInterface
->andWhere('center IN (:authorized_centers)')
->setParameter('authorized_centers', $centers);
;
foreach ($this->fields as $f) {
if (in_array($f, $data['fields'])) {
switch ($f) {
@ -265,12 +266,12 @@ class ListActivity implements ListInterface
$qb->addSelect(sprintf('activity.%s as %s', $f, $f));
break;
}
}
}
return $qb;
}

View File

@ -21,42 +21,42 @@ use Chill\ActivityBundle\Form\Type\TranslatableActivityReason;
class ActivityType extends AbstractType
{
use AppendScopeChoiceTypeTrait;
/**
* 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,
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");
@ -67,7 +67,7 @@ class ActivityType extends AbstractType
$this->translatableStringHelper = $translatableStringHelper;
$this->timeChoices = $timeChoices;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
@ -76,35 +76,36 @@ class ActivityType extends AbstractType
{
// 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',
'required' => true,
'widget' => 'single_text',
'format' => 'dd-MM-yyyy')
)
->add('durationTime', ChoiceType::class, $durationTimeOptions)
->add('remark', 'textarea', array(
'required' => false,
'empty_data' => ''
))
))
->add('attendee', ChoiceType::class, array(
'expanded' => true,
'required' => false,
'choices_as_values' => true,
'choices' => array(
true => 'present',
false => 'not present'
'present' => true,
'not present' => false
)
))
->add('user')
@ -118,44 +119,44 @@ class ActivityType extends AbstractType
'active_only' => true
))
;
$this->appendScopeChoices($builder, $options['role'],
$options['center'], $this->user, $this->authorizationHelper,
$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,
FormEvents::PRE_SET_DATA,
function(FormEvent $formEvent) use (
$timeChoices,
$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 ?
$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.
// 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,
'durationTime',
ChoiceType::class,
array_merge(
$durationTimeOptions,
array(
@ -168,7 +169,7 @@ class ActivityType extends AbstractType
}
});
}
/**
* @param OptionsResolverInterface $resolver
*/
@ -177,7 +178,7 @@ class ActivityType extends AbstractType
$resolver->setDefaults(array(
'data_class' => 'Chill\ActivityBundle\Entity\Activity'
));
$this->appendScopeChoicesOptions($resolver);
}