]> Raphaël G. Git Repositories - airbundle/blob - Form/RegisterType.php
Version 0.5.0
[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 Rapsys\AirBundle\Entity\Country;
17 use Rapsys\AirBundle\Entity\Dance;
18 use Rapsys\AirBundle\Entity\User;
19 use Rapsys\AirBundle\Transformer\DanceTransformer;
20 use Rapsys\AirBundle\Transformer\SubscriptionTransformer;
21
22 use Rapsys\PackBundle\Util\ImageUtil;
23 use Rapsys\PackBundle\Util\SluggerUtil;
24
25 use Rapsys\UserBundle\Form\RegisterType as BaseRegisterType;
26
27 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
28 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
29 use Symfony\Component\Form\Extension\Core\Type\TelType;
30 use Symfony\Component\Form\Extension\Core\Type\TextType;
31 use Symfony\Component\Form\FormBuilderInterface;
32 use Symfony\Component\OptionsResolver\OptionsResolver;
33 use Symfony\Contracts\Translation\TranslatorInterface;
34
35 /**
36 * {@inheritdoc}
37 */
38 class RegisterType extends BaseRegisterType {
39 /**
40 * Constructor
41 *
42 * @param EntityManagerInterface $manager The entity manager
43 * @param ?ImageUtil $image The image instance
44 * @param ?SluggerUtil $slugger The slugger instance
45 * @param ?TranslatorInterface $translator The translator instance
46 */
47 public function __construct(protected EntityManagerInterface $manager, protected ?ImageUtil $image = null, protected ?SluggerUtil $slugger = null, protected ?TranslatorInterface $translator = null) {
48 //Call parent constructor
49 parent::__construct($this->image, $this->slugger, $this->translator);
50 }
51
52 /**
53 * {@inheritdoc}
54 */
55 public function buildForm(FormBuilderInterface $builder, array $options): void {
56 //Add extra city field
57 if (!empty($options['city'])) {
58 $builder->add('city', TextType::class, ['attr' => ['placeholder' => 'Your city'], 'required' => false]);
59 }
60
61 //Add extra country field
62 if (!empty($options['country'])) {
63 //Add country field
64 $builder->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']]);
65 }
66
67 //Add extra dance field
68 if (!empty($options['dance'])) {
69 //Add dance field
70 #$builder->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']]);
71 $builder->add(
72 $builder
73 ->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])
74 ->addModelTransformer(new DanceTransformer($this->manager))
75 #->addModelTransformer(new CollectionToArrayTransformer)
76 );
77 /*, 'expanded' => true*/ /*, 'data' => $options['dance_default']*/
78 }
79
80 //Add extra phone field
81 if (!empty($options['phone'])) {
82 $builder->add('phone', TelType::class, ['attr' => ['placeholder' => 'Your phone'], 'required' => false]);
83 }
84
85 //Add extra pseudonym field
86 if (!empty($options['pseudonym'])) {
87 $builder->add('pseudonym', TextType::class, ['attr' => ['placeholder' => 'Your pseudonym'], 'required' => false]);
88 }
89
90 //Add extra subscription field
91 if (!empty($options['subscription'])) {
92 //Add subscription field
93 #$builder->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']]);
94 #$builder->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]);
95 $builder->add(
96 $builder
97 //XXX: by_reference need to be false to allow persisting of data from the read only inverse side
98 ->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])
99 ->addModelTransformer(new SubscriptionTransformer($this->manager))
100 #->addModelTransformer(new CollectionToArrayTransformer)
101 );
102 /*, 'expanded' => true*/ /*, 'data' => $options['subscription_default']*/
103 }
104
105 //Add extra zipcode field
106 if (!empty($options['zipcode'])) {
107 $builder->add('zipcode', TextType::class, ['attr' => ['placeholder' => 'Your zipcode'], 'required' => false]);
108 }
109
110 //Call parent
111 parent::buildForm($builder, $options);
112 }
113
114 /**
115 * {@inheritdoc}
116 */
117 public function configureOptions(OptionsResolver $resolver): void {
118 //Call parent configure options
119 parent::configureOptions($resolver);
120
121 //Set defaults
122 $resolver->setDefaults(
123 [
124 'city' => true,
125 'country' => true,
126 'country_class' => 'Rapsys\AirBundle\Entity\Country',
127 'country_default' => null,
128 'country_favorites' => [],
129 'dance' => false,
130 'dance_choices' => [],
131 #'dance_default' => null,
132 'dance_favorites' => [],
133 'phone' => true,
134 'pseudonym' => true,
135 'subscription' => false,
136 'subscription_choices' => [],
137 #'subscription_default' => null,
138 'subscription_favorites' => [],
139 'zipcode' => true
140 ]
141 );
142
143 //Add extra city option
144 $resolver->setAllowedTypes('city', 'boolean');
145
146 //Add extra country option
147 $resolver->setAllowedTypes('country', 'boolean');
148
149 //Add country class
150 $resolver->setAllowedTypes('country_class', 'string');
151
152 //Add country default
153 $resolver->setAllowedTypes('country_default', [Country::class, 'null']);
154
155 //Add country favorites
156 $resolver->setAllowedTypes('country_favorites', 'array');
157
158 //Add extra dance option
159 $resolver->setAllowedTypes('dance', 'boolean');
160
161 //Add dance choices
162 $resolver->setAllowedTypes('dance_choices', 'array');
163
164 //Add dance default
165 #$resolver->setAllowedTypes('dance_default', 'integer');
166
167 //Add dance favorites
168 $resolver->setAllowedTypes('dance_favorites', 'array');
169
170 //Add extra phone option
171 $resolver->setAllowedTypes('phone', 'boolean');
172
173 //Add extra pseudonym option
174 $resolver->setAllowedTypes('pseudonym', 'boolean');
175
176 //Add extra subscription option
177 $resolver->setAllowedTypes('subscription', 'boolean');
178
179 //Add subscription choices
180 $resolver->setAllowedTypes('subscription_choices', 'array');
181
182 //Add subscription default
183 #$resolver->setAllowedTypes('subscription_default', 'integer');
184
185 //Add subscription favorites
186 $resolver->setAllowedTypes('subscription_favorites', 'array');
187
188 //Add extra zipcode option
189 $resolver->setAllowedTypes('zipcode', 'boolean');
190 }
191
192 /**
193 * {@inheritdoc}
194 */
195 public function getName(): string {
196 return 'rapsysair_register';
197 }
198 }