<?php
namespace App\Controller;
use App\Entity\Activite;
use App\Entity\Membre;
use App\Form\ParentInscriActiviteType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Doctrine\Persistence\ManagerRegistry;
class InscrireParentActiviteEspaceMembreController extends AbstractController
{
#[Route('/inscrire-parent-activite-espace-membre', name: 'app_inscrire_parent_activite_espace_membre')]
public function index(ManagerRegistry $doctrine, Request $request): Response
{
$entityManager = $doctrine->getManager();
$user = $this->getUser();
if($user){
$userid = $user->getUserIdentifier();
$membre = $doctrine->getRepository(Membre::class)->findOneBy(['courriel' => $userid]);
$form = $this->createForm(ParentInscriActiviteType::class, $membre, [
'method' => 'POST',
]);
$form->handleRequest($request);
if($form->isSubmitted()){
$activiteEntities = $form->get('activite')->getData();
$activiteIds = [];
foreach ($activiteEntities as $activite) {
$activiteIds[] = $activite->getId();
}
$activites = $doctrine->getRepository(Activite::class)->findBy(['id' => $activiteIds]);
foreach ($activites as $activite) {
//dump($membre).die();
//var_dump($activite).die();
// dump($activite);
if ($membre->getActivite() === $activite) {
$this->addFlash('warning', "Vous êtes déjà inscrit à cette activité");
return $this->redirectToRoute('app_inscrire_parent_activite_espace_membre');
}
$membre->addActivite($activite);
//$activite->addMembre($membre);
}
$entityManager->persist($membre);
$entityManager->flush();
$this->addFlash('success', "Ajout avec succès!");
return $this->redirectToRoute('app_inscrire_parent_activite_espace_membre');
}
}
return $this->render('inscrire_parent_activite_espace_membre/index.html.twig', [
'form' => $form->createView(),
'membre' =>$membre,
]);
}
}