src/Form/ContactType.php line 23

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