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