src/Controller/KeywordsController.php line 22

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Keywords;
  4. use App\Repository\KeywordsRepository;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class KeywordsController extends AbstractController
  11. {
  12.     public function __construct(private KeywordsRepository $keywordsRepository)
  13.     {
  14.     }
  15.     #[\Symfony\Component\Routing\Attribute\Route(path'/keyword'name'keyword')]
  16.     public function keywordsRead(Request $request)
  17.     {
  18.         $data = [];
  19.         $category $request->query->get('categorie');
  20.         $keywordId $request->query->get('keyword');
  21.     
  22.         $keyword $this->keywordsRepository->find((int) $keywordId);
  23.         if (null === $keyword) {
  24.             throw new NotFoundHttpException();
  25.         }
  26.         if (null === $category || 'infos' === $category) {
  27.             // Get news
  28.             $news $keyword->getMentionsNews();
  29.             /** @var News $new */
  30.             foreach ($news as $new) {
  31.                 if ($new->isActive() === true) {
  32.                     $newsData = [
  33.                         'title' => $new->getTitle(),
  34.                         'content' => $this->cleanContent($new->getContent()),
  35.                         'type' => 'news',
  36.                         'id' => $new->getId()
  37.                     ];
  38.                     $data[$new->getCreatedAt()->getTimestamp()] = $newsData;
  39.                 }
  40.             }
  41.         }
  42.         if (null === $category || 'tutos' === $category) {
  43.             // Get tutos
  44.             $tutos $keyword->getMentionsTutos();
  45.             /** @var Tuto $tuto */
  46.             foreach ($tutos as $tuto) {
  47.                 if ($tuto->isActive() === true) {
  48.                     $tutoData = [
  49.                         'title' => $tuto->getTitle(),
  50.                         'content' => $this->cleanContent($tuto->getContent()),
  51.                         'type' => 'tuto',
  52.                         'id' => $tuto->getId()
  53.                     ];
  54.                     $data[$tuto->getCreatedAt()->getTimestamp()] = $tutoData;
  55.                 }
  56.             }
  57.         }
  58.         if (null === $category || 'jurisprudences' === $category) {
  59.             // Get jp
  60.             $jurisprudences $keyword->getMentionsJp();
  61.             /** @var Jurisprudence $jurisprudence */
  62.             foreach ($jurisprudences as $jurisprudence) {
  63.                 if ($jurisprudence->isActive() === true) {
  64.                     $jpData = [
  65.                         'title' => $jurisprudence->getName(),
  66.                         'content' => $this->cleanContent($jurisprudence->getContent()),
  67.                         'type' => 'jurisprudence',
  68.                         'id' => $jurisprudence->getId()
  69.                     ];
  70.                     $data[$jurisprudence->getCreatedAt()->getTimestamp()] = $jpData;
  71.                 }
  72.             }
  73.         }
  74.         
  75.         ksort($data);
  76.         return $this->render('search/keyword.html.twig', [
  77.             'controller_name' => 'HomepageController',
  78.             'active_menu' => 'homepage',
  79.             'dataContent' => $data,
  80.             'categoryRequest' => $category,
  81.             'keyword' => $keyword
  82.         ]);
  83. }
  84. private function cleanContent(string $content)
  85. {
  86.     $contentClean strip_tags($content'<br/><br>');
  87.     $contentClean str_replace("&rsquo;""'"$contentClean);
  88.     $contentClean str_replace("&#39;""'"$contentClean);
  89.     return $contentClean;
  90. }
  91. }