src/Form/GrossisteType.php line 31

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\Services\CountryCode;
  9. use App\Repository\VilleRepository;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\Form\AbstractType;
  14. use Symfony\Component\Form\FormInterface;
  15. use GeoIp2\Exception\AddressNotFoundException;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  18. use Symfony\Component\HttpKernel\KernelInterface;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Symfony\Component\Validator\Constraints\NotBlank;
  21. use Symfony\Component\OptionsResolver\OptionsResolver;
  22. use Symfony\Component\Form\Extension\Core\Type\TextType;
  23. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  24. use Symfony\Component\Form\Extension\Core\Type\CountryType;
  25. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  26. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  27. use Symfony\Contracts\Translation\TranslatorInterface;
  28. class GrossisteType extends AbstractType
  29. {
  30.     private $countryCode;
  31.     private $appKernel;
  32.     private $ip;
  33.     private $em;
  34.     private $tanslator;
  35.     public function __construct(EntityManagerInterface $emCountryCode $countryCodeKernelInterface $appKernelRequestStack $requestStackTranslatorInterface $tanslator){
  36.         $this->countryCode $countryCode;
  37.         $this->appKernel $appKernel;
  38.         $this->ip $requestStack->getCurrentRequest()->getClientIp();
  39.         $this->em $em;
  40.         $this->tanslator $tanslator;
  41.     }
  42.     
  43.     public function buildForm(FormBuilderInterface $builder, array $options): void
  44.     {
  45.         $GeoLiteDatabasePath $this->appKernel->getProjectDir() . '/src/db/GeoLite2-City.mmdb';
  46.         $reader = new Reader($GeoLiteDatabasePath);
  47.         try {
  48.             $record $reader->city($this->ip);
  49.         } catch (AddressNotFoundException $ex) {
  50.             $record $reader->city('154.126.115.219');
  51.         }
  52.         $builder
  53.             ->add('designation'TextType::class, [
  54.                 'label' => false,
  55.                 'required' => true,
  56.                 'attr'  => [
  57.                     'class' =>'form-control form-sm'
  58.                 ],
  59.                   'constraints' => array(
  60.                     new NotBlank(array(
  61.                         'message' => $this->tanslator->trans('Formulaire_obligtoire'),
  62.                     )),
  63.                 )
  64.             ])
  65.             ->add('address'TextType::class, [
  66.                 'label' => false,
  67.                 'required' => true,
  68.                 'attr'  => [
  69.                     'class' =>'form-control form-sm'
  70.                 ],
  71.                   'constraints' => array(
  72.                     new NotBlank(array(
  73.                         'message' => $this->tanslator->trans('Formulaire_obligtoire'),
  74.                     )),
  75.                 )
  76.             ])
  77.             /*->add('pays', CountryType::class, [
  78.                 'label' => false,
  79.                 //'disabled' => true,
  80.                 'preferred_choices' => [$record->country->isoCode],//[$this->countryCode->getCode()],//['MG'],
  81.                 'required' => true,
  82.                 'attr'  => [
  83.                     'class' =>'form-control form-sm'
  84.                 ],
  85.                   'constraints' => array(
  86.                     new NotBlank(array(
  87.                         'message' => 'Formulaire obligtoire',
  88.                     )),
  89.                 )
  90.             ])->add('ville', EntityType::class, [
  91.                 'label' => false,
  92.                 'class' => Ville::class,
  93.                 'choice_label'  => 'name',
  94.                 'multiple'      => 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.                 'query_builder' => function(VilleRepository $ville_repo) {
  105.                         return $ville_repo->getListVille();
  106.                 }
  107.             ])
  108.             ->add('quartier')*/
  109.             ->add('payslocalisation'EntityType::class, [
  110.                 'label' => false,
  111.                 'class' => Pays::class,
  112.                 'preferred_choices' => [$record->country->isoCode],//['MG'],//[$this->countryCode->getCode()],
  113.                 'choice_label'  => 'name',
  114.                 //'data' => $pay,
  115.                 'required' => true,
  116.                 'attr'  => [
  117.                     'class' =>'form-control form-sm'
  118.                 ],
  119.                   'constraints' => array(
  120.                     new NotBlank(array(
  121.                         'message' => $this->tanslator->trans('Formulaire_obligtoire'),
  122.                     )),
  123.                 ),
  124.                  /*'query_builder' => function (PaysRepository $prepo) {
  125.                                 return $prepo->createQueryBuilder('p')->orderBy('p.name', 'ASC');
  126.                 }*/
  127.             ])
  128.             ->add('email'EmailType::class, [
  129.                 'label' => false,
  130.                 'required' => true,
  131.                 'attr'  => [
  132.                     'class' =>'form-control form-sm'
  133.                 ],
  134.                   'constraints' => array(
  135.                     new NotBlank(array(
  136.                         'message' => $this->tanslator->trans('Formulaire_obligtoire'),
  137.                     )),
  138.                 )
  139.             ])
  140.             ->add('password'PasswordType::class, [
  141.                 'label' => false,
  142.                 'required' => true,
  143.                 'attr'  => [
  144.                     'class' =>'form-control form-sm'
  145.                 ],
  146.                   'constraints' => array(
  147.                     new NotBlank(array(
  148.                         'message' => $this->tanslator->trans('Formulaire_obligtoire'),
  149.                     )),
  150.                 )
  151.             ])
  152.             ->add('confirm_password'PasswordType::class, [
  153.                 'label' => false,
  154.                 'required' => true,
  155.                 'attr'  => [
  156.                     'class' =>'form-control form-sm'
  157.                 ],
  158.                   'constraints' => array(
  159.                     new NotBlank(array(
  160.                         'message' => $this->tanslator->trans('Formulaire_obligtoire'),
  161.                     )),
  162.                 )
  163.             ])
  164.             ->add('phone'TextType::class, [
  165.                 'label' => false,
  166.                 'required' => true,
  167.                 'attr'  => [
  168.                     'class' =>'form-control form-sm telephone'
  169.                 ],
  170.                   'constraints' => array(
  171.                     new NotBlank(array(
  172.                         'message' => $this->tanslator->trans('Formulaire_obligtoire'),
  173.                     )),
  174.                 )
  175.             ])
  176.            /* ->add('phonetwo', TextType::class, [
  177.                 'label' => false,
  178.                 'required' => false,
  179.                 'attr'  => [
  180.                     'class' =>'form-control form-sm'
  181.                 ]
  182.             ])
  183.             ->add('phonethree', TextType::class, [
  184.                 'label' => false,
  185.                 'required' => false,
  186.                 'attr'  => [
  187.                     'class' =>'form-control form-sm'
  188.                 ]
  189.             ])*/
  190.             /*->add('is_acceptcgv', CheckboxType::class, [
  191.                 'label' => false,
  192.                 'required' => true,
  193.                 'attr' => [
  194.                   'class' => 'custom-control custom-checkbox',
  195.                 ]
  196.             ])*/;
  197.             $formModifierPays = function (FormInterface $formPays $pays null) {
  198.         $villes null === $pays ? array() : $pays->getVilles();
  199.              
  200.             $form->add('ville'EntityType::class, array(
  201.                 'class'       => Ville::class,
  202.                 'label'       => false,
  203.                 'placeholder' => $this->tanslator->trans('Selectionner_un_ville'),
  204.                 'choices'     => $villes,
  205.                 'choice_label'=> 'name',
  206.                 'multiple'    => false,
  207.             ));
  208.  
  209.         };
  210.         $formModifierVille = function (FormInterface $formVille $ville null) {
  211.         $quartiers null === $ville ? array() : $ville->getQuartiers();
  212.              
  213.             $form->add('quartier'EntityType::class, array(
  214.                 'class'       => Quartier::class,
  215.                 'label'       => false,
  216.                 'placeholder' => $this->tanslator->trans('Selectionner_un_quartier'),
  217.                 'choices'     => $quartiers,
  218.                 'choice_label'=> 'name',
  219.                 'multiple'    => false,
  220.             ));
  221.         };
  222.         $builder->addEventListener(
  223.             FormEvents::PRE_SET_DATA,
  224.             function (FormEvent $event) use ($formModifierPays$formModifierVille) {
  225.  
  226.                 $data $event->getData();
  227.                 $pays null;
  228.                 $ville null;
  229.  
  230.                 if ($data instanceof Result){
  231.                     if (null !== $data->getPays() && null !== $data->getVille()->getId()) {
  232.                         $pays $data->getPays();
  233.                     }
  234.                     if (null !== $data->getVille() && null !== $data->getVille()->getId()) {
  235.                         $ville $data->getVille();
  236.                         $form;
  237.                     }
  238.                 }
  239.  
  240.                 $formModifierPays($event->getForm(), $pays);
  241.                 $formModifierVille($event->getForm(), $ville);
  242.             }
  243.         );
  244.         $builder->addEventListener(
  245.             FormEvents::PRE_SUBMIT,
  246.             function (FormEvent $event) use ($formModifierPays$formModifierVille) {
  247.  
  248.                 $form $event->getForm();
  249.                 $data $event->getData();
  250.  
  251.                 if (isset($data['payslocalisation']) && !empty($data['payslocalisation'])){
  252.                     $repository =  $this->em->getRepository(Pays::class);
  253.                     $pays $repository->find($data['payslocalisation']);
  254.                     $formModifierPays($form$pays);
  255.                 }
  256.                 if (isset($data['ville']) && !empty($data['ville'])){
  257.                     $repository =  $this->em->getRepository(Ville::class);
  258.                     $ville $repository->find($data['ville']);
  259.                     $formModifierVille($form$ville);
  260.                 }
  261.  
  262.             }
  263.         );
  264.     }
  265.     public function configureOptions(OptionsResolver $resolver): void
  266.     {
  267.         $resolver->setDefaults([
  268.             'data_class' => User::class,
  269.         ]);
  270.     }
  271. }