1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys AirBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\AirBundle\Form
;
14 use Doctrine\ORM\EntityManagerInterface
;
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
;
22 use Rapsys\PackBundle\Util\ImageUtil
;
23 use Rapsys\PackBundle\Util\SluggerUtil
;
25 use Rapsys\UserBundle\Form\RegisterType
as BaseRegisterType
;
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
;
38 class RegisterType
extends BaseRegisterType
{
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
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
);
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]);
61 //Add extra country field
62 if (!empty($options['country'])) {
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']]);
67 //Add extra dance field
68 if (!empty($options['dance'])) {
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']]);
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)
77 /*, 'expanded' => true*/ /*, 'data' => $options['dance_default']*/
80 //Add extra phone field
81 if (!empty($options['phone'])) {
82 $builder->add('phone', TelType
::class, ['attr' => ['placeholder' => 'Your phone'], 'required' => false]);
85 //Add extra pseudonym field
86 if (!empty($options['pseudonym'])) {
87 $builder->add('pseudonym', TextType
::class, ['attr' => ['placeholder' => 'Your pseudonym'], 'required' => false]);
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]);
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)
102 /*, 'expanded' => true*/ /*, 'data' => $options['subscription_default']*/
105 //Add extra zipcode field
106 if (!empty($options['zipcode'])) {
107 $builder->add('zipcode', TextType
::class, ['attr' => ['placeholder' => 'Your zipcode'], 'required' => false]);
111 parent
::buildForm($builder, $options);
117 public function configureOptions(OptionsResolver
$resolver): void {
118 //Call parent configure options
119 parent
::configureOptions($resolver);
122 $resolver->setDefaults(
126 'country_class' => 'Rapsys\AirBundle\Entity\Country',
127 'country_default' => null,
128 'country_favorites' => [],
130 'dance_choices' => [],
131 #'dance_default' => null,
132 'dance_favorites' => [],
135 'subscription' => false,
136 'subscription_choices' => [],
137 #'subscription_default' => null,
138 'subscription_favorites' => [],
143 //Add extra city option
144 $resolver->setAllowedTypes('city', 'boolean');
146 //Add extra country option
147 $resolver->setAllowedTypes('country', 'boolean');
150 $resolver->setAllowedTypes('country_class', 'string');
152 //Add country default
153 $resolver->setAllowedTypes('country_default', [Country
::class, 'null']);
155 //Add country favorites
156 $resolver->setAllowedTypes('country_favorites', 'array');
158 //Add extra dance option
159 $resolver->setAllowedTypes('dance', 'boolean');
162 $resolver->setAllowedTypes('dance_choices', 'array');
165 #$resolver->setAllowedTypes('dance_default', 'integer');
167 //Add dance favorites
168 $resolver->setAllowedTypes('dance_favorites', 'array');
170 //Add extra phone option
171 $resolver->setAllowedTypes('phone', 'boolean');
173 //Add extra pseudonym option
174 $resolver->setAllowedTypes('pseudonym', 'boolean');
176 //Add extra subscription option
177 $resolver->setAllowedTypes('subscription', 'boolean');
179 //Add subscription choices
180 $resolver->setAllowedTypes('subscription_choices', 'array');
182 //Add subscription default
183 #$resolver->setAllowedTypes('subscription_default', 'integer');
185 //Add subscription favorites
186 $resolver->setAllowedTypes('subscription_favorites', 'array');
188 //Add extra zipcode option
189 $resolver->setAllowedTypes('zipcode', 'boolean');
195 public function getName(): string {
196 return 'rapsysair_register';