src/Controller/Frontend/NewsController.php line 77
<?php
namespace App\Controller\Frontend;
use App\Entity\Content\News;
use App\Model\Content\NewsSearch;
use App\Service\Common\Owner\Render;
use App\Service\RSSWriter\Channel;
use App\Service\RSSWriter\Feed;
use App\Service\RSSWriter\Item;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class NewsController extends AbstractController
{
public function __construct(private readonly EntityManagerInterface $em)
{
}
/**
* @ Route("/news", name="news")
*
*/
/* public function news(Request $request, PaginatorInterface $paginator, \App\Elastic\Search\NewsSearch $newsSearch)
{
$news_params = $this->getParameter('news');
$data = new NewsSearch();
$form = $this->createForm(NewsType::class, $data, [
'type_category' => \App\Entity\Content\NewsType::TYPE_NEWS,
]);
$form->handleRequest($request);
$form->isSubmitted() && $form->isValid();
$elQb = $newsSearch->findPaginated($data);
// dump($elQb);
// $news = $this->em->getRepository(News::class)->findNewsQb($data);
// $news = $paginator->paginate($news, $request->query->getInt('page', 1), $news_params['news_page_count']);
$news = $paginator->paginate($elQb, $request->query->getInt('page', 1), $news_params['news_page_count']);
//dump($news);
// dump($news);
return $this->render(
'news/items.html.twig',
[
'form' => $form->createView(),
'items' => $news,
]
);
}*/
#[Route(path: '/news/{id}', name: 'news_item', requirements: ['id' => '\d+'])]
public function news_item(News $news, RequestStack $requestStack)
{
$news_params = $this->getParameter('news');
$filter = new NewsSearch();
$filter->setLocale($requestStack->getMainRequest()->getLocale());
$other = $this->em->getRepository(News::class)->findOtherNews(
[
$news->getId()
],
$news_params['main_page_news'],
$filter
);
return $this->render(
'news/item.html.twig',
[
'item' => $news,
'other_news' => $other,
]
);
}
#[Route(path: '/news.rss', name: 'news_rss')]
public function rssNews(PaginatorInterface $paginator, TranslatorInterface $translator, Render $render)
{
$news = $this->em->getRepository(News::class)->findNewsQb();
/** @var News[] $news */
$news = $paginator->paginate($news, 1, 50);
$feed = new Feed();
$channel = new Channel();
$channel->title($translator->trans('site.rss_name'));
$channel->appendTo($feed);
$channel->url($this->generateUrl('main_olymp', [], UrlGeneratorInterface::ABSOLUTE_URL))->feedUrl(
$this->generateUrl('news_rss', [], UrlGeneratorInterface::ABSOLUTE_URL)
)->language('ru-RU');
foreach ($news as $n) {
$item = new Item();
$url_news = $this->generateUrl('news_item', ['id' => $n->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
$item->title($n->getCaption())->description(trim(strip_tags($n->getPreview() ?? $n->getText())))->url(
$url_news
)
// ->author(strip_tags($render->render($n,'news_item')))
->pubDate($n->getPublishAt()->getTimestamp())->guid($url_news, true)
// ->preferCdata(true) // By this, title and description become CDATA wrapped HTML.
->appendTo($channel);
}
$response = new Response($feed->render());
$response->headers->add(
[
'Content-Type' => 'text/xml; charset=utf-8',
]
);
return $response;
}
}