<?php
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Language;
use Pimcore\Model\DataObject\News;
use Pimcore\Model\DataObject\Services;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use \Knp\Component\Pager\PaginatorInterface;
class NewsController extends FrontendController
{
/**
* @Template
* @param Request $request
* @return array
* @route("/{_locale}/news/{slug}", name="newsDetail",methods={"GET"})
*/
public function detailAction(Request $request)
{
$slug = $request->get('slug');
$newsListing = new News\Listing();
$newsListing->setCondition('slug like "%' . $slug . '%"');
$newsListing->setLimit(1);
$newsListing->load();
if(count($newsListing) > 0){
$news = News::getBySlug($slug)->load()[0];
}else{
$news = false;
}
return [
"news"=>$news,
"slug"=>$slug,
"seoNews" => true
];
}
/**
* @Template
* @param Request $request
* @route("/{_locale}/news", name="newsList",methods={"GET"})
* @return array
*/
public function listAction(Request $request, PaginatorInterface $paginator)
{
$newsListing = new News\Listing();
$newsListing->setOrderKey("publishdate");
$newsListing->setCondition('(publishdate IS NULL OR publishdate < UNIX_TIMESTAMP(NOW()))');
$newsListing->setOrder("DESC");
$newsListing->load();
$countPerPage = intval($this->document->getProperty("blogItemCountPerPage"));
if(!$countPerPage) :
$countPerPage = 24;
endif;
$paginator = $paginator->paginate(
$newsListing,
$request->get('page', 1),
$countPerPage
);
return [
"newsListing"=>$newsListing,
'paginator' => $paginator,
'paginationVariables' => $paginator->getPaginationData()
];
}
/**
* @Template
* @param Request $request
* @return array
*/
public function lastNewsAction(Request $request)
{
$newsListing = new News\Listing();
$newsListing->setOrderKey("publishdate");
$newsListing->setCondition('(publishdate IS NULL OR publishdate < UNIX_TIMESTAMP(NOW()))');
$newsListing->setOrder("DESC");
$newsListing->setLimit(4);
$newsListing->load();
return $this->render('snippets/last-news.html.twig', [
'newsListing' => $newsListing
]);
}
}