src/Controller/HomeController.php line 44

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Country;
  4. use App\Entity\GamePlay;
  5. use App\Entity\Goodies;
  6. use App\Entity\Page;
  7. use App\Entity\Popin;
  8. use App\Entity\SpeakyPart;
  9. use App\Entity\User;
  10. use App\Entity\UserChallengeReward;
  11. use App\Entity\UserSpeakyPart;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  16. use Symfony\Component\Form\Extension\Core\Type\TextType;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. #[Route('/{_locale}')]
  21. class HomeController extends AbstractController
  22. {
  23.     private $doctrine;
  24.     private $entityManager;
  25.     
  26.     public function __construct(ManagerRegistry $doctrine)
  27.     {
  28.         $this->doctrine $doctrine;
  29.         $this->entityManager $this->doctrine->getManager();
  30.     }
  31.     #[Route('/'name'homepage'priority: -1)]
  32.     public function index(): Response
  33.     {
  34.         return $this->render('home/homepage.html.twig', [
  35.             'controller_name' => 'HomeController',
  36.         ]);
  37.     }
  38.     #[Route('/home_header'name'home_header')]
  39.     public function homeHeader(Request $request)
  40.     {
  41.         $user $this->getUser();
  42.         if ((!$user instanceof User)) 
  43.         {
  44.             return $this->render('home/header_guest.html.twig', []);
  45.         } else
  46.         {
  47.             return $this->render('home/header_user.html.twig', ['user' => $user]);
  48.         }
  49.     }
  50.     #[Route('/home_footer'name'home_footer')]
  51.     public function homeFooter(Request $request)
  52.     {
  53.         $locale $request->get('_locale','fr');
  54.         $user $this->getUser();
  55.         $pageRepository $this->doctrine->getRepository(Page::class);
  56.         $cgv $pageRepository->getI18nPageByRoute('cgv',$locale);
  57.         $rgpd $pageRepository->getI18nPageByRoute('confidentialite',$locale);
  58.         return $this->render('home/footer.html.twig', ['cgv' => $cgv'rgpd' => $rgpd]);
  59.     }
  60.     public function indexV3Challenge($new false): Response
  61.     {
  62.         $challenge $this->doctrine->getRepository(Popin::class)->getLastChallenge();
  63.         if($challenge == null) {
  64.             return new Response();
  65.         }
  66.         $nextChallenge null;
  67.         
  68.         $gamePlayRepository $this->doctrine->getRepository(GamePlay::class);
  69.         /** @var User $user */
  70.         $user $this->getUser();
  71.         //on affiche les résultats que si le cron est passé
  72.         $topScores = array();
  73.         if ($challenge->isFinished() && $challenge->getIsRewardSent())
  74.         {
  75.             if ((!$challenge->getWinner() && !$challenge->getSecond() && !$challenge->getThird()))
  76.             {
  77.                 //TODO: move all this in a repository (something like 'closeChallenge')
  78.                 /*$topScoresGamePlay = $gamePlayRepository->getChallengeTopScores($challenge);
  79.                 if ($topScoresGamePlay) 
  80.                 {
  81.                     if (count($topScoresGamePlay) > 0)
  82.                         $challenge->setWinner($topScoresGamePlay[0]);
  83.                     if (count($topScoresGamePlay) > 1)
  84.                         $challenge->setSecond($topScoresGamePlay[1]);
  85.                     if (count($topScoresGamePlay) > 2)
  86.                         $challenge->setThird($topScoresGamePlay[2]);
  87.                     
  88.                     //Now prepare the participation popin
  89.                     $challengeParticipation = new Popin();
  90.                     $challengeParticipation->setType(Popin::TYPE_CHALLENGE_PARTICIPATION);
  91.                     $challengeParticipation->setChallenge($challenge);
  92.                     for ($i = 3; $i < count($topScoresGamePlay); $i++) {
  93.                         $gamePlay = $topScoresGamePlay[$i];
  94.                         $challengeParticipation->addPopinTarget($gamePlay->getUser());
  95.                     }
  96.                     
  97.                     $em = $this->entityManager;
  98.                     $em->persist($challengeParticipation);
  99.                     $em->flush();
  100.                 }*/
  101.             }
  102.             $topScores $challenge->getTopScores();
  103.             $userScore $this->doctrine->getRepository(GamePlay::class)->getChallengeUserScore($challenge$user->getId());
  104.         } else 
  105.         {
  106.             $topScoresGamePlay $gamePlayRepository->getChallengeTopScores($challenge3);
  107.             if ($topScoresGamePlay) {
  108.                 foreach ($topScoresGamePlay as $gamePlay) {
  109.                     $topScore = array('user' => $gamePlay->getUser()->getUsername(),
  110.                             'gender' => $gamePlay->getUser()->getMale(),
  111.                             'score' => $gamePlay->getScore(),
  112.                             'avatar' => $gamePlay->getUser()->getAvatar(),
  113.                             'usernameCanonical' => $gamePlay->getUser()->getUsernameCanonical(),
  114.                             'male' => $gamePlay->getUser()->getMale(),
  115.                             'id' => $gamePlay->getUser()->getId());
  116.                     
  117.                     $topScores[] = $topScore;
  118.                 }
  119.             }
  120.             $userScore $this->doctrine->getRepository(GamePlay::class)->getChallengeUserScore($challenge$user->getId());
  121.         }
  122.         $nextChallenge $this->doctrine->getRepository(Popin::class)->getNextChallenge();
  123.         
  124.         $userInPodium 0;
  125.         foreach ($topScores as $topScore){
  126.             if($topScore['id'] == $user->getId())
  127.                 $userInPodium 1;
  128.         }
  129.         $response = new Response();
  130.         //The cache length depends on the day of the week
  131.         $now = new \DateTime();
  132.         $challengeToday $now->format('N') == '3' || $now->format('N') == '6' || $now->format('N') == '7' || true;
  133.         if ($challengeToday && !$challenge->isFinished())
  134.         {
  135.             $response->setCache(['no_cache' => true'max_age' => 0,  's_maxage' => 0]);
  136.         } else {
  137.             $response->setCache(['max_age' => CachedController::ONE_HOUR_CACHE*2,  's_maxage' => CachedController::ONE_HOUR_CACHE*2]);
  138.         }
  139.         return $this->render('home/challengeV3.html.twig', [
  140.             'user' => $user
  141.             'userInPodium' => $userInPodium
  142.             'challenge' => $challenge
  143.             'nextChallenge' => $nextChallenge
  144.             'topScores' => $topScores
  145.             'userScore' => $userScore
  146.         ], $response);
  147.     }
  148.     #[Route('/defi/choisis-ton-cadeau.html'name'front_challenge_goodies')]
  149.     public function showGoodies(Request $request)
  150.     {
  151.         $locale $request->get('_locale','fr');
  152.         $user $this->getUser();
  153.         $userId $user->getId();
  154.         $repository $this->doctrine->getRepository(UserChallengeReward::class);
  155.         $userReward $repository->getFirstByUser($userId);
  156.         $aGoodies = array();
  157.         
  158.         if($userReward != null){
  159.             if ($request->getMethod() == 'POST')
  160.             {
  161.                 $session $request->getSession();
  162.                 $selection $request->get('selection');
  163.                 
  164.                 $session->set('rewardId',$userReward->getId());
  165.                 $session->set('goodiesId',$selection);
  166.                 return $this->redirectToRoute('front_challenge_information');
  167.             }
  168.             $repository $this->doctrine->getRepository(Goodies::class);
  169.             $goodies $repository->getByLevel($userReward->getLevel());
  170.             
  171.             $partRepository $this->doctrine->getRepository(UserSpeakyPart::class);
  172.             foreach ($goodies as $goodie){
  173.                 if(!$goodie->isSpeakyPart() && $goodie->isAvailable($userId))
  174.                     $aGoodies[] = $goodie;
  175.                 elseif($goodie->isSpeakyPart() && $partRepository->ownPart($goodie->getPart()->getId(),$userId))
  176.                     $aGoodies[] = $goodie;
  177.             }
  178.         }
  179.         return $this->render('user/challenge/goodies.html.twig', [
  180.             'userReward' => $userReward
  181.             'goodies' => $aGoodies
  182.             'userReward' => $userReward
  183.         ]);
  184.     }
  185.     #[Route('/defi/informations.html'name'front_challenge_information')]
  186.     public function setInformation(Request $request)
  187.     {
  188.         $locale $request->get('_locale','fr');
  189.         $user $this->getUser();
  190.         $form $this->createFormBuilder($user)
  191.             ->add('firstName'TextType::class)
  192.             ->add('lastName'TextType::class)
  193.             ->add('address'TextType::class)
  194.             ->add('zipCode'TextType::class)
  195.             ->add('city'TextType::class)
  196.             ->add('ecountry'EntityType::class, ['class' => Country::class, 'choice_label' => 'canonicalName''choice_translation_domain' => 'countries'])
  197.             ->add('save'SubmitType::class, ['label' => 'Continuer'])
  198.             ->getForm();
  199.         $form->handleRequest($request);
  200.         if ($form->isSubmitted() && $form->isValid()) 
  201.         {
  202.             $user $form->getData();
  203.             
  204.             $session $request->getSession();
  205.             $rewardId $session->get('rewardId',null);
  206.             $repository $this->doctrine->getRepository(UserChallengeReward::class);
  207.             $userReward $repository->findOneBy(['user' => $user->getId(), 'id' => $rewardId'state' => UserChallengeReward::STATE_NEW]);
  208.             if($userReward) {
  209.                 $goodiesId $session->get('goodiesId',null);
  210.                 if($goodiesId != null) {
  211.                     $repository $this->doctrine->getRepository(Goodies::class);
  212.                     $goodies $repository->getGoodiesForLevel($goodiesId,$userReward->getLevel());
  213.                     if($goodies && $goodies->hasStock()) {
  214.                         $userReward->setGoodies($goodies);
  215.                         $userReward->setState(UserChallengeReward::STATE_WAITING);
  216.                         $rewardType 'real';
  217.                         $user $userReward->getUser();
  218.                         if($user) {
  219.                             switch($goodies->getType()) {
  220.                                 case Goodies::TYPE_SPEAKOS:
  221.                                     $user->addSpeakos($goodies->getSpeakos());
  222.                                     $userReward->setState(UserChallengeReward::STATE_SENT);
  223.                                     $rewardType 'virtual';
  224.                                 break;
  225.                                 case Goodies::TYPE_SPPART:
  226.                                     $gPart $goodies->getPart();
  227.                                     if($gPart != null){
  228.                                         $repository $this->doctrine->getRepository(SpeakyPart::class);
  229.                                         $part $repository->getUnownedPart($gPart->getId(),$user->getId());
  230.                                         if($part != null && ($part['part_id'] == null || $part['owned'] != 1)){
  231.                                             if($part['part_id'] != null){
  232.                                                 $this->doctrine->getRepository(UserSpeakyPart::class)->acquirePart($gPart->getId(),$user->getId());
  233.                                             }else{
  234.                                                 $upart = new UserSpeakyPart();
  235.                                                 $upart->setUser($user);
  236.                                                 $upart->setPart($gPart);
  237.                                                 $upart->setOwned(1);
  238.                                                 $this->entityManager->persist($upart);
  239.                                             }
  240.                                         }
  241.                                     }
  242.                                     $userReward->setState(UserChallengeReward::STATE_SENT);
  243.                                     $rewardType 'virtual';
  244.                                 break;
  245.                             }
  246.                         }
  247.                         $this->entityManager->persist($userReward);
  248.                         $goodies->removeStock();
  249.                         $this->entityManager->persist($goodies);
  250.                         $session->remove('rewardId');
  251.                         
  252.                         $this->entityManager->flush();
  253.                         return $this->redirectToRoute('front_challenge_confirm');
  254.                     }
  255.                 }
  256.             }
  257.         }
  258.         return $this->renderForm('user/challenge/shipping_information.html.twig', [
  259.             'form' => $form,
  260.         ]);
  261.     }
  262.     #[Route('/defi/confirmation.html'name'front_challenge_confirm')]
  263.     public function showConfirmation(Request $request)
  264.     {
  265.         $locale $request->get('_locale','fr');
  266.         $user $this->getUser();
  267.         $session $request->getSession();
  268.         $goodiesId $session->get('goodiesId',null);
  269.         $rewardType Goodies::TYPE_REAL;
  270.         if($goodiesId != null){
  271.             $session->remove('goodiesId');
  272.             $session->remove('option');
  273.             $repository $this->doctrine->getRepository(Goodies::class);
  274.             $goodies $repository->findOneById($goodiesId);
  275.             if($goodies != null)
  276.                 $rewardType $goodies->getType();
  277.                 
  278.             return $this->render('user/challenge/confirmation.html.twig', ['goodies' => $goodies'rewardType' => $rewardType]);
  279.         } else {
  280.             return $this->redirectToRoute('front_challenge_goodies');
  281.         }
  282.     }
  283.     
  284. }