src/Form/ContactType.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Component\Captcha\Form\CaptchaType;
  4. use App\Component\Configuration\Util\Config;
  5. use App\Constraints\UrlNotAllowed;
  6. use App\Entity\Company;
  7. use App\Entity\Contact;
  8. use App\EventListener\FeatureFlagListener;
  9. use App\Form\Setting\ContactSettingType;
  10. use App\Service\LocalisedSettingService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Flagception\Manager\FeatureManagerInterface;
  13. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  14. use Symfony\Component\Form\AbstractType;
  15. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  16. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  18. use Symfony\Component\Form\Extension\Core\Type\TextType;
  19. use Symfony\Component\Form\FormBuilderInterface;
  20. use Symfony\Component\OptionsResolver\OptionsResolver;
  21. class ContactType extends AbstractType
  22. {
  23.     protected EntityManagerInterface $manager;
  24.     protected FeatureManagerInterface $featureManager;
  25.     private LocalisedSettingService $localisedSettingService;
  26.     private Config $config;
  27.     /**
  28.      * ContactType constructor.
  29.      */
  30.     public function __construct(
  31.         EntityManagerInterface $manager,
  32.         FeatureManagerInterface $featureManager,
  33.         LocalisedSettingService $localisedSettingService,
  34.         Config $config
  35.     ) {
  36.         $this->manager $manager;
  37.         $this->featureManager $featureManager;
  38.         $this->localisedSettingService $localisedSettingService;
  39.         $this->config $config;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function buildForm(FormBuilderInterface $builder, array $options)
  45.     {
  46.         if ($this->config->get('site_contact_show_company_form_field')) {
  47.             $builder->add('company');
  48.         }
  49.         $builder
  50.             ->add(child'firstName'options: [
  51.                 'constraints' => [
  52.                     new UrlNotAllowed(),
  53.                 ],
  54.             ])
  55.             ->add(child'lastName'options: [
  56.                 'constraints' => [
  57.                     new UrlNotAllowed(),
  58.                 ],
  59.             ]);
  60.         $builder
  61.             ->add('phone'PhoneNumberType::class, [
  62.                 'required' => false,
  63.             ])
  64.             ->add('email'EmailType::class);
  65.         $addressRequired = (
  66.             ContactSettingType::CONTACT_ADDRESS_FIELDS_STRATEGY_SHOW_AND_REQUIRE ===
  67.             $this->config->get('site_contact_address_fields_strategy')
  68.         );
  69.         if (\in_array(
  70.             $this->config->get('site_contact_address_fields_strategy'),
  71.             [
  72.                 ContactSettingType::CONTACT_ADDRESS_FIELDS_STRATEGY_SHOW,
  73.                 ContactSettingType::CONTACT_ADDRESS_FIELDS_STRATEGY_SHOW_AND_REQUIRE,
  74.             ],
  75.             true
  76.         )) {
  77.             $builder
  78.                 ->add('address'TextType::class, [
  79.                     'required' => $addressRequired,
  80.                 ])
  81.                 ->add('zipCode'TextType::class, [
  82.                     'required' => $addressRequired,
  83.                 ])
  84.                 ->add('city'TextType::class, [
  85.                     'required' => $addressRequired,
  86.                 ])
  87.             ;
  88.         }
  89.         $builder->add('remark'TextareaType::class, [
  90.                 'label' => 'Message',
  91.             ])
  92.             ->add('captcha'CaptchaType::class)
  93.         ;
  94.         if ($this->config->get('site_contact_show_company_selector')) {
  95.             $builder->add('contactedCompany'EntityType::class, [
  96.                 'class' => Company::class,
  97.                 'multiple' => false,
  98.                 'required' => false,
  99.                 'label' => 'Which company would you like to contact',
  100.             ]);
  101.         }
  102.         if (
  103.             $options['send_to_meta'] &&
  104.             $options['send_to_meta']['entity'] &&
  105.             $options['send_to_meta']['id']
  106.         ) {
  107.             $builder->add('sendToEntity'HiddenType::class, [
  108.                 'data' => $options['send_to_meta']['entity'],
  109.                 'mapped' => false,
  110.             ]);
  111.             $builder->add('sendToId'HiddenType::class, [
  112.                 'data' => $options['send_to_meta']['id'],
  113.                 'mapped' => false,
  114.             ]);
  115.         }
  116.         if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_NEWSLETTER) &&
  117.             $this->config->get('site_contact_show_subscribe_to_newsletter')) {
  118.             $builder->add('subscribeToNewsletter'CheckboxHtmlType::class, [
  119.                 'mapped' => false,
  120.                 'required' => false,
  121.                 'label' => 'Subscribe to our newsletter',
  122.                 'value' => 1,
  123.             ]);
  124.         }
  125.         if (ContactSettingType::PRIVACY_STATEMENT_STRATEGY_TEXT_AND_CHECKBOX ===
  126.             $this->config->get('site_contact_privacy_statement_option')) {
  127.             $builder->add('privacy_statement'CheckboxHtmlType::class, [
  128.                 'label' => $this->localisedSettingService->getLocalisedSetting('site_contact_privacy_statement_text'),
  129.                 'mapped' => false,
  130.                 'required' => true,
  131.             ]);
  132.         }
  133.     }
  134.     /**
  135.      * {@inheritdoc}
  136.      */
  137.     public function configureOptions(OptionsResolver $resolver)
  138.     {
  139.         $resolver->setDefaults([
  140.             'data_class' => Contact::class,
  141.             'translation_domain' => 'contact',
  142.             'send_to_meta' => null,
  143.             'attr' => [
  144.                 'class' => 'recaptcha-form',
  145.             ],
  146.         ]);
  147.     }
  148.     /**
  149.      * {@inheritdoc}
  150.      */
  151.     public function getBlockPrefix(): string
  152.     {
  153.         return 'contactbundle_contact';
  154.     }
  155. }