<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use App\Entity\Membre;
use App\Entity\Activite;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class ParentInscriActiviteType extends AbstractType
{
private $requestStack;
private $doctrine;
private $security;
private $translator;
public function __construct(RequestStack $requestStack,ManagerRegistry $doctrine,Security $security,TranslatorInterface $translator)
{
$this->requestStack = $requestStack;
$this->doctrine = $doctrine;
$this->security = $security;
$this->translator = $translator;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$user = $this->security->getUser();
if ($user) {
$userId = $user->getUserIdentifier();
dump($userId);
$membre = $this->doctrine->getRepository(Membre::class)->findOneBy(['courriel' => $userId]);
}
$activite = new Activite();
$builder
->add('activite', EntityType::class, [
'label' => $this->translator->trans("Nom de l'activité"),
'label_attr' => ['class' => 'form-label'],
'class' => Activite::class,
'expanded' => true,
'multiple' => true,
'choice_label' => function (Activite $activite) {
return sprintf(' %s / %s / %s',
$this->translator->trans($activite->getThemeactivite() ,[], 'messages', $this->translator->getLocale()),
$this->translator->trans($activite->getNomactivite(),[], 'messages', $this->translator->getLocale()),
$this->translator->trans($activite->getTypeactivite() ,[], 'messages', $this->translator->getLocale()),
);},
'query_builder' => function (EntityRepository $er)use ($membre) {
if ($membre->getType() == 'parent') {
$trancheage = ['Adulte', '0-5 ans'];
return $er->createQueryBuilder('a')
->where('a.inscription = :inscription')
->andWhere('a.trancheage IN (:trancheage)')
->setParameter('inscription', 'Ouverte')
->setParameter('trancheage', $trancheage);
}
else{
$trancheage = ['0-5 ans','5-12 ans','12 ans et plus'];
return $er->createQueryBuilder('a')
->where('a.inscription = :inscription')
->andWhere('a.trancheage IN (:trancheage)')
->setParameter('inscription', 'Ouverte')
->setParameter('trancheage', $trancheage);
}
},
'attr' =>['class' => 'mb-2 form-control form-control-solid ' ],
'choice_attr' => function($choice, $key, $value) {
return [
'class' => 'form-check-input', // Common class for all choices
'style' => 'display: inline-block; margin-right: 10px;min-height: 0;', // Adjust the styling as needed
];
},
])
->add('Enregistrer', SubmitType::class,[
'attr'=> [
'class'=> 'btn btn-primary',
]
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Membre::class,
]);
}
}