<?php
namespace App\Form\User;
use App\Form\User\AddInformationType;
use App\Manager\User\ReferentManager;
use App\Model\User\Referent;
use App\Model\User\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\NotNull;
class UserRegisterType extends AbstractType
{
private ReferentManager $referentManager;
private RequestStack $requestStack;
public function __construct(ReferentManager $referentManager, RequestStack $requestStack)
{
$this->referentManager = $referentManager;
$this->requestStack = $requestStack;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('companyName', TextType::class, [
'label' => 'form.register.company_name',
'required' => true,
'translation_domain' => 'form',
'constraints' => [
new Assert\NotBlank(['message' => 'La raison sociale est obligatoire.']),
new Assert\Length([
'max' => 50,
'maxMessage' => 'La raison sociale ne peut pas dépasser {{ limit }} caractères.',
]),
],
])
->add('email', RepeatedType::class, [
'type' => EmailType::class,
'invalid_message' => 'register.email.match',
'translation_domain' => 'form',
'first_options' => [
'label' => 'form.register.email.first',
'translation_domain' => 'form',
'constraints' => [
new Assert\NotBlank(['message' => 'Votre adresse email est obligatoire.']),
],
],
'second_options' => [
'label' => 'form.register.email.second',
'translation_domain' => 'form',
'constraints' => [
new Assert\NotBlank(['message' => 'La confirmation de votre adressse email est obligatoire.']),
],
],
])
->add('plainPassword', RepeatedType::class, [
'type' => PasswordType::class,
'invalid_message' => 'register.password.match',
'translation_domain' => 'form',
'first_options' => [
'label' => 'form.register.password.first',
'translation_domain' => 'form',
'constraints' => [
new Assert\NotBlank(['message' => 'Votre mot de passe est obligatoire.']),
],
],
'second_options' => [
'label' => 'form.register.password.second',
'translation_domain' => 'form',
'constraints' => [
new Assert\NotBlank(['message' => 'La confirmation de votre mot de passe est obligatoire.']),
],
],
])
->add('phoneNumber', TextType::class, [
'label' => 'form.register.number_phone',
'translation_domain' => 'form',
'constraints' => [
new Assert\Regex(['pattern' => '/^((\+)33|0)[1-9](\d{2}){4}$/','message' => 'Le numéro de téléphone est incorrect']),
new Assert\NotBlank(['message' => 'Le numéro de téléphone est obligatoire.']),
]
])
->add('optinSales', ChoiceType::class, [
'label' => 'form.register.optin.sales',
'translation_domain' => 'form',
'choices' => [
'Oui' => true,
'Non' => false,
],
'expanded' => true,
'multiple' => false,
'required' => true,
'constraints' => [
new NotNull(['message' => 'Veuillez sélectionner une option.']),
],
])
->add('hasAdditionalInformation', CheckboxType::class, [
'label' => 'Veuillez cocher cette case si vous commandez déjà chez un grossiste',
'translation_domain' => 'form',
])
->add('additionalInformation', CollectionType::class, [
'entry_type' => addInformationType::class,
'allow_add' => true,
'allow_delete' => true,
'entry_options' => ['label' => false],
'label' => false,
'translation_domain' => 'form',
])
->add('optinBrand', CheckboxType::class, [
'label' => 'form.register.optin.brand',
'translation_domain' => 'form',
])
->add('optinPhone', CheckboxType::class, [
'label' => 'form.register.optin.phone',
'translation_domain' => 'form',
])
->add('optinProcess', CheckboxType::class, [
'label' => 'form.register.optin.process',
'translation_domain' => 'form',
'constraints' => [
new Assert\NotBlank([
'message' => 'register.optin.process.required',
]),
],
])
;
$referents = $this->referentManager->getReferents();
if (!empty($referents)) {
$builder
->add('referent', ChoiceType::class, [
'choices' => $referents,
'multiple' => false,
'expanded' => false,
'label' => 'form.register.referent',
'translation_domain' => 'form',
'placeholder' => 'Sélectionnez votre activité',
'constraints' => [
new Assert\NotBlank([
'message' => 'register.referent.required',
]),
],
'choice_value' => function(?Referent $referent) {
return $referent ? '/api/referents/' . $referent->getId() : '';
},
'choice_label' => function(Referent $referent) {
return $referent->getCompanyName();
},
]);
}
$request = $this->requestStack;
$session = $this->requestStack->getSession();
$response = isset($request->getMasterRequest()->request->all()['user_register']['hasAdditionalInformation']) ? $request->getMasterRequest()->request->all()['user_register']['hasAdditionalInformation'] : false;
$session->set('hasAdditionalInformation',$response);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}