308 lines
12 KiB
PHP
308 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Etat;
|
|
use App\Entity\Sortie;
|
|
use App\Form\SortieType;
|
|
use App\Repository\EtatRepository;
|
|
use App\Repository\LieuRepository;
|
|
use App\Repository\ParticipantRepository;
|
|
use App\Repository\SortieRepository;
|
|
use App\Service\FileUploader;
|
|
use DateTime;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use OpenAI\Factory;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
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;
|
|
use OpenAI\Client;
|
|
|
|
#[Route('/sortie', name: 'sortie_')]
|
|
class SortieController extends AbstractController
|
|
{
|
|
#[Route('/liste', name: 'list', methods: ['GET'])]
|
|
public function index(
|
|
TokenStorageInterface $tokenStorage,
|
|
SortieRepository $sortieRepository,
|
|
Request $request
|
|
): Response {
|
|
$token = $tokenStorage->getToken();
|
|
$userConnect = $token?->getUser();
|
|
|
|
$search = $request->query->get('search', '');
|
|
$siteId = $request->query->get('site', '');
|
|
$startDate = $request->query->get('start_date', '');
|
|
$endDate = $request->query->get('end_date', '');
|
|
$organisateur = $request->query->get('organisateur', false);
|
|
$inscrit = $request->query->get('inscrit', false);
|
|
$nonInscrit = $request->query->get('non_inscrit', false);
|
|
$passees = $request->query->get('passees', false);
|
|
|
|
$sorties = $sortieRepository->findWithFilters($search, $siteId, $startDate, $endDate, $organisateur, $inscrit, $nonInscrit, $passees, $userConnect);
|
|
return $this->render('sortie/list.html.twig', [
|
|
'profile' => $userConnect,
|
|
'sorties' => $sorties,
|
|
'sites' => $sortieRepository->findAllSites(),
|
|
]);
|
|
}
|
|
|
|
|
|
#[Route('/creates', name: 'create', methods: ['GET', 'POST'])]
|
|
public function create(
|
|
Request $request,
|
|
EntityManagerInterface $entityManager,
|
|
TokenStorageInterface $tokenStorage,
|
|
LieuRepository $lieuRepository,
|
|
ParticipantRepository $participantRepository,
|
|
EtatRepository $etatRepository
|
|
): Response {
|
|
$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');
|
|
}
|
|
|
|
$participant = $participantRepository->find($userConnect->getIdParticipant());
|
|
if (!$participant) {
|
|
$this->addFlash('error', 'Impossible de trouver votre profil.');
|
|
return $this->redirectToRoute('sortie_create');
|
|
}
|
|
|
|
$imageFile = $form->get('imageFile')->getData();
|
|
if ($imageFile) {
|
|
$sortie->setImageFile($imageFile);
|
|
}
|
|
|
|
$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('sortie_view', ['id'=> $sortie->getIdSortie()]);
|
|
}
|
|
|
|
return $this->render('sortie/create.html.twig', [
|
|
'form' => $form->createView(),
|
|
'sortie' => $sortie,
|
|
]);
|
|
}
|
|
|
|
#[Route('/view/{id}', name: 'view', methods: ['GET'])]
|
|
public function view(
|
|
string $id,
|
|
EntityManagerInterface $entityManager,
|
|
): Response {
|
|
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
|
if (!$sortie) {
|
|
$this->addFlash('error', 'Cette sortie n\'existe pas.');
|
|
return $this->redirectToRoute('home');
|
|
}
|
|
if ($sortie->getDateHeureDebut() < (new DateTime())->modify('-1 month')) {
|
|
$this->addFlash('error', "Cette sortie à plus de 30 jours.");
|
|
return $this->redirectToRoute('home');
|
|
}
|
|
|
|
// Changer l'état si date de début commencé.
|
|
if($sortie->getDateLimiteInscription() < new DateTime()){
|
|
$sortie->setEtat($entityManager->getRepository(Etat::class)->findOneBy(['libelle' => 'Clôturée']));
|
|
}
|
|
if ($sortie->getDateHeureDebut() <= (new DateTime())) {
|
|
$sortie->setEtat($entityManager->getRepository(Etat::class)->findOneBy(['libelle' => 'En cours']));
|
|
}
|
|
|
|
return $this->render('sortie/view.html.twig', [
|
|
'sortie' => $sortie,
|
|
]);
|
|
}
|
|
|
|
#[Route('/inscription/{id}', name: 'inscription', methods: ['POST'])]
|
|
public function inscription(
|
|
string $id,
|
|
EntityManagerInterface $entityManager,
|
|
TokenStorageInterface $tokenStorage,
|
|
ParticipantRepository $participantRepository,
|
|
): Response {
|
|
$userConnect = $tokenStorage->getToken()?->getUser();
|
|
$participant = $participantRepository->find($userConnect->getIdParticipant());
|
|
if (!$participant) {
|
|
$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($participant)) {
|
|
$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($participant);
|
|
$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()) {
|
|
// Vérification et mise à jour du lieu
|
|
$lieu = $form->get('lieu')->getData();
|
|
if ($lieu) {
|
|
$sortie->setLieu($lieu);
|
|
}
|
|
|
|
$imageFile = $form->get('imageFile')->getData();
|
|
if ($imageFile) {
|
|
$sortie->setImageFile($imageFile);
|
|
}
|
|
|
|
$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,
|
|
]);
|
|
}
|
|
|
|
#[Route('/cancel/{id}', name: 'cancel', methods: ['POST'])]
|
|
public function cancel(
|
|
string $id,
|
|
Request $request,
|
|
EntityManagerInterface $entityManager,
|
|
TokenStorageInterface $tokenStorage
|
|
): Response {
|
|
$user = $tokenStorage->getToken()?->getUser();
|
|
|
|
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
|
|
|
if (!$sortie || $sortie->getOrganisateur()->getIdParticipant() !== $user->getIdParticipant()) {
|
|
$this->addFlash('error', 'Vous n\'avez pas l\'autorisation d\'annuler cette sortie.');
|
|
return $this->redirectToRoute('home');
|
|
}
|
|
|
|
$motif = $request->request->get('motif');
|
|
if (!$motif) {
|
|
$this->addFlash('error', 'Le motif d\'annulation est obligatoire.');
|
|
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
|
}
|
|
|
|
$sortie->setMotifAnnul($motif);
|
|
$sortie->setEtat($entityManager->getRepository(Etat::class)->findOneBy(['libelle' => 'Annulée']));
|
|
|
|
$entityManager->flush();
|
|
|
|
$this->addFlash('success', 'La sortie a été annulée avec succès.');
|
|
return $this->redirectToRoute('home');
|
|
}
|
|
|
|
#[Route('/confirm/{id}', name: 'confirm', methods: ['POST'])]
|
|
public function confirm(string $id, EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage)
|
|
{
|
|
$user = $tokenStorage->getToken()?->getUser();
|
|
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
|
|
|
if (!$sortie || $sortie->getOrganisateur()->getIdParticipant() !== $user->getIdParticipant()) {
|
|
$this->addFlash('error', 'Vous n\'avez pas l\'autorisation de démarrer les inscriptions pour cette sortie.');
|
|
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
|
}
|
|
|
|
$sortie->setEtat($entityManager->getRepository(Etat::class)->findOneBy(['libelle' => 'Ouverte']));
|
|
$entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Les inscriptions sont ouvertes pour cette sortie');
|
|
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
|
}
|
|
|
|
}
|