* * 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; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; 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; /** * A type to create a participation * * If the `event` option is defined, the role will be restricted * * @author Julien Fastré */ class ParticipationType extends AbstractType { /** * * @var TranslatableStringHelper */ protected $translatableStringHelper; public function __construct(TranslatableStringHelper $translatableStringHelper) { $this->translatableStringHelper = $translatableStringHelper; } public function buildForm(FormBuilderInterface $builder, array $options) { // local copy of variable for Closure $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->getLabel()); } )); // 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->getLabel()); } )); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefined('event_type') ->setAllowedTypes('event_type', array('null', EventType::class)) ->setDefault('event_type', 'null'); } }