factorize PickRoleType, PickStatusType and rename TranslatableEventType

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

View File

@ -5,7 +5,7 @@ namespace Chill\EventBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Chill\EventBundle\Form\Type\TranslatableEventType;
use Chill\EventBundle\Form\Type\PickEventType;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Doctrine\Common\Persistence\ObjectManager;
@ -107,7 +107,7 @@ class EventType extends AbstractType
);
},
))
->add('type', TranslatableEventType::class)
->add('type', PickEventType::class)
;
}

View File

@ -26,8 +26,9 @@ use Chill\EventBundle\Entity\EventType;
use Chill\EventBundle\Entity\Status;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Chill\EventBundle\Entity\Role;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\EventBundle\Form\Type\PickRoleType;
use Chill\EventBundle\Form\Type\PickStatusType;
/**
* A type to create a participation
@ -55,55 +56,13 @@ class ParticipationType extends AbstractType
$translatableStringHelper = $this->translatableStringHelper;
// add role
$builder->add('role', EntityType::class, array(
'class' => Role::class,
'query_builder' => function (EntityRepository $er) use ($options) {
$qb = $er->createQueryBuilder('r');
if ($options['event_type'] instanceof EventType) {
$qb->where($qb->expr()->eq('r.type', ':event_type'))
->setParameter('event_type', $options['event_type']);
}
$qb->andWhere($qb->expr()->eq('r.active', ':active'))
->setParameter('active', true);
return $qb;
},
'choice_attr' => function(Role $r) {
return array(
'data-event-type' => $r->getType()->getId()
);
},
'choice_label' => function(Role $r) use ($translatableStringHelper) {
return $translatableStringHelper->localize($r->getName());
}
));
$builder->add('role', PickRoleType::class, array(
'event_type' => $options['event_type']
));
// add a status
$builder->add('status', EntityType::class, array(
'class' => Status::class,
'choice_attr' => function(Status $s) {
return array(
'data-event-type' => $s->getType()->getId()
);
},
'query_builder' => function (EntityRepository $er) use ($options) {
$qb = $er->createQueryBuilder('s');
if ($options['event_type'] instanceof EventType) {
$qb->where($qb->expr()->eq('s.type', ':event_type'))
->setParameter('event_type', $options['event_type']);
}
$qb->andWhere($qb->expr()->eq('s.active', ':active'))
->setParameter('active', true);
return $qb;
},
'choice_label' => function(Status $s) use ($translatableStringHelper) {
return $translatableStringHelper->localize($s->getName());
}
$builder->add('status', PickStatusType::class, array(
'event_type' => $options['event_type']
));
}

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;
}
}

View File

@ -1,6 +1,6 @@
services:
chill.event.form.type.translatable_event_type:
class: Chill\EventBundle\Form\Type\TranslatableEventType
chill.event.form.type.pick_event_type:
class: Chill\EventBundle\Form\Type\PickEventType
arguments:
- "@chill.main.helper.translatable_string"
tags:
@ -21,3 +21,21 @@ services:
- "@chill.main.helper.translatable_string"
tags:
- { name: form.type }
chill.event.form.pick_role_type:
class: Chill\EventBundle\Form\Type\PickRoleType
arguments:
- "@chill.main.helper.translatable_string"
- "@translator"
- "@chill_event.repository.role"
tags:
- { name: form.type }
chill.event.form.pick_status_type:
class: Chill\EventBundle\Form\Type\PickStatusType
arguments:
- "@chill.main.helper.translatable_string"
- "@translator"
- "@chill_event.repository.status"
tags:
- { name: form.type }

View File

@ -1,6 +1,18 @@
services:
chill_group.repository.event:
chill_event.repository.event:
class: Doctrine\ORM\EntityRepository
factory: ['@doctrine.orm.entity_manager', getRepository]
arguments:
- 'Chill\EventBundle\Entity\Event'
- 'Chill\EventBundle\Entity\Event'
chill_event.repository.role:
class: Doctrine\ORM\EntityRepository
factory: ['@doctrine.orm.entity_manager', getRepository]
arguments:
- 'Chill\EventBundle\Entity\Role'
chill_event.repository.status:
class: Doctrine\ORM\EntityRepository
factory: ['@doctrine.orm.entity_manager', getRepository]
arguments:
- 'Chill\EventBundle\Entity\Status'

View File

@ -3,7 +3,7 @@ services:
class: Chill\EventBundle\Search\EventSearch
arguments:
- "@security.token_storage"
- "@chill_group.repository.event"
- "@chill_event.repository.event"
- "@chill.main.security.authorization.helper"
- "@templating"
tags: