]> Raphaël G. Git Repositories - airbundle/blob - Form/RegisterType.php
Fix header comment
[airbundle] / Form / RegisterType.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Form;
13
14 use Doctrine\ORM\EntityManagerInterface;
15
16 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
17 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
18 use Symfony\Component\Form\Extension\Core\Type\TelType;
19 use Symfony\Component\Form\Extension\Core\Type\TextType;
20 use Symfony\Component\Form\FormBuilderInterface;
21 use Symfony\Component\OptionsResolver\OptionsResolver;
22
23 use Rapsys\AirBundle\Entity\Country;
24 use Rapsys\AirBundle\Entity\Dance;
25 use Rapsys\AirBundle\Entity\User;
26 use Rapsys\AirBundle\Transformer\DanceTransformer;
27 use Rapsys\AirBundle\Transformer\SubscriptionTransformer;
28
29 use Rapsys\UserBundle\Form\RegisterType as BaseRegisterType;
30
31 /**
32 * {@inheritdoc}
33 */
34 class RegisterType extends BaseRegisterType {
35 /**
36 * Public constructor
37 *
38 * @param EntityManagerInterface $manager The entity manager
39 */
40 public function __construct(private EntityManagerInterface $manager) {
41 }
42
43 /**
44 * {@inheritdoc}
45 */
46 public function buildForm(FormBuilderInterface $builder, array $options): FormBuilderInterface {
47 //Call parent build form
48 $form = parent::buildForm($builder, $options);
49
50 //Add extra city field
51 if (!empty($options['city'])) {
52 $form->add('city', TextType::class, ['attr' => ['placeholder' => 'Your city'], 'required' => false]);
53 }
54
55 //Add extra country field
56 if (!empty($options['country'])) {
57 //Add country field
58 $form->add('country', EntityType::class, ['class' => $options['country_class'], 'choice_label' => 'title'/*, 'choices' => $options['location_choices']*/, 'preferred_choices' => $options['country_favorites'], 'attr' => ['placeholder' => 'Your country'], 'choice_translation_domain' => false, 'required' => true, 'data' => $options['country_default']]);
59 }
60
61 //Add extra dance field
62 if (!empty($options['dance'])) {
63 //Add dance field
64 #$form->add('dances', EntityType::class, ['class' => $options['dance_class'], 'choice_label' => null, 'preferred_choices' => $options['dance_favorites'], 'attr' => ['placeholder' => 'Your dance'], 'choice_translation_domain' => false, 'required' => false, 'data' => $options['dance_default']]);
65 $form->add(
66 $form
67 ->create('dances', ChoiceType::class, ['attr' => ['placeholder' => 'Your dance']/*, 'by_reference' => false*/, 'choice_attr' => ['class' => 'row'], 'choice_translation_domain' => false, 'choices' => $options['dance_choices'], 'multiple' => true, 'preferred_choices' => $options['dance_favorites'], 'required' => false])
68 ->addModelTransformer(new DanceTransformer($this->manager))
69 #->addModelTransformer(new CollectionToArrayTransformer)
70 );
71 /*, 'expanded' => true*/ /*, 'data' => $options['dance_default']*/
72 }
73
74 //Add extra phone field
75 if (!empty($options['phone'])) {
76 $form->add('phone', TelType::class, ['attr' => ['placeholder' => 'Your phone'], 'required' => false]);
77 }
78
79 //Add extra pseudonym field
80 if (!empty($options['pseudonym'])) {
81 $form->add('pseudonym', TextType::class, ['attr' => ['placeholder' => 'Your pseudonym'], 'required' => false]);
82 }
83
84 //Add extra subscription field
85 if (!empty($options['subscription'])) {
86 //Add subscription field
87 #$form->add('subscriptions', EntityType::class, ['class' => $options['subscription_class'], 'choice_label' => 'pseudonym', 'preferred_choices' => $options['subscription_favorites'], 'attr' => ['placeholder' => 'Your subscription'], 'choice_translation_domain' => false, 'required' => false, 'data' => $options['subscription_default']]);
88 #$form->add('subscriptions', ChoiceType::class, ['attr' => ['placeholder' => 'Your subscription'], 'choice_attr' => ['class' => 'row'], 'choice_translation_domain' => false, 'choices' => $options['subscription_choices'], 'multiple' => true, 'preferred_choices' => $options['subscription_favorites'], 'required' => false]);
89 $form->add(
90 $form
91 //XXX: by_reference need to be false to allow persisting of data from the read only inverse side
92 ->create('subscriptions', ChoiceType::class, ['attr' => ['placeholder' => 'Your subscription']/*, 'by_reference' => false*/, 'choice_attr' => ['class' => 'row'], 'choice_translation_domain' => false, 'choices' => $options['subscription_choices'], 'multiple' => true, 'preferred_choices' => $options['subscription_favorites'], 'required' => false])
93 ->addModelTransformer(new SubscriptionTransformer($this->manager))
94 #->addModelTransformer(new CollectionToArrayTransformer)
95 );
96 /*, 'expanded' => true*/ /*, 'data' => $options['subscription_default']*/
97 }
98
99 //Add extra zipcode field
100 if (!empty($options['zipcode'])) {
101 $form->add('zipcode', TextType::class, ['attr' => ['placeholder' => 'Your zipcode'], 'required' => false]);
102 }
103
104 //Return form
105 return $form;
106 }
107
108 /**
109 * {@inheritdoc}
110 */
111 public function configureOptions(OptionsResolver $resolver): void {
112 //Call parent configure options
113 parent::configureOptions($resolver);
114
115 //Set defaults
116 $resolver->setDefaults(
117 [
118 'city' => true,
119 'country' => true,
120 'country_class' => 'Rapsys\AirBundle\Entity\Country',
121 'country_default' => null,
122 'country_favorites' => [],
123 'dance' => false,
124 'dance_choices' => [],
125 #'dance_default' => null,
126 'dance_favorites' => [],
127 'phone' => true,
128 'pseudonym' => true,
129 'subscription' => false,
130 'subscription_choices' => [],
131 #'subscription_default' => null,
132 'subscription_favorites' => [],
133 'zipcode' => true
134 ]
135 );
136
137 //Add extra city option
138 $resolver->setAllowedTypes('city', 'boolean');
139
140 //Add extra country option
141 $resolver->setAllowedTypes('country', 'boolean');
142
143 //Add country class
144 $resolver->setAllowedTypes('country_class', 'string');
145
146 //Add country default
147 $resolver->setAllowedTypes('country_default', [Country::class, 'null']);
148
149 //Add country favorites
150 $resolver->setAllowedTypes('country_favorites', 'array');
151
152 //Add extra dance option
153 $resolver->setAllowedTypes('dance', 'boolean');
154
155 //Add dance choices
156 $resolver->setAllowedTypes('dance_choices', 'array');
157
158 //Add dance default
159 #$resolver->setAllowedTypes('dance_default', 'integer');
160
161 //Add dance favorites
162 $resolver->setAllowedTypes('dance_favorites', 'array');
163
164 //Add extra phone option
165 $resolver->setAllowedTypes('phone', 'boolean');
166
167 //Add extra pseudonym option
168 $resolver->setAllowedTypes('pseudonym', 'boolean');
169
170 //Add extra subscription option
171 $resolver->setAllowedTypes('subscription', 'boolean');
172
173 //Add subscription choices
174 $resolver->setAllowedTypes('subscription_choices', 'array');
175
176 //Add subscription default
177 #$resolver->setAllowedTypes('subscription_default', 'integer');
178
179 //Add subscription favorites
180 $resolver->setAllowedTypes('subscription_favorites', 'array');
181
182 //Add extra zipcode option
183 $resolver->setAllowedTypes('zipcode', 'boolean');
184 }
185
186 /**
187 * {@inheritdoc}
188 */
189 public function getName(): string {
190 return 'rapsysair_register';
191 }
192 }