]> Raphaƫl G. Git Repositories - airbundle/blob - Form/ApplicationType.php
Add register template
[airbundle] / Form / ApplicationType.php
1 <?php
2
3 namespace Rapsys\AirBundle\Form;
4
5 use Symfony\Component\Form\AbstractType;
6 use Symfony\Component\Form\FormBuilderInterface;
7 use Symfony\Component\OptionsResolver\OptionsResolver;
8 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
9 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
10 use Symfony\Component\Form\Extension\Core\Type\DateType;
11 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
12 use Symfony\Component\Validator\Constraints\Date;
13 use Symfony\Component\Validator\Constraints\NotBlank;
14 use Symfony\Bridge\Doctrine\RegistryInterface;
15 use Symfony\Component\Translation\TranslatorInterface;
16 use Rapsys\AirBundle\Entity\User;
17 use Rapsys\AirBundle\Entity\Slot;
18 use Rapsys\AirBundle\Entity\Location;
19
20 class ApplicationType extends AbstractType {
21 //Doctrine instance
22 private $doctrine;
23
24 //Translator instance
25 protected $translator;
26
27 public function __construct(RegistryInterface $doctrine, TranslatorInterface $translator) {
28 $this->doctrine = $doctrine;
29 $this->translator = $translator;
30 }
31
32 /**
33 * {@inheritdoc}
34 */
35 public function buildForm(FormBuilderInterface $builder, array $options) {
36 //Retrieve translated slot
37 #$slots = $this->doctrine->getRepository(Slot::class)->findAllWithTranslatedTitle($this->translator);
38
39 //Create base form
40 $form = $builder
41 ->add('date', DateType::class, ['attr' => ['placeholder' => 'Your date', 'class' => 'date'], 'html5' => true, 'input' => 'datetime', 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', 'data' => new \DateTime('+7 day'), 'constraints' => [new NotBlank(['message' => 'Please provide your date']), new Date(['message' => 'Your date doesn\'t seems to be valid'])]])
42 #->add('slot', ChoiceType::class, ['attr' => ['placeholder' => 'Your slot'], 'constraints' => [new NotBlank(['message' => 'Please provide your slot'])], 'choices' => $slots, 'data' => $options['slot']])
43 ->add('slot', EntityType::class, ['class' => 'RapsysAirBundle:Slot', 'attr' => ['placeholder' => 'Your slot'], 'constraints' => [new NotBlank(['message' => 'Please provide your slot'])], 'choice_translation_domain' => true, 'data' => $options['slot']])
44 ->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
45
46 //Add extra user field
47 if (!empty($options['admin'])) {
48 $users = $this->doctrine->getRepository(User::class)->findAllWithTranslatedGroupAndCivility($this->translator);
49 $form
50 ->add('location', EntityType::class, ['class' => 'RapsysAirBundle:Location', 'attr' => ['placeholder' => 'Your location'], 'choice_translation_domain' => true, 'constraints' => [new NotBlank(['message' => 'Please provide your location'])], 'data' => $options['location']])
51 ->add('user', ChoiceType::class, ['attr' => ['placeholder' => 'Your user'], 'choice_translation_domain' => false, 'constraints' => [new NotBlank(['message' => 'Please provide your user'])], 'choices' => $users, 'data' => $options['user']]);
52 //Add user locations
53 } else {
54 $locations = $this->doctrine->getRepository(Location::class)->findByUserId($options['user']);
55 $form->add('location', EntityType::class, ['class' => 'RapsysAirBundle:Location', 'choices' => $locations, 'attr' => ['placeholder' => 'Your location'], 'choice_translation_domain' => true, 'constraints' => [new NotBlank(['message' => 'Please provide your location'])], 'data' => $options['location']]);
56 }
57
58 //Return form
59 return $form;
60 }
61
62 /**
63 * {@inheritdoc}
64 */
65 public function configureOptions(OptionsResolver $resolver) {
66 //XXX: 1 should be the first user
67 $resolver->setDefaults(['error_bubbling' => true, 'admin' => false, 'slot' => null, 'location' => null, 'user' => 1]);
68 $resolver->setAllowedTypes('admin', 'boolean');
69 $resolver->setAllowedTypes('location', [Location::class, 'null']);
70 $resolver->setAllowedTypes('slot', [Slot::class, 'null']);
71 $resolver->setAllowedTypes('user', 'integer');
72 }
73
74 /**
75 * {@inheritdoc}
76 */
77 public function getName() {
78 return 'rapsys_air_application';
79 }
80 }