src/Controller/NewsDefaultController.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\News;
  4. use App\Annotation\CmsComponent;
  5. use App\Service\ServiceController;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Knp\Component\Pager\PaginatorInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. class NewsDefaultController extends AbstractController
  13. {
  14. public function __construct(private readonly ServiceController $serviceController) {}
  15. /**
  16. * @CmsComponent("News Overview", active=true, routeName="embed_news_overview")
  17. *
  18. * @param mixed $request
  19. */
  20. #[Route(path: '/test-news/overview', name: 'embed_news_overview')]
  21. public function embedNewsOverview(EntityManagerInterface $em, PaginatorInterface $paginator, Request $request): Response
  22. {
  23. $perpage = 10;
  24. $now = new \DateTime();
  25. if ($this->isGranted('ROLE_ADMIN')) {
  26. $query = $em->createQuery('SELECT e FROM App:News e WHERE e.deleted = 0 ORDER BY e.publishDate DESC');
  27. } else {
  28. $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')
  29. ->setParameter('now', $now);
  30. }
  31. $featuredArticle = $em->getRepository(News::class)->findOneBy(['deleted' => false, 'active' => true, 'featuredOnNewsPage' => true], ['publishDate' => 'DESC'], 1, 0);
  32. if ($featuredArticle) {
  33. if ($this->isGranted('ROLE_ADMIN')) {
  34. $query = $em->createQuery('SELECT e FROM App:News e WHERE e.deleted = 0 AND e.id != :id ORDER BY e.publishDate DESC')
  35. ->setParameter('id', $featuredArticle->getId());
  36. } else {
  37. $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')
  38. ->setParameter('now', $now)
  39. ->setParameter('id', $featuredArticle->getId());
  40. }
  41. }
  42. $paginatedNews = $paginator->paginate($query, $request->query->getInt('page', 1), $perpage);
  43. return $this->render('@theme/news/embedNewsOverview.html.twig', [
  44. 'featuredArticle' => $featuredArticle,
  45. 'articles' => $paginatedNews,
  46. ]);
  47. }
  48. /**
  49. * @CmsComponent("News Article", slug="{news_slug}", slugEntity="News", active=true, routeName="embed_news_article")
  50. *
  51. * @param mixed $request
  52. */
  53. #[Route(path: '/test-news/{news_slug}', name: 'embed_news_article')]
  54. public function embedNewsArticle(EntityManagerInterface $em, Request $request, mixed $news_slug)
  55. {
  56. $prev = $next = null;
  57. if ($this->isGranted('ROLE_ADMIN')) {
  58. $news = $em->getRepository(News::class)->findOneBy(['slug' => $news_slug, 'deleted' => false]);
  59. } else {
  60. $news = $em->getRepository(News::class)->findOneBy(['slug' => $news_slug, 'deleted' => false, 'active' => true]);
  61. }
  62. if (!$news && $this->getParameter('multilingual')) {
  63. $news = $em->getRepository(News::class)->findSlugWithLocale($news_slug, $request->getLocale());
  64. if (!$news) {
  65. return $this->serviceController->componentRedirect('/news-not-found');
  66. }
  67. }
  68. $now = new \DateTime();
  69. if (!$news || $news->getPublishDate() > $now) {
  70. if (!$this->isGranted('ROLE_ADMIN')) {
  71. return $this->serviceController->componentRedirect('/news-not-found');
  72. }
  73. }
  74. $next = $em->getRepository(News::class)->findNextPublished($news->getPublishDate());
  75. $prev = $em->getRepository(News::class)->findLastPublished($news->getPublishDate());
  76. return $this->render('@theme/news/embedNewsArticle.html.twig', [
  77. 'news' => $news,
  78. 'prev' => $prev,
  79. 'next' => $next,
  80. ]);
  81. }
  82. }