<?php declare(strict_types=1);
/**
* @author Mehrez Labidi
*/
namespace App\Form\Type;
use App\Form\Models\NewslettersInscription;
use App\Helper\TransProvider;
use App\Services\ManagerEntity\LocalisationsManagers;
use App\Services\ManagerEntity\PaysManagers;
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
use stdClass;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\{ChoiceType,RepeatedType,TextType};
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
class NewslettersInscriptionType extends AbstractType
{
/**
* @var TransProvider
*/
private TransProvider $transProvider;
/**
* @var ContainerBagInterface
*/
private ContainerBagInterface $params;
/**
* @var PaysManagers
*/
private PaysManagers $paysManagers;
/**
* @var LocalisationsManagers
*/
private LocalisationsManagers $localisationsManagers;
/**
* @param TransProvider $transProvider
* @param ContainerBagInterface $params
* @param PaysManagers $paysManagers
* @param LocalisationsManagers $localisationsManagers
*/
public function __construct(TransProvider $transProvider, ContainerBagInterface $params, PaysManagers $paysManagers, LocalisationsManagers $localisationsManagers)
{
$this->transProvider = $transProvider;
$this->params = $params;
$this->paysManagers = $paysManagers;
$this->localisationsManagers = $localisationsManagers;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
* @return void
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('mail', TextType::class, [
'required' => false
])
->add('mail', RepeatedType::class, [
'required' => false
])
->add('pays', ChoiceType::class, [
'required' => false,
'attr' => ['v-model' => 'pays' , 'class' => 'form-control'], // event Vuejs
'choices' => $this->transChoice($this->listChoixPays()->listChoix),
'choice_attr' => function ($key, $val, $index) {
return ($key == "") ? ['disabled' => 'disabled'] : [];
},
'data' => $this->listChoixPays()->defaultChoix
])
->add('region', ChoiceType::class,
[
"attr" => ["autocomplete" => "off", 'class' => 'form-control']
]
)
->add('metier', ChoiceType::class, [
'required' => false,
"attr" => ["autocomplete" => "off", 'class' => 'form-control'] ,
'choices' => $this->transChoice(FormChoices::listMetiersletters()),
])
->add('pays_newsletter', ChoiceType::class, [
'choices' => $this->transChoice($this->listChoixPaysNewsletter()->listChoix),
"attr" => ["autocomplete" => "off" ] ,
'data' => $this->listChoixPaysNewsletter()->defaultChoix,
'expanded' => true,
'multiple' => true, 'label_html' => true,
])
->add('recaptcha', EWZRecaptchaType::class,
['mapped' => false,'language' => $this->transProvider->getLocale()]
)
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
// get the form from the event
$form = $event->getForm();
// get the form element and its options
$config = $form->get('region')->getConfig();
$options = $config->getOptions();
// get the form data, that got submitted by the user with this request / event
$data = $event->getData();
if (array_key_exists("region", $data)) {
$pays = $data["pays"];
$region = $data["region"];
$form->add(
'region', ChoiceType::class,
[
'choices' => $this->listChoixRegion($pays),
'data' => $region
]
);
}
});
}
/**
* @param $var
* @return mixed|string
*/
private function transChoice($var)
{
if (is_string($var)) {
$var = $this->transProvider
->translate($var, "", "M");
}
if (is_array($var)) {
foreach ($var as $key => $item) {
unset($var[$key]);
$key = $this->transProvider
->translate($key, "", "M");
$var[$key] = $item;
}
}
return $var;
}
/**
* @return stdClass
*/
private function listChoixPays()
{
$list = $this->paysManagers->getListPaysNewsletters();
$result = new stdClass();
$result->defaultChoix = NULL;
$result->listChoix = FormChoices::selectionnerChoisir();
foreach ($list as $item) {
$result->listChoix[$item["nomPays"]] = $item["idLocalisation"];
if ($_SESSION['codePays'] && ($_SESSION['codePays'] == strtolower($item['codePays']))) {
$result->defaultChoix = strtolower($item['idLocalisation']);
}
}
return $result;
}
/**
* @return array
*/
private function listChoixPaysNewsletter()
{
$paramsNewslettersPays = $this->params->get('app.newsletters_pays');
$return = new stdClass();
$return->defaultChoix = NULL;
$result = [];
// on prend codePays de session pour afficher le pays du site en premier
foreach ($paramsNewslettersPays as $item) {
if ($_SESSION['codePays'] == strtolower($item[1])) {
$return->defaultChoix = [0 => strtolower($item[1])];
$tmpVar = html_entity_decode(" <img src='http://www.help-fusacq.com/fr/images/$item[3].png' alt='$item[0]'> ") . $item[0];
}
$result[html_entity_decode(" <img src='http://www.help-fusacq.com/fr/images/$item[3].png' alt='$item[0]'> ") . $item[0]] = strtolower($item[1]);
}
if (isset($tmpVar)) {
$result = [$tmpVar => $_SESSION['codePays']] + $result;
}
$return->listChoix = $result;
return $return;
}
/**
* @param $idlocalisation
* @return array
*/
private function listChoixRegion($idlocalisation)
{
$result = FormChoices::selectionnerChoisir();
foreach ($this->localisationsManagers->getListRegionsByCountry($idlocalisation) as $item) {
$result[$item["nom_localisation"]] = $item["id_localisation"];
}
return $result;
}
/**
* @param OptionsResolver $resolver
* @return void
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => NewslettersInscription::class,
'csrf_protection' => false,
'allow_extra_fields' => true,
]);
}
/**
* @return string
*/
public function getBlockPrefix()
{ // pour unifier avec les inputs definis dans html
return 'form';
}
}