src/Form/PharmacieType.php line 204

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