<?php
namespace App\Controller;
use App\Entity\Blog;
use App\Entity\Categorie;
use App\Entity\CodePromo;
use App\Entity\MessageClient;
use App\Entity\PageAboutUs;
use App\Entity\Prediction;
use App\Entity\Reservation;
use App\Entity\User;
use App\Form\MessageClientFormType;
use App\Form\PredictionFormType;
use App\Repository\BlogRepository;
use App\Repository\CategorieRepository;
use App\Repository\CodePromoRepository;
use App\Repository\TestNumerologieInfoElmtRepository;
use App\Repository\TestNumerologieInfoRepository;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Service\MailService;
use App\Service\ReservationService;
use App\Service\SecurityService;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
class HomeController extends AbstractController
{
private $mailService;
private $entityManager;
private $messageClientRepository;
private $userRepository;
private $reservationService;
public function __construct(MailService $mailService, EntityManagerInterface $entityManager, ReservationService $reservationService, UserRepository $userRepository, private CodePromoRepository $codePromoRepository){
$this->mailService = $mailService;
$this->entityManager = $entityManager;
$this->reservationService = $reservationService;
$this->userRepository = $userRepository;
}
#[Route('/', name: 'app_home')]
public function index(Request $request, CategorieRepository $categorieRepository): Response
{
$prediction = new Prediction();
$predictionForm = $this->createForm(PredictionFormType::class, $prediction);
$predictionForm->handleRequest($request);
$error = null;
if ($predictionForm->isSubmitted() && $predictionForm->isValid()) {
try{
$prediction = $predictionForm->getData();
$prediction->setCreatedAt(new \DateTime());
$this->entityManager->persist($prediction);
$client = $this->userRepository->findByMailIngoreCase($prediction->getEmail());
if(!$client){
$client = new User();
$client->setFirstname($prediction->getName());
$client->setMail($prediction->getEmail());
$client->setRoles(['ROLE_CLIENT']);
$client->setTypeInscription(User::TYPE_INSCRIPTION_PAGE_NUMEROLOGIE);
$this->entityManager->persist($client);
}
$this->entityManager->flush();
return $this->redirectToRoute('app_prediction', [
'nm'=>SecurityService::encrypt($prediction->getName(), SecurityService::$KEY) ,
'dg'=>SecurityService::encrypt($prediction->getDigit(), SecurityService::$KEY)
]);
} catch(\Exception $ex){
$this->addFlash('danger', $ex->getMessage());
}
}
return $this->render('home/home.html.twig', [
'controller_name' => 'HomeController',
'predictionForm' => $predictionForm->createView(),
'error'=>$error
]);
}
#[Route('/nos-informations/{id}', name: 'app_about_us')]
public function aboutUs( PageAboutUs $pageAboutUs): Response
{
return $this->render('about_us/page_about_us_show.html.twig', [
'controller_name' => 'aboutUsController',
'pageAboutUs' => $pageAboutUs
]);
}
#[Route('/services', name: 'app_services')]
public function services(Request $request): Response
{
return $this->render('services/services.html.twig', [
'controller_name' => 'servicesController'
]);
}
#[Route('/details', name: 'app_details')]
public function details(Request $request): Response
{
return $this->render('details/details.html.twig', [
'controller_name' => 'detailsController'
]);
}
#[Route('/services-voyance-cartomancie', name: 'app_prestations')]
public function prestations(Request $request, CategorieRepository $categorieRepository): Response
{
return $this->render('prestations/prestations.html.twig', [
'controller_name' => 'prestationsController',
'categories' => $categorieRepository->findAll()
]);
}
#[Route('/prestation/category/{id}/details', name: 'app_prestations_category_details')]
public function app_prestations_details(Categorie $catPrestation): Response
{
return $this->render('prestations/prestations_details.html.twig', [
'catPrestation' => $catPrestation
]);
}
#[Route('/e-formations', name: 'app_formations')]
public function formations(Request $request): Response
{
return $this->render('formations/formations.html.twig', [
'controller_name' => 'formationsController'
]);
}
#[Route('/temoignages', name: 'app_testimonials')]
public function testimonials(Request $request): Response
{
return $this->render('testimonials/testimonials.html.twig', [
'controller_name' => 'testimonialsController'
]);
}
#[Route('/prediction', name: 'app_prediction')]
public function prediction(Request $request, TestNumerologieInfoRepository $testNumerologieInfoRepository, TestNumerologieInfoElmtRepository $testNumerologieInfoElmtRepository): Response
{
$error = null;
$name = SecurityService::decrypt($request->query->get('nm'), SecurityService::$KEY);
$digit = SecurityService::decrypt($request->query->get('dg'), SecurityService::$KEY);
$info = $testNumerologieInfoRepository->find($digit);
if($info!=null) $info->setElmts($testNumerologieInfoElmtRepository->findValidByParent($info));
return $this->render('prediction/prediction.html.twig', [
'name' => $name,
'info' => $info,
'error'=>$error
]);
}
#[Route('/contact', name: 'app_contact')]
public function contact(Request $request, MailService $mailService): Response
{
$messageClient = new MessageClient();
$form = $this->createForm(MessageClientFormType::class, $messageClient);
$form->handleRequest($request);
$error = "";
if ($form->isSubmitted() && $form->isValid()) {
try{
$messageClient = $form->getData();
$mail = [
'to' => $this->getParameter('contact_recipient_mail'),
'subject' => "Nouveau contact de ".$messageClient->getName(),
'body' => $mailService->renderTwig('emails/contact.html.twig', ['contact' => $messageClient])
];
$mailService->sendMail($mail);
$messageClient->setCreatedAt(new \DateTime());
$this->entityManager->persist($messageClient);
$this->entityManager->flush();
$this->addFlash('success', 'Message envoyé');
unset($messageClient);
unset($form);
$messageClient = new MessageClient();
$form = $this->createForm(MessageClientFormType::class, $messageClient);
} catch(\Exception $ex){
$this->addFlash('danger', $ex->getMessage());
}
}
return $this->render('contact/contact.html.twig', [
'form' => $form->createView(),
'contact' => $messageClient,
'error'=>$error
]);
}
#[Route('/rendez-vous/etape-1', name: 'app_appointment_step_1')]
public function appointmentStep1(Request $request): Response
{
$orders = [];
if ($request->isMethod('POST')) {
return $this->redirectToRoute('app_appointment_step_2');
}
return $this->render('appointment/appointment_step_1.html.twig', [
'controller_name' => 'appointmentController',
'orders'=>$orders
]);
}
#[Route('/rendez-vous/etape-2', name: 'app_appointment_step_2')]
public function appointmentStep2(Request $request): Response
{
$orders = [];
if ($request->isMethod('POST')) {
return $this->redirectToRoute('app_appointment_step_3');
}
return $this->render('appointment/appointment_step_2.html.twig', [
'controller_name' => 'appointmentController',
'orders'=>$orders
]);
}
#[Route('/rendez-vous/etape-3', name: 'app_appointment_step_3')]
public function appointmentStep3(Request $request): Response
{
$orders = [];
if ($request->isMethod('POST')) {
return $this->redirectToRoute('app_home');
}
return $this->render('appointment/appointment_step_3.html.twig', [
'controller_name' => 'appointmentController',
'orders'=>$orders
]);
}
#[Route('/rendez-vous', name: 'app_appointment')]
public function appointment(Request $request): Response
{
return $this->redirectToRoute('app_appointment_step_1');
}
#[Route('/mentions-legales', name: 'app_legal_notice')]
public function legalNotice(Request $request): Response
{
return $this->render('legal-notice/legal-notice.html.twig', [
'controller_name' => 'legalNoticeController'
]);
}
#[Route('/take-appointment', name: 'app_take_appointment', methods:"POST")]
public function takeAppointment(Request $request): JsonResponse
{
try{
$params = json_decode( $request->getContent(), true);
$reservation = $this->reservationService->parseReservation($params);
$reservation->setFromAdmin($this->isGranted('ROLE_ADMIN'));
$this->reservationService->saveReservation($reservation);
return new JsonResponse(['reservationId' => $reservation->getId()]);
} catch(Exception $ex){
return new JsonResponse(['message' => $ex->getMessage()], 500);
}
}
public function checkStatus($statusShould, Reservation $reservation): ?Response{
if($statusShould != Reservation::CREATED && $reservation->getStatut() == Reservation::CREATED)
return $this->redirectToRoute('app_take_appointment_step_1', ['id' => $reservation->getId()]);
else if($statusShould != Reservation::SCHEDULED && $reservation->getStatut() == Reservation::SCHEDULED)
return $this->redirectToRoute('app_take_appointment_step_2', ['id' => $reservation->getId()]);
else if($statusShould != Reservation::PAIED && $reservation->getStatut() == Reservation::PAIED)
return $this->redirectToRoute('app_take_appointment_step_3', ['id' => $reservation->getId()]);
return null;
}
#[Route('/take-appointment/{id}/step-1', name: 'app_take_appointment_step_1')]
public function takeAppointmentStep1(Reservation $reservation, Request $request): Response
{
$response = $this->checkStatus(Reservation::CREATED, $reservation);
if($response) return $response;
return $this->render('take_appointment/take_appointment_step_1.html.twig', [
'controller_name' => 'appointmentController',
'reservation'=>$reservation,
'clientId'=>$this->getUser() != null ? $this->getUser()->getId() : null
]);
}
#[Route('/take-appointment/{id}/step-2', name: 'app_take_appointment_step_2')]
public function takeAppointmentStep2(Reservation $reservation, Request $request): Response
{
$response = $this->checkStatus(Reservation::SCHEDULED, $reservation);
if($response) return $response;
return $this->render('take_appointment/take_appointment_step_2.html.twig', [
'controller_name' => 'appointmentController',
'reservation'=>$reservation
]);
}
#[Route('/take-appointment/{id}/step-3', name: 'app_take_appointment_step_3')]
public function takeAppointmentStep3(Reservation $reservation, Request $request): Response
{
$response = $this->checkStatus(Reservation::PAIED, $reservation);
if($reservation->getCodePromo() != null && $reservation->getCodePromo()->getId() != null){
$reservation->setCodePromo($this->codePromoRepository->find($reservation->getCodePromo()->getId()));
}
if($response) return $response;
return $this->render('take_appointment/take_appointment_step_3.html.twig', [
'controller_name' => 'appointmentController',
'reservation'=>$reservation
]);
}
#[Route('/reservation/{id}/payement-intent/{codePromo}', name: 'app_reservation_payment_intent', methods:"GET")]
public function getPaymentIntent(Reservation $reservation, $codePromo = null): JsonResponse
{
try{
$codePromoObj = null;
if($codePromo != null){
$codePromoObj = new CodePromo();
$codePromoObj->setValue($codePromo);
$reservation->setCodePromo($codePromoObj);
}
$clientSecret = $this->reservationService->createReservationPaymentIntent($reservation);
return new JsonResponse(['clientSecret' => $clientSecret]);
} catch(Exception $ex){
return new JsonResponse(['message' => $ex->getMessage()], 500);
}
}
#[Route('/reservation/{id}/pay', name: 'app_reservation_pay', methods:"PUT")]
public function pay(Reservation $reservation): JsonResponse
{
try{
$reservation->setPaymentApiMethod(Reservation::PAYMENT_API_METHOD_STRIPE);
$this->reservationService->payReservation($reservation);
return new JsonResponse(['message' => 'ok']);
} catch(Exception $ex){
return new JsonResponse(['message' => $ex->getMessage()], 500);
}
}
#[Route('/reservation/{id}/paypal', name: 'app_reservation_paypal', methods:"PUT")]
public function paypal(Reservation $reservation, Request $request): JsonResponse
{
try{
$reservation->setPaymentApiMethod(Reservation::PAYMENT_API_METHOD_PAYPAL);
$params = json_decode( $request->getContent(), true);
$this->reservationService->payReservationPaypal($reservation, $params['authorizationId']);
return new JsonResponse(['message' => 'ok']);
} catch(Exception $ex){
return new JsonResponse(['message' => $ex->getMessage()], 500);
}
}
#[Route('/reservaton/{id}/infos', name: 'app_reservation_save_infos', methods:"POST")]
public function saveInfos(Reservation $reservation, Request $request): JsonResponse
{
try{
$params = json_decode( $request->getContent(), true);
$params['client']=null;
if($params['clientId']!=null) $params['client']=$this->userRepository->find($params['clientId']);
$reservation = $this->reservationService->saveInfosReservation($reservation, $params);
return new JsonResponse(['message' => 'ok']);
} catch(Exception $ex){
return new JsonResponse(['message' => $ex->getMessage()], 500);
}
}
// #[Route('/requete/sql/exec', name: 'req_sql_exec')]
// public function req_sql_exec(BlogRepository $blogRepository, EntityManagerInterface $em)
// {
// $allBlog = $blogRepository->findAll();
// /**
// * @var Blog $blog
// */
// foreach ($allBlog as $blog) {
// $blog->setMetatitle($blog->getTitre());
// $blog->setMetadescription($blog->getDescription());
// $blog->setNomAuteur('Françoise');
// }
// $em->flush();
// return $this->redirectToRoute('app_about_us');
// }
}