src/Form/Ecommerce/CustomerFrontType.php line 21

Open in your IDE?
  1. <?php
  2. namespace Acme\SudcmsBundle\Form\Ecommerce;
  3. use Acme\SudcmsBundle\Entity\EcoCustomers;
  4. use Acme\SudcmsBundle\Service\ApiService;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  7. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  8. use Symfony\Component\Form\Extension\Core\Type\DateType;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  11. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  12. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  13. use Symfony\Component\Form\Extension\Core\Type\TextType;
  14. use Symfony\Component\Form\FormBuilderInterface;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. use Throwable;
  17. class CustomerFrontType extends AbstractType
  18. {
  19.     private ApiService $api;
  20.     public function __construct(ApiService $api)
  21.     {
  22.         $this->api $api;
  23.     }
  24.     /**
  25.      * @throws Throwable
  26.      */
  27.     public function buildForm(FormBuilderInterface $builder, array $options)
  28.     {
  29.         $defaultEmail '';
  30.         $defaultCivilite '';
  31.         $defaultNom '';
  32.         $defaultPrenom '';
  33.         $defaultDateDeNaissance = new \DateTime();
  34.         $defaultSexe '';
  35.         $defaultTelFixe '';
  36.         $defaultTelMobile '';
  37.         $defaultAdresse1 '';
  38.         $defaultAdresse2 '';
  39.         $defaultAdresse3 '';
  40.         $defaultCodePostale '';
  41.         $defaultVille '';
  42.         $defaultPays 'FR';
  43.         $defaultRefusCom 0;
  44.         if (empty($options['odysseeClient'])) {
  45.             $defaultEmail '';
  46.             $defaultCivilite 'MME';
  47.             $defaultNom '';
  48.             $defaultPrenom '';
  49.             $defaultSexe 'F';
  50.             $defaultTelFixe '';
  51.             $defaultTelMobile '';
  52.             $defaultAdresse1 '';
  53.             $defaultAdresse2 '';
  54.             $defaultAdresse3 '';
  55.             $defaultCodePostale '';
  56.             $defaultVille '';
  57.             $defaultPays '';
  58.             $defaultRefusCom 0;
  59.         } elseif (!empty($options['odysseeClient'])) {
  60.             $defaultEmail $options['odysseeClient']->{'AdrCourriel'};
  61.             $defaultCivilite $options['odysseeClient']->{'Civilite'};
  62.             $defaultNom $options['odysseeClient']->{'Nom'};
  63.             $defaultPrenom $options['odysseeClient']->{'Prenom'};
  64.             $defaultDateDeNaissance = new \DateTime($options['odysseeClient']->{'DateNaissance'});
  65.             $defaultSexe $options['odysseeClient']->{'Sexe'};
  66.             $defaultTelFixe $options['odysseeClient']->{'TelFixe'};
  67.             $defaultTelMobile $options['odysseeClient']->{'TelMobile'};
  68.             $defaultAdresse1 $options['odysseeClient']->{'Adresse1'};
  69.             $defaultAdresse2 $options['odysseeClient']->{'Adresse2'};
  70.             $defaultAdresse3 $options['odysseeClient']->{'Adresse3'};
  71.             $defaultCodePostale $options['odysseeClient']->{'CodePostal'};
  72.             $defaultVille $options['odysseeClient']->{'Ville'};
  73.             $defaultPays $options['odysseeClient']->{'Pays'};
  74.             $defaultRefusCom $options['odysseeClient']->{'RefusComCom'};
  75.         }
  76.         // Récupération des sexes
  77.         $choicesSexe = [];
  78.         foreach ($this->api->listerSexe() as $sexe) {
  79.             $choicesSexe[$sexe->{'Libelle_Sexe'}] = $sexe->{'Code_Sexe'};
  80.         }
  81.         // Récupération des pays
  82.         $choicesPays = [];
  83.         foreach ($this->api->listerPays() as $country) {
  84.             $choicesPays[$country->{'Libelle_Pays'}] = $country->{'Code_Pays'};
  85.         }
  86.         if ($options['type'] != 'refOdyssee') {
  87.             $builder
  88.                 ->add('AdrCourriel'EmailType::class, [
  89.                     'mapped' => false,
  90.                     'required' => true,
  91.                     'label' => false,
  92.                     'data' => $defaultEmail
  93.                 ]);
  94.         }
  95.         if ($options['type'] != 'account') {
  96.             try {
  97.                 $civiliteList $this->api->listerCivilite();
  98.             } catch (\Throwable $e) {
  99.                 $civiliteList = [];
  100.             }
  101.             $civiliteListChoices = [];
  102.             foreach ($civiliteList as $civiliteItem) {
  103.                 if (in_array($civiliteItem->{'Code_Civilite'}, ['MME''MLE''MR'])) {
  104.                     $civiliteListChoices[$civiliteItem->{'Libelle_Civilite'}] = $civiliteItem->{'Code_Civilite'};
  105.                 }
  106.             }
  107.             $builder
  108.                 ->add('Civilite'ChoiceType::class, [
  109.                     'mapped' => false,
  110.                     'required' => true,
  111.                     'choices' => $civiliteListChoices,
  112.                     'data' => $defaultCivilite
  113.                 ])
  114.                 ->add('Nom'TextType::class, [
  115.                     'mapped' => false,
  116.                     'required' => true,
  117.                     'attr' => [
  118.                         'maxlength' => 25
  119.                     ],
  120.                     'data' => $defaultNom
  121.                 ])
  122.                 ->add('Prenom'TextType::class, [
  123.                     'mapped' => false,
  124.                     'required' => true,
  125.                     'attr' => [
  126.                         'maxlength' => 25
  127.                     ],
  128.                     'data' => $defaultPrenom
  129.                 ])
  130.                 ->add('DateNaissance'DateType::class, [
  131.                     'mapped' => false,
  132.                     'required' => true,
  133.                     'widget' => 'single_text',
  134.                     //                'html5'    => false,
  135.                     //                'attr'     => ['class' => 'js-datepicker'],
  136.                     'data' => $defaultDateDeNaissance
  137.                 ])
  138.                 ->add('Sexe'ChoiceType::class, [
  139.                     'mapped' => false,
  140.                     'required' => true,
  141.                     'choices' => $choicesSexe,
  142.                     'data' => $defaultSexe
  143.                 ])
  144.                 ->add('Adresse1'TextType::class, [
  145.                     'mapped' => false,
  146.                     'required' => true,
  147.                     'attr' => [
  148.                         'maxlength' => 32
  149.                     ],
  150.                     'data' => $defaultAdresse1
  151.                 ])
  152.                 ->add('Adresse2'TextType::class, [
  153.                     'mapped' => false,
  154.                     'required' => false,
  155.                     'attr' => [
  156.                         'maxlength' => 32
  157.                     ],
  158.                     'data' => $defaultAdresse2
  159.                 ])
  160.                 ->add('Adresse3'TextType::class, [
  161.                     'mapped' => false,
  162.                     'required' => false,
  163.                     'attr' => [
  164.                         'maxlength' => 32
  165.                     ],
  166.                     'data' => $defaultAdresse3
  167.                 ])
  168.                 ->add('CodePostal'TextType::class, [
  169.                     'mapped' => false,
  170.                     'required' => true,
  171.                     'attr' => [
  172.                         'maxlength' => 5
  173.                     ],
  174.                     'data' => $defaultCodePostale
  175.                 ])
  176.                 ->add('Ville'TextType::class, [
  177.                     'mapped' => false,
  178.                     'required' => true,
  179.                     'attr' => [
  180.                         'maxlength' => 32
  181.                     ],
  182.                     'data' => $defaultVille
  183.                 ])
  184.                 ->add('Pays'ChoiceType::class, [
  185.                     'mapped' => false,
  186.                     'required' => true,
  187.                     'choices' => $choicesPays,
  188.                     'data' => $choicesPays['France']
  189.                 ])
  190.                 ->add('TelFixe'TextType::class, [
  191.                     'mapped' => false,
  192.                     'required' => false,
  193.                     'attr' => [
  194.                         'maxlength' => 20
  195.                     ],
  196.                     'data' => $defaultTelFixe
  197.                 ])
  198.                 ->add('TelMobile'TextType::class, [
  199.                     'mapped' => false,
  200.                     'required' => false,
  201.                     'attr' => [
  202.                         'maxlength' => 20
  203.                     ],
  204.                     'data' => $defaultTelMobile
  205.                 ])
  206.                 ->add('RefusComCom'CheckboxType::class, [
  207.                     'mapped' => false,
  208.                     'required' => false,
  209.                     'data' => !($defaultRefusCom == 0)
  210.                 ]);
  211.         }
  212.         if ($options['odysseeClient'] == null) {
  213.             $builder
  214.                 ->add('password'RepeatedType::class, [
  215.                     'type' => PasswordType::class,
  216.                     'mapped' => false,
  217.                     'invalid_message' => 'Les mots de passes ne sont pas les mêmes',
  218.                     'required' => true
  219.                 ])
  220.                 ->add('rgpd'CheckboxType::class, [
  221.                     'mapped' => false,
  222.                     'required' => true,
  223.                 ]);
  224.         } elseif ($options['type'] == 'account') {
  225.             $builder
  226.                 ->add('password'RepeatedType::class, [
  227.                     'type' => PasswordType::class,
  228.                     'mapped' => false,
  229.                     'invalid_message' => 'Les mots de passes ne sont pas les mêmes',
  230.                     'required' => false
  231.                 ]);
  232.         }
  233.         // submit
  234.         $builder->add('save'SubmitType::class, [
  235.                 'attr' => ['class' => 'loginBtn']
  236.             ]
  237.         );
  238.     }
  239.     public function configureOptions(OptionsResolver $resolver)
  240.     {
  241.         $resolver->setDefaults([
  242.             'data_class' => EcoCustomers::class,
  243.             'odysseeClient' => null,
  244.             'type' => 'new'
  245.         ]);
  246.     }
  247. }