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