<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Controller;
use Eccube\Controller\AbstractController;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Form\Type\Admin\NewsType;
use Eccube\Repository\NewsRepository;
use Eccube\Util\CacheUtil;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class NewsController extends AbstractController
{
/**
* @var NewsRepository
*/
protected $newsRepository;
/**
* NewsController constructor.
*
* @param NewsRepository $newsRepository
*/
public function __construct(NewsRepository $newsRepository)
{
$this->newsRepository = $newsRepository;
}
/**
* 新着情報一覧を表示する。
*
* @Route("/news/list", name="news_list", methods={"GET"})
* @Template("News/index.twig")
*
* @param Request $request
* @param int $page_no
* @param PaginatorInterface $paginator
*
* @return array
*/
public function index(Request $request, PaginatorInterface $paginator, $page_no = 1)
{
$req_page_no = $request->query->get('pageno', $page_no);
// $qb = $this->newsRepository->getQueryBuilderAll();
$qb = $this->newsRepository->getList();
$pagination = $paginator->paginate(
$qb,
!empty($req_page_no) ? $req_page_no : $page_no,
$this->eccubeConfig->get('eccube_result_cache_lifetime_short') // default page limit
);
return [
'pagination' => $pagination,
];
}
/**
* 新着情報を登録・編集する。
*
* @Route("/news/detail", name="news_detail", methods={"GET", "POST"})
* @Route("/news/detail/{id}", requirements={"id" = "\d+"}, name="news_detail", methods={"GET"})
* @Template("News/detail.twig")
*
* @param Request $request
* @param null $id
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function detail(Request $request, CacheUtil $cacheUtil, $id = null)
{
if ($id) {
$News = $this->newsRepository->find($id);
if (!$News) {
throw new NotFoundHttpException();
}
} else {
$News = new \Eccube\Entity\News();
$News->setPublishDate(new \DateTime());
}
$builder = $this->formFactory
->createBuilder(NewsType::class, $News);
$event = new EventArgs(
[
'builder' => $builder,
'News' => $News,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_INITIALIZE);
$form = $builder->getForm();
$form->handleRequest($request);
return [
'form' => $form->createView(),
'News' => $News,
];
}
}