]> Raphaƫl G. Git Repositories - airbundle/blob - Form/SessionType.php
Cleanup
[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\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(ManagerRegistry $doctrine) {
29 $this->doctrine = $doctrine;
30 }
31
32 /**
33 * @todo: clean that shit
34 * @todo: mapped => false for each button not related with session !!!!
35 * @todo: set stuff in the SessionController, no loggic here please !!!
36 * @todo: add the dance link stuff
37 *
38 * {@inheritdoc}
39 */
40 public function buildForm(FormBuilderInterface $builder, array $options) {
41 //Is admin or user with rainfall >= 2
42 if (!empty($options['raincancel'])) {
43 //Add raincancel item
44 $builder->add('raincancel', SubmitType::class, ['label' => 'Rain cancel', 'attr' => ['class' => 'submit']]);
45 //Is admin
46 } elseif (!empty($options['admin'])) {
47 //Add forcecancel item
48 $builder->add('forcecancel', SubmitType::class, ['label' => 'Force cancel', 'attr' => ['class' => 'submit']]);
49 }
50
51 //Is admin or owner
52 if (!empty($options['modify'])) {
53 if (!empty($options['admin'])) {
54 $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 Type(['type' => \DateTime::class, 'message' => 'Your date doesn\'t seems to be valid'])]]);
55 }
56
57 $builder
58 //TODO: avertissement + minimum et maximum ???
59 ->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'])]])
60 //TODO: avertissement + minimum et maximum ???
61 ->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'])]])
62 ->add('modify', SubmitType::class, ['label' => 'Modify', 'attr' => ['class' => 'submit']]);
63 }
64
65 //Is admin or applicant
66 if (!empty($options['cancel'])) {
67 $builder->add('cancel', SubmitType::class, ['label' => 'Cancel', 'attr' => ['class' => 'submit']]);
68 }
69
70 //Is admin or senior owner
71 if (!empty($options['move'])) {
72 //Load locations
73 $locations = $this->doctrine->getRepository(Location::class)->findComplementBySessionId($options['session']);
74 $builder
75 //TODO: class senior en orange ???
76 ->add('location', ChoiceType::class, ['attr' => ['placeholder' => 'Your location'], 'constraints' => [new NotBlank(['message' => 'Please provide your location'])], 'choices' => $locations, 'choice_translation_domain' => true])
77 ->add('move', SubmitType::class, ['label' => 'Move', 'attr' => ['class' => 'submit']]);
78 }
79
80 //Add extra user field
81 if (!empty($options['admin'])) {
82 //Load users
83 $users = $this->doctrine->getRepository(User::class)->findBySessionId($options['session']);
84 //Add admin fields
85 $builder
86 //TODO: class admin en rouge ???
87 ->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])
88 ->add('lock', SubmitType::class, ['label' => 'Lock', 'attr' => ['class' => 'submit']]);
89
90 //Is admin and locked === null
91 if (!empty($options['attribute'])) {
92 //Add attribute fields
93 $builder
94 ->add('attribute', SubmitType::class, ['label' => 'Attribute', 'attr' => ['class' => 'submit']])
95 ->add('autoattribute', SubmitType::class, ['label' => 'Auto attribute', 'attr' => ['class' => 'submit']]);
96 }
97 }
98
99 //Return form
100 return $builder;
101 }
102
103 /**
104 * {@inheritdoc}
105 */
106 public function configureOptions(OptionsResolver $resolver) {
107 $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]);
108 $resolver->setAllowedTypes('admin', 'boolean');
109 #TODO: voir si c'est le bon type
110 $resolver->setAllowedTypes('date', 'datetime');
111 $resolver->setAllowedTypes('begin', 'datetime');
112 $resolver->setAllowedTypes('length', 'datetime');
113 $resolver->setAllowedTypes('cancel', 'boolean');
114 $resolver->setAllowedTypes('raincancel', 'boolean');
115 $resolver->setAllowedTypes('modify', 'boolean');
116 $resolver->setAllowedTypes('move', 'boolean');
117 $resolver->setAllowedTypes('attribute', 'boolean');
118 $resolver->setAllowedTypes('user', 'integer');
119 $resolver->setAllowedTypes('session', 'integer');
120 }
121
122 /**
123 * {@inheritdoc}
124 */
125 public function getName() {
126 return 'rapsys_air_session_edit';
127 }
128 }