src/CoreBundle/Form/Areabrick/ContactForm.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CoreBundle\Form\Areabrick;
  4. use Pimcore\Model\DataObject\ContactFormRequest;
  5. use Gregwar\CaptchaBundle\Type\CaptchaType;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  8. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\Extension\Core\Type\FileType;
  11. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  12. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  13. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\Form\FormBuilderInterface;
  16. use Symfony\Component\Form\FormInterface;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. use Symfony\Component\Form\FormEvents;
  19. use Symfony\Component\Form\FormEvent;
  20. class ContactForm extends AbstractType
  21. {
  22.     public function buildForm(FormBuilderInterface $builder, array $options = ['allow_extra_fields' => true]): void
  23.     {
  24.         $builder
  25.             ->add('gender'ChoiceType::class, [
  26.                 'label' => 'Anrede',
  27.                 'choices' => [
  28.                     'Herr' => 'male',
  29.                     'Frau' => 'female',
  30.                     'Divers' => 'divers'
  31.                 ],
  32.                 'choice_translation_domain' => false
  33.             ])
  34.             ->add('firstname'TextType::class, [
  35.                 'label' => 'Vorname'
  36.             ])
  37.             ->add('lastname'TextType::class, [
  38.                 'label' => 'Nachname'
  39.             ])
  40.             ->add('title'TextType::class, [
  41.                 'label' => 'Titel',
  42.                 'required' => false
  43.             ])
  44.             ->add('street'TextType::class, [
  45.                 'label' => 'Straße',
  46.                 'required' => false
  47.             ])
  48.             ->add('plz'TextType::class, [
  49.                 'label' => 'Postleitzahl',
  50.                 'required' => false
  51.             ])
  52.             ->add('city'TextType::class, [
  53.                 'label' => 'Stadt',
  54.                 'required' => false
  55.             ])
  56.             ->add('phone'TextType::class, [
  57.                 'label' => 'Telefon',
  58.                 'required' => false
  59.             ])
  60.             ->add('email'EmailType::class, [
  61.                 'label' => 'Email'
  62.             ])
  63.             ->add('topic'ChoiceType::class, [
  64.                 'label' => 'Bitte wählen Sie ein Thema:',
  65.                 'choices' => $options['form_topic_selection'] ?: [],
  66.                 'choice_translation_domain' => false,
  67.                 'required' => true
  68.             ])
  69.             ->add('message'TextareaType::class, [
  70.                 'label' => 'Ihre Nachricht',
  71.                 'required' => true
  72.             ])
  73.             ->add('upload'FileType::class, [
  74.                 'multiple' => true,
  75.                 'label' => 'Dateiupload',
  76.                 'required' => false,
  77.                 /*'constraints' => [
  78.                     new File([
  79.                         'maxSize' => '10M',
  80.                         'maxSizeMessage' => 'hallo'
  81.                     ])
  82.                 ]*/
  83.             ])
  84.             ->add('confirm'CheckboxType::class, [
  85.                 'label' => false])
  86.             ->add('code'HiddenType::class, [
  87.                 'data' => 1,
  88.             ])
  89.             ->add('captcha'CaptchaType::class)
  90.             ->add('submit'SubmitType::class, [
  91.                 'label' => 'einsenden',
  92.                 'attr' => [
  93.                     'class' => 'button btn-default'
  94.                 ]
  95.             ]);
  96.         $formModifier = function (FormInterface $form$type null) {
  97.         };
  98.         $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($formModifier) {
  99.             $data $event->getData();
  100.             $form $event->getForm();
  101.             $options $event->getForm()->getConfig()->getOptions();
  102.             $form->remove('submit');
  103.             if ($data['topic'] == 'onlineshop') {
  104.                 $form
  105.                     ->add('orderNumber'TextType::class, [
  106.                         'label' => 'Bestellnummer',
  107.                         'required' => false
  108.                     ])
  109.                     ->add('customerNumber'TextType::class, [
  110.                         'label' => 'Kundennummer',
  111.                         'required' => false
  112.                     ])
  113.                     ->add('retoure'TextType::class, [
  114.                         'label' => 'Rücksendung/Versand',
  115.                         'required' => false
  116.                     ]);
  117.             }
  118.             if ($data['topic'] == 'produktreklamation') {
  119.                 $form
  120.                     ->add('product'TextType::class, [
  121.                         'label' => 'Produktname',
  122.                         'required' => true
  123.                     ])
  124.                     ->add('bestBeforeDate'TextType::class, [
  125.                         'label' => 'MHD',
  126.                         'required' => true
  127.                     ])
  128.                     ->add('lot_number'TextType::class, [
  129.                         'label' => 'LOT Nummer',
  130.                         'required' => false
  131.                     ])
  132.                     ->add('purchased'TextType::class, [
  133.                         'label' => 'Gekauft bei',
  134.                         'required' => false
  135.                     ])
  136.                     ->add('available'CheckboxType::class, [
  137.                         'label' => 'Produkt noch vorhanden',
  138.                         'required' => false
  139.                     ]);
  140.             }
  141.             if ($data['topic'] == 'produktverfuegbarkeit' && !empty($options['form_product_selection'])) {
  142.                 /** Disabled. @See: https://blackbit.atlassian.net/browse/DEVELEY-132 */
  143.                 /*$form
  144.                     ->add('product', ChoiceType::class, [
  145.                         'label' => 'Welches Produkt suchen Sie',
  146.                         'choices' => $options['form_product_selection'],
  147.                         'choice_translation_domain' => false,
  148.                         'required' => false
  149.                     ]);*/
  150.             }
  151.             $form
  152.                 ->add('captcha'CaptchaType::class)
  153.                 ->add('submit'SubmitType::class, [
  154.                     'label' => 'einsenden',
  155.                     'attr' => [
  156.                         'class' => 'button btn-default'
  157.                     ]
  158.                 ]);
  159.         });
  160.         $builder->get('topic')->addEventListener(
  161.             FormEvents::PRE_SUBMIT,
  162.             function (FormEvent $event) use ($formModifier) {
  163.                 $data $event->getForm()->getData();
  164.                 if ($data == 'Online-Shop') {
  165.                     $formModifier($event->getForm()->getParent(), $data);
  166.                 }
  167.             });
  168.     }
  169.     public function getBlockPrefix(): string
  170.     {
  171.         return '';
  172.     }
  173.     public function configureOptions(OptionsResolver $resolver): void
  174.     {
  175.         $resolver
  176.             ->setDefined(['form_product_selection''form_topic_selection'])
  177.             ->setDefaults(['data_class' => ContactFormRequest::class]);
  178.     }
  179. }