]> Raphaƫl G. Git Repositories - airbundle/blob - Form/SessionType.php
Upgrade date constraint to type constraint
[airbundle] / Form / SessionType.php
1 <?php
2
3 namespace Rapsys\AirBundle\Form;
4
5 use Symfony\Bridge\Doctrine\RegistryInterface;
6 use Symfony\Component\Form\AbstractType;
7 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
8 use Symfony\Component\Form\Extension\Core\Type\DateType;
9 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
10 use Symfony\Component\Form\Extension\Core\Type\TimeType;
11 use Symfony\Component\Form\FormBuilderInterface;
12 use Symfony\Component\OptionsResolver\OptionsResolver;
13 use Symfony\Component\Translation\TranslatorInterface;
14 use Symfony\Component\Validator\Constraints\Date;
15 use Symfony\Component\Validator\Constraints\NotBlank;
16 use Symfony\Component\Validator\Constraints\Time;
17
18 use Rapsys\AirBundle\Entity\Location;
19 use Rapsys\AirBundle\Entity\User;
20
21 class SessionType extends AbstractType {
22 //Doctrine instance
23 private $doctrine;
24
25 /**
26 * {@inheritdoc}
27 */
28 public function __construct(RegistryInterface $doctrine) {
29 $this->doctrine = $doctrine;
30 }
31
32 /**
33 * {@inheritdoc}
34 */
35 public function buildForm(FormBuilderInterface $builder, array $options) {
36 //Is admin or user with rainfall >= 2
37 if (!empty($options['raincancel'])) {
38 //Add raincancel item
39 $builder->add('raincancel', SubmitType::class, ['label' => 'Rain cancel', 'attr' => ['class' => 'submit']]);
40 //Is admin
41 } elseif (!empty($options['admin'])) {
42 //Add forcecancel item
43 $builder->add('forcecancel', SubmitType::class, ['label' => 'Force cancel', 'attr' => ['class' => 'submit']]);
44 }
45
46 //Is admin or owner
47 if (!empty($options['modify'])) {
48 if (!empty($options['admin'])) {
49 $builder->add('date', DateType::class, ['attr' => ['placeholder' => 'Your date', 'class' => 'date'], 'html5' => true, 'input' => 'datetime', 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', 'data' => $options['date'], 'constraints' => [new NotBlank(['message' => 'Please provide your date']), new Date(['message' => 'Your date doesn\'t seems to be valid'])]]);
50 }
51
52 $builder
53 //TODO: avertissement + minimum et maximum ???
54 ->add('begin', TimeType::class, ['attr' => ['placeholder' => 'Your begin', 'class' => 'time'], 'html5' => true, 'input' => 'datetime', 'widget' => 'single_text', 'data' => $options['begin'], 'constraints' => [new NotBlank(['message' => 'Please provide your begin']), new Time(['message' => 'Your begin doesn\'t seems to be valid'])]])
55 //TODO: avertissement + minimum et maximum ???
56 ->add('length', TimeType::class, ['attr' => ['placeholder' => 'Your length', 'class' => 'time'], 'html5' => true, 'input' => 'datetime', 'widget' => 'single_text', 'data' => $options['length'], 'constraints' => [new NotBlank(['message' => 'Please provide your length']), new Time(['message' => 'Your length doesn\'t seems to be valid'])]])
57 ->add('modify', SubmitType::class, ['label' => 'Modify', 'attr' => ['class' => 'submit']]);
58 }
59
60 //Is admin or applicant
61 if (!empty($options['cancel'])) {
62 $builder->add('cancel', SubmitType::class, ['label' => 'Cancel', 'attr' => ['class' => 'submit']]);
63 }
64
65 //Is admin or senior owner
66 if (!empty($options['move'])) {
67 //Load locations
68 $locations = $this->doctrine->getRepository(Location::class)->findComplementBySessionId($options['session']);
69 $builder
70 //TODO: class senior en orange ???
71 ->add('location', ChoiceType::class, ['attr' => ['placeholder' => 'Your location'], 'constraints' => [new NotBlank(['message' => 'Please provide your location'])], 'choices' => $locations, 'choice_translation_domain' => true])
72 ->add('move', SubmitType::class, ['label' => 'Move', 'attr' => ['class' => 'submit']]);
73 }
74
75 //Add extra user field
76 if (!empty($options['admin'])) {
77 //Load users
78 $users = $this->doctrine->getRepository(User::class)->findAllApplicantBySession($options['session']);
79 //Add admin fields
80 $builder
81 //TODO: class admin en rouge ???
82 ->add('user', ChoiceType::class, ['attr' => ['placeholder' => 'Your user'], 'constraints' => [new NotBlank(['message' => 'Please provide your user'])], 'choices' => $users, 'data' => $options['user'], 'choice_translation_domain' => false])
83 ->add('lock', SubmitType::class, ['label' => 'Lock', 'attr' => ['class' => 'submit']]);
84
85 //Is admin and locked === null
86 if (!empty($options['attribute'])) {
87 //Add attribute fields
88 $builder
89 ->add('attribute', SubmitType::class, ['label' => 'Attribute', 'attr' => ['class' => 'submit']])
90 ->add('autoattribute', SubmitType::class, ['label' => 'Auto attribute', 'attr' => ['class' => 'submit']]);
91 }
92 }
93
94 //Return form
95 return $builder;
96 }
97
98 /**
99 * {@inheritdoc}
100 */
101 public function configureOptions(OptionsResolver $resolver) {
102 $resolver->setDefaults(['error_bubbling' => true, 'admin' => false, 'date' => null, 'begin' => null, 'length' => null, 'cancel' => false, 'raincancel' => false, 'modify' => false, 'move' => false, 'attribute' => false, 'user' => null, 'session' => null]);
103 $resolver->setAllowedTypes('admin', 'boolean');
104 #TODO: voir si c'est le bon type
105 $resolver->setAllowedTypes('date', 'datetime');
106 $resolver->setAllowedTypes('begin', 'datetime');
107 $resolver->setAllowedTypes('length', 'datetime');
108 $resolver->setAllowedTypes('cancel', 'boolean');
109 $resolver->setAllowedTypes('raincancel', 'boolean');
110 $resolver->setAllowedTypes('modify', 'boolean');
111 $resolver->setAllowedTypes('move', 'boolean');
112 $resolver->setAllowedTypes('attribute', 'boolean');
113 $resolver->setAllowedTypes('user', 'integer');
114 $resolver->setAllowedTypes('session', 'integer');
115 }
116
117 /**
118 * {@inheritdoc}
119 */
120 public function getName() {
121 return 'rapsys_air_session_edit';
122 }
123 }