<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\ArticleType;
use App\Entity\Article;
use App\Entity\Event;
use App\Entity\QuestionAnswer;
use App\Entity\Structure;
use App\Entity\Tag;
use App\Entity\Theme;
use App\Form\RechercheType;
class ThemeController extends AbstractController
{
/**
* @Route("/themes", name="themeList")
*/
public function themeList(Request $request)
{
$themeRepository = $this->getDoctrine()->getRepository(Theme::class);
$themes = $themeRepository->findAllOrdered();
$searchForm = $this->createForm(RechercheType::class); // cf. https://symfony.com/doc/current/form/without_class.html
$searchForm->handleRequest($request);
if ($searchForm->isSubmitted() && $searchForm->isValid()) {
$term = $searchForm->getData()["term"];
// $term = $request->request->get('recherche')["term"]; // fonctionne aussi
// dump($searchForm->getData()); dump($request->request->get('recherche')["term"]); exit;
return $this->redirect($this->generateUrl('showTagBySlug', ['slug' => $term]));
}
return $this->render('theme/list.html.twig', [
'themes' => $themes,
'searchForm' => $searchForm->createView(),
]);
}
/**
* @Route("/themes/show", name="showThemes")
* Pour obtenir la liste des themes et l'afficher
*/
public function showThemes()
{
$themeRepository = $this->getDoctrine()->getRepository(Theme::class);
$themes = $themeRepository->findAllOrdered();
return $this->render('theme/showThemes.html.twig', [
'themes' => $themes,
]);
}
/**
* @Route("/theme/{id}", name="showTheme", requirements={"id"="\d+"})
*/
public function showTheme($id)
{
$themeRepository = $this->getDoctrine()->getRepository(Theme::class);
$theme = $themeRepository->find($id);
if (!$theme) {
throw $this->createNotFoundException('Pas de thème avec l\'id ' . $id . '.');
}
$slug = $theme->getSlug();
if($slug && $slug !== "") {
return $this->redirect($this->generateUrl('showThemeBySlug', ['slug' => $slug]));
} else {
$tags = [];
$articles = [];
$events = [];
$questionAnswers = [];
$structures = [];
$allThemes = $themeRepository->findAllOrdered();
return $this->render('theme/show.html.twig', [
'slug' => $slug,
'theme' => $theme,
'tags' => $tags,
'articles' => $articles,
'events' => $events,
'questionAnswers' => $questionAnswers,
'structures' => $structures,
'allThemes' => $allThemes,
]);
}
}
/**
* @Route("/theme/{slug}", name="showThemeBySlug")
* @param bool $listWithThumbnails si on veut afficher une liste d'images, sinon liste textuelle
*/
public function showThemeBySlug($slug, $listWithThumbnails = true)
{
$articleRepository = $this->getDoctrine()->getRepository(Article::class);
$eventRepository = $this->getDoctrine()->getRepository(Event::class);
$questionAnswerRepository = $this->getDoctrine()->getRepository(QuestionAnswer::class);
$structureRepository = $this->getDoctrine()->getRepository(Structure::class);
$themeRepository = $this->getDoctrine()->getRepository(Theme::class);
$theme = $themeRepository->findOneBySlug($slug);
if ($theme) {
$tags = $theme->getTags();
$articles = [];
// foreach ($tags as $tag) {
// $articlesByTag = $articleRepository->findAllByTag($tag);
// foreach ($articlesByTag as $article) {
// $key = sprintf('%08d', $article->getPriority()) . "_" . substr($article->getName(), 0, 20) . "_" . sprintf('%08d', $article->getId()); // pour ksort par la suite
// if( ! isset($articles[$key]) ) $articles[$key] = $article;
// }
// }
$events = [];
// foreach ($tags as $tag) {
// $eventsByTag = $eventRepository->findAllByTag($tag);
// foreach ($eventsByTag as $event) {
// $key = sprintf('%08d', $event->getPriority()) . "_" . substr($event->getName(), 0, 20) . "_" . sprintf('%08d', $event->getId()); // pour ksort par la suite
// if( ! isset($events[$key]) ) $events[$key] = $event;
// }
$questionAnswers = [];
foreach ($tags as $tag) {
$questionAnswersByTag = $questionAnswerRepository->findAllByTag($tag);
foreach ($questionAnswersByTag as $questionAnswer) {
$key = sprintf('%08d', $questionAnswer->getPriority()) . "_" . substr($questionAnswer->getName(), 0, 20) . "_" . sprintf('%08d', $questionAnswer->getId()); // pour ksort par la suite
if( ! isset($questionAnswers[$key]) ) $questionAnswers[$key] = $questionAnswer;
}
}
ksort($questionAnswers);
$structures = [];
foreach ($tags as $tag) {
$structuresByTag = $structureRepository->findAllByTag($tag);
foreach ($structuresByTag as $structure) {
$key = sprintf('%08d', $structure->getPriority()) . "_" . substr($structure->getName(), 0, 20) . "_" . sprintf('%08d', $structure->getId()); // pour ksort par la suite
if( ! isset($structures[$key]) ) $structures[$key] = $structure;
}
}
ksort($structures);
// dump($structures); exit;
} else {
// throw $this->createNotFoundException('Pas de theme avec ce slug : ' . $slug . '.');
$theme = null;
$tags = [];
$articles = [];
$events = [];
$questionAnswers = [];
$structures = [];
}
$allThemes = $themeRepository->findAllOrdered();
return $this->render('theme/show.html.twig', [
'listWithThumbnails' => $listWithThumbnails,
'slug' => $slug,
'theme' => $theme,
'tags' => $tags,
'articles' => $articles,
'events' => $events,
'questionAnswers' => $questionAnswers,
'structures' => $structures,
'allThemes' => $allThemes,
]);
}
}