src/Controller/Front/HomeController.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\ApiUser;
  4. use App\Entity\User;
  5. use App\Entity\Visitor;
  6. use App\Form\UserType;
  7. use App\Form\VisitorDetailType;
  8. use App\Form\VisitorEmailType;
  9. use App\Form\VisitorRegistrationType;
  10. use App\Form\VisitorType;
  11. use App\Repository\ApiUserRepository;
  12. use App\Repository\DirectorRepository;
  13. use App\Repository\ModuleNoticeRepository;
  14. use App\Repository\ModuleRepository;
  15. use App\Repository\QuoteRepository;
  16. use App\Repository\UserRepository;
  17. use App\Utils\Constants;
  18. use Doctrine\Persistence\ManagerRegistry;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpFoundation\Session\Session;
  24. use Symfony\Component\Notifier\NotifierInterface;
  25. use Symfony\Component\Notifier\Recipient\Recipient;
  26. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  29. use Symfony\Component\Security\Http\LoginLink\LoginLinkNotification;
  30. use App\Controller\API\V1\Game\WordSearchController;
  31. use App\Repository\Game\WordSearch\GridRepository;
  32. use JMS\Serializer\SerializerInterface;
  33. use App\Entity\Game\WordSearch\Grid;
  34. use App\Entity\Game\WordSearch\Word;
  35. use App\Helper\ControllerHelper;
  36. use App\Repository\CourseRepository;
  37. use App\Repository\Game\WordSearch\WordRepository;
  38. use App\Repository\LiveRepository;
  39. class HomeController extends AbstractController
  40. {
  41.     use ControllerHelper;
  42.     private WordSearchController $wordSearchController;
  43.     public function __construct(WordSearchController $wordSearchController)
  44.     {
  45.         $this->wordSearchController $wordSearchController;
  46.     }
  47.     /**
  48.      * @Route("", name="app_front_homepage")
  49.      */
  50.     public function index(QuoteRepository $quoteRepositoryDirectorRepository $directorRepositoryModuleRepository $moduleRepositoryModuleNoticeRepository $moduleNoticeRepositoryCourseRepository $courseRepositoryUserRepository $userRepositoryLiveRepository $liveRepository): Response
  51.     {
  52.         // return $this->redirectToRoute("app_front_library_page");
  53.         $session = new Session();
  54.         if (!$session->has('visitor-email') && $this->getUser() === null) {
  55.             return $this->redirectToRoute("app_login");
  56.         }
  57.         $quote $quoteRepository->findActiveQuote();
  58.         $director $directorRepository->findActiveDirector();
  59.         $modules $moduleRepository->findPublishedModules();
  60.         $notices $moduleNoticeRepository->findBy(['module' => null'isPublished' => true]);
  61.         $courseCount $courseRepository->getTotalCourse();
  62.         $learnerCount $userRepository->getLearnersCount(nullnull);
  63.         $topLive $liveRepository->getTopActiveLive("live");
  64.         $topLiveHasModule = ($topLive != null) ? $this->getActiveModule($topLive) : null;
  65.         return $this->render('front/index.html.twig', [
  66.             'quote' => $quote,
  67.             'director' => $director,
  68.             'modules' => $modules,
  69.             'notices' => $notices,
  70.             'courseCount' => $courseCount,
  71.             'learnerCount' => $learnerCount,
  72.             'topLive' => ($topLiveHasModule != null) ? $topLive null
  73.         ]);
  74.     }
  75.     /**
  76.      * @Route("/visiteur", name="app_front_visitor_registration_page")
  77.      */
  78.     public function visitorRegistrationPage(NotifierInterface $notifierLoginLinkHandlerInterface $loginLinkHandlerUserRepository $userRepositoryRequest $requestManagerRegistry $doctrineUserPasswordHasherInterface $passwordHasher): Response
  79.     {
  80.         $entityManager $doctrine->getManager();
  81.         $visitor = new User();
  82.         $form $this->createForm(VisitorRegistrationType::class, $visitor);
  83.         $form->handleRequest($request);
  84.         // if ($form->isSubmitted() && $form->isValid()) {
  85.         if ($request->isXmlHttpRequest()) {
  86.             // $session = new Session();
  87.             // $session->set('visitor-email', $form->get('email')->getData());
  88.             // $email = $form->get('email')->getData();
  89.             // $user = $userRepository->findOneBy(['email' => $email]);
  90.             $email $request->request->get('email');
  91.             $user $userRepository->findOneBy(['email' => $email]);
  92.             if ($user === null) {
  93.                 // $session = new Session();
  94.                 // $session->set('visitor-email', $form->get('email')->getData());
  95.                 $visitor->setRoles(['ROLE_VISITOR']);
  96.                 $hashedPassword $passwordHasher->hashPassword(
  97.                     $visitor,
  98.                     Constants::VISITOR_DEFAULT_PASSWORD_PREFIX."_".$email
  99.                 );
  100.                 $visitor->setPassword($hashedPassword);
  101.                 $userRepository->add($visitortrue);
  102.                 // $entityManager->persist($visitor);
  103.                 // $entityManager->flush();
  104.                 // create a login link for $user this returns an instance
  105.                 // of LoginLinkDetails
  106.                 $loginLinkDetails $loginLinkHandler->createLoginLink($visitor);
  107.                 //$loginLink = $loginLinkDetails->getUrl();
  108.                 // create a notification based on the login link details
  109.                 $notification = new LoginLinkNotification(
  110.                     $loginLinkDetails,
  111.                     'Bienvenue sur le site de Cofina Academy !' // email subject
  112.                 );
  113.                 // create a recipient for this user
  114.                 $recipient = new Recipient($visitor->getEmail());
  115.                 try {
  116.                     // send the notification to the user
  117.                     $notifier->send($notification$recipient);
  118.                 } catch (\Throwable $th) {
  119.                     //throw $th;
  120.                 }
  121.                 // $this->addFlash(
  122.                 //     'visitor_login_notice',
  123.                 //     'Un lien de connexion vous a été envoyé à votre adresse'
  124.                 // );
  125.                 // return $this->redirectToRoute("app_login");
  126.                 return new JsonResponse([
  127.                     'response' => 'Un lien de connexion vous a été envoyé à votre adresse'
  128.                 ], Response::HTTP_OK);
  129.             }
  130.             // create a login link for $user this returns an instance
  131.             // of LoginLinkDetails
  132.             $loginLinkDetails $loginLinkHandler->createLoginLink($user);
  133.             $loginLink $loginLinkDetails->getUrl();
  134.             // return $this->redirect($loginLink);
  135.             return new JsonResponse([
  136.                 'login_url' => $loginLink
  137.             ], Response::HTTP_OK);
  138.         }
  139.         return $this->render('front/pages/authentication/visitor_registration_page.html.twig', [
  140.             'form' => $form->createView(),
  141.         ]);
  142.     }
  143.     /**
  144.      * @Route("/check-visitor", name="app_front_check_visitor")
  145.      */
  146.     public function checkEmailVisitor(LoginLinkHandlerInterface $loginLinkHandlerUserRepository $userRepositoryRequest $request): Response
  147.     {
  148.         if ($request->isXmlHttpRequest()) {
  149.             $email $request->request->get('email');
  150.             $user $userRepository->findOneBy(['email' => $email]);
  151.             if ($user !== null) {
  152.                 if(in_array('ROLE_VISITOR'$user->getRoles())) {
  153.                     $loginLinkDetails $loginLinkHandler->createLoginLink($user);
  154.                     $loginLink $loginLinkDetails->getUrl();
  155.                     if($user->isIsFirstConnexion()) {
  156.                         return new JsonResponse([
  157.                             'login_url' => null
  158.                         ], Response::HTTP_NO_CONTENT);
  159.                     }
  160.     
  161.                     return new JsonResponse([
  162.                         'login_url' => $loginLink
  163.                     ], Response::HTTP_OK);
  164.                 } else {
  165.                     return new JsonResponse([
  166.                         'message' => "Ce compte n'est pas un compte visiteur."
  167.                     ], Response::HTTP_OK);
  168.                 }
  169.                 
  170.             } 
  171.         }
  172.         return new JsonResponse([
  173.             'login_url' => null
  174.         ], Response::HTTP_NO_CONTENT);
  175.     }
  176.     /**
  177.      * @Route("/sso-login", name="app_front_sso_login")
  178.      */
  179.     public function ssoLogin(LoginLinkHandlerInterface $loginLinkHandlerUserRepository $userRepositoryRequest $request): Response
  180.     {
  181.         if ($request->isXmlHttpRequest()) {
  182.             $email $request->request->get('email');
  183.             $user $userRepository->findOneBy(['email' => $email]);
  184.             if ($user !== null) {
  185.                 $loginLinkDetails $loginLinkHandler->createLoginLink($user);
  186.                     $loginLink $loginLinkDetails->getUrl();
  187.     
  188.                     return new JsonResponse([
  189.                         'login_url' => $loginLink
  190.                     ], Response::HTTP_OK);
  191.             } 
  192.         }
  193.         return new JsonResponse([
  194.             'login_url' => null
  195.         ], Response::HTTP_NO_CONTENT);
  196.     }
  197.     /**
  198.      * @Route("/visiteur/details", name="app_front_visitor_details_page")
  199.      */
  200.     public function visitorDetailsPage(Request $requestManagerRegistry $doctrine): Response
  201.     {
  202.         $entityManager $doctrine->getManager();
  203.         $visitor = new User();
  204.         $form $this->createForm(VisitorDetailType::class, $visitor);
  205.         $form->handleRequest($request);
  206.         if ($form->isSubmitted() && $form->isValid()) {
  207.             $session = new Session();
  208.             if ($session->has('visitor-email')) {
  209.                 $visitor->setEmail($session->get('visitor-email'));
  210.                 $visitor->setRoles(['ROLE_VISITOR']);
  211.             } else {
  212.                 return $this->redirectToRoute("app_front_visitor_registration_page");
  213.             }
  214.             $entityManager->persist($visitor);
  215.             $entityManager->flush();
  216.             return $this->redirectToRoute("app_front_homepage");
  217.         }
  218.         return $this->render('front/pages/authentication/visitor_registration_details_page.html.twig', [
  219.             'form' => $form->createView(),
  220.         ]);
  221.     }
  222.     /**
  223.      * @Route("/message", name="app_front_message_page")
  224.      */
  225.     public function messagePage(Request $request): Response
  226.     {
  227.         return $this->render('front/pages/random_page/message.html.twig');
  228.     }
  229.     /**
  230.      * @Route("/bienvenue", name="app_front_welcome_page")
  231.      */
  232.     public function welcomePage(Request $request): Response
  233.     {
  234.         $user = new User();
  235.         $form $this->createForm(UserType::class, $user);
  236.         $form->handleRequest($request);
  237.         if ($form->isSubmitted() && $form->isValid()) {
  238.             return $this->redirectToRoute("app_front_homepage");
  239.         }
  240.         return $this->render('front/pages/authentication/welcome_page.html.twig', [
  241.             'form' => $form->createView(),
  242.         ]);
  243.     }
  244.     /**
  245.      * @Route("/quiz", name="app_front_dashboard_quiz_page")
  246.      */
  247.     public function quiz(): Response
  248.     {
  249.         $user $this->getUser();
  250.         return $this->render('front/pages/dashboard/quiz.html.twig', [
  251.             'controller_name' => 'DashboardController',
  252.             'user'=>$user
  253.         ]);
  254.     }
  255.     /**
  256.      * @Route("/jeux", name="app_front_game_page")
  257.      */
  258.     public function game(): Response
  259.     {
  260.         return $this->render('front/game/index.html.twig', [
  261.             'user' => $this->getUser()
  262.         ]);
  263.     }
  264.      /**
  265.       * @Route("/jeux/quiz", name="app_front_game_quiz_time_page",  methods={"GET"})
  266.       */
  267.      public function playQuizTime(Request $request): Response
  268.      {
  269.          $user $this->getUser();
  270.          $game = [
  271.              "question" => "Quand et par qui a été fondé COFINA ?",
  272.              "response_1" => "Jean Luc GASSE, 1999",
  273.              "response_2" => "Jean PIQUET, 2005",
  274.              "response_3" => "Carlos GOSHN, 2001",
  275.              "response_4" => "Jean-Luc KONAN, 2003",
  276.              "number_of_response" => 1,
  277.              "correct_response" => "Carlos GOSHN, 2001"
  278.          ];
  279.          return $this->render('front/game/quiz-time.html.twig', [
  280.              'controller_name' => 'QuizTimeController',
  281.              'user'=>$user,
  282.              'game' => $game
  283.          ]);
  284.      }
  285. }