plein de truc
This commit is contained in:
@@ -30,17 +30,22 @@ class LieuController extends AbstractController
|
||||
): JsonResponse {
|
||||
$data = json_decode($request->getContent(), true);
|
||||
|
||||
|
||||
if (!isset($data['nom'], $data['rue'], $data['latitude'], $data['longitude'], $data['villeId'])) {
|
||||
return new JsonResponse(['error' => 'Données manquantes'], Response::HTTP_BAD_REQUEST);
|
||||
if (!isset($data['nom'], $data['latitude'], $data['longitude'], $data['villeId'])) {
|
||||
return new JsonResponse(['error' => 'Données manquantes.'], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
|
||||
$ville = $villeRepository->find($data['villeId']);
|
||||
if (!$ville) {
|
||||
return new JsonResponse(['error' => 'Ville non trouvée'], Response::HTTP_NOT_FOUND);
|
||||
return new JsonResponse(['error' => 'Ville non trouvée.'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$existingLieuByName = $entityManager->getRepository(Lieu::class)->findOneBy([
|
||||
'nom' => $data['nom'],
|
||||
'ville' => $ville,
|
||||
]);
|
||||
if ($existingLieuByName) {
|
||||
return new JsonResponse(['error' => "Un lieu avec le nom '{$data['nom']}' existe déjà."], Response::HTTP_CONFLICT);
|
||||
}
|
||||
|
||||
$lieu = new Lieu();
|
||||
$lieu->setNom($data['nom']);
|
||||
@@ -49,20 +54,16 @@ class LieuController extends AbstractController
|
||||
$lieu->setLongitude($data['longitude']);
|
||||
$lieu->setVille($ville);
|
||||
|
||||
|
||||
$entityManager->persist($lieu);
|
||||
$entityManager->flush();
|
||||
|
||||
|
||||
return new JsonResponse([
|
||||
'id' => $lieu->getIdLieu(),
|
||||
'nom' => $lieu->getNom(),
|
||||
'rue' => $lieu->getRue(),
|
||||
'latitude' => $lieu->getLatitude(),
|
||||
'longitude' => $lieu->getLongitude(),
|
||||
], Response::HTTP_CREATED);
|
||||
}
|
||||
|
||||
|
||||
#[Route('/get-bounds/{villeId}', name: 'get_bounds', methods: ['GET'])]
|
||||
public function getBounds(VilleRepository $villeRepository, string $villeId): JsonResponse
|
||||
{
|
||||
|
||||
@@ -2,26 +2,46 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Repository\SiteRepository;
|
||||
use App\Repository\SortieRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class MainController extends AbstractController
|
||||
{
|
||||
#[Route('/', name: 'home')]
|
||||
public function index(TokenStorageInterface $tokenStorage, SortieRepository $sortieRepository): Response
|
||||
{
|
||||
public function index(
|
||||
TokenStorageInterface $tokenStorage,
|
||||
SortieRepository $sortieRepository,
|
||||
Request $request
|
||||
): Response {
|
||||
$token = $tokenStorage->getToken();
|
||||
$userConnect = $token?->getUser();
|
||||
$sorties = $sortieRepository->findAll();
|
||||
|
||||
// Récupérer les paramètres de filtre
|
||||
$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('main/index.html.twig', [
|
||||
'profile' => $userConnect,
|
||||
'sorties' => $sorties,
|
||||
'sites' => $sortieRepository->findAllSites(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[Route('/inscription', name: 'inscription')]
|
||||
public function inscription(TokenStorageInterface $tokenStorage): Response
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ class SortieController extends AbstractController
|
||||
TokenStorageInterface $tokenStorage,
|
||||
LieuRepository $lieuRepository,
|
||||
ParticipantRepository $participantRepository,
|
||||
EtatRepository $etatRepository // Ajout du repository pour les états
|
||||
EtatRepository $etatRepository
|
||||
): Response {
|
||||
$sortie = new Sortie();
|
||||
|
||||
@@ -63,7 +63,6 @@ class SortieController extends AbstractController
|
||||
}
|
||||
|
||||
$sortie->setSite($participant->getSite());
|
||||
$sortie->setParticipant($participant);
|
||||
|
||||
// Récupérer l'état "en création" avec l'ID donné
|
||||
$etat = $etatRepository->find('019349ba-38ca-7a39-93c3-62f046671525');
|
||||
@@ -75,6 +74,9 @@ class SortieController extends AbstractController
|
||||
// Associer l'état à la sortie
|
||||
$sortie->setEtat($etat);
|
||||
|
||||
// Associer l'organisateur à la sortie
|
||||
$sortie->setOrganisateur($participant);
|
||||
|
||||
// Sauvegarder la sortie
|
||||
$entityManager->persist($sortie);
|
||||
$entityManager->flush();
|
||||
@@ -89,4 +91,128 @@ class SortieController extends AbstractController
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/view/{id}', name: 'view', methods: ['GET'])]
|
||||
public function view(string $id, EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage): Response
|
||||
{
|
||||
// Récupérer l'utilisateur connecté
|
||||
$token = $tokenStorage->getToken();
|
||||
$userConnect = $token?->getUser();
|
||||
|
||||
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
||||
|
||||
if (!$sortie) {
|
||||
$this->addFlash('error', 'La sortie demandée n\'existe pas.');
|
||||
return $this->redirectToRoute('home');
|
||||
}
|
||||
|
||||
// Récupérer le profil de l'utilisateur connecté
|
||||
$profile = $this->getUser();
|
||||
|
||||
return $this->render('sortie/view.html.twig', [
|
||||
'sortie' => $sortie,
|
||||
'profile' => $userConnect,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/inscription/{id}', name: 'inscription', methods: ['POST'])]
|
||||
public function inscription(string $id, EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage): Response
|
||||
{
|
||||
// Récupérer l'utilisateur connecté
|
||||
$token = $tokenStorage->getToken();
|
||||
$userConnect = $token?->getUser();
|
||||
|
||||
if (!$userConnect) {
|
||||
$this->addFlash('error', 'Vous devez être connecté pour vous inscrire.');
|
||||
return $this->redirectToRoute('app_login');
|
||||
}
|
||||
|
||||
// Récupérer la sortie
|
||||
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
||||
|
||||
if (!$sortie) {
|
||||
$this->addFlash('error', 'La sortie demandée n\'existe pas.');
|
||||
return $this->redirectToRoute('home');
|
||||
}
|
||||
|
||||
// Vérifier que la sortie est "Ouverte"
|
||||
if ($sortie->getEtat()->getLibelle() !== 'Ouverte') {
|
||||
$this->addFlash('error', 'Vous ne pouvez pas vous inscrire à cette sortie car elle n\'est pas ouverte.');
|
||||
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
||||
}
|
||||
|
||||
// Vérifier si l'utilisateur est déjà inscrit
|
||||
if ($sortie->getParticipants()->contains($userConnect)) {
|
||||
$this->addFlash('error', 'Vous êtes déjà inscrit à cette sortie.');
|
||||
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
||||
}
|
||||
|
||||
// Vérifier le nombre maximum d'inscriptions
|
||||
if ($sortie->getParticipants()->count() >= $sortie->getNbInscriptionsMax()) {
|
||||
$this->addFlash('error', 'Le nombre maximum d\'inscriptions a été atteint pour cette sortie.');
|
||||
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
||||
}
|
||||
|
||||
// Ajouter l'utilisateur à la liste des participants
|
||||
$sortie->addParticipant($userConnect);
|
||||
$entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'Vous êtes inscrit à la sortie avec succès !');
|
||||
|
||||
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 {
|
||||
// Récupérer la sortie
|
||||
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
||||
|
||||
if (!$sortie) {
|
||||
$this->addFlash('error', 'La sortie demandée n\'existe pas.');
|
||||
return $this->redirectToRoute('home');
|
||||
}
|
||||
|
||||
// Vérifier si l'utilisateur est l'organisateur
|
||||
$token = $tokenStorage->getToken();
|
||||
$userConnect = $token?->getUser();
|
||||
|
||||
if ($userConnect->getIdParticipant() !== $sortie->getOrganisateur()->getIdParticipant()) {
|
||||
$this->addFlash('error', 'Vous n\'avez pas l\'autorisation de modifier cette sortie.');
|
||||
return $this->redirectToRoute('home');
|
||||
}
|
||||
|
||||
// Créer le formulaire
|
||||
$form = $this->createForm(SortieType::class, $sortie);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
// Mettre à jour le lieu si modifié
|
||||
$lieuId = $form->get('lieu')->getData();
|
||||
$lieu = $lieuRepository->find($lieuId);
|
||||
|
||||
if ($lieu) {
|
||||
$sortie->setLieu($lieu);
|
||||
}
|
||||
|
||||
// Sauvegarder les modifications
|
||||
$entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'La sortie a été mise à jour avec succès.');
|
||||
|
||||
return $this->redirectToRoute('sortie_view', ['id' => $sortie->getIdSortie()]);
|
||||
}
|
||||
|
||||
return $this->render('sortie/edit.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
'sortie' => $sortie,
|
||||
'profile' => $userConnect,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ class SortieType extends AbstractType
|
||||
'label' => 'Date et heure de début',
|
||||
'widget' => 'single_text',
|
||||
'html5' => true,
|
||||
'input' => 'datetime_immutable',
|
||||
'attr' => ['class' => 'form-control'],
|
||||
])
|
||||
->add('duree', IntegerType::class, [
|
||||
@@ -41,6 +42,7 @@ class SortieType extends AbstractType
|
||||
'label' => "Date limite d'inscription",
|
||||
'widget' => 'single_text',
|
||||
'html5' => true,
|
||||
'input' => 'datetime_immutable',
|
||||
'attr' => ['class' => 'form-control'],
|
||||
])
|
||||
->add('nbInscriptionsMax', IntegerType::class, [
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Site;
|
||||
use App\Entity\Sortie;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
@@ -16,6 +17,64 @@ class SortieRepository extends ServiceEntityRepository
|
||||
parent::__construct($registry, Sortie::class);
|
||||
}
|
||||
|
||||
public function findWithFilters($search, $siteId, $startDate, $endDate, $organisateur, $inscrit, $nonInscrit, $passees, $user)
|
||||
{
|
||||
$qb = $this->createQueryBuilder('s');
|
||||
|
||||
if ($search) {
|
||||
$qb->andWhere('s.nom LIKE :search')
|
||||
->setParameter('search', '%' . $search . '%');
|
||||
}
|
||||
|
||||
if ($siteId) {
|
||||
$qb->andWhere('s.site = :siteId')
|
||||
->setParameter('siteId', $siteId);
|
||||
}
|
||||
|
||||
if ($startDate) {
|
||||
$qb->andWhere('s.dateHeureDebut >= :startDate')
|
||||
->setParameter('startDate', new \DateTime($startDate));
|
||||
}
|
||||
|
||||
if ($endDate) {
|
||||
$qb->andWhere('s.dateHeureDebut <= :endDate')
|
||||
->setParameter('endDate', new \DateTime($endDate));
|
||||
}
|
||||
|
||||
if ($organisateur) {
|
||||
$qb->andWhere('s.organisateur = :user')
|
||||
->setParameter('user', $user);
|
||||
}
|
||||
|
||||
if ($inscrit) {
|
||||
$qb->andWhere(':user MEMBER OF s.participants')
|
||||
->setParameter('user', $user);
|
||||
}
|
||||
|
||||
if ($nonInscrit) {
|
||||
$qb->andWhere(':user NOT MEMBER OF s.participants')
|
||||
->setParameter('user', $user);
|
||||
}
|
||||
|
||||
if ($passees) {
|
||||
$qb->andWhere('s.dateHeureDebut < :now')
|
||||
->setParameter('now', new \DateTime());
|
||||
} else {
|
||||
$qb->andWhere('s.dateHeureDebut >= :now')
|
||||
->setParameter('now', new \DateTime());
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function findAllSites()
|
||||
{
|
||||
return $this->getEntityManager()
|
||||
->getRepository(Site::class)
|
||||
->findAll();
|
||||
}
|
||||
|
||||
|
||||
// /**
|
||||
// * @return Sortie[] Returns an array of Sortie objects
|
||||
// */
|
||||
|
||||
Reference in New Issue
Block a user