src/Controller/SiteArticleController.php line 51

Open in your IDE?
  1. <?php 
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
  9. use App\Entity\SiteArticleCategory;
  10. use App\Entity\SiteArticleCategoryTranslation;
  11. use App\Entity\SiteArticle;
  12. /**
  13.  * Pages du "Guide d'achat"
  14.  * - Liste des articles simples avec 1 niveau de catégorie
  15.  */
  16. class SiteArticleController extends AbstractController
  17. {
  18.     public function indexAction(Breadcrumbs $breadcrumbs)
  19.     {
  20.         $em $this->get('doctrine')->getManager();
  21.         $articleCategories $em->getRepository(SiteArticleCategory::class)
  22.         ->createQueryBuilder('a')
  23.         ->getQuery()
  24.         ->getResult();
  25.         $breadcrumbs->addRouteItem("sylius.ui.home""sylius_shop_homepage");
  26.         $breadcrumbs->addItem("Le guide");
  27.         $articles $em->getRepository(SiteArticle::class)
  28.         ->createQueryBuilder('a')
  29.         ->orderBy('a.createdAt''DESC')
  30.         ->where('a.enabled = :enabled')
  31.         ->andWhere('a.dateDiffusion < :today')
  32.         ->setParameter('today'date("Y-m-d H:i:s"))
  33.         ->setParameter('enabled'true)       
  34.         ->getQuery()
  35.         ->getResult();
  36.         // dump($articles);
  37.         return $this->render('site_leguide_index.html.twig', [
  38.             'articleCategories' => $articleCategories,
  39.             'articles' => $articles
  40.         ]);
  41.     }
  42.     public function pageArticleCategoryAction(Breadcrumbs $breadcrumbs,$slug)
  43.     {
  44.         $em $this->get('doctrine')->getManager();
  45.         $articleCategoryTranslationRepository $em->getRepository(SiteArticleCategoryTranslation::class);
  46.         $articleCategoryTranslation $articleCategoryTranslationRepository->findOneBySlug($slug);
  47.         if (empty($articleCategoryTranslation)) {
  48.             throw new NotFoundHttpException("Cet catégorie n'existe pas.");
  49.         }
  50.         $articleCategory $articleCategoryTranslation->getTranslatable();
  51.         // print_pre($articleCategory);
  52.         $articles = [];       
  53.         $articles $em->getRepository(SiteArticle::class)
  54.         ->createQueryBuilder('a')
  55.         ->where('a.articleCategory = :category')
  56.         ->andwhere('a.enabled = true')  
  57.         ->andWhere('a.dateDiffusion < :today')
  58.         ->setParameter('today'date("Y-m-d H:i:s"))     
  59.         ->orderBy('a.createdAt''DESC')
  60.         ->setParameter('category'$articleCategory)
  61.         ->getQuery()
  62.         ->getResult();
  63.         $breadcrumbs->addRouteItem("sylius.ui.home""sylius_shop_homepage");
  64.         $breadcrumbs->addRouteItem("Le guide""app_leguide");
  65.         $breadcrumbs->addItem($articleCategory->getName());
  66.         
  67.         return $this->render('site_leguide_category_show.html.twig', [
  68.             'articleCategory' => $articleCategory,
  69.             'articles' => $articles
  70.         ]);
  71.     }
  72.     public function pageArticleAction(Breadcrumbs $breadcrumbs$slugCategory$slug$articleId)
  73.     {
  74.         $em $this->get('doctrine')->getManager();
  75.         $articleRepository $em->getRepository(SiteArticle::class);
  76.         $article $articleRepository->findOneById($articleId);
  77.         if (empty($article)) {
  78.             throw new NotFoundHttpException("Cet article n'existe pas.");
  79.         }
  80.         if ($slug != $article->getSlug()) {
  81.             return $this->redirectToRoute('app_leguide_article', [
  82.                 'slugCategory' => $article->getArticleCategory()->getSlug(),
  83.                 'slug' => $article->getSlug(),
  84.                 'articleId' => $article->getId()
  85.             ]);
  86.         }
  87.         if ($slugCategory != $article->getArticleCategory()->getSlug()) {
  88.             return $this->redirectToRoute('app_leguide_article', [
  89.                 'slugCategory' => $article->getArticleCategory()->getSlug(),
  90.                 'slug' => $article->getSlug(),
  91.                 'articleId' => $article->getId()
  92.             ]);
  93.         }
  94.         $breadcrumbs->addRouteItem("sylius.ui.home""sylius_shop_homepage");
  95.         $breadcrumbs->addRouteItem("Le guide""app_leguide");
  96.         $breadcrumbs->addRouteItem($article->getArticleCategory()->getName(), "app_leguide_category", [
  97.             'slug' =>  $slugCategory,
  98.         ]);        
  99.         $breadcrumbs->addItem($article->getTitle());
  100.         return $this->render('site_leguide_show.html.twig', [
  101.             'article' => $article
  102.         ]);
  103.     }
  104. }