src/Controller/NewsArticleController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\NewsPageRepository;
  4. use Gedmo\Tree\RepositoryInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Pagerfanta\Adapter\ArrayAdapter;
  9. use Pagerfanta\Exception\OutOfRangeCurrentPageException;
  10. use Pagerfanta\Pagerfanta;
  11. class NewsArticleController extends Controller
  12. {
  13.     public function serviceAction(Request $request)
  14.     {
  15.         $em $this->getDoctrine()->getManager();
  16.         $searchCategory $request->get('category') ? explode(','$request->get('category')) : null;
  17.         $searchTag $request->get('tag') ? explode(','$request->get('tag')) : null;
  18.         $pageRepository $em->getRepository('App:Pages\NewsPage');
  19.         $result $pageRepository->getArticles($request->getLocale(), nullnull$searchCategory$searchTag);
  20.         // Filter the results for this page
  21.         $adapter = new ArrayAdapter($result);
  22.         $pagerfanta = new Pagerfanta($adapter);
  23.         $pagerfanta->setMaxPerPage(9);
  24.         $pagenumber = (int)$request->get('page');
  25.         if (!$pagenumber || $pagenumber 1) {
  26.             $pagenumber 1;
  27.         }
  28.         try {
  29.             $pagerfanta->setCurrentPage($pagenumber);
  30.         } catch (OutOfRangeCurrentPageException $oore) {
  31.             $repo $em->getRepository('KunstmaanNodeBundle:NodeTranslation');
  32.             $nt $repo->getNodeTranslationByLanguageAndInternalName($request->getLocale(), 'news');
  33.             $url $nt $nt->getUrl() : '';
  34.             $url $this->get('router')->generate('_slug', array('url' => $url'_locale' => $request->getLocale()));
  35.             return new RedirectResponse($url);
  36.         }
  37.         // Add news post url's
  38.         $results $pagerfanta->getCurrentPageResults();
  39.         $context['results'] = $results;
  40.         $context['pagerfanta'] = $pagerfanta;
  41.         $context['nodeTranslation'] = $request->attributes->get('_nodeTranslation');
  42.         $request->attributes->set('_renderContext'$context);
  43.     }
  44.     public function lastNews(Request $request$limit null$node null$categories null$categoryName null)
  45.     {
  46.         $em $this->getDoctrine()->getManager();
  47.         /** @var NewsPageRepository $pageRepository */
  48.         $pageRepository $em->getRepository('App:Pages\NewsPage');
  49.         if ($categoryName) {
  50.             $categories = [$categoryName];
  51.         }
  52.         $result $pageRepository->getLatestNews($request->getLocale(), null$limit$categories);
  53.         return $this->render(
  54.             'News/view.html.twig',
  55.             array(
  56.                 'news' => $result,
  57.                 'node' => $node,
  58.             )
  59.         );
  60.     }
  61.     public function showTags(Request $request$sportsString null)
  62.     {
  63.         $em $this->getDoctrine()->getManager();
  64.         $pageRepository $em->getRepository('App:NewsTag');
  65.         $result $pageRepository->findAll();
  66.         return $this->render(
  67.             'Tags/view.html.twig',
  68.             array(
  69.                 'tags' => $result,
  70.                 'sportsString' => $sportsString
  71.             )
  72.         );
  73.     }
  74. }