src/Form/PrescripteurType.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Pays;
  4. use App\Entity\User;
  5. use App\Entity\Ville;
  6. use App\Entity\Quartier;
  7. use App\Entity\Speciality;
  8. use GeoIp2\Database\Reader;
  9. use App\Services\CountryCode;
  10. use App\Repository\VilleRepository;
  11. use Symfony\Component\Form\FormEvent;
  12. use App\Repository\QuartierRepository;
  13. use Symfony\Component\Form\FormEvents;
  14. use Symfony\Component\Form\AbstractType;
  15. use Symfony\Component\Form\FormInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use GeoIp2\Exception\AddressNotFoundException;
  18. use Symfony\Component\Form\FormBuilderInterface;
  19. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  20. use Symfony\Component\HttpKernel\KernelInterface;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Symfony\Component\Validator\Constraints\NotBlank;
  23. use Symfony\Component\OptionsResolver\OptionsResolver;
  24. use Symfony\Component\Form\Extension\Core\Type\TextType;
  25. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  26. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  27. use Symfony\Component\Form\Extension\Core\Type\CountryType;
  28. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  29. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  30. use Doctrine\ORM\EntityManagerInterface;
  31. class PrescripteurType extends AbstractType
  32. {
  33.     private $appKernel;
  34.     private $ip;
  35.     private $em;
  36.     private $request;
  37.     public function __construct(EntityManagerInterface $emKernelInterface $appKernelRequestStack $requestStack){
  38.         $this->appKernel $appKernel;
  39.         $this->ip $requestStack->getCurrentRequest()->getClientIp();
  40.         $this->em $em;
  41.         $this->request $requestStack->getCurrentRequest();
  42.     }
  43.     public function buildForm(FormBuilderInterface $builder, array $options): void
  44.     {
  45.         //pour la multilangue
  46.         $location_lang $this->request->getLocale();
  47.         //end multilangue
  48.         $GeoLiteDatabasePath $this->appKernel->getProjectDir() . '/src/db/GeoLite2-City.mmdb';
  49.         $reader = new Reader($GeoLiteDatabasePath);
  50.         try {
  51.             $record $reader->city($this->ip);
  52.         } catch (AddressNotFoundException $ex) {
  53.             $record $reader->city('154.126.115.219');
  54.         }
  55.         
  56.         $builder
  57.             ->add('firstname'TextType::class, [
  58.                 'label' => false,
  59.                 'required' => true,
  60.                 'attr'  => [
  61.                     'class' =>'form-control form-sm'
  62.                 ],
  63.                   'constraints' => array(
  64.                     new NotBlank(array(
  65.                         'message' => 'Formulaire obligtoire',
  66.                     )),
  67.                 )
  68.             ])
  69.             ->add('lastname'TextType::class, [
  70.                 'label' => false,
  71.                 'required' => true,
  72.                 'attr'  => [
  73.                     'class' =>'form-control form-sm'
  74.                 ],
  75.                   'constraints' => array(
  76.                     new NotBlank(array(
  77.                         'message' => 'Formulaire obligtoire',
  78.                     )),
  79.                 )
  80.             ])
  81.             ->add('ordre'TextType::class, [
  82.                 'label' => false,
  83.                 'required' => true,
  84.                 'attr'  => [
  85.                     'class' =>'form-control form-sm'
  86.                 ],
  87.                   'constraints' => array(
  88.                     new NotBlank(array(
  89.                         'message' => 'Formulaire obligtoire',
  90.                     )),
  91.                 )
  92.             ])
  93.             ->add('address'TextType::class, [
  94.                 'label' => false,
  95.                 'required' => true,
  96.                 'attr'  => [
  97.                     'class' =>'form-control form-sm'
  98.                 ],
  99.                   'constraints' => array(
  100.                     new NotBlank(array(
  101.                         'message' => 'Formulaire obligtoire',
  102.                     )),
  103.                 )
  104.             ])
  105.             ->add('payslocalisation'EntityType::class, [
  106.                 'label' => false,
  107.                 'class' => Pays::class,
  108.                 'preferred_choices' => [$record->country->isoCode],//['MG'],//[$this->countryCode->getCode()],
  109.                 'choice_label'  => 'name',
  110.                 //'data' => $pay,
  111.                 'required' => true,
  112.                 'attr'  => [
  113.                     'class' =>'form-control form-sm'
  114.                 ],
  115.                   'constraints' => array(
  116.                     new NotBlank(array(
  117.                         'message' => 'Formulaire obligtoire',
  118.                     )),
  119.                 ),
  120.                  /*'query_builder' => function (PaysRepository $prepo) {
  121.                                 return $prepo->createQueryBuilder('p')->orderBy('p.name', 'ASC');
  122.                 }*/
  123.             ])
  124.             /*->add('pays', CountryType::class, [
  125.                 'label' => false,
  126.                 //'disabled' => true,
  127.                 'preferred_choices' => [$record->country->isoCode],//['MG'],//$this->requestStack->getCurrentRequest()->getClientIp()
  128.                 'required' => true,
  129.                 'attr'  => [
  130.                     'class' =>'form-control form-sm'
  131.                 ],
  132.                   'constraints' => array(
  133.                     new NotBlank(array(
  134.                         'message' => 'Formulaire obligtoire',
  135.                     )),
  136.                 )
  137.             ])
  138.             ->add('ville', EntityType::class, [
  139.                 'label' => false,
  140.                 'class' => Ville::class,
  141.                 'choice_label'  => 'name',
  142.                 'multiple'      => false,
  143.                 'required' => true,
  144.                 'attr'  => [
  145.                     'class' =>'form-control form-sm'
  146.                 ],
  147.                   'constraints' => array(
  148.                     new NotBlank(array(
  149.                         'message' => 'Formulaire obligtoire',
  150.                     )),
  151.                 ),
  152.                 'query_builder' => function(VilleRepository $ville_repo) {
  153.                         return $ville_repo->getListVille();
  154.                 }
  155.             ])
  156.             ->add('quartier')*/
  157.             ->add('email'EmailType::class, [
  158.                 'label' => false,
  159.                 'required' => true,
  160.                 'attr'  => [
  161.                     'class' =>'form-control form-sm'
  162.                 ],
  163.                   'constraints' => array(
  164.                     new NotBlank(array(
  165.                         'message' => 'Formulaire obligtoire',
  166.                     )),
  167.                 )
  168.             ])
  169.             ->add('password'PasswordType::class, [
  170.                 'label' => false,
  171.                 'required' => true,
  172.                 'attr'  => [
  173.                     'class' =>'form-control form-sm'
  174.                 ],
  175.                   'constraints' => array(
  176.                     new NotBlank(array(
  177.                         'message' => 'Formulaire obligtoire',
  178.                     )),
  179.                 )
  180.             ])
  181.             ->add('confirm_password'PasswordType::class, [
  182.                 'label' => false,
  183.                 'required' => true,
  184.                 'attr'  => [
  185.                     'class' =>'form-control form-sm'
  186.                 ],
  187.                   'constraints' => array(
  188.                     new NotBlank(array(
  189.                         'message' => 'Formulaire obligtoire',
  190.                     )),
  191.                 )
  192.             ])
  193.             ->add('phone'TextType::class, [
  194.                 'label' => false,
  195.                 'required' => true,
  196.                 'attr'  => [
  197.                     'class' =>'form-control form-sm telephone'
  198.                 ],
  199.                   'constraints' => array(
  200.                     new NotBlank(array(
  201.                         'message' => 'Formulaire obligtoire',
  202.                     )),
  203.                 )
  204.             ])
  205.             /*->add('phonetwo', TextType::class, [
  206.                 'label' => false,
  207.                 'required' => false,
  208.                 'attr'  => [
  209.                     'class' =>'form-control form-sm'
  210.                 ]
  211.             ])
  212.             ->add('phonethree', TextType::class, [
  213.                 'label' => false,
  214.                 'required' => false,
  215.                 'attr'  => [
  216.                     'class' =>'form-control form-sm'
  217.                 ]
  218.             ])*/
  219.             /*->add('is_acceptcgv', CheckboxType::class, [
  220.                 'label' => false,
  221.                 'attr' => [
  222.                   'class' => 'custom-control custom-checkbox',
  223.                 ],
  224.                   'constraints' => array(
  225.                     new NotBlank(array(
  226.                         'message' => 'Formulaire obligtoire',
  227.                     )),
  228.                 )
  229.             ])*/
  230.             ->add('speciality'EntityType::class,[
  231.                 'label' => false,
  232.                 'class' => Speciality::class,
  233.                 'choice_label' => $location_lang == 'fr' 'name' 'name_en',
  234.                 'multiple' => false,
  235.                 'required' => true,
  236.                   'constraints' => array(
  237.                     new NotBlank(array(
  238.                         'message' => 'Formulaire obligtoire',
  239.                     )),
  240.                 )
  241.             ])
  242.         ;
  243.         $formModifierPays = function (FormInterface $formPays $pays null) {
  244.         $villes null === $pays ? array() : $pays->getVilles();
  245.              
  246.             $form->add('ville'EntityType::class, array(
  247.                 'class'       => Ville::class,
  248.                 'label'       => false,
  249.                 'placeholder' => 'Selectionner un ville',
  250.                 'choices'     => $villes,
  251.                 'choice_label'=> 'name',
  252.                 'multiple'    => false,
  253.             ));
  254.  
  255.         };
  256.         $formModifierVille = function (FormInterface $formVille $ville null) {
  257.         $quartiers null === $ville ? array() : $ville->getQuartiers();
  258.              
  259.             $form->add('quartier'EntityType::class, array(
  260.                 'class'       => Quartier::class,
  261.                 'label'       => false,
  262.                 'placeholder' => 'Selectionner un quartier',
  263.                 'choices'     => $quartiers,
  264.                 'choice_label'=> 'name',
  265.                 'multiple'    => false,
  266.             ));
  267.         };
  268.         $builder->addEventListener(
  269.             FormEvents::PRE_SET_DATA,
  270.             function (FormEvent $event) use ($formModifierPays$formModifierVille) {
  271.  
  272.                 $data $event->getData();
  273.                 $pays null;
  274.                 $ville null;
  275.  
  276.                 if ($data instanceof Result){
  277.                     if (null !== $data->getPays() && null !== $data->getVille()->getId()) {
  278.                         $pays $data->getPays();
  279.                     }
  280.                     if (null !== $data->getVille() && null !== $data->getVille()->getId()) {
  281.                         $ville $data->getVille();
  282.                         $form;
  283.                     }
  284.                 }
  285.  
  286.                 $formModifierPays($event->getForm(), $pays);
  287.                 $formModifierVille($event->getForm(), $ville);
  288.             }
  289.         );
  290.         $builder->addEventListener(
  291.             FormEvents::PRE_SUBMIT,
  292.             function (FormEvent $event) use ($formModifierPays$formModifierVille) {
  293.  
  294.                 $form $event->getForm();
  295.                 $data $event->getData();
  296.  
  297.                 if (isset($data['payslocalisation']) && !empty($data['payslocalisation'])){
  298.                     $repository =  $this->em->getRepository(Pays::class);
  299.                     $pays $repository->find($data['payslocalisation']);
  300.                     $formModifierPays($form$pays);
  301.                 }
  302.                 if (isset($data['ville']) && !empty($data['ville'])){
  303.                     $repository =  $this->em->getRepository(Ville::class);
  304.                     $ville $repository->find($data['ville']);
  305.                     $formModifierVille($form$ville);
  306.                 }
  307.  
  308.             }
  309.         );
  310.     }
  311. /*
  312.     public function getCountryCod(Request $request)
  313.     {
  314.         $ip = $this->container->get('request')->getClientIp();
  315.     }*/
  316.     public function configureOptions(OptionsResolver $resolver): void
  317.     {
  318.         $resolver->setDefaults([
  319.             'data_class' => User::class,
  320.         ]);
  321.     }
  322. }