src/Controller/ThemesWebsite/Whileresume/Website/ArticlesController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ThemesWebsite\Whileresume\Website;
  3. use App\Entity\Articles\Articles;
  4. use App\Entity\Cvs\Jobs;
  5. use App\Entity\Pages\Pages;
  6. use App\Entity\Pages\PagesHasBlocks;
  7. use App\Services\Core\Core;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. /**
  20.  * Système d'articles
  21.  */
  22. class ArticlesController extends AbstractController
  23. {
  24.     private $em;
  25.     private $paginator;
  26.     public function __construct (EntityManagerInterface $em,
  27.                                  PaginatorInterface $paginator
  28.     ){
  29.         $this->em $em;
  30.         $this->paginator $paginator;
  31.     }
  32.     /**
  33.      * Liste des articles
  34.      * @param Request $request
  35.      * @return mixed
  36.      */
  37.     public function articles(Request $request)
  38.     {
  39.         $locale $request->getLocale();
  40.         $page =  $this->em->getRepository(Pages::class)->findOneBy(['locale' => $locale'name' => 'articles']);
  41.         if($page->getType() == "brouillon") {
  42.             return $this->redirectToRoute('homepage');
  43.         }
  44.         if(!empty($page->getRedirect())) {
  45.             return $this->redirect($page->getRedirect());
  46.         }
  47.         $articles $this->em->getRepository(Articles::class)->getFinalArticles($locale);
  48.         $pagination $this->paginator->paginate(
  49.             $articles,
  50.             $request->query->getInt('page'1),
  51.             20
  52.         );
  53.         $blocks $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page'type' => 'prod''startPage' => false],['sequence' => 'ASC']);
  54.         $page->setViews((int)$page->getViews() + 1);
  55.         $this->em->persist($page);
  56.         $this->em->flush();
  57.         return $this->render('application/whileresume/website/articles/list.html.twig', [
  58.             'pagination' => $pagination,
  59.             'page' => $page,
  60.             'blocks' => $blocks,
  61.             'formNewsletter' => null
  62.         ]);
  63.     }
  64.     /**
  65.      * Lire un article
  66.      * @param Request $request
  67.      * @param $slug
  68.      * @return mixed
  69.      */
  70.     public function article(Request $request$slug)
  71.     {
  72.         $locale $request->getLocale();
  73.         $article $this->em->getRepository(Articles::class)->getArticle($locale,$slug);
  74.         if($article->getVisibility() == false) {
  75.             return $this->redirectToRoute('homepage');
  76.         }
  77.         // Articles similaires (5 max) — basés sur tags + pageslug + keyword + featured + author, puis shuffle pour de la variété
  78.         $similarArticles $this->em->getRepository(Articles::class)->getSimilarArticles($article5);
  79.         // Jobs aléatoires (5 max) — pool des 100 derniers jobs online dans la locale, mélangés à chaque chargement
  80.         $recommendedJobs $this->getRandomJobsForArticle($locale5);
  81.         return $this->render('application/whileresume/website/articles/article.html.twig',[
  82.             'article' => $article,
  83.             'similarArticles' => $similarArticles,
  84.             'recommendedJobs' => $recommendedJobs,
  85.         ]);
  86.     }
  87.     /**
  88.      * Sélection aléatoire de jobs online dans la même locale, à afficher sous l'article.
  89.      *
  90.      * On récupère un pool large des derniers jobs online, puis on shuffle côté PHP.
  91.      * Ça évite ORDER BY RAND() (lent sur grosses tables) tout en garantissant de la
  92.      * variété à chaque rechargement de page.
  93.      *
  94.      * @param string $locale
  95.      * @param int $limit
  96.      * @return Jobs[]
  97.      */
  98.     private function getRandomJobsForArticle(string $localeint $limit 5): array
  99.     {
  100.         $jobsRepo $this->em->getRepository(Jobs::class);
  101.         // Pool large = on prend les 100 derniers jobs online dans la locale
  102.         $pool $jobsRepo->createQueryBuilder('j')
  103.             ->where('j.online = :online')
  104.             ->andWhere('j.locale = :locale')
  105.             ->setParameter('online'true)
  106.             ->setParameter('locale'$locale)
  107.             ->orderBy('j.createdAt''DESC')
  108.             ->setMaxResults(100)
  109.             ->getQuery()
  110.             ->getResult();
  111.         if (empty($pool)) {
  112.             return [];
  113.         }
  114.         shuffle($pool);
  115.         return array_slice($pool0$limit);
  116.     }
  117. }