app/Plugin/NewsPageSelfReliance42/Controller/NpsrController.php line 46

Open in your IDE?
  1. <?php
  2. namespace Plugin\NewsPageSelfReliance42\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Plugin\NewsPageSelfReliance42\Repository\NpsrNewsRepository;
  5. use Eccube\Entity\News;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Knp\Component\Pager\PaginatorInterface;
  15. class NpsrController extends AbstractController
  16. {
  17.   /**
  18.    * @var NpsrNewsRepository
  19.    */
  20.   protected $newsRepository;
  21.   /**
  22.    * NewsController constructor.
  23.    */
  24.   public function __construct(
  25.     NpsrNewsRepository $newsRepository
  26.   )
  27.   {
  28.     $this->newsRepository $newsRepository;
  29.   }
  30.   /**
  31.    * ニュース一覧画面.
  32.    *
  33.    * @Route( "/news" , name="news_index" )
  34.    * @Template("News/index.twig")
  35.    */
  36.   public function indexRequest $requestPaginatorInterface $paginator )
  37.   {
  38.     // handleRequestは空のqueryの場合は無視するため
  39.     if ($request->getMethod() === 'GET') {
  40.         $request->query->set('pageno'$request->query->get('pageno''1'));
  41.     }
  42.     $qb $this->newsRepository->getQueryBuilderPublished();
  43.     $query $qb->getQuery()->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  44.     /** @var SlidingPagination $pagination */
  45.     $pagination $paginator->paginate(
  46.         $query,
  47.         $request->query->get('pageno''1')
  48.     );
  49.     return [
  50.       'pagination' => $pagination,
  51.     ];
  52.   }
  53.   /**
  54.    * ニュース詳細画面.
  55.    *
  56.    * @Route("/news/{id}" , name="news_detail" )
  57.    * @Template("News/detail.twig")
  58.    * @ParamConverter("News", options={"id" = "id"})
  59.    */
  60.   public function detailRequest $requestNews $News )
  61.   {
  62.     
  63.     if ( !$this->checkVisibility($News) ) {
  64.       throw new NotFoundHttpException();
  65.     }
  66.     $NewsUrl $News->getUrl();
  67.     if ( $NewsUrl !== null ){
  68.       return new RedirectResponse$NewsUrl );
  69.     }
  70.     return [
  71.       'news' => $News,
  72.     ];
  73.   }
  74.   /**
  75.    * 閲覧可能なニュースかどうかを判定
  76.    *
  77.    * @param News $News
  78.    *
  79.    * @return boolean 閲覧可能な場合はtrue
  80.    */
  81.   protected function checkVisibility(News $News)
  82.   {
  83.       $is_admin $this->session->has('_security_admin');
  84.       $date time();
  85.       
  86.       // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  87.       if (!$is_admin) {
  88.           // 公開ステータスでない商品は表示しない.
  89.           if ( $News->isVisible() === false ) {
  90.             return false;
  91.           }elseif( $News->getPublishDate()->getTimestamp() >= $date ) {
  92.             return false;
  93.           }else{
  94.             return true;
  95.           }
  96.       }
  97.       return true;
  98.   }
  99. }