<?php
namespace App\Controller\Front;
use App\Entity\ApiUser;
use App\Entity\User;
use App\Entity\Visitor;
use App\Form\UserType;
use App\Form\VisitorDetailType;
use App\Form\VisitorEmailType;
use App\Form\VisitorRegistrationType;
use App\Form\VisitorType;
use App\Repository\ApiUserRepository;
use App\Repository\DirectorRepository;
use App\Repository\ModuleNoticeRepository;
use App\Repository\ModuleRepository;
use App\Repository\QuoteRepository;
use App\Repository\UserRepository;
use App\Utils\Constants;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
use Symfony\Component\Security\Http\LoginLink\LoginLinkNotification;
use App\Controller\API\V1\Game\WordSearchController;
use App\Repository\Game\WordSearch\GridRepository;
use JMS\Serializer\SerializerInterface;
use App\Entity\Game\WordSearch\Grid;
use App\Entity\Game\WordSearch\Word;
use App\Helper\ControllerHelper;
use App\Repository\CourseRepository;
use App\Repository\Game\WordSearch\WordRepository;
use App\Repository\LiveRepository;
class HomeController extends AbstractController
{
use ControllerHelper;
private WordSearchController $wordSearchController;
public function __construct(WordSearchController $wordSearchController)
{
$this->wordSearchController = $wordSearchController;
}
/**
* @Route("", name="app_front_homepage")
*/
public function index(QuoteRepository $quoteRepository, DirectorRepository $directorRepository, ModuleRepository $moduleRepository, ModuleNoticeRepository $moduleNoticeRepository, CourseRepository $courseRepository, UserRepository $userRepository, LiveRepository $liveRepository): Response
{
// return $this->redirectToRoute("app_front_library_page");
$session = new Session();
if (!$session->has('visitor-email') && $this->getUser() === null) {
return $this->redirectToRoute("app_login");
}
$quote = $quoteRepository->findActiveQuote();
$director = $directorRepository->findActiveDirector();
$modules = $moduleRepository->findPublishedModules();
$notices = $moduleNoticeRepository->findBy(['module' => null, 'isPublished' => true]);
$courseCount = $courseRepository->getTotalCourse();
$learnerCount = $userRepository->getLearnersCount(null, null);
$topLive = $liveRepository->getTopActiveLive("live");
$topLiveHasModule = ($topLive != null) ? $this->getActiveModule($topLive) : null;
return $this->render('front/index.html.twig', [
'quote' => $quote,
'director' => $director,
'modules' => $modules,
'notices' => $notices,
'courseCount' => $courseCount,
'learnerCount' => $learnerCount,
'topLive' => ($topLiveHasModule != null) ? $topLive : null
]);
}
/**
* @Route("/visiteur", name="app_front_visitor_registration_page")
*/
public function visitorRegistrationPage(NotifierInterface $notifier, LoginLinkHandlerInterface $loginLinkHandler, UserRepository $userRepository, Request $request, ManagerRegistry $doctrine, UserPasswordHasherInterface $passwordHasher): Response
{
$entityManager = $doctrine->getManager();
$visitor = new User();
$form = $this->createForm(VisitorRegistrationType::class, $visitor);
$form->handleRequest($request);
// if ($form->isSubmitted() && $form->isValid()) {
if ($request->isXmlHttpRequest()) {
// $session = new Session();
// $session->set('visitor-email', $form->get('email')->getData());
// $email = $form->get('email')->getData();
// $user = $userRepository->findOneBy(['email' => $email]);
$email = $request->request->get('email');
$user = $userRepository->findOneBy(['email' => $email]);
if ($user === null) {
// $session = new Session();
// $session->set('visitor-email', $form->get('email')->getData());
$visitor->setRoles(['ROLE_VISITOR']);
$hashedPassword = $passwordHasher->hashPassword(
$visitor,
Constants::VISITOR_DEFAULT_PASSWORD_PREFIX."_".$email
);
$visitor->setPassword($hashedPassword);
$userRepository->add($visitor, true);
// $entityManager->persist($visitor);
// $entityManager->flush();
// create a login link for $user this returns an instance
// of LoginLinkDetails
$loginLinkDetails = $loginLinkHandler->createLoginLink($visitor);
//$loginLink = $loginLinkDetails->getUrl();
// create a notification based on the login link details
$notification = new LoginLinkNotification(
$loginLinkDetails,
'Bienvenue sur le site de Cofina Academy !' // email subject
);
// create a recipient for this user
$recipient = new Recipient($visitor->getEmail());
try {
// send the notification to the user
$notifier->send($notification, $recipient);
} catch (\Throwable $th) {
//throw $th;
}
// $this->addFlash(
// 'visitor_login_notice',
// 'Un lien de connexion vous a été envoyé à votre adresse'
// );
// return $this->redirectToRoute("app_login");
return new JsonResponse([
'response' => 'Un lien de connexion vous a été envoyé à votre adresse'
], Response::HTTP_OK);
}
// create a login link for $user this returns an instance
// of LoginLinkDetails
$loginLinkDetails = $loginLinkHandler->createLoginLink($user);
$loginLink = $loginLinkDetails->getUrl();
// return $this->redirect($loginLink);
return new JsonResponse([
'login_url' => $loginLink
], Response::HTTP_OK);
}
return $this->render('front/pages/authentication/visitor_registration_page.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/check-visitor", name="app_front_check_visitor")
*/
public function checkEmailVisitor(LoginLinkHandlerInterface $loginLinkHandler, UserRepository $userRepository, Request $request): Response
{
if ($request->isXmlHttpRequest()) {
$email = $request->request->get('email');
$user = $userRepository->findOneBy(['email' => $email]);
if ($user !== null) {
if(in_array('ROLE_VISITOR', $user->getRoles())) {
$loginLinkDetails = $loginLinkHandler->createLoginLink($user);
$loginLink = $loginLinkDetails->getUrl();
if($user->isIsFirstConnexion()) {
return new JsonResponse([
'login_url' => null
], Response::HTTP_NO_CONTENT);
}
return new JsonResponse([
'login_url' => $loginLink
], Response::HTTP_OK);
} else {
return new JsonResponse([
'message' => "Ce compte n'est pas un compte visiteur."
], Response::HTTP_OK);
}
}
}
return new JsonResponse([
'login_url' => null
], Response::HTTP_NO_CONTENT);
}
/**
* @Route("/sso-login", name="app_front_sso_login")
*/
public function ssoLogin(LoginLinkHandlerInterface $loginLinkHandler, UserRepository $userRepository, Request $request): Response
{
if ($request->isXmlHttpRequest()) {
$email = $request->request->get('email');
$user = $userRepository->findOneBy(['email' => $email]);
if ($user !== null) {
$loginLinkDetails = $loginLinkHandler->createLoginLink($user);
$loginLink = $loginLinkDetails->getUrl();
return new JsonResponse([
'login_url' => $loginLink
], Response::HTTP_OK);
}
}
return new JsonResponse([
'login_url' => null
], Response::HTTP_NO_CONTENT);
}
/**
* @Route("/visiteur/details", name="app_front_visitor_details_page")
*/
public function visitorDetailsPage(Request $request, ManagerRegistry $doctrine): Response
{
$entityManager = $doctrine->getManager();
$visitor = new User();
$form = $this->createForm(VisitorDetailType::class, $visitor);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$session = new Session();
if ($session->has('visitor-email')) {
$visitor->setEmail($session->get('visitor-email'));
$visitor->setRoles(['ROLE_VISITOR']);
} else {
return $this->redirectToRoute("app_front_visitor_registration_page");
}
$entityManager->persist($visitor);
$entityManager->flush();
return $this->redirectToRoute("app_front_homepage");
}
return $this->render('front/pages/authentication/visitor_registration_details_page.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/message", name="app_front_message_page")
*/
public function messagePage(Request $request): Response
{
return $this->render('front/pages/random_page/message.html.twig');
}
/**
* @Route("/bienvenue", name="app_front_welcome_page")
*/
public function welcomePage(Request $request): Response
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
return $this->redirectToRoute("app_front_homepage");
}
return $this->render('front/pages/authentication/welcome_page.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("/quiz", name="app_front_dashboard_quiz_page")
*/
public function quiz(): Response
{
$user = $this->getUser();
return $this->render('front/pages/dashboard/quiz.html.twig', [
'controller_name' => 'DashboardController',
'user'=>$user
]);
}
/**
* @Route("/jeux", name="app_front_game_page")
*/
public function game(): Response
{
return $this->render('front/game/index.html.twig', [
'user' => $this->getUser()
]);
}
/**
* @Route("/jeux/quiz", name="app_front_game_quiz_time_page", methods={"GET"})
*/
public function playQuizTime(Request $request): Response
{
$user = $this->getUser();
$game = [
"question" => "Quand et par qui a été fondé COFINA ?",
"response_1" => "Jean Luc GASSE, 1999",
"response_2" => "Jean PIQUET, 2005",
"response_3" => "Carlos GOSHN, 2001",
"response_4" => "Jean-Luc KONAN, 2003",
"number_of_response" => 1,
"correct_response" => "Carlos GOSHN, 2001"
];
return $this->render('front/game/quiz-time.html.twig', [
'controller_name' => 'QuizTimeController',
'user'=>$user,
'game' => $game
]);
}
}