<?php
namespace App\Form;
use App\Component\Captcha\Form\CaptchaType;
use App\Component\Configuration\Util\Config;
use App\Constraints\UrlNotAllowed;
use App\Entity\Company;
use App\Entity\Contact;
use App\EventListener\FeatureFlagListener;
use App\Form\Setting\ContactSettingType;
use App\Service\LocalisedSettingService;
use Doctrine\ORM\EntityManagerInterface;
use Flagception\Manager\FeatureManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ContactType extends AbstractType
{
protected EntityManagerInterface $manager;
protected FeatureManagerInterface $featureManager;
private LocalisedSettingService $localisedSettingService;
private Config $config;
/**
* ContactType constructor.
*/
public function __construct(
EntityManagerInterface $manager,
FeatureManagerInterface $featureManager,
LocalisedSettingService $localisedSettingService,
Config $config
) {
$this->manager = $manager;
$this->featureManager = $featureManager;
$this->localisedSettingService = $localisedSettingService;
$this->config = $config;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($this->config->get('site_contact_show_company_form_field')) {
$builder->add('company');
}
$builder
->add(child: 'firstName', options: [
'constraints' => [
new UrlNotAllowed(),
],
])
->add(child: 'lastName', options: [
'constraints' => [
new UrlNotAllowed(),
],
]);
$builder
->add('phone', PhoneNumberType::class, [
'required' => false,
])
->add('email', EmailType::class);
$addressRequired = (
ContactSettingType::CONTACT_ADDRESS_FIELDS_STRATEGY_SHOW_AND_REQUIRE ===
$this->config->get('site_contact_address_fields_strategy')
);
if (\in_array(
$this->config->get('site_contact_address_fields_strategy'),
[
ContactSettingType::CONTACT_ADDRESS_FIELDS_STRATEGY_SHOW,
ContactSettingType::CONTACT_ADDRESS_FIELDS_STRATEGY_SHOW_AND_REQUIRE,
],
true
)) {
$builder
->add('address', TextType::class, [
'required' => $addressRequired,
])
->add('zipCode', TextType::class, [
'required' => $addressRequired,
])
->add('city', TextType::class, [
'required' => $addressRequired,
])
;
}
$builder->add('remark', TextareaType::class, [
'label' => 'Message',
])
->add('captcha', CaptchaType::class)
;
if ($this->config->get('site_contact_show_company_selector')) {
$builder->add('contactedCompany', EntityType::class, [
'class' => Company::class,
'multiple' => false,
'required' => false,
'label' => 'Which company would you like to contact',
]);
}
if (
$options['send_to_meta'] &&
$options['send_to_meta']['entity'] &&
$options['send_to_meta']['id']
) {
$builder->add('sendToEntity', HiddenType::class, [
'data' => $options['send_to_meta']['entity'],
'mapped' => false,
]);
$builder->add('sendToId', HiddenType::class, [
'data' => $options['send_to_meta']['id'],
'mapped' => false,
]);
}
if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_NEWSLETTER) &&
$this->config->get('site_contact_show_subscribe_to_newsletter')) {
$builder->add('subscribeToNewsletter', CheckboxHtmlType::class, [
'mapped' => false,
'required' => false,
'label' => 'Subscribe to our newsletter',
'value' => 1,
]);
}
if (ContactSettingType::PRIVACY_STATEMENT_STRATEGY_TEXT_AND_CHECKBOX ===
$this->config->get('site_contact_privacy_statement_option')) {
$builder->add('privacy_statement', CheckboxHtmlType::class, [
'label' => $this->localisedSettingService->getLocalisedSetting('site_contact_privacy_statement_text'),
'mapped' => false,
'required' => true,
]);
}
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Contact::class,
'translation_domain' => 'contact',
'send_to_meta' => null,
'attr' => [
'class' => 'recaptcha-form',
],
]);
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix(): string
{
return 'contactbundle_contact';
}
}