<?php
namespace App\Controller;
use Pimcore\Model\DataObject\News;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Website\LinkGenerator\NewsLinkGenerator;
use App\Website\Navigation\BreadcrumbHelperService;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Twig\Extension\Templating\HeadTitle;
use Pimcore\Twig\Extension\Templating\Placeholder;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class NewsController extends BaseController
{
const NEWS_DEFAULT_DOCUMENT_PROPERTY_NAME = 'news_default_document';
/**
* @param Request $request
*
* @return Response
*
* @throws \Exception
*/
public function listingAction(Request $request, PaginatorInterface $paginator)
{
$newsListing = new News\Listing();
$newsListing->setOrderKey('publish_date');
$newsListing->setOrder('DESC');
$paginator = $paginator->paginate(
$newsListing,
$request->get('page', 1),
6
);
return $this->render('news/listing.html.twig', [
'newsListing' => $paginator,
'paginationVariables' => $paginator->getPaginationData()
]);
// return $this->render('news/listing.html.twig', ['newsListing' => $newsListing]);
}
/**
* @Route("{path}/{newstitle}-nid{news}", name="news-detail", defaults={"path"=""}, requirements={"path"=".*?", "newstitle"="[\w-]+", "news"="\d+"})
*
* @param Request $request
* @param HeadTitle $headTitleHelper
* @param Placeholder $placeholderHelper
* @param NewsLinkGenerator $newsLinkGenerator
* @param BreadcrumbHelperService $breadcrumbHelperService
*
* @return Response
*/
public function detailAction(Request $request, HeadTitle $headTitleHelper, Placeholder $placeholderHelper, NewsLinkGenerator $newsLinkGenerator, BreadcrumbHelperService $breadcrumbHelperService)
{
$news = News::getById($request->get('news'));
//$news = News::getById(4);
if (!($news instanceof News && ($news->isPublished() || $this->verifyPreviewRequest($request, $news)))) {
throw new NotFoundHttpException('News not found.');
}
$headTitleHelper($news->getTitle());
$breadcrumbHelperService->enrichNewsPage($news);
$placeholderHelper('canonical')->set($newsLinkGenerator->generate($news, ['document' => $this->document->getProperty(self::NEWS_DEFAULT_DOCUMENT_PROPERTY_NAME)]));
return $this->render('news/detail.html.twig', [
'news' => $news
]);
}
/**
* @Route("/News-Events/News/{path}{newstitle}", name="news-oldpath", defaults={"newstitle"="","path"=""}, requirements={"path"=".*?", "newstitle"="[\w-]+"})
*
* @param Request $request
* @param HeadTitle $headTitleHelper
* @param Placeholder $placeholderHelper
* @param NewsLinkGenerator $newsLinkGenerator
* @param BreadcrumbHelperService $breadcrumbHelperService
*
* @return Response
*/
public function titleAction(Request $request, HeadTitle $headTitleHelper, Placeholder $placeholderHelper, NewsLinkGenerator $newsLinkGenerator, BreadcrumbHelperService $breadcrumbHelperService)
{
//return '/News-Events/News/CESSDA/' . $request->get('newstitle');
$news = News::getByOld_path('News-Events/News/' . $request->get('path') . $request->get('newstitle'), ['limit' => 1]);
//$news = News::getByOld_path('News-Events/News/CESSDA/Academy-of-Finland-awards-funding-for-FSD-to-develop-services-for-researchers', ['limit' => 1]);
$document = \Pimcore\Model\Document::getById(43);
if (!($news instanceof News && ($news->isPublished() || $this->verifyPreviewRequest($request, $news)))) {
throw new NotFoundHttpException('News not found.');
}
$headTitleHelper($news->getTitle());
$breadcrumbHelperService->enrichNewsPage($news);
// $placeholderHelper('canonical')->set($newsLinkGenerator->generate($news, ['document' => $this->document->getProperty(self::NEWS_DEFAULT_DOCUMENT_PROPERTY_NAME)]));
$placeholderHelper('canonical')->set($newsLinkGenerator->generate($news, ['document' => $document]));
return $this->render('news/detail.html.twig', [
'news' => $news,
'document' => $document
]);
}
}