204 lines
7.8 KiB
PHP
204 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Sortie;
|
|
use App\Form\SortieType;
|
|
use App\Repository\EtatRepository;
|
|
use App\Repository\LieuRepository;
|
|
use App\Repository\ParticipantRepository;
|
|
use DateTime;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
|
|
|
#[Route('/sortie', name: 'sortie_')]
|
|
class SortieController extends AbstractController
|
|
{
|
|
#[Route('/creates', name: 'create', methods: ['GET', 'POST'])]
|
|
public function create(
|
|
Request $request,
|
|
EntityManagerInterface $entityManager,
|
|
TokenStorageInterface $tokenStorage,
|
|
LieuRepository $lieuRepository,
|
|
ParticipantRepository $participantRepository,
|
|
EtatRepository $etatRepository
|
|
): Response {
|
|
// Vérifier si l'utilisateur est connecté
|
|
$userConnect = $tokenStorage->getToken()?->getUser();
|
|
if (!$userConnect) {
|
|
$this->addFlash('error', 'Vous devez être connecté pour créer une sortie.');
|
|
return $this->redirectToRoute('app_login');
|
|
}
|
|
|
|
$sortie = new Sortie();
|
|
$form = $this->createForm(SortieType::class, $sortie);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
// Vérifier le lieu
|
|
$lieu = $form->get('lieu')->getData();
|
|
if (!$lieu) {
|
|
$this->addFlash('error', 'Veuillez sélectionner un lieu valide.');
|
|
return $this->redirectToRoute('sortie_create');
|
|
}
|
|
|
|
// Vérifier le participant (organisateur)
|
|
$participant = $participantRepository->find($userConnect->getIdParticipant());
|
|
if (!$participant) {
|
|
$this->addFlash('error', 'Impossible de trouver votre profil.');
|
|
return $this->redirectToRoute('sortie_create');
|
|
}
|
|
|
|
// Définir les relations et l'état initial
|
|
$etat = $etatRepository->findOneBy(['libelle' => 'Créée']);
|
|
if (!$etat) {
|
|
$this->addFlash('error', 'Erreur interne : état introuvable.');
|
|
return $this->redirectToRoute('sortie_create');
|
|
}
|
|
|
|
$sortie->setLieu($lieu)
|
|
->setSite($participant->getSite())
|
|
->setEtat($etat)
|
|
->setOrganisateur($participant);
|
|
|
|
$entityManager->persist($sortie);
|
|
$entityManager->flush();
|
|
|
|
$this->addFlash('success', 'La sortie a été créée avec succès.');
|
|
return $this->redirectToRoute('home');
|
|
}
|
|
|
|
return $this->render('sortie/create.html.twig', [
|
|
'profile' => $userConnect,
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
|
|
#[Route('/view/{id}', name: 'view', methods: ['GET'])]
|
|
public function view(
|
|
string $id,
|
|
EntityManagerInterface $entityManager,
|
|
TokenStorageInterface $tokenStorage
|
|
): Response {
|
|
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
|
if (!$sortie) {
|
|
$this->addFlash('error', 'Cette sortie n\'existe pas.');
|
|
return $this->redirectToRoute('home');
|
|
}
|
|
|
|
return $this->render('sortie/view.html.twig', [
|
|
'sortie' => $sortie,
|
|
'profile' => $tokenStorage->getToken()?->getUser(),
|
|
]);
|
|
}
|
|
|
|
#[Route('/inscription/{id}', name: 'inscription', methods: ['POST'])]
|
|
public function inscription(
|
|
string $id,
|
|
EntityManagerInterface $entityManager,
|
|
TokenStorageInterface $tokenStorage
|
|
): Response {
|
|
$userConnect = $tokenStorage->getToken()?->getUser();
|
|
if (!$userConnect) {
|
|
$this->addFlash('error', 'Vous devez être connecté pour vous inscrire.');
|
|
return $this->redirectToRoute('app_login');
|
|
}
|
|
|
|
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
|
if (!$sortie) {
|
|
$this->addFlash('error', 'Cette sortie n\'existe pas.');
|
|
return $this->redirectToRoute('home');
|
|
}
|
|
|
|
// Conditions supplémentaires pour l'inscription
|
|
if ($sortie->getEtat()->getLibelle() !== 'Ouverte') {
|
|
$this->addFlash('error', 'Cette sortie n\'est pas ouverte aux inscriptions.');
|
|
} elseif ($sortie->getParticipants()->contains($userConnect)) {
|
|
$this->addFlash('error', 'Vous êtes déjà inscrit à cette sortie.');
|
|
} elseif ($sortie->getParticipants()->count() >= $sortie->getNbInscriptionsMax()) {
|
|
$this->addFlash('error', 'Cette sortie est complète.');
|
|
} elseif ($sortie->getDateLimiteInscription() < new DateTime()) {
|
|
$this->addFlash('error', "La date limite d'inscription est dépassée");
|
|
} else {
|
|
$sortie->addParticipant($userConnect);
|
|
$entityManager->persist($sortie);
|
|
$entityManager->flush();
|
|
$this->addFlash('success', 'Votre inscription a été validée.');
|
|
}
|
|
|
|
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
|
}
|
|
|
|
#[Route('/unsubscribe/{id}', name: 'unsubscribe', methods: ['POST'])]
|
|
public function unsubscribe(string $id, EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage)
|
|
{
|
|
$userConnect = $tokenStorage->getToken()?->getUser();
|
|
if (!$userConnect) {
|
|
$this->addFlash('error', 'Vous devez être connecté pour vous inscrire.');
|
|
return $this->redirectToRoute('app_login');
|
|
}
|
|
|
|
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
|
if (!$sortie) {
|
|
$this->addFlash('error', 'Cette sortie n\'existe pas.');
|
|
return $this->redirectToRoute('home');
|
|
}
|
|
|
|
if (!$sortie->getParticipants()->contains($userConnect)) {
|
|
$this->addFlash('error', 'Vous devez être inscrit pour vous désinscrire d\'une sortie.');
|
|
} else {
|
|
$sortie->removeParticipant($userConnect);
|
|
$entityManager->persist($sortie);
|
|
$entityManager->flush();
|
|
$this->addFlash('success', 'Vous ne participez pu à cette sortie.');
|
|
}
|
|
|
|
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
|
}
|
|
|
|
#[Route('/edit/{id}', name: 'edit', methods: ['GET', 'POST'])]
|
|
public function edit(
|
|
string $id,
|
|
Request $request,
|
|
EntityManagerInterface $entityManager,
|
|
TokenStorageInterface $tokenStorage,
|
|
LieuRepository $lieuRepository
|
|
): Response {
|
|
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
|
if (!$sortie) {
|
|
$this->addFlash('error', 'Cette sortie n\'existe pas.');
|
|
return $this->redirectToRoute('home');
|
|
}
|
|
|
|
$userConnect = $tokenStorage->getToken()?->getUser();
|
|
if ($userConnect->getIdParticipant() !== $sortie->getOrganisateur()->getIdParticipant()) {
|
|
$this->addFlash('error', 'Vous ne pouvez pas modifier cette sortie.');
|
|
return $this->redirectToRoute('home');
|
|
}
|
|
|
|
$form = $this->createForm(SortieType::class, $sortie);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$lieu = $form->get('lieu')->getData();
|
|
if ($lieu) {
|
|
$sortie->setLieu($lieu);
|
|
}
|
|
$entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Les modifications ont été enregistrées.');
|
|
return $this->redirectToRoute('sortie_view', ['id' => $sortie->getIdSortie()]);
|
|
}
|
|
|
|
return $this->render('sortie/edit.html.twig', [
|
|
'form' => $form->createView(),
|
|
'sortie' => $sortie,
|
|
'profile' => $userConnect,
|
|
]);
|
|
}
|
|
}
|