<?php
namespace App\Controller;
use App\Entity\BFChallenge;
use App\Entity\BFChallengeEdition;
use App\Entity\BFFestival;
use App\Entity\BFEdition;
use App\Entity\BFDescription;
use App\Entity\BFMailTournoi;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use App\Form\Type\BFEditionType;
use App\Form\Type\BFDescriptionEditionImagesType;
use App\Form\Type\BFMailTournoiType;
class BFChallengeEditionController extends AbstractController
{
/**
* @Route("challenge/edition-{id}/delete", name="bfchallengeedition_delete")
*/
public function DeleteEdition(int $id, Request $request)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$edition = $this->getDoctrine()
->getRepository(BFChallengeEdition::class)
->find($id);
$challenge=$edition->getChallenge();
$this->denyAccessUnlessGranted('delete', $challenge);
$challengeid = $edition->getChallenge()->getId();
if($edition!=null)
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($edition);
$entityManager->flush();
$this->addFlash('secondary', 'L\'édition du challenge est supprimée');
return $this->redirectToRoute('admin_challenge',[
'idchallenge'=>$challengeid,
]);
}
return $this->redirectToRoute('admin');
}
/**
* @Route("challenge/edition-{id}/unactive", name="bfchallengeedition_unactive")
*/
public function UnactiveEdition(int $id, Request $request)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
return $this->ChangeActiveEdition($id, false, false, $request);
}
/**
* @Route("challenge/edition-{id}/active", name="bfchallengeedition_active")
*/
public function ActiveEdition(int $id, Request $request)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
return $this->ChangeActiveEdition($id, true, false, $request);
}
public function ChangeActiveEdition(int $id, bool $isactive, bool $fromadmin, Request $request)
{
$edition = $this->getDoctrine()
->getRepository(BFChallengeEdition::class)
->find($id);
$challenge=$edition->getChallenge();
$this->denyAccessUnlessGranted('edit', $challenge);
//check option for activate more editions at the same time => there is no restriction on challenges
$activecount = $challenge->getCountActive();
if($activecount>0 && $isactive==true )
{
//because we try to add one
$activecount++;
if(!$this->isGranted('option_editionactive_challenge_'.$activecount,$challenge))
{
$this->addFlash('danger', 'Vous ne pouvez pas activer plus d\'éditions, consultez les offres');
if($fromadmin==true)
{
return $this->redirectToRoute('admin_challenge_edition',[
'idedition'=>$edition->getId(),
]);
}
else
{
return $this->redirectToRoute('admin_challenge',[
'idchallenge'=>$challenge->getId(),
]);
}
}
}
if($edition!=null)
{
$edition->setIsactive($isactive);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($edition);
$entityManager->flush();
if($isactive)
{
$this->addFlash('success', 'L\'édition a été activée');
}
else
{
$this->addFlash('warning', 'L\'édition a été archivée');
}
}
return $this->redirectToRoute('admin_challenge',[
'idchallenge'=>$challenge->getId(),
]);
}
public function ShowBFChallengeEditionsTable(int $challengeid)
{
$challenge = $this->getDoctrine()
->getRepository(BFChallenge::class)
->find($challengeid);
$this->denyAccessUnlessGranted('edit', $challenge);
$editions = $challenge->getEditions();
$activeeditions = array();
foreach ($editions as $edition )
{
if($edition->getIsactive()==true)
{
array_push($activeeditions, $edition);
}
}
return $this->render('challenges/adminchallengeeditionstable.html.twig', [
'bfchallengeeditions' => $activeeditions,
'bfchallenge' => $challenge,
]);
}
public function ShowBFChallengeEditionsHome()
{
$challengeeditions = array();
//If user, we get the subscriptions
if($this->isGranted('IS_AUTHENTICATED_FULLY'))
{
$user = $this->getUser();
$challengesubscriptions = $this->getDoctrine()->getRepository(BFChallengeEdition::class)->findNearestSubscriptions($user, $isactive=true, 6);
if(count($challengesubscriptions)>0)
$challengeeditions=array_merge($challengeeditions,$challengesubscriptions);
}
if(count($challengeeditions)<6)
{
$nearestchallengeedition = $this->getDoctrine()->getRepository(BFChallengeEdition::class)->findNearestIsactive(true, 6-count($challengeeditions));
$challengeeditions=array_merge($challengeeditions,$nearestchallengeedition);
}
//Be sure that there is only 6
//$challengeeditionssix = array_slice($challengeeditions,0,6);
return $this->render('challenges/homechallengestrending.html.twig', [
'bfchallengeeditions' => $challengeeditions,
]);
}
public function ShowBFChallengEdtionImageCard(int $idchallengeedition)
{
$bfchallengeedition = $this->getDoctrine()->getRepository(BFChallengeEdition::class)->find($idchallengeedition);
if($bfchallengeedition==null)
throw $this->createNotFoundException();
return $this->render('challenges/challengeeditionimagecard.html.twig', [
'bfchallengeedition' => $bfchallengeedition,
]);
}
public function ShowUnactiveBFEditionsTable(int $challengeid)
{
$challenge = $this->getDoctrine()
->getRepository(BFChallenge::class)
->find($challengeid);
$this->denyAccessUnlessGranted('edit', $challenge);
$editions = $challenge->getEditions();
$activeeditions = array();
foreach ($editions as $edition )
{
if($edition->getIsactive()==false)
{
array_push($activeeditions, $edition);
}
}
return $this->render('challenges/adminchallengeeditionstableunactive.html.twig', [
'bfchallengeeditions' => $activeeditions,
'bfchallenge' => $challenge,
]);
}
public function ShowBreadcrumb(int $id, $iscreate=false)
{
if($iscreate)
{
$bfchallenge=$this->getDoctrine()
->getRepository(BFChallenge::class)
->find($id);
$bfedition= new BFChallengeEdition();
}
else
{
$bfedition = $this->getDoctrine()
->getRepository(BFChallengeEdition::class)
->find($id);
$bfchallenge= $bfedition->getChallenge();
}
return $this->render('breadcrumb/challengeeditionbreadcrumb.html.twig', [
'bfchallengeedition' => $bfedition,
'bfchallenge' => $bfchallenge,
'iscreate' => $iscreate,
]);
}
public function ShowActiveButton(int $id)
{
$bfchallengeedition = $this->getDoctrine()
->getRepository(BFChallengeEdition::class)
->find($id);
return $this->render('challenges/adminchallengeeditionactivationbutton.html.twig', [
'bfchallengeedition' => $bfchallengeedition,
]);
}
public function ShowSmallView(int $id)
{
$bfchallengeedition = $this->getDoctrine()->getRepository(BFChallengeEdition::class)->find($id);
if($bfchallengeedition==null) return null;
return $this->render('challenges/challengeeditionsmallview.html.twig', [
'bfchallengeedition' => $bfchallengeedition,
]);
}
}
?>