<?php
namespace Acme\SudcmsBundle\Form\Ecommerce;
use Acme\SudcmsBundle\Entity\EcoCustomers;
use Acme\SudcmsBundle\Service\ApiService;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Throwable;
class CustomerFrontType extends AbstractType
{
private ApiService $api;
public function __construct(ApiService $api)
{
$this->api = $api;
}
/**
* @throws Throwable
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$defaultEmail = '';
$defaultCivilite = '';
$defaultNom = '';
$defaultPrenom = '';
$defaultDateDeNaissance = new \DateTime();
$defaultSexe = '';
$defaultTelFixe = '';
$defaultTelMobile = '';
$defaultAdresse1 = '';
$defaultAdresse2 = '';
$defaultAdresse3 = '';
$defaultCodePostale = '';
$defaultVille = '';
$defaultPays = 'FR';
$defaultRefusCom = 0;
if (empty($options['odysseeClient'])) {
$defaultEmail = '';
$defaultCivilite = 'MME';
$defaultNom = '';
$defaultPrenom = '';
$defaultSexe = 'F';
$defaultTelFixe = '';
$defaultTelMobile = '';
$defaultAdresse1 = '';
$defaultAdresse2 = '';
$defaultAdresse3 = '';
$defaultCodePostale = '';
$defaultVille = '';
$defaultPays = '';
$defaultRefusCom = 0;
} elseif (!empty($options['odysseeClient'])) {
$defaultEmail = $options['odysseeClient']->{'AdrCourriel'};
$defaultCivilite = $options['odysseeClient']->{'Civilite'};
$defaultNom = $options['odysseeClient']->{'Nom'};
$defaultPrenom = $options['odysseeClient']->{'Prenom'};
$defaultDateDeNaissance = new \DateTime($options['odysseeClient']->{'DateNaissance'});
$defaultSexe = $options['odysseeClient']->{'Sexe'};
$defaultTelFixe = $options['odysseeClient']->{'TelFixe'};
$defaultTelMobile = $options['odysseeClient']->{'TelMobile'};
$defaultAdresse1 = $options['odysseeClient']->{'Adresse1'};
$defaultAdresse2 = $options['odysseeClient']->{'Adresse2'};
$defaultAdresse3 = $options['odysseeClient']->{'Adresse3'};
$defaultCodePostale = $options['odysseeClient']->{'CodePostal'};
$defaultVille = $options['odysseeClient']->{'Ville'};
$defaultPays = $options['odysseeClient']->{'Pays'};
$defaultRefusCom = $options['odysseeClient']->{'RefusComCom'};
}
// Récupération des sexes
$choicesSexe = [];
foreach ($this->api->listerSexe() as $sexe) {
$choicesSexe[$sexe->{'Libelle_Sexe'}] = $sexe->{'Code_Sexe'};
}
// Récupération des pays
$choicesPays = [];
foreach ($this->api->listerPays() as $country) {
$choicesPays[$country->{'Libelle_Pays'}] = $country->{'Code_Pays'};
}
if ($options['type'] != 'refOdyssee') {
$builder
->add('AdrCourriel', EmailType::class, [
'mapped' => false,
'required' => true,
'label' => false,
'data' => $defaultEmail
]);
}
if ($options['type'] != 'account') {
try {
$civiliteList = $this->api->listerCivilite();
} catch (\Throwable $e) {
$civiliteList = [];
}
$civiliteListChoices = [];
foreach ($civiliteList as $civiliteItem) {
if (in_array($civiliteItem->{'Code_Civilite'}, ['MME', 'MLE', 'MR'])) {
$civiliteListChoices[$civiliteItem->{'Libelle_Civilite'}] = $civiliteItem->{'Code_Civilite'};
}
}
$builder
->add('Civilite', ChoiceType::class, [
'mapped' => false,
'required' => true,
'choices' => $civiliteListChoices,
'data' => $defaultCivilite
])
->add('Nom', TextType::class, [
'mapped' => false,
'required' => true,
'attr' => [
'maxlength' => 25
],
'data' => $defaultNom
])
->add('Prenom', TextType::class, [
'mapped' => false,
'required' => true,
'attr' => [
'maxlength' => 25
],
'data' => $defaultPrenom
])
->add('DateNaissance', DateType::class, [
'mapped' => false,
'required' => true,
'widget' => 'single_text',
// 'html5' => false,
// 'attr' => ['class' => 'js-datepicker'],
'data' => $defaultDateDeNaissance
])
->add('Sexe', ChoiceType::class, [
'mapped' => false,
'required' => true,
'choices' => $choicesSexe,
'data' => $defaultSexe
])
->add('Adresse1', TextType::class, [
'mapped' => false,
'required' => true,
'attr' => [
'maxlength' => 32
],
'data' => $defaultAdresse1
])
->add('Adresse2', TextType::class, [
'mapped' => false,
'required' => false,
'attr' => [
'maxlength' => 32
],
'data' => $defaultAdresse2
])
->add('Adresse3', TextType::class, [
'mapped' => false,
'required' => false,
'attr' => [
'maxlength' => 32
],
'data' => $defaultAdresse3
])
->add('CodePostal', TextType::class, [
'mapped' => false,
'required' => true,
'attr' => [
'maxlength' => 5
],
'data' => $defaultCodePostale
])
->add('Ville', TextType::class, [
'mapped' => false,
'required' => true,
'attr' => [
'maxlength' => 32
],
'data' => $defaultVille
])
->add('Pays', ChoiceType::class, [
'mapped' => false,
'required' => true,
'choices' => $choicesPays,
'data' => $choicesPays['France']
])
->add('TelFixe', TextType::class, [
'mapped' => false,
'required' => false,
'attr' => [
'maxlength' => 20
],
'data' => $defaultTelFixe
])
->add('TelMobile', TextType::class, [
'mapped' => false,
'required' => false,
'attr' => [
'maxlength' => 20
],
'data' => $defaultTelMobile
])
->add('RefusComCom', CheckboxType::class, [
'mapped' => false,
'required' => false,
'data' => !($defaultRefusCom == 0)
]);
}
if ($options['odysseeClient'] == null) {
$builder
->add('password', RepeatedType::class, [
'type' => PasswordType::class,
'mapped' => false,
'invalid_message' => 'Les mots de passes ne sont pas les mêmes',
'required' => true
])
->add('rgpd', CheckboxType::class, [
'mapped' => false,
'required' => true,
]);
} elseif ($options['type'] == 'account') {
$builder
->add('password', RepeatedType::class, [
'type' => PasswordType::class,
'mapped' => false,
'invalid_message' => 'Les mots de passes ne sont pas les mêmes',
'required' => false
]);
}
// submit
$builder->add('save', SubmitType::class, [
'attr' => ['class' => 'loginBtn']
]
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => EcoCustomers::class,
'odysseeClient' => null,
'type' => 'new'
]);
}
}