]> Raphaƫl G. Git Repositories - airbundle/blob - Form/SessionType.php
Protect from getLocale call on empty request happening in console commands
[airbundle] / Form / SessionType.php
1 <?php
2
3 namespace Rapsys\AirBundle\Form;
4
5 use Doctrine\Persistence\ManagerRegistry;
6 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
7 use Symfony\Component\Form\AbstractType;
8 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
9 use Symfony\Component\Form\Extension\Core\Type\DateType;
10 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
11 use Symfony\Component\Form\Extension\Core\Type\TimeType;
12 use Symfony\Component\Form\FormBuilderInterface;
13 use Symfony\Component\OptionsResolver\OptionsResolver;
14 use Symfony\Component\Translation\TranslatorInterface;
15 use Symfony\Component\Validator\Constraints\Type;
16 use Symfony\Component\Validator\Constraints\NotBlank;
17
18 use Rapsys\AirBundle\Entity\Dance;
19 use Rapsys\AirBundle\Entity\Location;
20 use Rapsys\AirBundle\Entity\Slot;
21 use Rapsys\AirBundle\Entity\User;
22
23 class SessionType extends AbstractType {
24 //Doctrine instance
25 private $doctrine;
26
27 /**
28 * {@inheritdoc}
29 */
30 public function __construct(ManagerRegistry $doctrine) {
31 $this->doctrine = $doctrine;
32 }
33
34 /**
35 * @todo: clean that shit
36 * @todo: mapped => false for each button not related with session !!!!
37 * @todo: set stuff in the SessionController, no loggic here please !!!
38 * @todo: add the dance link stuff
39 *
40 * {@inheritdoc}
41 */
42 public function buildForm(FormBuilderInterface $builder, array $options) {
43 //Is admin or user with rainfall >= 2
44 if (!empty($options['raincancel'])) {
45 //Add raincancel item
46 $builder->add('raincancel', SubmitType::class, ['label' => 'Rain cancel', 'attr' => ['class' => 'submit']]);
47 //Is admin
48 } elseif (!empty($options['admin'])) {
49 //Add forcecancel item
50 $builder->add('forcecancel', SubmitType::class, ['label' => 'Force cancel', 'attr' => ['class' => 'submit']]);
51 }
52
53 //Is admin or owner
54 if (!empty($options['modify'])) {
55 if (!empty($options['admin'])) {
56 $builder
57 //Add dance field
58 ->add('dance', EntityType::class, ['class' => 'Rapsys\AirBundle\Entity\Dance', 'choices' => $options['dance_choices'], 'preferred_choices' => $options['dance_favorites'], 'attr' => ['placeholder' => 'Your dance'], 'choice_translation_domain' => true, 'constraints' => [new NotBlank(['message' => 'Please provide your dance'])], 'data' => $options['dance_default']])
59
60 //Add slot field
61 ->add('slot', EntityType::class, ['class' => 'Rapsys\AirBundle\Entity\Slot', 'attr' => ['placeholder' => 'Your slot'], 'constraints' => [new NotBlank(['message' => 'Please provide your slot'])], 'choice_translation_domain' => true, 'data' => $options['slot_default']])
62 //Add date field
63 ->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 Type(['type' => \DateTime::class, 'message' => 'Your date doesn\'t seems to be valid'])]]);
64 }
65
66 $builder
67 //TODO: avertissement + minimum et maximum ???
68 ->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 Type(['type' => \DateTime::class, 'message' => 'Your begin doesn\'t seems to be valid'])]])
69 //TODO: avertissement + minimum et maximum ???
70 ->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 Type(['type' => \DateTime::class, 'message' => 'Your length doesn\'t seems to be valid'])]])
71 ->add('modify', SubmitType::class, ['label' => 'Modify', 'attr' => ['class' => 'submit']]);
72 }
73
74 //Is admin or applicant
75 if (!empty($options['cancel'])) {
76 $builder->add('cancel', SubmitType::class, ['label' => 'Cancel', 'attr' => ['class' => 'submit']]);
77 }
78
79 //Is admin or senior owner
80 if (!empty($options['move'])) {
81 //Load locations
82 $locations = $this->doctrine->getRepository(Location::class)->findComplementBySessionId($options['session']);
83 $builder
84 //TODO: class senior en orange ???
85 ->add('location', ChoiceType::class, ['attr' => ['placeholder' => 'Your location'], 'constraints' => [new NotBlank(['message' => 'Please provide your location'])], 'choices' => $locations, 'choice_translation_domain' => true])
86 ->add('move', SubmitType::class, ['label' => 'Move', 'attr' => ['class' => 'submit']]);
87 }
88
89 //Add extra user field
90 if (!empty($options['admin'])) {
91 //Load users
92 $users = $this->doctrine->getRepository(User::class)->findBySessionId($options['session']);
93 //Add admin fields
94 $builder
95 //TODO: class admin en rouge ???
96 ->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])
97 ->add('lock', SubmitType::class, ['label' => 'Lock', 'attr' => ['class' => 'submit']]);
98
99 //Is admin and locked === null
100 if (!empty($options['attribute'])) {
101 //Add attribute fields
102 $builder
103 ->add('attribute', SubmitType::class, ['label' => 'Attribute', 'attr' => ['class' => 'submit']])
104 ->add('autoattribute', SubmitType::class, ['label' => 'Auto attribute', 'attr' => ['class' => 'submit']]);
105 }
106 }
107
108 //Return form
109 return $builder;
110 }
111
112 /**
113 * {@inheritdoc}
114 */
115 public function configureOptions(OptionsResolver $resolver) {
116 $resolver->setDefaults(['error_bubbling' => true, 'admin' => false, 'dance_choices' => [], 'dance_default' => null, 'dance_favorites' => [], 'date' => null, 'begin' => null, 'length' => null, 'cancel' => false, 'raincancel' => false, 'modify' => false, 'move' => false, 'attribute' => false, 'user' => null, 'session' => null, 'slot_default' => null]);
117
118 //Add admin
119 $resolver->setAllowedTypes('admin', 'boolean');
120
121 //Add dance choices
122 $resolver->setAllowedTypes('dance_choices', 'array');
123
124 //Add dance default
125 $resolver->setAllowedTypes('dance_default', [Dance::class, 'null']);
126
127 //Add dance favorites
128 $resolver->setAllowedTypes('dance_favorites', 'array');
129
130 //Add date
131 $resolver->setAllowedTypes('date', 'datetime');
132
133 //Add begin
134 $resolver->setAllowedTypes('begin', 'datetime');
135
136 //Add length
137 $resolver->setAllowedTypes('length', 'datetime');
138
139 //Add cancel
140 $resolver->setAllowedTypes('cancel', 'boolean');
141
142 //Add raincancel
143 $resolver->setAllowedTypes('raincancel', 'boolean');
144
145 //Add modify
146 $resolver->setAllowedTypes('modify', 'boolean');
147
148 //Add move
149 $resolver->setAllowedTypes('move', 'boolean');
150
151 //Add attribute
152 $resolver->setAllowedTypes('attribute', 'boolean');
153
154 //Add user
155 $resolver->setAllowedTypes('user', 'integer');
156
157 //Add session
158 $resolver->setAllowedTypes('session', 'integer');
159 }
160
161 /**
162 * {@inheritdoc}
163 */
164 public function getName() {
165 return 'rapsys_air_session_edit';
166 }
167 }