src/Controller/ThemeController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use App\Entity\ArticleType;
  7. use App\Entity\Article;
  8. use App\Entity\Event;
  9. use App\Entity\QuestionAnswer;
  10. use App\Entity\Structure;
  11. use App\Entity\Tag;
  12. use App\Entity\Theme;
  13. use App\Form\RechercheType;
  14. class ThemeController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/themes", name="themeList")
  18.      */
  19.     public function themeList(Request $request)
  20.     {
  21.         $themeRepository $this->getDoctrine()->getRepository(Theme::class);
  22.         $themes $themeRepository->findAllOrdered();
  23.         $searchForm $this->createForm(RechercheType::class); // cf. https://symfony.com/doc/current/form/without_class.html
  24.         $searchForm->handleRequest($request);
  25.         if ($searchForm->isSubmitted() && $searchForm->isValid()) {
  26.             $term $searchForm->getData()["term"];
  27.             // $term = $request->request->get('recherche')["term"]; // fonctionne aussi
  28.             // dump($searchForm->getData()); dump($request->request->get('recherche')["term"]); exit;
  29.             return $this->redirect($this->generateUrl('showTagBySlug', ['slug' => $term]));
  30.         }
  31.         return $this->render('theme/list.html.twig', [
  32.             'themes' => $themes,
  33.             'searchForm' => $searchForm->createView(),
  34.         ]);
  35.     }
  36.     /**
  37.      * @Route("/themes/show", name="showThemes")
  38.      * Pour obtenir la liste des themes et l'afficher
  39.      */
  40.     public function showThemes()
  41.     {
  42.         $themeRepository $this->getDoctrine()->getRepository(Theme::class);
  43.         $themes $themeRepository->findAllOrdered();
  44.         return $this->render('theme/showThemes.html.twig', [
  45.             'themes' => $themes,
  46.         ]);
  47.     }
  48.     
  49.     /**
  50.      * @Route("/theme/{id}", name="showTheme", requirements={"id"="\d+"})
  51.      */
  52.     public function showTheme($id)
  53.     {
  54.         $themeRepository $this->getDoctrine()->getRepository(Theme::class);
  55.         $theme $themeRepository->find($id);
  56.         if (!$theme) {
  57.             throw $this->createNotFoundException('Pas de thème avec l\'id ' $id '.');
  58.         }
  59.         $slug $theme->getSlug();
  60.         if($slug && $slug !== "") {
  61.             return $this->redirect($this->generateUrl('showThemeBySlug', ['slug' => $slug]));
  62.         } else {
  63.             $tags = [];
  64.             $articles = [];
  65.             $events = [];
  66.             $questionAnswers = [];
  67.             $structures = [];
  68.             $allThemes $themeRepository->findAllOrdered();
  69.             
  70.             return $this->render('theme/show.html.twig', [
  71.                 'slug' => $slug,
  72.                 'theme' => $theme,
  73.                 'tags' => $tags,
  74.                 'articles' => $articles,
  75.                 'events' => $events,
  76.                 'questionAnswers' => $questionAnswers,
  77.                 'structures' => $structures,
  78.                 'allThemes' => $allThemes,
  79.                 
  80.             ]);
  81.         }
  82.     }
  83.     /**
  84.      * @Route("/theme/{slug}", name="showThemeBySlug")
  85.      * @param bool $listWithThumbnails si on veut afficher une liste d'images, sinon liste textuelle 
  86.      */
  87.     public function showThemeBySlug($slug$listWithThumbnails true)
  88.     {
  89.         $articleRepository $this->getDoctrine()->getRepository(Article::class);
  90.         $eventRepository $this->getDoctrine()->getRepository(Event::class);
  91.         $questionAnswerRepository $this->getDoctrine()->getRepository(QuestionAnswer::class);
  92.         $structureRepository $this->getDoctrine()->getRepository(Structure::class);
  93.         $themeRepository $this->getDoctrine()->getRepository(Theme::class);
  94.         $theme $themeRepository->findOneBySlug($slug);
  95.         if ($theme) {
  96.             $tags $theme->getTags();
  97.             $articles = [];
  98.             // foreach ($tags as $tag) {
  99.             //     $articlesByTag = $articleRepository->findAllByTag($tag);
  100.             //     foreach ($articlesByTag as $article) {
  101.             //         $key = sprintf('%08d', $article->getPriority()) . "_" . substr($article->getName(), 0, 20) . "_" . sprintf('%08d', $article->getId()); // pour ksort par la suite
  102.             //         if( ! isset($articles[$key]) ) $articles[$key] = $article;
  103.             //     }
  104.             // }
  105.             $events = [];
  106.             // foreach ($tags as $tag) {
  107.             //     $eventsByTag = $eventRepository->findAllByTag($tag);
  108.             //     foreach ($eventsByTag as $event) {
  109.             //         $key = sprintf('%08d', $event->getPriority()) . "_" . substr($event->getName(), 0, 20) . "_" . sprintf('%08d', $event->getId()); // pour ksort par la suite
  110.             //         if( ! isset($events[$key]) ) $events[$key] = $event;
  111.             // }
  112.             $questionAnswers = [];
  113.             foreach ($tags as $tag) {
  114.                 $questionAnswersByTag $questionAnswerRepository->findAllByTag($tag);
  115.                 foreach ($questionAnswersByTag as $questionAnswer) {
  116.                     $key sprintf('%08d'$questionAnswer->getPriority()) . "_" substr($questionAnswer->getName(), 020) . "_" sprintf('%08d'$questionAnswer->getId()); // pour ksort par la suite
  117.                     if( ! isset($questionAnswers[$key]) ) $questionAnswers[$key] = $questionAnswer;
  118.                 }
  119.             }
  120.             ksort($questionAnswers);
  121.             $structures = [];
  122.             foreach ($tags as $tag) {
  123.                 $structuresByTag $structureRepository->findAllByTag($tag);
  124.                 foreach ($structuresByTag as $structure) {
  125.                     $key sprintf('%08d'$structure->getPriority()) . "_" substr($structure->getName(), 020) . "_" sprintf('%08d'$structure->getId()); // pour ksort par la suite
  126.                     if( ! isset($structures[$key]) ) $structures[$key] = $structure;
  127.                 }
  128.             }
  129.             ksort($structures);
  130.             // dump($structures); exit;
  131.         } else {
  132.             // throw $this->createNotFoundException('Pas de theme avec ce slug : ' . $slug . '.');
  133.             $theme null;
  134.             $tags = [];
  135.             $articles = [];
  136.             $events = [];
  137.             $questionAnswers = [];
  138.             $structures = [];
  139.         }
  140.         $allThemes $themeRepository->findAllOrdered();
  141.         return $this->render('theme/show.html.twig', [
  142.             'listWithThumbnails' => $listWithThumbnails,
  143.             'slug' => $slug,
  144.             'theme' => $theme,
  145.             'tags' => $tags,
  146.             'articles' => $articles,
  147.             'events' => $events,
  148.             'questionAnswers' => $questionAnswers,
  149.             'structures' => $structures,
  150.             'allThemes' => $allThemes,
  151.         ]);
  152.     }
  153. }