factorize PickRoleType, PickStatusType and rename TranslatableEventType

This commit is contained in:
2016-04-19 15:30:44 +02:00
parent 78c53fe7b0
commit cf4a0d9e75
8 changed files with 326 additions and 56 deletions

View File

@@ -34,7 +34,7 @@ use Chill\EventBundle\Entity\EventType;
*
* @author Champs-Libres Coop
*/
class TranslatableEventType extends AbstractType
class PickEventType extends AbstractType
{
/**
* @var TranslatableStringHelper
@@ -64,6 +64,9 @@ class TranslatableEventType extends AbstractType
'choice_label' => function (EventType $t) use ($helper) {
return $helper->localize($t->getName());
},
'choice_attrs' => function (EventType $t) {
return array('data-link-category' => $t->getId());
}
)
);
}

144
Form/Type/PickRoleType.php Normal file
View File

@@ -0,0 +1,144 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2016, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\EventBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Chill\EventBundle\Entity\Role;
use Chill\EventBundle\Entity\EventType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
/**
* Allow to pick a choice amongst different choices
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
*/
class PickRoleType extends AbstractType
{
/**
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
/**
*
* @var TranslatorInterface
*/
protected $translator;
/**
*
* @var EntityRepository
*/
protected $roleRepository;
public function __construct(
TranslatableStringHelper $translatableStringHelper,
TranslatorInterface $translator,
EntityRepository $roleRepository
) {
$this->translatableStringHelper = $translatableStringHelper;
$this->translator = $translator;
$this->roleRepository = $roleRepository;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// create copy for use in Closure
$translatableStringHelper = $this->translatableStringHelper;
// create copy for easier management
$qb = $options['query_builder'];
if ($options['event_type'] instanceof EventType) {
$options['query_builder']->where($qb->expr()->eq('r.type', ':event_type'))
->setParameter('event_type', $options['event_type']);
}
if ($options['active_only'] === true) {
$options['query_builder']->andWhere($qb->expr()->eq('r.active', ':active'))
->setParameter('active', true);
}
// if ($options['show_group_type'] === true) {
// $closure = $options['choice_label'];
// $options['choice_label'] = function(Role $r)
// use ($translatableStringHelper, $closure) {
// return $translatableStringHelper->localize($r->get)
//
// }
// }
}
public function configureOptions(OptionsResolver $resolver)
{
// create copy for use in Closure
$translatableStringHelper = $this->translatableStringHelper;
$translator = $this->translator;
$resolver
// add option "event_type"
->setDefined('event_type')
->setAllowedTypes('event_type', array('null', EventType::class))
->setDefault('event_type', null)
// add option allow unactive
->setDefault('active_only', true)
->setAllowedTypes('active_only', array('boolean'))
// add possibility to prepend by group_type
->setDefault('show_group_type', false)
->setAllowedTypes('show_group_type', array('boolean'))
;
$qb = $this->roleRepository->createQueryBuilder('r');
$resolver->setDefaults(array(
'class' => Role::class,
'query_builder' => $qb,
'choice_attr' => function(Role $r) {
return array(
'data-event-type' => $r->getType()->getId(),
'data-link-category' => $r->getType()->getId()
);
},
'choice_label' => function(Role $r)
use ($translatableStringHelper, $translator) {
return $translatableStringHelper->localize($r->getName()).
($r->getActive() === true ? '' :
' ('.$translator->trans('unactive').')');
}
));
}
public function getParent()
{
return EntityType::class;
}
}

View File

@@ -0,0 +1,134 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2016, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\EventBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Chill\EventBundle\Entity\Status;
use Chill\EventBundle\Entity\EventType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
/**
* Allow to pick amongst type
*
* parameters :
*
* - event_type : restricts to a certain event type. Default null (= all event types)
* - active_only: restricts to active type only. Default true
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
*/
class PickStatusType extends AbstractType
{
/**
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
/**
*
* @var TranslatorInterface
*/
protected $translator;
/**
*
* @var EntityRepository
*/
protected $statusRepository;
public function __construct(
TranslatableStringHelper $translatableStringHelper,
TranslatorInterface $translator,
EntityRepository $statusRepository
) {
$this->translatableStringHelper = $translatableStringHelper;
$this->translator = $translator;
$this->statusRepository = $statusRepository;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$qb = $options['query_builder'];
if ($options['event_type'] instanceof EventType) {
$options['query_builder']->where($qb->expr()->eq('r.type', ':event_type'))
->setParameter('event_type', $options['event_type']);
}
if ($options['active_only'] === true) {
$options['query_builder']->andWhere($qb->expr()->eq('r.active', ':active'))
->setParameter('active', true);
}
}
public function configureOptions(OptionsResolver $resolver)
{
// create copy for use in Closure
$translatableStringHelper = $this->translatableStringHelper;
$translator = $this->translator;
$resolver
// add option "event_type"
->setDefined('event_type')
->setAllowedTypes('event_type', array('null', EventType::class))
->setDefault('event_type', null)
// add option allow unactive
->setDefault('active_only', true)
->setAllowedTypes('active_only', array('boolean'))
;
$qb = $this->statusRepository->createQueryBuilder('r');
$resolver->setDefaults(array(
'class' => Status::class,
'query_builder' => $qb,
'choice_attr' => function(Status $s) {
return array(
'data-event-type' => $s->getType()->getId()
);
},
'choice_label' => function(Status $s)
use ($translatableStringHelper, $translator) {
return $translatableStringHelper->localize($s->getName()).
($s->getActive() === true ? '' :
' ('.$translator->trans('unactive').')');
}
));
}
public function getParent()
{
return EntityType::class;
}
}