src/Controller/HomeController.php line 44
<?php
namespace App\Controller;
use App\Entity\Country;
use App\Entity\GamePlay;
use App\Entity\Goodies;
use App\Entity\Page;
use App\Entity\Popin;
use App\Entity\SpeakyPart;
use App\Entity\User;
use App\Entity\UserChallengeReward;
use App\Entity\UserSpeakyPart;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/{_locale}')]
class HomeController extends AbstractController
{
private $doctrine;
private $entityManager;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
$this->entityManager = $this->doctrine->getManager();
}
#[Route('/', name: 'homepage', priority: -1)]
public function index(): Response
{
return $this->render('home/homepage.html.twig', [
'controller_name' => 'HomeController',
]);
}
#[Route('/home_header', name: 'home_header')]
public function homeHeader(Request $request)
{
$user = $this->getUser();
if ((!$user instanceof User))
{
return $this->render('home/header_guest.html.twig', []);
} else
{
return $this->render('home/header_user.html.twig', ['user' => $user]);
}
}
#[Route('/home_footer', name: 'home_footer')]
public function homeFooter(Request $request)
{
$locale = $request->get('_locale','fr');
$user = $this->getUser();
$pageRepository = $this->doctrine->getRepository(Page::class);
$cgv = $pageRepository->getI18nPageByRoute('cgv',$locale);
$rgpd = $pageRepository->getI18nPageByRoute('confidentialite',$locale);
return $this->render('home/footer.html.twig', ['cgv' => $cgv, 'rgpd' => $rgpd]);
}
public function indexV3Challenge($new = false): Response
{
$challenge = $this->doctrine->getRepository(Popin::class)->getLastChallenge();
if($challenge == null) {
return new Response();
}
$nextChallenge = null;
$gamePlayRepository = $this->doctrine->getRepository(GamePlay::class);
/** @var User $user */
$user = $this->getUser();
//on affiche les résultats que si le cron est passé
$topScores = array();
if ($challenge->isFinished() && $challenge->getIsRewardSent())
{
if ((!$challenge->getWinner() && !$challenge->getSecond() && !$challenge->getThird()))
{
//TODO: move all this in a repository (something like 'closeChallenge')
/*$topScoresGamePlay = $gamePlayRepository->getChallengeTopScores($challenge);
if ($topScoresGamePlay)
{
if (count($topScoresGamePlay) > 0)
$challenge->setWinner($topScoresGamePlay[0]);
if (count($topScoresGamePlay) > 1)
$challenge->setSecond($topScoresGamePlay[1]);
if (count($topScoresGamePlay) > 2)
$challenge->setThird($topScoresGamePlay[2]);
//Now prepare the participation popin
$challengeParticipation = new Popin();
$challengeParticipation->setType(Popin::TYPE_CHALLENGE_PARTICIPATION);
$challengeParticipation->setChallenge($challenge);
for ($i = 3; $i < count($topScoresGamePlay); $i++) {
$gamePlay = $topScoresGamePlay[$i];
$challengeParticipation->addPopinTarget($gamePlay->getUser());
}
$em = $this->entityManager;
$em->persist($challengeParticipation);
$em->flush();
}*/
}
$topScores = $challenge->getTopScores();
$userScore = $this->doctrine->getRepository(GamePlay::class)->getChallengeUserScore($challenge, $user->getId());
} else
{
$topScoresGamePlay = $gamePlayRepository->getChallengeTopScores($challenge, 3);
if ($topScoresGamePlay) {
foreach ($topScoresGamePlay as $gamePlay) {
$topScore = array('user' => $gamePlay->getUser()->getUsername(),
'gender' => $gamePlay->getUser()->getMale(),
'score' => $gamePlay->getScore(),
'avatar' => $gamePlay->getUser()->getAvatar(),
'usernameCanonical' => $gamePlay->getUser()->getUsernameCanonical(),
'male' => $gamePlay->getUser()->getMale(),
'id' => $gamePlay->getUser()->getId());
$topScores[] = $topScore;
}
}
$userScore = $this->doctrine->getRepository(GamePlay::class)->getChallengeUserScore($challenge, $user->getId());
}
$nextChallenge = $this->doctrine->getRepository(Popin::class)->getNextChallenge();
$userInPodium = 0;
foreach ($topScores as $topScore){
if($topScore['id'] == $user->getId())
$userInPodium = 1;
}
$response = new Response();
//The cache length depends on the day of the week
$now = new \DateTime();
$challengeToday = $now->format('N') == '3' || $now->format('N') == '6' || $now->format('N') == '7' || true;
if ($challengeToday && !$challenge->isFinished())
{
$response->setCache(['no_cache' => true, 'max_age' => 0, 's_maxage' => 0]);
} else {
$response->setCache(['max_age' => CachedController::ONE_HOUR_CACHE*2, 's_maxage' => CachedController::ONE_HOUR_CACHE*2]);
}
return $this->render('home/challengeV3.html.twig', [
'user' => $user,
'userInPodium' => $userInPodium,
'challenge' => $challenge,
'nextChallenge' => $nextChallenge,
'topScores' => $topScores,
'userScore' => $userScore
], $response);
}
#[Route('/defi/choisis-ton-cadeau.html', name: 'front_challenge_goodies')]
public function showGoodies(Request $request)
{
$locale = $request->get('_locale','fr');
$user = $this->getUser();
$userId = $user->getId();
$repository = $this->doctrine->getRepository(UserChallengeReward::class);
$userReward = $repository->getFirstByUser($userId);
$aGoodies = array();
if($userReward != null){
if ($request->getMethod() == 'POST')
{
$session = $request->getSession();
$selection = $request->get('selection');
$session->set('rewardId',$userReward->getId());
$session->set('goodiesId',$selection);
return $this->redirectToRoute('front_challenge_information');
}
$repository = $this->doctrine->getRepository(Goodies::class);
$goodies = $repository->getByLevel($userReward->getLevel());
$partRepository = $this->doctrine->getRepository(UserSpeakyPart::class);
foreach ($goodies as $goodie){
if(!$goodie->isSpeakyPart() && $goodie->isAvailable($userId))
$aGoodies[] = $goodie;
elseif($goodie->isSpeakyPart() && $partRepository->ownPart($goodie->getPart()->getId(),$userId))
$aGoodies[] = $goodie;
}
}
return $this->render('user/challenge/goodies.html.twig', [
'userReward' => $userReward,
'goodies' => $aGoodies,
'userReward' => $userReward
]);
}
#[Route('/defi/informations.html', name: 'front_challenge_information')]
public function setInformation(Request $request)
{
$locale = $request->get('_locale','fr');
$user = $this->getUser();
$form = $this->createFormBuilder($user)
->add('firstName', TextType::class)
->add('lastName', TextType::class)
->add('address', TextType::class)
->add('zipCode', TextType::class)
->add('city', TextType::class)
->add('ecountry', EntityType::class, ['class' => Country::class, 'choice_label' => 'canonicalName', 'choice_translation_domain' => 'countries'])
->add('save', SubmitType::class, ['label' => 'Continuer'])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$user = $form->getData();
$session = $request->getSession();
$rewardId = $session->get('rewardId',null);
$repository = $this->doctrine->getRepository(UserChallengeReward::class);
$userReward = $repository->findOneBy(['user' => $user->getId(), 'id' => $rewardId, 'state' => UserChallengeReward::STATE_NEW]);
if($userReward) {
$goodiesId = $session->get('goodiesId',null);
if($goodiesId != null) {
$repository = $this->doctrine->getRepository(Goodies::class);
$goodies = $repository->getGoodiesForLevel($goodiesId,$userReward->getLevel());
if($goodies && $goodies->hasStock()) {
$userReward->setGoodies($goodies);
$userReward->setState(UserChallengeReward::STATE_WAITING);
$rewardType = 'real';
$user = $userReward->getUser();
if($user) {
switch($goodies->getType()) {
case Goodies::TYPE_SPEAKOS:
$user->addSpeakos($goodies->getSpeakos());
$userReward->setState(UserChallengeReward::STATE_SENT);
$rewardType = 'virtual';
break;
case Goodies::TYPE_SPPART:
$gPart = $goodies->getPart();
if($gPart != null){
$repository = $this->doctrine->getRepository(SpeakyPart::class);
$part = $repository->getUnownedPart($gPart->getId(),$user->getId());
if($part != null && ($part['part_id'] == null || $part['owned'] != 1)){
if($part['part_id'] != null){
$this->doctrine->getRepository(UserSpeakyPart::class)->acquirePart($gPart->getId(),$user->getId());
}else{
$upart = new UserSpeakyPart();
$upart->setUser($user);
$upart->setPart($gPart);
$upart->setOwned(1);
$this->entityManager->persist($upart);
}
}
}
$userReward->setState(UserChallengeReward::STATE_SENT);
$rewardType = 'virtual';
break;
}
}
$this->entityManager->persist($userReward);
$goodies->removeStock();
$this->entityManager->persist($goodies);
$session->remove('rewardId');
$this->entityManager->flush();
return $this->redirectToRoute('front_challenge_confirm');
}
}
}
}
return $this->renderForm('user/challenge/shipping_information.html.twig', [
'form' => $form,
]);
}
#[Route('/defi/confirmation.html', name: 'front_challenge_confirm')]
public function showConfirmation(Request $request)
{
$locale = $request->get('_locale','fr');
$user = $this->getUser();
$session = $request->getSession();
$goodiesId = $session->get('goodiesId',null);
$rewardType = Goodies::TYPE_REAL;
if($goodiesId != null){
$session->remove('goodiesId');
$session->remove('option');
$repository = $this->doctrine->getRepository(Goodies::class);
$goodies = $repository->findOneById($goodiesId);
if($goodies != null)
$rewardType = $goodies->getType();
return $this->render('user/challenge/confirmation.html.twig', ['goodies' => $goodies, 'rewardType' => $rewardType]);
} else {
return $this->redirectToRoute('front_challenge_goodies');
}
}
}