<?php
declare(strict_types=1);
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
use App\Entity\SiteArticleCategory;
use App\Entity\SiteArticleCategoryTranslation;
use App\Entity\SiteArticle;
/**
* Pages du "Guide d'achat"
* - Liste des articles simples avec 1 niveau de catégorie
*/
class SiteArticleController extends AbstractController
{
public function indexAction(Breadcrumbs $breadcrumbs)
{
$em = $this->get('doctrine')->getManager();
$articleCategories = $em->getRepository(SiteArticleCategory::class)
->createQueryBuilder('a')
->getQuery()
->getResult();
$breadcrumbs->addRouteItem("sylius.ui.home", "sylius_shop_homepage");
$breadcrumbs->addItem("Le guide");
$articles = $em->getRepository(SiteArticle::class)
->createQueryBuilder('a')
->orderBy('a.createdAt', 'DESC')
->where('a.enabled = :enabled')
->andWhere('a.dateDiffusion < :today')
->setParameter('today', date("Y-m-d H:i:s"))
->setParameter('enabled', true)
->getQuery()
->getResult();
// dump($articles);
return $this->render('site_leguide_index.html.twig', [
'articleCategories' => $articleCategories,
'articles' => $articles
]);
}
public function pageArticleCategoryAction(Breadcrumbs $breadcrumbs,$slug)
{
$em = $this->get('doctrine')->getManager();
$articleCategoryTranslationRepository = $em->getRepository(SiteArticleCategoryTranslation::class);
$articleCategoryTranslation = $articleCategoryTranslationRepository->findOneBySlug($slug);
if (empty($articleCategoryTranslation)) {
throw new NotFoundHttpException("Cet catégorie n'existe pas.");
}
$articleCategory = $articleCategoryTranslation->getTranslatable();
// print_pre($articleCategory);
$articles = [];
$articles = $em->getRepository(SiteArticle::class)
->createQueryBuilder('a')
->where('a.articleCategory = :category')
->andwhere('a.enabled = true')
->andWhere('a.dateDiffusion < :today')
->setParameter('today', date("Y-m-d H:i:s"))
->orderBy('a.createdAt', 'DESC')
->setParameter('category', $articleCategory)
->getQuery()
->getResult();
$breadcrumbs->addRouteItem("sylius.ui.home", "sylius_shop_homepage");
$breadcrumbs->addRouteItem("Le guide", "app_leguide");
$breadcrumbs->addItem($articleCategory->getName());
return $this->render('site_leguide_category_show.html.twig', [
'articleCategory' => $articleCategory,
'articles' => $articles
]);
}
public function pageArticleAction(Breadcrumbs $breadcrumbs, $slugCategory, $slug, $articleId)
{
$em = $this->get('doctrine')->getManager();
$articleRepository = $em->getRepository(SiteArticle::class);
$article = $articleRepository->findOneById($articleId);
if (empty($article)) {
throw new NotFoundHttpException("Cet article n'existe pas.");
}
if ($slug != $article->getSlug()) {
return $this->redirectToRoute('app_leguide_article', [
'slugCategory' => $article->getArticleCategory()->getSlug(),
'slug' => $article->getSlug(),
'articleId' => $article->getId()
]);
}
if ($slugCategory != $article->getArticleCategory()->getSlug()) {
return $this->redirectToRoute('app_leguide_article', [
'slugCategory' => $article->getArticleCategory()->getSlug(),
'slug' => $article->getSlug(),
'articleId' => $article->getId()
]);
}
$breadcrumbs->addRouteItem("sylius.ui.home", "sylius_shop_homepage");
$breadcrumbs->addRouteItem("Le guide", "app_leguide");
$breadcrumbs->addRouteItem($article->getArticleCategory()->getName(), "app_leguide_category", [
'slug' => $slugCategory,
]);
$breadcrumbs->addItem($article->getTitle());
return $this->render('site_leguide_show.html.twig', [
'article' => $article
]);
}
}