<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Controller;
use App\Entity\AudioFile;
use App\Entity\Candidate;
use App\Entity\Commune;
use App\Entity\Falc;
use App\Entity\Motivation;
use App\Entity\Region;
use App\Entity\Theme;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Controller used to manage all the menu in the site.
*/
class DefaultController extends AbstractController
{
#[Route('/', methods: ['GET'], name: 'intro')]
public function indexIntro(): Response
{
return $this->render('front/default/intro.html.twig');
}
#[Route('/search/{election_type}', methods: ['GET'], name: 'homepage')]
public function search(Request $request, EntityManagerInterface $entityManager): Response
{
$election_type = $request->get('election_type');
$regions = $entityManager->getRepository(Region::class)->findAll();
return $this->render('front/default/homepage.html.twig',[
'regions' => $regions,
'election_type' => $election_type
]);
}
#[Route('/cam', methods: ['GET'], name: 'cam_home')]
public function showCamera(Request $request): Response
{
return $this->render('front/default/cam_page.html.twig');
}
#[Route('/front/regions/{election_type}/candidates/{region}', methods: ['GET'], name: 'app_region_candidates')]
public function candidatesOfRegion(
Request $request,
EntityManagerInterface $entityManager
): Response
{
$candidates = null;
$communes = null;
$region_id = $request->get('region');
$election_type = $request->get('election_type');
$regions = $entityManager->getRepository(Region::class)->findAll();
if ($election_type == 'MUNICIPALE'){
$current_region = $entityManager->getRepository(Region::class)->find($region_id);
return $this->redirectToRoute('app_commune_candidates',
array(
'commune' => 2,
'region' => $current_region->getId()
));
}elseif ($election_type == 'REGIONALE'){
$current_region = $entityManager->getRepository(Region::class)->find($region_id);
}else{
throw $this->createNotFoundException('Aucune circonscription de ce id existe');
}
if($current_region instanceof Region) {
$candidates = $entityManager->getRepository(Candidate::class)->findBy(array('region'=>$current_region));
}
if($current_region instanceof Commune) {
$candidates = $entityManager->getRepository(Candidate::class)->findBy(array('commune'=>$current_region));
}
return $this->render('front/candidate/list.html.twig',[
'candidates' => $candidates,
'election_type' => $election_type,
'communes' => $communes,
'current_region' => $current_region,
'regions' => $regions
]);
}
#[Route('/front/commune/{region}/candidates/{commune}', methods: ['GET'], name: 'app_commune_candidates')]
public function candidatesOfCommune(
Request $request,
EntityManagerInterface $entityManager
): Response
{
if ($request->get('region')){
$current_region = $entityManager->getRepository(Region::class)->find($request->get('region'));
}
$communes = $entityManager->getRepository(Commune::class)->findBy(array('region'=>$request->get('region')));
if ($request->get('commune')){
$current_commune = $entityManager->getRepository(Commune::class)->find($request->get('commune'));
}
if ($current_commune!=null and $current_commune instanceof Commune){
$candidates = $entityManager->getRepository(Candidate::class)->findBy(array('commune'=>$current_commune));
}
return $this->render('front/candidate/list.html.twig',[
'candidates' => $candidates,
'communes' => $communes,
'current_commune' => $current_commune,
'current_region' => $current_region
]);
}
#[Route('front/motivation/{id}', name: 'app_front_motivation_show', methods: ['GET'])]
public function showMotivation(EntityManagerInterface $entityManager, Motivation $motivation): Response
{
if (empty($motivation)){
throw $this->createNotFoundException('La profession de foi est inexistante');
}
$user = $this->getCurrentUser($entityManager, $motivation);
if ($user instanceof User){
$candidate = $user->getCandidate();
}
$audioFiles = $entityManager->getRepository(AudioFile::class)->findBy(array('motivation'=>$candidate->getMotivation()));
$profilePicFileName = $candidate->getMotivation()->getProfilePicFileName();
return $this->render('front/motivation/show.html.twig', [
'user' => $user,
'candidate' => $candidate,
'audioFiles' => $audioFiles,
'profilePicFileName' => $profilePicFileName
]);
}
#[Route('front/theme/{id}', name: 'app_front_theme_show', methods: ['GET'])]
public function showTheme(Theme $theme,
EntityManagerInterface $entityManager): Response
{
$falc = $theme->getFalc();
if ($falc){
$candidate = $entityManager->getRepository(Candidate::class)->findOneBy(array('falc'=>$falc));
}
$user = $this->getCurrentUser($entityManager, $theme);
if ($user instanceof User){
$candidate = $user->getCandidate();
}
$audioFiles = $entityManager->getRepository(AudioFile::class)->findBy(array('theme'=>$theme));
$themePicFileName = $theme->getThemePicFileName();
return $this->render('front/theme/show.html.twig', [
'user' => $user,
'candidate' => $candidate,
'theme' => $theme,
'audioFiles' => $audioFiles,
'themePicFileName' => $themePicFileName
]);
}
#[Route('/front/{id}/falc', methods: ['GET'], name: 'app_fiche_falc_show')]
public function getFfalc(Falc $falc,
EntityManagerInterface $entityManager): Response
{
$candidate = $entityManager->getRepository(Candidate::class)->findOneBy(array('falc' => $falc));
$themes = $entityManager->getRepository(Theme::class)->findBy(array('falc' => $falc));
$arrData = array();
if (!empty($themes)){
foreach ($themes as $theme) {
$audioFiles = $entityManager->getRepository(AudioFile::class)->findBy(array('theme'=>$theme));
//$themePicFileName = $theme->getThemePicFileName();
$arrData[] = array('theme'=>$theme, 'audioFiles'=>$audioFiles, 'videoFiles'=>null);
}
}
return $this->render('front/falc/show.html.twig',[
'falc' => $falc,
'arrData' => $arrData,
'candidate' =>$candidate
]);
}
public function getCurrentUser(EntityManagerInterface $entityManager, $object_type)
{
if ($object_type instanceof Motivation){
$candidate = $entityManager->getRepository(Candidate::class)->findOneBy(array('motivation'=>$object_type));
}
if ($object_type instanceof Theme){
$falc = $object_type->getFalc();
$candidate = $entityManager->getRepository(Candidate::class)->findOneBy(array('falc'=>$falc));
}
if (!empty($candidate)){
$user = $entityManager->getRepository(User::class)->findOneBy(array('candidate'=>$candidate));
}
return $user;
}
}