src/Form/WidgetForm.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\WidgetRequest;
  4. use App\Form\Transform\DomainsTransformer;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\CallbackTransformer;
  7. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  8. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  9. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  10. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  11. use Symfony\Component\Form\FormBuilderInterface;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. class WidgetForm extends AbstractType
  17. {
  18. const WIDGETS_FILTER_BY_SUBNETWORKS = [
  19. 'HORAIRE_INTEGRE',
  20. 'HORAIRE_REDIRECTION_SIM',
  21. 'HORAIRE_REDIRECTION_SITE_SITE',
  22. 'HORAIRE_REDIRECTION_PAGE_PAGE',
  23. 'TRAFIC_INTEGRE',
  24. 'FULL'
  25. ];
  26. public function buildForm(FormBuilderInterface $builder, array $options)
  27. {
  28. $subNetworks = $options['subNetworks'];
  29. $builder
  30. ->add('name', TextType::class, ['label' => 'form.brandName', 'required' => true])
  31. ->add('email', EmailType::class, ['label' => 'form.email', 'required' => true])
  32. ->add('widgetType', ChoiceType::class, [
  33. 'required' => true,
  34. 'label' => 'form.widgetType',
  35. 'placeholder' => 'form.select_option',
  36. 'choices' => [
  37. 'widget.journey.integrated' => 'ITINERAIRE_INTEGRE',
  38. 'widget.journey.redirect_sim' => 'ITINERAIRE_REDIRECTION_SIM',
  39. 'widget.journey.redirect_internal' => 'ITINERAIRE_REDIRECTION_PAGE_PAGE',
  40. 'widget.schedule.integrated' => 'HORAIRE_INTEGRE',
  41. 'widget.schedule.redirect_sim' => 'HORAIRE_REDIRECTION_SIM',
  42. 'widget.schedule.redirect_internal' => 'HORAIRE_REDIRECTION_PAGE_PAGE',
  43. 'widget.traffic.integrated' => 'TRAFIC_INTEGRE',
  44. 'widget.products.full' => 'FULL',
  45. 'widget.products.full_except_traffic' => 'FULL_EXCEPT_TRAFFIC',
  46. 'widget.products.full_except_traffic_redirection_external' => 'FULL_EXCEPT_TRAFFIC_REDIRECTION_SIM'
  47. ],
  48. 'choice_attr' => function ($choice) {
  49. if (in_array($choice, self::WIDGETS_FILTER_BY_SUBNETWORKS)) {
  50. return ['class' => 'is-Show-SubNetwork'];
  51. }
  52. return [];
  53. }
  54. ])
  55. ->add('subNetworks', ChoiceType::class, [
  56. 'required' => false,
  57. 'label' => 'form.subNetworks',
  58. 'choices' => $subNetworks,
  59. 'expanded' => false,
  60. 'multiple' => true
  61. ])
  62. ->add('domain', TextareaType::class, [
  63. 'label' => 'form.domain',
  64. 'required' => true
  65. ])
  66. ->add('start', TextType::class, [
  67. 'label' => 'journey.departure',
  68. 'required' => false,
  69. 'attr' => [
  70. 'placeholder' => 'journey.departure_placeholder'
  71. ]
  72. ])
  73. ->add('startId', HiddenType::class)
  74. ->add('end', TextType::class, [
  75. 'label' => 'journey.arrival',
  76. 'required' => false,
  77. 'attr' => [
  78. 'placeholder' => 'journey.arrival_placeholder'
  79. ]
  80. ])
  81. ->add('endId', HiddenType::class)
  82. ->add('prefilled', ChoiceType::class, [
  83. 'required' => true,
  84. 'label' => 'widget.prefilled',
  85. 'choices' => [
  86. 'widget.no' => 'NO',
  87. 'widget.departure' => 'DEPARTURE',
  88. 'widget.arrival' => 'ARRIVAL',
  89. ],
  90. ])
  91. ->add('customizeMap', CheckboxType::class, [
  92. 'required' => false,
  93. 'mapped' => false,
  94. 'label' => 'widget.customize_map',
  95. 'attr' => [
  96. 'checked' => false,
  97. ],
  98. ])
  99. ->add('latitude', NumberType::class, [
  100. 'required' => false,
  101. 'label' => 'widget.latitude',
  102. 'attr' => [
  103. 'data-is-map-param' => 'lat',
  104. ],
  105. ])
  106. ->add('longitude', NumberType::class, [
  107. 'required' => false,
  108. 'label' => 'widget.longitude',
  109. 'attr' => [
  110. 'data-is-map-param' => 'lng',
  111. ],
  112. ])
  113. ->add('zoom', NumberType::class, [
  114. 'required' => false,
  115. 'label' => 'widget.zoom',
  116. 'attr' => [
  117. 'data-is-map-param' => 'zoom',
  118. ],
  119. ]);
  120. // Adding a transformer to remove trailing slashes from domains
  121. $builder->get('domain')->addModelTransformer(new DomainsTransformer);
  122. }
  123. public function configureOptions(OptionsResolver $resolver)
  124. {
  125. $resolver->setRequired([
  126. 'subNetworks'
  127. ]);
  128. $resolver->setDefaults(
  129. [
  130. 'data_class' => WidgetRequest::class
  131. ]
  132. );
  133. }
  134. }