app/Customize/Controller/NewsController.php line 86

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Customize\Controller;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Event\EccubeEvents;
  15. use Eccube\Event\EventArgs;
  16. use Eccube\Form\Type\Admin\NewsType;
  17. use Eccube\Repository\NewsRepository;
  18. use Eccube\Util\CacheUtil;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  24. class NewsController extends AbstractController
  25. {
  26.     /**
  27.      * @var NewsRepository
  28.      */
  29.     protected $newsRepository;
  30.     /**
  31.      * NewsController constructor.
  32.      *
  33.      * @param NewsRepository $newsRepository
  34.      */
  35.     public function __construct(NewsRepository $newsRepository)
  36.     {
  37.         $this->newsRepository $newsRepository;
  38.     }
  39.     /**
  40.      * 新着情報一覧を表示する。
  41.      *
  42.      * @Route("/news/list", name="news_list", methods={"GET"})
  43.      * @Template("News/index.twig")
  44.      *
  45.      * @param Request $request
  46.      * @param int $page_no
  47.      * @param PaginatorInterface $paginator
  48.      *
  49.      * @return array
  50.      */
  51.     public function index(Request $requestPaginatorInterface $paginator$page_no 1)
  52.     {
  53.         $req_page_no $request->query->get('pageno'$page_no);
  54. //        $qb = $this->newsRepository->getQueryBuilderAll();
  55.         $qb $this->newsRepository->getList();
  56.         $pagination $paginator->paginate(
  57.             $qb,
  58.             !empty($req_page_no) ? $req_page_no $page_no,
  59.             $this->eccubeConfig->get('eccube_result_cache_lifetime_short'// default page limit
  60.         );
  61.         return [
  62.             'pagination' => $pagination,
  63.         ];
  64.     }
  65.     /**
  66.      * 新着情報を登録・編集する。
  67.      *
  68.      * @Route("/news/detail", name="news_detail", methods={"GET", "POST"})
  69.      * @Route("/news/detail/{id}", requirements={"id" = "\d+"}, name="news_detail", methods={"GET"})
  70.      * @Template("News/detail.twig")
  71.      *
  72.      * @param Request $request
  73.      * @param null $id
  74.      *
  75.      * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
  76.      */
  77.     public function detail(Request $requestCacheUtil $cacheUtil$id null)
  78.     {
  79.         if ($id) {
  80.             $News $this->newsRepository->find($id);
  81.             if (!$News) {
  82.                 throw new NotFoundHttpException();
  83.             }
  84.         } else {
  85.             $News = new \Eccube\Entity\News();
  86.             $News->setPublishDate(new \DateTime());
  87.         }
  88.         $builder $this->formFactory
  89.             ->createBuilder(NewsType::class, $News);
  90.         $event = new EventArgs(
  91.             [
  92.                 'builder' => $builder,
  93.                 'News' => $News,
  94.             ],
  95.             $request
  96.         );
  97.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_NEWS_EDIT_INITIALIZE);
  98.         $form $builder->getForm();
  99.         $form->handleRequest($request);
  100.         return [
  101.             'form' => $form->createView(),
  102.             'News' => $News,
  103.         ];
  104.     }
  105. }