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