src/Form/PharmacieType.php line 203

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