suppr table ville
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Lieu;
|
||||
use App\Entity\Ville;
|
||||
use App\Repository\LieuRepository;
|
||||
use App\Repository\VilleRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
@@ -30,18 +30,12 @@ class LieuController extends AbstractController
|
||||
): JsonResponse {
|
||||
$data = json_decode($request->getContent(), true);
|
||||
|
||||
if (!isset($data['nom'], $data['latitude'], $data['longitude'], $data['villeId'])) {
|
||||
if (!isset($data['nom'], $data['latitude'], $data['longitude'], $data['ville'], $data['codePostal'])) {
|
||||
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);
|
||||
}
|
||||
|
||||
$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);
|
||||
@@ -52,7 +46,8 @@ class LieuController extends AbstractController
|
||||
$lieu->setRue($data['rue']);
|
||||
$lieu->setLatitude($data['latitude']);
|
||||
$lieu->setLongitude($data['longitude']);
|
||||
$lieu->setVille($ville);
|
||||
$lieu->setVille($data['ville']);
|
||||
$lieu->setCodePostal($data['codePostal']);
|
||||
|
||||
$entityManager->persist($lieu);
|
||||
$entityManager->flush();
|
||||
@@ -63,51 +58,21 @@ class LieuController extends AbstractController
|
||||
], Response::HTTP_CREATED);
|
||||
}
|
||||
|
||||
|
||||
#[Route('/get-bounds/{villeId}', name: 'get_bounds', methods: ['GET'])]
|
||||
public function getBounds(VilleRepository $villeRepository, string $villeId): JsonResponse
|
||||
#[Route('/lieux/{id}', name: 'lieu_details', methods: ['GET'])]
|
||||
public function getLieuDetails(string $id, LieuRepository $lieuRepository): JsonResponse
|
||||
{
|
||||
$ville = $villeRepository->find($villeId);
|
||||
$lieu = $lieuRepository->find($id);
|
||||
|
||||
if (!$ville) {
|
||||
return new JsonResponse(['error' => 'Ville non trouvée'], Response::HTTP_NOT_FOUND);
|
||||
if (!$lieu) {
|
||||
return new JsonResponse(['error' => 'Lieu introuvable'], 404);
|
||||
}
|
||||
|
||||
$params = [
|
||||
'q' => $ville->getNom(),
|
||||
'format' => 'json',
|
||||
'polygon_geojson' => 1,
|
||||
];
|
||||
$url = 'https://nominatim.openstreetmap.org/search?' . http_build_query($params);
|
||||
|
||||
try {
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'header' => "User-Agent: MyApp/1.0 (contact@myapp.com)\r\n",
|
||||
],
|
||||
]);
|
||||
|
||||
$response = file_get_contents($url, false, $context);
|
||||
$data = json_decode($response, true);
|
||||
|
||||
if (!empty($data[0]['boundingbox'])) {
|
||||
$boundingBox = $data[0]['boundingbox'];
|
||||
$centerLat = ($boundingBox[0] + $boundingBox[1]) / 2;
|
||||
$centerLng = ($boundingBox[2] + $boundingBox[3]) / 2;
|
||||
|
||||
return new JsonResponse([
|
||||
'south' => $boundingBox[0],
|
||||
'north' => $boundingBox[1],
|
||||
'west' => $boundingBox[2],
|
||||
'east' => $boundingBox[3],
|
||||
'centerLat' => $centerLat,
|
||||
'centerLng' => $centerLng,
|
||||
]);
|
||||
}
|
||||
|
||||
return new JsonResponse(['error' => 'Bounding box non trouvée'], Response::HTTP_NOT_FOUND);
|
||||
} catch (\Exception $e) {
|
||||
return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return new JsonResponse([
|
||||
'rue' => $lieu->getRue(),
|
||||
'codePostal' => $lieu->getCodePostal(),
|
||||
'latitude' => $lieu->getLatitude(),
|
||||
'longitude' => $lieu->getLongitude(),
|
||||
'ville' => $lieu->getVille(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,54 +26,48 @@ class SortieController extends AbstractController
|
||||
ParticipantRepository $participantRepository,
|
||||
EtatRepository $etatRepository
|
||||
): Response {
|
||||
$sortie = new Sortie();
|
||||
|
||||
$token = $tokenStorage->getToken();
|
||||
$userConnect = $token?->getUser();
|
||||
|
||||
// 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()) {
|
||||
$lieuId = $form->get('lieu')->getData();
|
||||
$lieu = $lieuRepository->find($lieuId);
|
||||
|
||||
// Vérifier le lieu
|
||||
$lieu = $form->get('lieu')->getData();
|
||||
if (!$lieu) {
|
||||
$this->addFlash('error', 'Le lieu sélectionné est invalide.');
|
||||
$this->addFlash('error', 'Veuillez sélectionner un lieu valide.');
|
||||
return $this->redirectToRoute('sortie_create');
|
||||
}
|
||||
|
||||
$sortie->setLieu($lieu);
|
||||
|
||||
// Vérifier le participant (organisateur)
|
||||
$participant = $participantRepository->find($userConnect->getIdParticipant());
|
||||
|
||||
if (!$participant) {
|
||||
$this->addFlash('error', 'Impossible de déterminer le site de l\'utilisateur.');
|
||||
$this->addFlash('error', 'Impossible de trouver votre profil.');
|
||||
return $this->redirectToRoute('sortie_create');
|
||||
}
|
||||
|
||||
$sortie->setSite($participant->getSite());
|
||||
|
||||
$etat = $etatRepository->find('019349ba-38ca-7a39-93c3-62f046671525');
|
||||
// Définir les relations et l'état initial
|
||||
$etat = $etatRepository->findOneBy(['libelle' => 'Créée']);
|
||||
if (!$etat) {
|
||||
$this->addFlash('error', 'État non trouvé.');
|
||||
$this->addFlash('error', 'Erreur interne : état introuvable.');
|
||||
return $this->redirectToRoute('sortie_create');
|
||||
}
|
||||
|
||||
$sortie->setEtat($etat);
|
||||
|
||||
$sortie->setOrganisateur($participant);
|
||||
$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');
|
||||
}
|
||||
|
||||
@@ -84,64 +78,54 @@ class SortieController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/view/{id}', name: 'view', methods: ['GET'])]
|
||||
public function view(string $id, EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage): Response
|
||||
{
|
||||
$token = $tokenStorage->getToken();
|
||||
$userConnect = $token?->getUser();
|
||||
|
||||
public function view(
|
||||
string $id,
|
||||
EntityManagerInterface $entityManager,
|
||||
TokenStorageInterface $tokenStorage
|
||||
): Response {
|
||||
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
||||
|
||||
if (!$sortie) {
|
||||
$this->addFlash('error', 'La sortie demandée n\'existe pas.');
|
||||
$this->addFlash('error', 'Cette sortie n\'existe pas.');
|
||||
return $this->redirectToRoute('home');
|
||||
}
|
||||
|
||||
$profile = $this->getUser();
|
||||
|
||||
return $this->render('sortie/view.html.twig', [
|
||||
'sortie' => $sortie,
|
||||
'profile' => $userConnect,
|
||||
'profile' => $tokenStorage->getToken()?->getUser(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/inscription/{id}', name: 'inscription', methods: ['POST'])]
|
||||
public function inscription(string $id, EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage): Response
|
||||
{
|
||||
$token = $tokenStorage->getToken();
|
||||
$userConnect = $token?->getUser();
|
||||
|
||||
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', 'La sortie demandée n\'existe pas.');
|
||||
$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', 'Vous ne pouvez pas vous inscrire à cette sortie car elle n\'est pas ouverte.');
|
||||
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
||||
}
|
||||
|
||||
if ($sortie->getParticipants()->contains($userConnect)) {
|
||||
$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.');
|
||||
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
||||
} elseif ($sortie->getParticipants()->count() >= $sortie->getNbInscriptionsMax()) {
|
||||
$this->addFlash('error', 'Cette sortie est complète.');
|
||||
} else {
|
||||
$sortie->addParticipant($userConnect);
|
||||
$entityManager->flush();
|
||||
$this->addFlash('success', 'Votre inscription a été validée.');
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
$sortie->addParticipant($userConnect);
|
||||
$entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'Vous êtes inscrit à la sortie avec succès !');
|
||||
|
||||
return $this->redirectToRoute('sortie_view', ['id' => $id]);
|
||||
}
|
||||
|
||||
@@ -154,17 +138,14 @@ class SortieController extends AbstractController
|
||||
LieuRepository $lieuRepository
|
||||
): Response {
|
||||
$sortie = $entityManager->getRepository(Sortie::class)->find($id);
|
||||
|
||||
if (!$sortie) {
|
||||
$this->addFlash('error', 'La sortie demandée n\'existe pas.');
|
||||
$this->addFlash('error', 'Cette sortie n\'existe pas.');
|
||||
return $this->redirectToRoute('home');
|
||||
}
|
||||
|
||||
$token = $tokenStorage->getToken();
|
||||
$userConnect = $token?->getUser();
|
||||
|
||||
$userConnect = $tokenStorage->getToken()?->getUser();
|
||||
if ($userConnect->getIdParticipant() !== $sortie->getOrganisateur()->getIdParticipant()) {
|
||||
$this->addFlash('error', 'Vous n\'avez pas l\'autorisation de modifier cette sortie.');
|
||||
$this->addFlash('error', 'Vous ne pouvez pas modifier cette sortie.');
|
||||
return $this->redirectToRoute('home');
|
||||
}
|
||||
|
||||
@@ -172,17 +153,13 @@ class SortieController extends AbstractController
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$lieuId = $form->get('lieu')->getData();
|
||||
$lieu = $lieuRepository->find($lieuId);
|
||||
|
||||
$lieu = $form->get('lieu')->getData();
|
||||
if ($lieu) {
|
||||
$sortie->setLieu($lieu);
|
||||
}
|
||||
|
||||
$entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'La sortie a été mise à jour avec succès.');
|
||||
|
||||
$this->addFlash('success', 'Les modifications ont été enregistrées.');
|
||||
return $this->redirectToRoute('sortie_view', ['id' => $sortie->getIdSortie()]);
|
||||
}
|
||||
|
||||
@@ -192,5 +169,4 @@ class SortieController extends AbstractController
|
||||
'profile' => $userConnect,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ use App\Entity\Lieu;
|
||||
use App\Entity\Participant;
|
||||
use App\Entity\Site;
|
||||
use App\Entity\Sortie;
|
||||
use App\Entity\Ville;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||
|
||||
Reference in New Issue
Block a user