<?php
namespace App\Controller;
use App\Entity\Agent;
use App\Entity\MessageClient;
use App\Entity\User;
use App\Form\InscriptionAgentType;
use App\Form\MessageClientFormType;
use App\Form\ProspectFormType;
use App\Repository\BlogRepository;
use App\Repository\CategorieRepository;
use App\Repository\ConferenceRepository;
use App\Repository\EvenementRepository;
use App\Repository\PackageRepository;
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\OrderService;
use App\Service\ProspectService;
use App\Service\SecurityService;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Contracts\Translation\TranslatorInterface;
class HomeController extends AbstractController
{
private $mailService;
private $entityManager;
private $messageClientRepository;
private $userRepository;
private $repoPackage;
private $repoBlog;
private $securityService;
public function __construct(
MailService $mailService,
EntityManagerInterface $entityManager,
UserRepository $userRepository,
PackageRepository $repoPackage,
BlogRepository $repoBlog,
SecurityService $securityService,
private OrderService $orderService,
private TranslatorInterface $translator,
private EvenementRepository $evenementRepository,
private ProspectService $prospectService,
private ConferenceRepository $conferenceRepository,
){
$this->mailService = $mailService;
$this->entityManager = $entityManager;
$this->userRepository = $userRepository;
$this->repoPackage = $repoPackage;
$this->repoBlog = $repoBlog;
$this->securityService = $securityService;
}
#[Route('/', name: 'app_home')]
public function index(Request $request, CategorieRepository $categorieRepository): Response
{
$packages = $this->repoPackage->findAll();
$blogs = $this->repoBlog->findBy([], ['datePublication' => 'DESC'], 3);
$events = $this->evenementRepository->findUpcomingEvent();
$form = $this->createForm(ProspectFormType::class,[]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try{
$content = $this->prospectService->saveProspectToMether($form->getData());
$this->addFlash($content['class'], $content['message']);
return $this->redirectToRoute('app_home');;
// $targetUrl = $route->getTargetUrl();
// return $this->redirect($targetUrl.'#message-area');
} catch(\Exception $ex){
// $error = $ex->getMessage();
$this->addFlash('danger', $ex->getMessage());
}
}
// Exemple de datetime à afficher
$upComingConference = $this->conferenceRepository->getUpComingConference();
if($upComingConference){
$datetime = $upComingConference->getDateConference();
}else{
$datetime = (new \DateTime())->modify('-1 hour');
}
return $this->render('home/home.html.twig', [
'packages' => $packages,
'blogs' => $blogs,
'calendly_link' => $_ENV["CALENDLY_LINK"],
'events' => $events,
'form' => $form->createView(),
'prospection' => $_ENV['SHOW_PROSPECTION'],
'datetime'=>$datetime
]);
}
// #[Route('/about-us', name: 'app_about_us')]
// public function about_us(Request $request): Response
// {
// return $this->render('about_us/about_us.html.twig', [
// 'controller_name' => 'about_usController'
// ]);
// }
// #[Route('/checkout', name: 'app_checkout')]
// public function checkout(Request $request, MailService $mailService): Response
// {
// $user = new Agent();
// $form = $this->createForm(InscriptionAgentType::class, $user);
// $form->handleRequest($request);
// if($form->isSubmitted() && $form->isValid()) {
// try{
// $plainFirstPassword = $request->request->get('inscription_agent')['password']['first'];
// $correctPassword = $this->securityService->setPassword($user, $plainFirstPassword, $request->request->get('inscription_agent')['password']['second'], true);
// if(!$correctPassword) throw new \Exception("Password mismatch");
// $res = $this->orderService->insertAgent($user, $plainFirstPassword);
// return $this->redirectToRoute('app_checkout_success');
// }
// catch(ServerException $ex){
// $data = json_decode($ex->getResponse()->getContent(false), true);
// // dd($data);
// throw new Exception($data["message"] ?? "Erreur du serveur");
// }
// catch(Exception $ex){
// throw $ex;
// }
// }
// return $this->render('checkout/checkout.html.twig', [
// 'form' => $form->createView()
// ]);
// }
// #[Route('/checkout-success', name: 'app_checkout_success')]
// public function checkoutSuccess(Request $request, MailService $mailService): Response
// {
// return $this->render('checkout/checkout-success.html.twig', [
// 'controller_name' => 'checkoutController'
// ]);
// }
// #[Route('/portfolio', name: 'app_portfolio')]
// public function portfolio(Request $request): Response
// {
// return $this->render('portfolio/portfolio.html.twig', [
// 'title' => 'Projets',
// 'img_banner' => 'assets/images/webp/home/banner/portfolio-banner.webp'
// ]);
// }
#[Route('/legale-notice', name: 'app_legale_notice')]
public function app_legale_notice(Request $request): Response
{
return $this->render('/home/legale-notice.html.twig', [
'title' => $this->translator->trans('Mentions légales')
]);
}
#[Route('/terms-and-conditions', name: 'app_terms_and_conditions')]
public function app_terms_and_conditions(Request $request): Response
{
return $this->render('/home/terms-and-conditions.html.twig', [
'title' => $this->translator->trans('Conditions générales de vente')
]);
}
#[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->getFullName() ,
'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é');
return $this->redirectToRoute('app_contact');
// } catch(\Exception $ex){
// $this->addFlash('danger', $ex->getMessage());
// }
}
return $this->render('contact/contact.html.twig', [
'form' => $form->createView(),
'contact' => $messageClient,
'error'=>$error
]);
}
}