<?php
namespace App\Controller;
use App\Repository\NewsPageRepository;
use Gedmo\Tree\RepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
use Pagerfanta\Pagerfanta;
class NewsArticleController extends Controller
{
public function serviceAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$searchCategory = $request->get('category') ? explode(',', $request->get('category')) : null;
$searchTag = $request->get('tag') ? explode(',', $request->get('tag')) : null;
$pageRepository = $em->getRepository('App:Pages\NewsPage');
$result = $pageRepository->getArticles($request->getLocale(), null, null, $searchCategory, $searchTag);
// Filter the results for this page
$adapter = new ArrayAdapter($result);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(9);
$pagenumber = (int)$request->get('page');
if (!$pagenumber || $pagenumber < 1) {
$pagenumber = 1;
}
try {
$pagerfanta->setCurrentPage($pagenumber);
} catch (OutOfRangeCurrentPageException $oore) {
$repo = $em->getRepository('KunstmaanNodeBundle:NodeTranslation');
$nt = $repo->getNodeTranslationByLanguageAndInternalName($request->getLocale(), 'news');
$url = $nt ? $nt->getUrl() : '';
$url = $this->get('router')->generate('_slug', array('url' => $url, '_locale' => $request->getLocale()));
return new RedirectResponse($url);
}
// Add news post url's
$results = $pagerfanta->getCurrentPageResults();
$context['results'] = $results;
$context['pagerfanta'] = $pagerfanta;
$context['nodeTranslation'] = $request->attributes->get('_nodeTranslation');
$request->attributes->set('_renderContext', $context);
}
public function lastNews(Request $request, $limit = null, $node = null, $categories = null, $categoryName = null)
{
$em = $this->getDoctrine()->getManager();
/** @var NewsPageRepository $pageRepository */
$pageRepository = $em->getRepository('App:Pages\NewsPage');
if ($categoryName) {
$categories = [$categoryName];
}
$result = $pageRepository->getLatestNews($request->getLocale(), null, $limit, $categories);
return $this->render(
'News/view.html.twig',
array(
'news' => $result,
'node' => $node,
)
);
}
public function showTags(Request $request, $sportsString = null)
{
$em = $this->getDoctrine()->getManager();
$pageRepository = $em->getRepository('App:NewsTag');
$result = $pageRepository->findAll();
return $this->render(
'Tags/view.html.twig',
array(
'tags' => $result,
'sportsString' => $sportsString
)
);
}
}