src/Controller/KeywordsController.php line 22

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