From cf4a0d9e75542f122d26453198a489a473c7d2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 19 Apr 2016 15:30:44 +0200 Subject: [PATCH] factorize PickRoleType, PickStatusType and rename TranslatableEventType --- Form/EventType.php | 4 +- Form/ParticipationType.php | 55 +------ ...latableEventType.php => PickEventType.php} | 5 +- Form/Type/PickRoleType.php | 144 ++++++++++++++++++ Form/Type/PickStatusType.php | 134 ++++++++++++++++ Resources/config/services/forms.yml | 22 ++- Resources/config/services/repositories.yml | 16 +- Resources/config/services/search.yml | 2 +- 8 files changed, 326 insertions(+), 56 deletions(-) rename Form/Type/{TranslatableEventType.php => PickEventType.php} (91%) create mode 100644 Form/Type/PickRoleType.php create mode 100644 Form/Type/PickStatusType.php diff --git a/Form/EventType.php b/Form/EventType.php index a3417a81e..2d4895efa 100644 --- a/Form/EventType.php +++ b/Form/EventType.php @@ -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) ; } diff --git a/Form/ParticipationType.php b/Form/ParticipationType.php index 5b754dcf9..d7186ac91 100644 --- a/Form/ParticipationType.php +++ b/Form/ParticipationType.php @@ -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'] )); } diff --git a/Form/Type/TranslatableEventType.php b/Form/Type/PickEventType.php similarity index 91% rename from Form/Type/TranslatableEventType.php rename to Form/Type/PickEventType.php index 6b7a80d03..0d41fb2e3 100644 --- a/Form/Type/TranslatableEventType.php +++ b/Form/Type/PickEventType.php @@ -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()); + } ) ); } diff --git a/Form/Type/PickRoleType.php b/Form/Type/PickRoleType.php new file mode 100644 index 000000000..4ea4b46a2 --- /dev/null +++ b/Form/Type/PickRoleType.php @@ -0,0 +1,144 @@ +, + * + * 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 . + */ + +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é + * @author Champs Libres + */ +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; + } +} diff --git a/Form/Type/PickStatusType.php b/Form/Type/PickStatusType.php new file mode 100644 index 000000000..c4bde94e2 --- /dev/null +++ b/Form/Type/PickStatusType.php @@ -0,0 +1,134 @@ +, + * + * 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 . + */ + +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é + * @author Champs Libres + */ +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; + } +} diff --git a/Resources/config/services/forms.yml b/Resources/config/services/forms.yml index 2f622a326..b587bac49 100644 --- a/Resources/config/services/forms.yml +++ b/Resources/config/services/forms.yml @@ -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 } diff --git a/Resources/config/services/repositories.yml b/Resources/config/services/repositories.yml index 44e653cc0..d27130bcd 100644 --- a/Resources/config/services/repositories.yml +++ b/Resources/config/services/repositories.yml @@ -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' \ No newline at end of file + - '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' \ No newline at end of file diff --git a/Resources/config/services/search.yml b/Resources/config/services/search.yml index 99b455bae..a1e0b2f17 100644 --- a/Resources/config/services/search.yml +++ b/Resources/config/services/search.yml @@ -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: