src/Form/RegistrationFormType.php line 229

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @author Mehrez Labidi
  5.  */
  6. namespace App\Form;
  7. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  8. use App\Validator\Constraints\Nom;
  9. use App\Validator\Constraints\Prenom;
  10. use stdClass;
  11. use App\Entity\Utilisateur;
  12. use App\Form\DataTransformer\MessageTransformer;
  13. use App\Form\Type\FormChoices;
  14. use App\Helper\TransProvider;
  15. use App\Helper\Utils;
  16. use App\Services\TelephoneInternational;
  17. use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
  18. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  19. use Symfony\Component\Form\AbstractType;
  20. use Symfony\Component\Form\ChoiceList\ChoiceList;
  21. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  22. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  23. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  24. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  25. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  26. use Symfony\Component\Form\Extension\Core\Type\TextType;
  27. use Symfony\Component\Form\FormBuilderInterface;
  28. use Symfony\Component\OptionsResolver\OptionsResolver;
  29. use Symfony\Component\Validator\Constraints\IsTrue;
  30. use Symfony\Component\Validator\Constraints\Length;
  31. use Symfony\Component\Validator\Constraints\NotBlank;
  32. use Rollerworks\Component\PasswordStrength\Validator\Constraints as RollerworksPassword;
  33. use App\Validator\Constraints\UniqueEmail;
  34. use App\Validator\Constraints\PhoneNumber;
  35. use App\Validator\Constraints\ContainsOnlyAlphabetic;
  36. use \App\Services\ManagerEntity\LocalisationsManagers;
  37. use App\Repository\ParametresDiversRepository;
  38. use App\Form\Type\Inscription\InscriptionRegionListener;
  39. use Symfony\Component\Validator\Constraints as Assert;
  40. class RegistrationFormType extends AbstractType {
  41.     /**
  42.      * @var MessageTransformer
  43.      */
  44.     private $messageTransformer;
  45.     /**
  46.      * @var TelephoneInternational
  47.      */
  48.     private $telephoneInternational;
  49.     /**
  50.      * @var TransProvider
  51.      */
  52.     private $transProvider;
  53.     /**
  54.      * @var \App\Services\ManagerEntity\LocalisationsManagers 
  55.      */
  56.     private $localisationsManagers;
  57.     /**
  58.      * @var ContainerBagInterface
  59.      */
  60.     private $params;
  61.     /**
  62.      * 
  63.      * @var type
  64.      */
  65.     private $parametresDiversRepository;
  66.     /**
  67.      * @param TransProvider $transProvider
  68.      * @param ContainerBagInterface $params
  69.      * @param MessageTransformer $messageTransformer
  70.      * @param TelephoneInternational $telephoneInternational
  71.      * @param LocalisationsManagers $localisationsManagers
  72.      */
  73.     public function __construct(ParametresDiversRepository $parametresDiversRepositoryTransProvider $transProviderContainerBagInterface $paramsMessageTransformer $messageTransformerTelephoneInternational $telephoneInternationalLocalisationsManagers $localisationsManagers) {
  74.         $this->params $params;
  75.         $this->transProvider $transProvider;
  76.         $this->messageTransformer $messageTransformer;
  77.         $this->telephoneInternational $telephoneInternational;
  78.         $this->localisationsManagers $localisationsManagers;
  79.         $this->parametresDiversRepository $parametresDiversRepository;
  80.     }
  81.     /**
  82.      * @param FormBuilderInterface $builder
  83.      * @param array                $options
  84.      * @return void
  85.      */
  86.     public function buildForm(FormBuilderInterface $builder, array $options): void {
  87.         $builder
  88.                 ->add('civilite'ChoiceType::class, [
  89.                     'choices' => ['M' => 'masculin''Mme' => 'feminin'],
  90.                     'expanded' => true,
  91.                     'multiple' => false,
  92.                     "constraints" => [
  93.                         new NotBlank([
  94.                             "message" => 'le champ civilite est obligatoire',
  95.                                 ]),
  96.                     ],
  97.                 ])
  98.                 ->add('type_utilisateur'ChoiceType::class,
  99.                         [
  100.                             "attr" => [],
  101.                             'choices' => $this->transChoice(Utils::reverseArrayKeyValueValueKey(Utilisateur::TYPE_UTILISATEUR)),
  102.                             'expanded' => true,
  103.                             'multiple' => false,
  104.                             "constraints" => [
  105.                                 new NotBlank([
  106.                                     "message" => 'le champ type utilisateur est obligatoire',
  107.                                         ]),
  108.                             ],
  109.                         ]
  110.                 )
  111.                 // id_type_structure pour les choix expert /investisseur
  112.                 ->add('id_type_structure_investisseur'ChoiceType::class,
  113.                         [
  114.                             "attr" => [
  115.                                 "v-if" => "id_type_structure_investisseur"
  116.                             ],
  117.                             "choices" => $this->transChoice(FormChoices::typeInvestisseur()),
  118.                             "required" => true'mapped' => false,
  119.                         ]
  120.                 )
  121.                 // id_type_structure pour les choix expert /investisseur
  122.                 ->add('id_type_structure_expert'ChoiceType::class,
  123.                         [
  124.                             "attr" => [
  125.                                 "v-if" => "id_type_structure_expert"
  126.                             ],
  127.                             "choices" => $this->transChoice(FormChoices::typeExpert()),
  128.                             "required" => true'mapped' => false,
  129.                         ]
  130.                 )
  131.                 ->add('nom_utilisateur'TextType::class,
  132.                         [
  133.                             "constraints" => [
  134.                                 new NotBlank([
  135.                                     "message" => $this->transChoice('le champ nom est obligatoire'),
  136.                                         ]),
  137.                                 new Nom()
  138.                             ],
  139.                         ]
  140.                 )
  141.                 ->add('prenom_utilisateur'TextType::class,
  142.                         [
  143.                             "constraints" => [
  144.                                 new NotBlank([
  145.                                     "message" => $this->transChoice('le champ prenom est obligatoire'),
  146.                                         ]),
  147.                                 new Prenom()
  148.                             ],
  149.                         ]
  150.                 )
  151.                 ->add('id_pays_utilisateur'ChoiceType::class,
  152.                         [
  153.                             "choices" => $this->listPays(),
  154.                             'choice_attr' => function ($key$val$index) {
  155.                                 return ($key == "") ? ['disabled' => 'disabled'] : [];
  156.                             },
  157.                             'data' => array_values($this->listPays())[0],
  158.                             "constraints" => [
  159.                                 new NotBlank([
  160.                                     "message" => $this->transChoice('le champ pays est obligatoire'),
  161.                                         ]),
  162.                             ],
  163.                             "attr" => [
  164.                                 "v-model" => "pays_utilisateur"
  165.                             ],
  166.                         ]
  167.                 )
  168.                 ->add('id_region'ChoiceType::class,
  169.                         ["choices" => [],
  170.                             "attr" => [
  171.                                 "v-html" => "regionHtml",
  172.                                 "v-if" => "seen_list_regions",
  173.                                 "class" => "form-control"
  174.                             ],
  175.                             "constraints" => [
  176.                                 new NotBlank([
  177.                                     "message" => $this->transChoice('le champ region est obligatoire'),
  178.                                         ]),
  179.                             ],
  180.                         ]
  181.                 )->addEventSubscriber(new InscriptionRegionListener($this->localisationsManagers))
  182.                 ->add('ville_utilisateur'TextType::class,
  183.                         [
  184.                             "constraints" => [],
  185.                         ]
  186.                 )
  187.                 ->add('code_postal_utilisateur'TextType::class,
  188.                         [
  189.                             "constraints" => [],
  190.                         ]
  191.                 )
  192.                 ->add('adresse_utilisateur'TextType::class,
  193.                         [
  194.                             "constraints" => [],
  195.                         ]
  196.                 )
  197.                 ->add('factur_nom_societe'TextType::class,
  198.                         [
  199.                             "constraints" => [],
  200.                         ]
  201.                 )
  202.                 ->add('telephone_utilisateur'TextType::class,
  203.                         [
  204.                             "constraints" => [
  205.                                 new NotBlank([
  206.                                     "message" => $this->transChoice('le champ telephone est obligatoire'),
  207.                                         ]),
  208.                                 new PhoneNumber()
  209.                             ],
  210.                         ]
  211.                 )
  212.                 ->add('indicatif_tel'ChoiceType::class, [
  213.                     "attr" => [
  214.                         "v-model" => "indicatif_tel"
  215.                     ],
  216.                     'choices' => $this->telephoneInternational->listIndicatifChoiceForm(),
  217.                     'choice_attr' => function ($key$val$index) {
  218.                         return ($key == "") ? ['disabled' => 'disabled'] : [];
  219.                     },
  220.                     'data' => array_values($this->telephoneInternational->listIndicatifChoiceForm())[0]
  221.                         ]
  222.                 )
  223.                 ->add('provenance_notoriete_fusacq'ChoiceType::class,
  224.                         [
  225.                             'choices' => $this->transChoice(FormChoices::provenanceNotorieteFusacq())
  226.                         ]
  227.                 )
  228.                 ->add('newsletters_internationals'ChoiceType::class, [
  229.                     'choices' => $this->transChoice($this->listChoixPaysNewsletter()->listChoix),
  230.                     'data' => $this->listChoixPaysNewsletter()->defaultChoix,
  231.                     'expanded' => true'mapped' => false,
  232.                     'multiple' => true'label_html' => true,
  233.                 ])
  234.                 ->add('email_utilisateur'TextType::class,
  235.                         [
  236.                             "constraints" => [
  237.                                 new Assert\Email(["message" => "l'adresse email saisie est invalide"]),
  238.                                 new NotBlank([
  239.                                     "message" => $this->transChoice('le champ email est obligatoire'),
  240.                                         ]), // UniqueEmail unique mail mais pour les utilisateurs en mode non inscrit sont acceptées!
  241.                                 new UniqueEmail()
  242.                             ],
  243.                         ]
  244.                 )
  245.                 ->add('plainPassword'RepeatedType::class, [
  246.                     'type' => PasswordType::class,
  247.                     'first_options' => [
  248.                         'attr' => ['autocomplete' => 'new-password'],
  249.                         'constraints' => [
  250.                             new NotBlank([
  251.                                 "message" => 'le champ mot de passe est obligatoire',
  252.                                     ]),
  253.                             // https://github.com/rollerworks/PasswordStrengthValidator
  254.                             new RollerworksPassword\PasswordStrength([
  255.                                 'minLength' => 8,
  256.                                 'minStrength' => 3
  257.                                     ]),
  258.                         ],
  259.                         'label' => 'le nouveau mot de passe',
  260.                     ],
  261.                     'second_options' => [
  262.                         'attr' => ['autocomplete' => 'new-password'],
  263.                         'label' => 'le champ confirmation mot de passe est obligatoire',
  264.                     ],
  265.                     'invalid_message' => 'les deux mots de passe ne sont pas identiques',
  266.                     // Instead of being set onto the object directly,
  267.                     // this is read and encoded in the controller
  268.                     'mapped' => false,
  269.                 ])
  270.                 ->add('langue_utilisee'ChoiceType::class,
  271.                         [
  272.                             "required" => true,
  273.                             "attr" => ["autocomplete" => "off""class" => "form-control"],
  274.                             "choices" => Utils::reverseArrayKeyValueValueKey($this->parametresDiversRepository->getLangueForSelect())
  275.                         ]
  276.                 )
  277.                 ->add('non_assujetti_tva'CheckboxType::class, ['required' => false'label' => false])
  278.                 ->add('tva_intracommunautaire'TextType::class, ["constraints" => [],])
  279.                 ->add('agreeTerms'CheckboxType::class, [
  280.                     'mapped' => false,
  281.                     'label_html' => true,
  282.                     'label' => html_entity_decode(
  283.                             "<small>" .
  284.                             $this->transChoice("je reconnais avoir pris connaissance des <a  href='/conditions-generales-utilisation' target='_blank' style='text-decoration: underline;' >Conditions Générales d'utilisation</a> de FUSACQ et les accepte") .
  285.                             "</small>"),
  286.                     "constraints" => [
  287.                         new IsTrue([
  288.                             "message" => $this->transChoice("Vous devez accepter les conditions générales d'utilisation pour votre s'inscrire"),
  289.                                 ]),
  290.                     ],
  291.                 ])
  292.                 ->add('btn'SubmitType::class, [
  293.                     'label' => $this->transChoice('creer mon compte'),
  294.                     'attr' => ['class' => 'btn btn-bleu active'],
  295.                 ])
  296.                 ->add('recaptcha'EWZRecaptchaType::class,
  297.                         ['mapped' => false'language' => $this->transProvider->getLocale()]
  298.                 )
  299.         ;
  300.     }
  301.     /**
  302.      * @param $var
  303.      * @return mixed|string
  304.      */
  305.     private function transChoice($var) {
  306.         if (is_string($var)) {
  307.             $var $this->transProvider
  308.                     ->translate($var"""M");
  309.         }
  310.         if (is_array($var)) {
  311.             foreach ($var as $key => $item) {
  312.                 unset($var[$key]);
  313.                 $key $this->transProvider
  314.                         ->translate($key"""M");
  315.                 $var[$key] = $item;
  316.             }
  317.         }
  318.         return $var;
  319.     }
  320.     /**
  321.      * @return array
  322.      */
  323.     private function listChoixPaysNewsletter() {
  324.         $paramsNewslettersPays $this->params->get('app.newsletters_pays');
  325.         $return = new stdClass();
  326.         $return->defaultChoix NULL;
  327.         $result = [];
  328.         // on prend  codePays de session pour afficher le pays du site en premier
  329.         foreach ($paramsNewslettersPays as $item) {
  330.             if ($_SESSION['codePays'] == strtolower($item[1])) {
  331.                 $return->defaultChoix = [=> strtolower($item[1])];
  332.                 $tmpVar html_entity_decode(" <img src='http://www.help-fusacq.com/fr/images/$item[3].png' alt='$item[0]'> ") . $item[0];
  333.             }
  334.             $result[html_entity_decode(" <img src='http://www.help-fusacq.com/fr/images/$item[3].png' alt='$item[0]'> ") . $item[0]] = strtolower($item[1]);
  335.         }
  336.         if (isset($tmpVar)) {
  337.             $result = [$tmpVar => $_SESSION['codePays']] + $result;
  338.         }
  339.         $return->listChoix $result;
  340.         return $return;
  341.     }
  342.     /**
  343.      * Récupère la liste des pays en excluant les DOM-TOM
  344.      *
  345.      * Cette méthode retourne un tableau associatif des pays disponibles
  346.      * en filtrant les départements et territoires d'outre-mer (DOM-TOM).
  347.      * Le tableau retourné utilise le nom de la localisation comme clé
  348.      * et l'identifiant de localisation comme valeur.
  349.      *
  350.      * @return array<string, string> Tableau associatif [nom_localisation => id_localisation]
  351.      */
  352.     private function listPays(): array { // sans les DOM_TOM
  353.         $nomDomTom array_column($this->localisationsManagers->getListDOMTOM(), "nom_localisation");
  354.         $list = [];
  355.         foreach ($this->localisationsManagers->getListPays(true) as $pays) {
  356.             if (in_array($pays["nom_localisation"], $nomDomTom)) {
  357.                 continue;
  358.             }
  359.             $list[$pays["nom_localisation"]] = $pays["id_localisation"];
  360.         }
  361.         return $list;
  362.     }
  363.     /**
  364.      * @param OptionsResolver $resolver
  365.      * @return void
  366.      */
  367.     public function configureOptions(OptionsResolver $resolver): void {
  368.         $resolver->setDefaults([
  369.             'data_class' => Utilisateur::class,
  370.             'csrf_protection' => false,
  371.             'allow_extra_fields' => true,
  372.         ]);
  373.     }
  374. }