<?php
namespace App\Controller;
use App\Entity\Note;
use App\Entity\Movie;
use App\Form\NoteType;
use App\Entity\Session;
use App\Form\MovieType;
use App\Entity\Reservation;
use Doctrine\ORM\Mapping\Id;
use App\Form\ReservationType;
use App\Repository\NoteRepository;
use App\Repository\UserRepository;
use App\Repository\MovieRepository;
use App\Repository\SessionRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\ReservationRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
class MainController extends AbstractController
{
#[Route('/', name: 'main')]
public function index(MovieRepository $movieRepository): Response
{
return $this->render('main/index.html.twig', [
'controller_name' => 'MainController',
'movies' => $movieRepository->findAll(),
]);
}
#[Route('/films', name: 'films')]
public function films(MovieRepository $movieRepository): Response
{
return $this->render('main/films.html.twig', [
'controller_name' => 'MainController',
'movies' => $movieRepository->findAll(),
]);
}
#[Route('/films/{id}', name: 'filmsShow', methods: ['GET', 'POST'])]
public function filmsShow(Request $request, SessionRepository $sessionRepository,NoteRepository $noteRepository ,Movie $movie, EntityManagerInterface $entityManager): Response
{
$note = new Note();
$user = $this->getUser();
$allNotesByUser = $noteRepository->findBy(array('idUser' => $user));
$notesStatus = 0;
foreach($allNotesByUser as $value){
if($value->getIdMovie() == $movie){
$notesStatus = 1;
}
}
$note->setIdUser($this->getUser());
$note->setIdMovie($movie);
$form = $this->createForm(NoteType::class, $note);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($note);
$entityManager->flush();
return $this->redirectToRoute('filmsShow', ['id' => $movie->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('main/filmsShow.html.twig', [
'controller_name' => 'MainController',
'movie' => $movie,
'form' => $form,
'notesStatus' => $notesStatus,
'allNotesByMovie' => $noteRepository->findBy(array('idMovie' => $movie)),
'allSessionsByMovie' => $sessionRepository->findBy(array('idMovie' => $movie)),
]);
}
#[Route('/contacts', name: 'contacts')]
public function contact(): Response
{
return $this->render('main/contact.html.twig', [
'controller_name' => 'MainController',
]);
}
#[Route('/compte', name: 'compte')]
public function compte(UserRepository $userRepository, SessionRepository $sessionRepository, ReservationRepository $reservationRepository, MovieRepository $movieRepository, NoteRepository $noteRepository): Response
{
$user = $this->getUser();
// Recupere les données des réservations
$allReservations = $reservationRepository->findBy(array('idUser' => $user));
$allSessionsReserved = [];
foreach ($allReservations as $value) {
$value = $sessionRepository->findOneBy(array('id' => $value->getIdSession()));
array_push($allSessionsReserved, $value);
}
// Recupere les données des notes
$allNotesByUser = $noteRepository->findBy(array('idUser' => $user));
return $this->render('main/profile.html.twig', [
'controller_name' => 'MainController',
'allReservation' => $allReservations,
'allSessionsReserved' => $allSessionsReserved,
'allNotesByUser' => $allNotesByUser,
]);
}
#[Route('/tarifs', name: 'tarifs')]
public function tarifs(): Response
{
return $this->render('main/tarifs.html.twig', [
'controller_name' => 'MainController',
]);
}
#[Route('/réservation/{id}', name: 'userReservation', methods: ['GET', 'POST'])]
public function userReservation(Session $session): Response
{
$sessionD = $session->getNumberD();
return $this->render('main/userReservation.html.twig', [
'controller_name' => 'MainController',
'session' => $session,
'sessionD' => $sessionD,
]);
}
#[Route('/réservation/validation/{id}', name: 'userReservationValidation', methods: ['GET', 'POST'])]
public function userReservationValidation(Session $session, ReservationRepository $reservationRepository, EntityManagerInterface $entityManager): Response
{
$user = $this->getUser();
if($user){
// Preparation de la reservation
$reservation = new Reservation();
$reservation->setIdUser($this->getUser());
$reservation->setIdSession($session);
// Recupere le nombre de places
$sessionD = $session->getNumberD();
// Verification si l'utilisateur est connecte ET si il y a de la place dans la séance ET si l'utilisateur n'a pas deja reserve PUIS envoie
$sessionId = strval($session->getId());
$userReservationCheck = $reservationRepository->findOneBy(['idUser' => $user, 'idSession' => $sessionId]);
if( $user && $sessionD != 0 && $userReservationCheck == null){
// -1 place dans la salle
$sessionD = $sessionD - 1;
$session->setNumberD($sessionD);
// envoie des données
$entityManager->persist($reservation, $session);
$entityManager->flush();
// redirection
return $this->redirectToRoute('validationPage', ['id' => "1"], Response::HTTP_SEE_OTHER);
}
}
return $this->render('main/userReservation.html.twig', [
'controller_name' => 'MainController',
'session' => $session,
'sessionD' => $sessionD,
]);
}
#[Route('/validation/{id}', name: 'validationPage', methods: ['GET', 'POST'])]
public function validationPage(Request $request, EntityManagerInterface $entityManager): Response
{
return $this->render('announcement/validation.html.twig', [
'controller_name' => 'MainController',
]);
}
#[Route('/erreur/{id}', name: 'errorPage', methods: ['GET', 'POST'])]
public function errorPage(Request $request, EntityManagerInterface $entityManager): Response
{
return $this->render('announcement/error.html.twig', [
'controller_name' => 'MainController',
]);
}
public function header(): Response
{
return $this->render('/includes/header.html.twig', [
]);
}
public function footer(): Response
{
return $this->render('/includes/footer.html.twig', [
]);
}
#[Route('/admin', name: 'app_admin'),
IsGranted("ROLE_ADMIN")]
public function admin(): Response
{
return $this->render('admin/index.html.twig', []);
}
}