<?php
namespace App\Controller;
use App\Entity\News;
use App\Annotation\CmsComponent;
use App\Service\ServiceController;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class NewsDefaultController extends AbstractController
{
public function __construct(private readonly ServiceController $serviceController) {}
/**
* @CmsComponent("News Overview", active=true, routeName="embed_news_overview")
*
* @param mixed $request
*/
#[Route(path: '/test-news/overview', name: 'embed_news_overview')]
public function embedNewsOverview(EntityManagerInterface $em, PaginatorInterface $paginator, Request $request): Response
{
$perpage = 10;
$now = new \DateTime();
if ($this->isGranted('ROLE_ADMIN')) {
$query = $em->createQuery('SELECT e FROM App:News e WHERE e.deleted = 0 ORDER BY e.publishDate DESC');
} else {
$query = $em->createQuery('SELECT e FROM App:News e WHERE e.deleted = 0 AND e.active = 1 AND e.publishDate <= :now ORDER BY e.publishDate DESC')
->setParameter('now', $now);
}
$featuredArticle = $em->getRepository(News::class)->findOneBy(['deleted' => false, 'active' => true, 'featuredOnNewsPage' => true], ['publishDate' => 'DESC'], 1, 0);
if ($featuredArticle) {
if ($this->isGranted('ROLE_ADMIN')) {
$query = $em->createQuery('SELECT e FROM App:News e WHERE e.deleted = 0 AND e.id != :id ORDER BY e.publishDate DESC')
->setParameter('id', $featuredArticle->getId());
} else {
$query = $em->createQuery('SELECT e FROM App:News e WHERE e.deleted = 0 AND e.active = 1 AND e.publishDate <= :now AND e.id != :id ORDER BY e.publishDate DESC')
->setParameter('now', $now)
->setParameter('id', $featuredArticle->getId());
}
}
$paginatedNews = $paginator->paginate($query, $request->query->getInt('page', 1), $perpage);
return $this->render('@theme/news/embedNewsOverview.html.twig', [
'featuredArticle' => $featuredArticle,
'articles' => $paginatedNews,
]);
}
/**
* @CmsComponent("News Article", slug="{news_slug}", slugEntity="News", active=true, routeName="embed_news_article")
*
* @param mixed $request
*/
#[Route(path: '/test-news/{news_slug}', name: 'embed_news_article')]
public function embedNewsArticle(EntityManagerInterface $em, Request $request, mixed $news_slug)
{
$prev = $next = null;
if ($this->isGranted('ROLE_ADMIN')) {
$news = $em->getRepository(News::class)->findOneBy(['slug' => $news_slug, 'deleted' => false]);
} else {
$news = $em->getRepository(News::class)->findOneBy(['slug' => $news_slug, 'deleted' => false, 'active' => true]);
}
if (!$news && $this->getParameter('multilingual')) {
$news = $em->getRepository(News::class)->findSlugWithLocale($news_slug, $request->getLocale());
if (!$news) {
return $this->serviceController->componentRedirect('/news-not-found');
}
}
$now = new \DateTime();
if (!$news || $news->getPublishDate() > $now) {
if (!$this->isGranted('ROLE_ADMIN')) {
return $this->serviceController->componentRedirect('/news-not-found');
}
}
$next = $em->getRepository(News::class)->findNextPublished($news->getPublishDate());
$prev = $em->getRepository(News::class)->findLastPublished($news->getPublishDate());
return $this->render('@theme/news/embedNewsArticle.html.twig', [
'news' => $news,
'prev' => $prev,
'next' => $next,
]);
}
}