src/Form/RegistrationType.php line 207

Open in your IDE?
  1. <?php
  2. // src/Form/RegistrationType.php
  3. namespace App\Form;
  4. use App\Service\SynchroHubService;
  5. use App\Entity\Contact;
  6. use App\Entity\User;
  7. use App\Entity\Category;
  8. use App\Entity\TypologieStructure;
  9. use App\Repository\TypologieStructureRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
  12. use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;
  13. use Sulu\Bundle\CategoryBundle\Entity\CategoryRepository;
  14. use Sulu\Bundle\ContactBundle\Entity\Address;
  15. use Sulu\Bundle\ContactBundle\Entity\AddressType;
  16. use Sulu\Bundle\ContactBundle\Entity\ContactAddress;
  17. use Sulu\Bundle\SecurityBundle\Entity\Role;
  18. use Sulu\Bundle\SecurityBundle\Entity\UserRole;
  19. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  20. use Symfony\Component\Form\AbstractType;
  21. use Symfony\Component\Form\FormBuilderInterface;
  22. use Symfony\Component\Form\FormEvents;
  23. use Symfony\Component\Form\FormInterface;
  24. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  25. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  26. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  27. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  28. use Symfony\Component\Form\Extension\Core\Type\FileType;
  29. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  30. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  31. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  32. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  33. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  34. use Symfony\Component\Form\Extension\Core\Type\TextType;
  35. use Symfony\Component\Form\FormError;
  36. use Symfony\Component\Form\FormEvent;
  37. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  38. use Symfony\Component\OptionsResolver\OptionsResolver;
  39. use Symfony\Component\Validator\Constraints as Assert;
  40. class RegistrationType extends AbstractType
  41. {
  42.     private $emi;
  43.     private $flashbag;
  44.     private $SynchroHubService;
  45.     const BUTTON_CSS_CLASS 'CTA center';
  46.     const LABEL_ATTR_CSS_CLASS 'fw400 fs14 lh20';
  47.     public function __construct(
  48.         EntityManagerInterface $emi,
  49.         FlashBagInterface $flashbag,
  50.         SynchroHubService $SynchroHubService
  51.     ) {
  52.         $this->emi $emi;
  53.         $this->flashbag $flashbag;
  54.         $this->SynchroHubService $SynchroHubService;
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function buildForm(FormBuilderInterface $builder, array $options)
  60.     {
  61.         $builder->add('firstName'TextType::class, [
  62.             'property_path' => 'contact.firstName',
  63.             'label' => 'Prénom *',
  64.             'required' => true,
  65.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  66.             'attr' => ['placeholder' => 'Prénom'],
  67.             'constraints' => [
  68.                 new Assert\NotBlank(['message' => 'Le prénom est requis.']),
  69.             ],
  70.         ]);
  71.         $builder->add('lastName'TextType::class, [
  72.             'property_path' => 'contact.lastName',
  73.             'label' => 'Nom *',
  74.             'required' => true,
  75.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  76.             'attr' => ['placeholder' => 'Nom'],
  77.             'constraints' => [
  78.                 new Assert\NotBlank(['message' => 'Le nom est requis.']),
  79.             ],
  80.         ]);
  81.         $builder->add('mainEmail'EmailType::class, [
  82.             'property_path' => 'contact.mainEmail',
  83.             'label' => 'Email *',
  84.             'required' => true,
  85.             'label_attr' => ["class" => self::LABEL_ATTR_CSS_CLASS],
  86.             'attr' => ['placeholder' => 'Email'],
  87.             'constraints' => [
  88.                 new Assert\NotBlank(['message' => 'L\'email est requis.']),
  89.                 new Assert\Email(['message' => 'L\'email doit être au format valide.']),
  90.             ],
  91.         ]);
  92.         $builder->add('avatar'FileType::class, [
  93.             'mapped' => false,
  94.             'property_path' => 'contact.avatar',
  95.             'required' => false,
  96.             'label_attr' => [
  97.                 'class' => self::LABEL_ATTR_CSS_CLASS,
  98.                 'id' => 'filelabel',
  99.                 'tabindex' => 0
  100.             ]
  101.         ]);
  102.         $builder->add('number'TextType::class, [
  103.             'property_path' => 'contact.mainPhone',
  104.             'required' => true,
  105.             'label' => "Téléphone *",
  106.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  107.             'attr' => ['placeholder' => 'Téléphone']
  108.         ]);
  109.         $builder->add('socialMediaProfiles'CollectionType::class, [
  110.             'property_path' => 'contact.socialMediaProfiles',
  111.             'entry_type' => SocialMediaType::class,
  112.             'allow_add'  => true,
  113.             'allow_delete'  => true,
  114.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  115.             'label' => 'Vos réseaux sociaux',
  116.             'attr' => ['class' => 'dflex jcsb mt-1em']
  117.         ]);
  118.         $builder->add('note'TextareaType::class, [
  119.             'property_path' => 'contact.note',
  120.             'required' => false,
  121.             'label' => 'Décrivez ce que vous faites / votre champs d\'action ',
  122.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  123.             'attr' => ['placeholder' => 'Description''class' => 'br6']
  124.         ]);
  125.         $builder->add('structure'TextType::class, [
  126.             'property_path' => 'contact.structure',
  127.             'required' => true,
  128.             'label' => 'Structure *',
  129.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  130.             'attr' => ['placeholder' => 'Structure'],
  131.             'constraints' => [
  132.                 new Assert\NotBlank(['message' => 'La structure est requise.']),
  133.             ],
  134.         ]);
  135.         $builder->add('structureText'TextType::class, [
  136.             'mapped' => false,
  137.             'required' => true,
  138.             'label' => 'Typologie de structure *',
  139.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  140.             'attr' => ['placeholder' => 'Typologie de structure']
  141.         ]);
  142.         $builder->add('zip'TextType::class, [
  143.             'property_path' => 'contact.mainAddress.zip',
  144.             'required' => true,
  145.             'label' => 'Code postal *',
  146.             'label_attr' => ["class" => self::LABEL_ATTR_CSS_CLASS],
  147.             'attr' => ['placeholder' => 'Code postal']
  148.         ]);
  149.         $builder->add('street'TextType::class, [
  150.             'property_path' => 'contact.mainAddress.street',
  151.             'required' => true,
  152.             'label' => 'Adresse *',
  153.             'label_attr' => ["class" => self::LABEL_ATTR_CSS_CLASS],
  154.             'attr' => ['placeholder' => 'Adresse']
  155.         ]);
  156.         $builder->add('city'TextType::class, [
  157.             'property_path' => 'contact.mainAddress.city',
  158.             'required' => true,
  159.             'label' => 'Ville *',
  160.             'label_attr' => ["class" => self::LABEL_ATTR_CSS_CLASS],
  161.             'attr' => ['placeholder' => 'Ville']
  162.         ]);
  163.         $builder->add('typologieStructure'EntityType::class, [
  164.             'property_path' => 'contact.typologieStructure',
  165.             'class' => TypologieStructure::class,
  166.             'multiple' => false,
  167.             'expanded' => true,
  168.             'required' => false,
  169.             'placeholder' => 'Aucune',
  170.             'query_builder' => function (TypologieStructureRepository $sr) {
  171.                 return $sr->createQueryBuilder('sr')
  172.                     ->orderBy('sr.name''ASC');
  173.             },
  174.             'choice_label' => function ($structure) {
  175.                 return $structure->getName();
  176.             },
  177.             'label' => ' ',
  178.             'attr' => ['tabIndex' => 0]
  179.         ]);
  180.         $builder->add('fonction_desc'TextType::class, [
  181.             'label' => 'Précisez votre fonction *',
  182.             'required' => true,
  183.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS]
  184.         ]);
  185.         $builder->add(
  186.             'thematiques',
  187.             EntityType::class,
  188.             [
  189.                 'property_path' => 'contact.categories',
  190.                 'class' => Category::class,
  191.                 'multiple' => true,
  192.                 'required' => true,
  193.                 'expanded' => true,
  194.                 'query_builder' => function (CategoryRepository $er) {
  195.                     return $er->createQueryBuilder('c')
  196.                         ->leftJoin('c.parent''p')
  197.                         ->where('p.key = :key')
  198.                         ->setParameter('key''thematiques')
  199.                         ->orderBy('c.key''ASC');
  200.                 },
  201.                 'choice_label' => function ($category) {
  202.                     return $category->findTranslationByLocale('fr')->getTranslation();
  203.                 },
  204.                 'label' => 'Thématiques d\'intervention et d\'intérêt*'
  205.             ]
  206.         );
  207.         $builder->add(
  208.             'optins',
  209.             EntityType::class,
  210.             [
  211.                 'property_path' => 'contact.optins',
  212.                 'class' => Category::class,
  213.                 'multiple' => true,
  214.                 'required' => true,
  215.                 'expanded' => true,
  216.                 'query_builder' => function (CategoryRepository $er) {
  217.                     return $er->createQueryBuilder('c')
  218.                         ->leftJoin('c.parent''p')
  219.                         ->where('p.key = :key')
  220.                         ->setParameter('key''optins')
  221.                         ->orderBy('c.key''ASC');
  222.                 },
  223.                 'choice_label' => function ($category) {
  224.                     return $category->findTranslationByLocale('fr')->getTranslation();
  225.                 },
  226.                 'label' => 'Abonnement aux lettres d\'informations'
  227.             ]
  228.         );
  229.         $builder->add('plainPassword'RepeatedType::class, [
  230.             'mapped' => false,
  231.             'type' => PasswordType::class,
  232.             'invalid_message' => 'Le mot de passe doit correspondre.',
  233.             'options' => ['attr' => ['class' => 'password-field']],
  234.             'required' => true,
  235.             'first_options' => [
  236.                 'label' => 'Choix du mot de passe*',
  237.                 'attr' => [
  238.                     'placeholder' => 'Mot de passe',
  239.                     'autocomplete' => 'new-password'
  240.                 ],
  241.                 'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  242.                 'constraints' => [
  243.                     new Assert\NotBlank(['message' => 'Le mot de passe est requis.']),
  244.                     new Assert\Length([
  245.                         'min' => 8,
  246.                         'minMessage' => 'Le mot de passe doit comporter au moins {{ limit }} caractères.',
  247.                     ]),
  248.                 ],
  249.             ],
  250.             'second_options' => [
  251.                 'label' => 'Confirmez le mot de passe*',
  252.                 'attr' => [
  253.                     'placeholder' => 'Confirmez le mot de passe',
  254.                     'autocomplete' => 'new-password'
  255.                 ],
  256.                 'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  257.             ],
  258.         ]);
  259.         $builder->add('visibleAnnuaire'ChoiceType::class, [
  260.             'property_path' => 'contact.visibleAnnuaire',
  261.             'expanded' => true,
  262.             'multiple' => false,
  263.             'choices' => [
  264.                 'Oui' => 1,
  265.                 'Non' => 0
  266.             ],
  267.             'data' => 0,
  268.             'required' => true,
  269.             'label' => 'Souhaitez-vous que votre profil soit visible sur l\'annuaire ?*',
  270.             'label_attr' => ['class' => 'mb-1em']
  271.         ]);
  272.         $builder->add(
  273.             'terms',
  274.             CheckboxType::class,
  275.             [
  276.                 'mapped' => false,
  277.                 'required' => true,
  278.                 'label' => 'J\'accepte que les données saisies soient utilisées par PQN-A',
  279.                 'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS]
  280.             ]
  281.         );
  282.         $builder->add('typologieActeurValue'ChoiceType::class, [
  283.             'label' => 'Type d\'acteur *',
  284.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  285.             'choices' => [
  286.                 'Selectionner votre typologie d\'acteur *' => '',
  287.                 'Equipe technique des territoires et ingénierie associée' => 'Equipe technique des territoires et ingénierie associée',
  288.                 'Décideurs locaux' => 'Décideurs locaux',
  289.                 'Partenaires' => 'Partenaires',
  290.                 'Opérateurs' => 'Opérateurs',
  291.                 'Autres' => 'Autres',
  292.             ],
  293.             'choice_attr' => ['Selectionner votre typologie d\'acteur *' => ['disabled' => '']],
  294.             'required' => true,
  295.         ]);
  296.         $builder->add('autresTypologieActeur'TextType::class, [
  297.             'property_path' => 'contact.autresTypologieActeur',
  298.             'required' => true,
  299.             'label' => 'Précisez :',
  300.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS'id' => 'autres-typologie']
  301.         ]);
  302.         $builder->add('departement'EntityType::class, [
  303.             'property_path' => 'contact.departement',
  304.             'class' => Category::class,
  305.             'multiple' => false,
  306.             'expanded' => false,
  307.             'query_builder' => function (CategoryRepository $cr) {
  308.                 return $cr->createQueryBuilder('c')
  309.                     ->leftJoin('c.parent''p')
  310.                     ->where('p.key = :key')
  311.                     ->setParameter('key''departements')
  312.                     ->orderBy('c.key''ASC');
  313.             },
  314.             'choice_label' => function ($category) {
  315.                 return $category->findTranslationByLocale('fr')->getTranslation();
  316.             },
  317.             'placeholder' => 'Sélectionnez votre département *',
  318.             'required' => true,
  319.             'empty_data' => '',
  320.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS],
  321.             'label' => 'Département *'
  322.         ]);
  323.         $builder->add('territoireIntervention'EntityType::class, [
  324.             'property_path' => 'contact.territoireInterv',
  325.             'class' => Category::class,
  326.             'multiple' => false,
  327.             'expanded' => false,
  328.             'query_builder' => function (CategoryRepository $cr) {
  329.                 return $cr->createQueryBuilder('c')
  330.                     ->leftJoin('c.parent''p')
  331.                     ->orderBy('c.key''ASC');
  332.             },
  333.             'choice_label' => function ($category) {
  334.                 return $category->findTranslationByLocale('fr')->getTranslation();
  335.             },
  336.             'required' => false,
  337.             'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS]
  338.         ]);
  339.         $builder->add('username'HiddenType::class);
  340.         $builder->add('email'HiddenType::class);
  341.         $builder->add('submit'SubmitType::class, [
  342.             'label' => 'Valider',
  343.             'attr' => ['class' => self::BUTTON_CSS_CLASS]
  344.         ]);
  345.         $builder->add('submit2'SubmitType::class, [
  346.             'label' => 'Valider',
  347.             'attr' => ['class' => self::BUTTON_CSS_CLASS]
  348.         ]);
  349.         $builder->add('recaptcha'EWZRecaptchaType::class, [
  350.             'attr' => [
  351.                 'options' => [
  352.                     'theme' => 'light',
  353.                     'type'  => 'image',
  354.                     'size' => 'normal',
  355.                 ],
  356.             ],
  357.             'mapped' => false,
  358.             'constraints' => [
  359.                 new RecaptchaTrue()
  360.             ],
  361.         ]);
  362.         $builder->add('submit3'SubmitType::class, [
  363.             'label' => 'S\'inscrire',
  364.             'attr' => ['class' => self::BUTTON_CSS_CLASS]
  365.         ]);
  366.         $builder->addEventListener(
  367.             FormEvents::PRE_SUBMIT,
  368.             function (FormEvent $event) {
  369.                 $form $event->getForm();
  370.                 $data $event->getData();
  371.                 $typologie $data['typologieActeurValue'] ?? null;
  372.                 if ($typologie === 'Autres') {
  373.                     $form->add('autresTypologieActeur'TextType::class, [
  374.                         'property_path' => 'contact.autresTypologieActeur',
  375.                         'required' => true,
  376.                         'label' => 'Précisez :',
  377.                         'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS'id' => 'autres-typologie']
  378.                     ]);
  379.                 } else {
  380.                     // Define the choices for fonction based on the selected typologie
  381.                     $fonctionChoices = [];
  382.                     if ($typologie === 'Equipe technique des territoires et ingénierie associée') {
  383.                         $fonctionChoices = [
  384.                             'Directeur régional/DGA/DGS' => 'Directeur régional/DGA/DGS',
  385.                             'Chargé de mission' => 'Chargé de mission',
  386.                             'Chef de projet' => 'Chef de projet',
  387.                             'Coordinateur' => 'Coordinateur',
  388.                             'Animateur' => 'Animateur',
  389.                             'Assistant / Secrétaire' => 'Assistant / Secrétaire',
  390.                             'Délégué du préfet' => 'Délégué du préfet',
  391.                             'Directeur de services' => 'Directeur de services',
  392.                             'Responsable' => 'Responsable',
  393.                             'Conseiller' => 'Conseiller',
  394.                             'Administrateur' => 'Administrateur',
  395.                             'Médiateur' => 'Médiateur',
  396.                             'Journaliste' => 'Journaliste',
  397.                             'Chargé de communication' => 'Chargé de communication',
  398.                             'Habitants / membres des CC ou de CD / Collectifs citoyens' => 'Habitants / membres des CC ou de CD / Collectifs citoyens',
  399.                             // Add more fonction choices for this typologie
  400.                         ];
  401.                     } elseif ($typologie === 'Décideurs locaux') {
  402.                         $fonctionChoices = [
  403.                             'Maire' => 'Maire',
  404.                             'Maire-adjoint' => 'Maire-adjoint',
  405.                             'Président Intercommunalité' => 'Président Intercommunalité',
  406.                             'Vice-Président Intercommunalité' => 'Vice-Président Intercommunalité',
  407.                             'Conseiller régional' => 'Conseiller régional',
  408.                             'Conseiller départemental' => 'Conseiller départemental',
  409.                             'Conseiller communautaire' => 'Conseiller communautaire',
  410.                             'Conseiller municipal' => 'Conseiller municipal',
  411.                             'Préfet/sous-Préfet' => 'Préfet/sous-Préfet'
  412.                             // Add more fonction choices for this typologie
  413.                         ];
  414.                     }
  415.                     // Add more elseif blocks for other typologie options
  416.                     $form->add('fonction'ChoiceType::class, [
  417.                         'label' => 'Fonction / poste occupé *',
  418.                         'choices' => $fonctionChoices,
  419.                         'required' => true,
  420.                         'label_attr' => ['class' => self::LABEL_ATTR_CSS_CLASS]
  421.                     ]);
  422.                 }
  423.                 if (array_key_exists('mainEmail'$data)) {
  424.                     $data['username'] = $data['mainEmail'];
  425.                     $data['email'] = $data['mainEmail'];
  426.                 }
  427.                 $event->setData($data);
  428.             }
  429.         );
  430.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  431.             $form $event->getForm();
  432.             if (!isset($_POST['g-recaptcha-response'])) {
  433.                 $form->addError(new FormError('Le captcha n\'est pas valide.'));
  434.                 $event->stopPropagation();
  435.                 return;
  436.             }
  437.             if (isset($_POST['g-recaptcha-response'])) {
  438.                 $isCaptchValid $this->isCaptchaValid($_POST['g-recaptcha-response'], $_ENV['EWZ_RECAPTCHA_SECRET']);
  439.                 if (!$isCaptchValid) {
  440.                     $form->addError(new FormError('Le captcha n\'est pas valide.'));
  441.                     $event->stopPropagation();
  442.                 }
  443.             }
  444.             if (!$form->isValid()) {
  445.                 $this->addValidationErrorsToFlashBag($form$this->flashbag);
  446.             }
  447.             if ($form->isValid()) {
  448.                 $datasArray = [
  449.                     'nom' => $form->get('lastName')->getData(),
  450.                     'prnom' => $form->get('firstName')->getData(),
  451.                     'e_mail' => $form->get('mainEmail')->getData(),
  452.                     'tl__fixedirectportable_' => $this->ifValueReturnValueOrNothing($form'number'),
  453.                     'adresse' => $this->ifValueReturnValueOrNothing($form'street'),
  454.                     'ville' => $this->ifValueReturnValueOrNothing($form'city'),
  455.                     'cp' => $this->ifValueReturnValueOrNothing($form'zip'),
  456.                     'dpartement' => $this->getDepartmentValueOrNothing($form),
  457.                     'fonction_dtaille__texte_libre_' => $this->ifValueReturnValueOrNothing($form'note'),
  458.                     'fonction_type_dacteurs' => $this->ifValueReturnValueOrNothing($form'typologieActeurValue'),
  459.                     'fonction__poste_occup' => $this->ifValueReturnValueOrNothing($form'fonction'),
  460.                     'structure' => $this->ifValueReturnValueOrNothing($form'structure'),
  461.                     'typologie_de_structure' => $this->ifValueReturnValueOrNothing($form'structureText')
  462.                 ];
  463.                 foreach ($form->get('thematiques')->getData() as $data) {
  464.                     if ($data->getKey() === "europe" || $data->getKey() === "developpement-territorial" || $data->getKey() === "participation-citoyenne") continue;
  465.                     $datasArray[$data->getKey()] = '1';
  466.                 }
  467.                 foreach ($form->get('optins')->getData() as $data) {
  468.                     $datasArray[$data->getKey()] = '1';
  469.                 }
  470.                 $this->SynchroHubService->createHubUser($datasArray''$_SERVER['SYNCHRO_HUB_DATABASE_ID']);
  471.             }
  472.         });
  473.     }
  474.     /**
  475.      * {@inheritdoc}
  476.      */
  477.     public function configureOptions(OptionsResolver $resolver)
  478.     {
  479.         $constraints = [
  480.             'recaptcha' => new RecaptchaTrue()
  481.         ];
  482.         $resolver->setDefaults(
  483.             [
  484.                 'constraints' => $constraints,
  485.                 'data_class' => User::class,
  486.                 'validation_groups' => ['registration'],
  487.                 'attr' => ['id' => 'submit-form''class' => 'recaptcha-form'],
  488.                 'empty_data' => function (FormInterface $form) {
  489.                     $user = new User();
  490.                     $contact = new Contact();
  491.                     $address = new Address();
  492.                     $address->setCity($form->get('city')->getData());
  493.                     $address->setZip($form->get('zip')->getData());
  494.                     $address->setStreet($form->get('street')->getData());
  495.                     $addressType $this->emi->getReference(AddressType::class, 1);
  496.                     $address->setAddressType($addressType);
  497.                     $mainAddress = new ContactAddress();
  498.                     $mainAddress->setMain(true);
  499.                     $mainAddress->setAddress($address);
  500.                     $mainAddress->setContact($contact);
  501.                     $contact->addContactAddress($mainAddress);
  502.                     $user->setContact($contact);
  503.                     /**
  504.                      * peut servir pour ajouter plusieurs roles
  505.                      */
  506.                     /* $roleQuery = $this->emi->createQueryBuilder()
  507.                         ->select('r')
  508.                         ->from(Role::class, 'r')
  509.                         ->where("r.name = 'PqnaGtEdit'");
  510.                     $roleQueryResult = $roleQuery->getQuery()->execute();
  511.                     if (sizeof($roleQueryResult) > 0) {
  512.                         $editRole = new UserRole();
  513.                         $editRole->setRole($roleQueryResult[0]);
  514.                         $editRole->setLocale('["fr"]');
  515.                         $editRole->setUser($user);
  516.                         $this->emi->persist($editRole);
  517.                         $user->addUserRole($editRole);
  518.                     } */
  519.                     $this->emi->persist($contact);
  520.                     $this->emi->persist($user);
  521.                     return $user;
  522.                 },
  523.                 'error_mapping' => [
  524.                     'data.email' => 'form.email',
  525.                     'data.mainEmail' => 'form.mainEmail'
  526.                 ],
  527.                 'error_bubbling' => true
  528.             ]
  529.         );
  530.     }
  531.     private function isCaptchaValid($response$secret): ?bool
  532.     {
  533.         try {
  534.             $url 'https://www.google.com/recaptcha/api/siteverify';
  535.             $data = [
  536.                 'secret'   => $secret,
  537.                 'response' => $response,
  538.                 'remoteip' => $_SERVER['REMOTE_ADDR']
  539.             ];
  540.             $options = [
  541.                 'http' => [
  542.                     'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
  543.                     'method'  => 'POST',
  544.                     'content' => http_build_query($data)
  545.                 ]
  546.             ];
  547.             $context  stream_context_create($options);
  548.             $result file_get_contents($urlfalse$context);
  549.             return json_decode($result)->success;
  550.         } catch (\Exception $e) {
  551.             return null;
  552.         }
  553.     }
  554.     private function addValidationErrorsToFlashBag(FormInterface $formFlashBagInterface $flashBag)
  555.     {
  556.         foreach ($form->getErrors(truefalse) as $error) {
  557.             $flashBag->add('error'$error->getMessage());
  558.         }
  559.     }
  560.     private function ifValueReturnValueOrNothing(FormInterface $formstring $value)
  561.     {
  562.         if ($form->has($value) && method_exists($form->get($value), 'getData'))
  563.             return !empty($form->get($value)->getData()) ? $form->get($value)->getData() : '';
  564.         return '';
  565.     }
  566.     private function getDepartmentValueOrNothing(FormInterface $form)
  567.     {
  568.         if ($form->get('departement') && method_exists($form->get('departement'), 'getData'))
  569.             return !empty($form->get('departement')->getData()) ? explode('_'$form->get('departement')->getData()->getKey())[0] : '';
  570.         return '';
  571.     }
  572. }