src/Controller/NewsB2CController.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Controller;
  15. use App\Website\LinkGenerator\NewsLinkGenerator;
  16. use App\Website\Navigation\BreadcrumbHelperService;
  17. use Knp\Component\Pager\PaginatorInterface;
  18. use Pimcore\Model\DataObject\News;
  19. use Pimcore\Twig\Extension\Templating\HeadTitle;
  20. use Pimcore\Twig\Extension\Templating\Placeholder;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  26. use FrontendPermissionToolkitBundle\Service;
  27. use Symfony\Component\Security\Core\User\UserInterface;
  28. class NewsB2CController extends BaseController
  29. {
  30.     const NEWS_DEFAULT_DOCUMENT_PROPERTY_NAME 'news_default_document';
  31.     var $service;   
  32.     /**
  33.      * @param Request $request
  34.      *
  35.      * @return Response
  36.      *
  37.      * @throws \Exception
  38.      */
  39.     public function listingAction(Request $requestPaginatorInterface $paginator,EventDispatcherInterface $eventDispatcher,UserInterface $user null)
  40.     {
  41.         // if($this->checkPermission("view_news",$eventDispatcher,$user) != true)
  42.         // {
  43.         //     throw new \Exception("Permission denied");
  44.         // }
  45.         // get a list of news objects and order them by date
  46.         $newsList = new News\Listing();
  47.         $newsList->setCondition('NewsType = ?',['B2C']);
  48.         $newsList->setOrderKey('date');
  49.         $newsList->setOrder('DESC');
  50.         $paginator $paginator->paginate(
  51.             $newsList,
  52.             $request->get('page'1),
  53.             6
  54.         );
  55.         return $this->render('news_b2c/listing.html.twig', [
  56.             'news' => $paginator,
  57.             'paginationVariables' => $paginator->getPaginationData()
  58.         ]);
  59.     }
  60.     /**
  61.      * @Route("/b2c/{path}{newstitle}~n{news}", name="b2c-news-detail", defaults={"path"=""}, requirements={"path"=".*?", "newstitle"="[\w-]+", "news"="\d+"})
  62.      *
  63.      * @param Request $request
  64.      * @param HeadTitle $headTitleHelper
  65.      * @param Placeholder $placeholderHelper
  66.      * @param NewsLinkGenerator $newsLinkGenerator
  67.      * @param BreadcrumbHelperService $breadcrumbHelperService
  68.      *
  69.      * @return Response
  70.      */
  71.     public function detailAction(Request $requestHeadTitle $headTitleHelperPlaceholder $placeholderHelperNewsLinkGenerator $newsLinkGeneratorBreadcrumbHelperService $breadcrumbHelperService,EventDispatcherInterface $eventDispatcher,UserInterface $user null)
  72.     {
  73.         // if($this->checkPermission("view_news",$eventDispatcher,$user) != true)
  74.         // {
  75.         //     throw new \Exception("Permission denied");
  76.         // }
  77.         $news News::getById($request->get('news'));
  78.         if (!($news instanceof News && ($news->isPublished() || $this->verifyPreviewRequest($request$news)))) {
  79.             throw new NotFoundHttpException('News not found.');
  80.         }      
  81.             
  82.        
  83.         $headTitleHelper($news->getTitle());
  84.         $breadcrumbHelperService->enrichNewsPage($news);
  85.         $placeholderHelper('canonical')->set($newsLinkGenerator->generateB2C($news, ['document' => $this->document->getProperty(self::NEWS_DEFAULT_DOCUMENT_PROPERTY_NAME)]));
  86.         $newsList = new News\Listing();    
  87.         $newsList->setOrderKey('date');
  88.         $newsList->setOrder('DESC');
  89.         $newsList->setLimit(5);
  90.         $newsList->setCondition('o_id != ? && NewsType = ?',[$news->getId(),'B2C']);
  91.         $additionalNews $newsList->load();   
  92.         
  93.         return $this->render('news_b2c/detail.html.twig', [
  94.             'news' => $news,
  95.             'additionalNews'=>$additionalNews
  96.         ]);
  97.     }
  98.     /**
  99.      * @param Request $request
  100.      *
  101.      * @return \Symfony\Component\HttpFoundation\Response
  102.      */
  103.     public function newsTeaserAction(Request $request)
  104.     {
  105.         $paramsBag = [];
  106.         if ($request->get('type') == 'object') {
  107.             $news News::getById($request->get('id'));
  108.             $paramsBag['news'] = $news;
  109.             return $this->render('news_b2c/news_teaser.html.twig'$paramsBag);
  110.         }
  111.         throw new NotFoundHttpException('News not found.');
  112.     }
  113.     /**
  114.      * @param Request $request
  115.      *
  116.      * @return \Symfony\Component\HttpFoundation\Response
  117.      */
  118.     public function emailNewsTeaserAction(Request $requestNewsLinkGenerator $newsLinkGenerator)
  119.     {
  120.         $paramsBag = [];
  121.         if ($request->get('type') == 'object') {
  122.             $news News::getById($request->get('id'));
  123.             $paramsBag['news'] = $news;
  124.             $paramsBag['detailLink'] = $newsLinkGenerator->generateB2C($news, ['document' => $this->document->getProperty('news_default_document')]);
  125.             return $this->render('news_b2c/email_news_teaser.html.twig'$paramsBag);
  126.         }
  127.         throw new NotFoundHttpException('News not found.');
  128.     }
  129.     private function checkPermission($key,EventDispatcherInterface $eventDispatcher,$userObject){
  130.         $allow false;  
  131.         $userObject \Pimcore::getContainer()->get('security.token_storage')->getToken()->getUser(); 
  132.         $this->service = new Service($eventDispatcher);          
  133.         if($userObject){
  134.             if($this->service->isAllowed($userObject$key)) {
  135.                 $allow true;   
  136.             }
  137.         }
  138.         return $allow;
  139.     }
  140. }