src/Eccube/Controller/Admin/Content/NewsController.php line 59

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 Eccube\Controller\Admin\Content;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Entity\News;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use Eccube\Form\Type\Admin\NewsType;
  18. use Eccube\Repository\NewsRepository;
  19. use Eccube\Util\CacheUtil;
  20. use Knp\Component\Pager\PaginatorInterface;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. class NewsController extends AbstractController
  26. {
  27.     /**
  28.      * @var NewsRepository
  29.      */
  30.     protected $newsRepository;
  31.     /**
  32.      * NewsController constructor.
  33.      *
  34.      * @param NewsRepository $newsRepository
  35.      */
  36.     public function __construct(NewsRepository $newsRepository)
  37.     {
  38.         $this->newsRepository $newsRepository;
  39.     }
  40.     /**
  41.      * 新着情報一覧を表示する。
  42.      *
  43.      * @Route("/%eccube_admin_route%/content/news", name="admin_content_news", methods={"GET"})
  44.      * @Route("/%eccube_admin_route%/content/news/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_content_news_page", methods={"GET"})
  45.      * @Template("@admin/Content/news.twig")
  46.      *
  47.      * @param Request $request
  48.      * @param int $page_no
  49.      * @param PaginatorInterface $paginator
  50.      *
  51.      * @return array
  52.      */
  53.     public function index(Request $requestPaginatorInterface $paginator$page_no 1)
  54.     {
  55.         $qb $this->newsRepository->getQueryBuilderAll();
  56.         $event = new EventArgs(
  57.             [
  58.                 'qb' => $qb,
  59.             ],
  60.             $request
  61.         );
  62.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_NEWS_INDEX_INITIALIZE);
  63.         $pagination $paginator->paginate(
  64.             $qb,
  65.             $page_no,
  66.             $this->eccubeConfig->get('eccube_default_page_count')
  67.         );
  68.         return [
  69.             'pagination' => $pagination,
  70.         ];
  71.     }
  72.     /**
  73.      * 新着情報を登録・編集する。
  74.      *
  75.      * @Route("/%eccube_admin_route%/content/news/new", name="admin_content_news_new", methods={"GET", "POST"})
  76.      * @Route("/%eccube_admin_route%/content/news/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_news_edit", methods={"GET", "POST"})
  77.      * @Template("@admin/Content/news_edit.twig")
  78.      *
  79.      * @param Request $request
  80.      * @param null $id
  81.      *
  82.      * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
  83.      */
  84.     public function edit(Request $requestCacheUtil $cacheUtil$id null)
  85.     {
  86.         if ($id) {
  87.             $News $this->newsRepository->find($id);
  88.             if (!$News) {
  89.                 throw new NotFoundHttpException();
  90.             }
  91.         } else {
  92.             $News = new \Eccube\Entity\News();
  93.             $News->setPublishDate(new \DateTime());
  94.         }
  95.         $builder $this->formFactory
  96.             ->createBuilder(NewsType::class, $News);
  97.         $event = new EventArgs(
  98.             [
  99.                 'builder' => $builder,
  100.                 'News' => $News,
  101.             ],
  102.             $request
  103.         );
  104.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_NEWS_EDIT_INITIALIZE);
  105.         $form $builder->getForm();
  106.         $form->handleRequest($request);
  107.         if ($form->isSubmitted() && $form->isValid()) {
  108.             if (!$News->getUrl()) {
  109.                 $News->setLinkMethod(false);
  110.             }
  111.             $this->newsRepository->save($News);
  112.             $event = new EventArgs(
  113.                 [
  114.                     'form' => $form,
  115.                     'News' => $News,
  116.                 ],
  117.                 $request
  118.             );
  119.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_NEWS_EDIT_COMPLETE);
  120.             $this->addSuccess('admin.common.save_complete''admin');
  121.             // キャッシュの削除
  122.             $cacheUtil->clearDoctrineCache();
  123.             return $this->redirectToRoute('admin_content_news_edit', ['id' => $News->getId()]);
  124.         }
  125.         return [
  126.             'form' => $form->createView(),
  127.             'News' => $News,
  128.         ];
  129.     }
  130.     /**
  131.      * 指定した新着情報を削除する。
  132.      *
  133.      * @Route("/%eccube_admin_route%/content/news/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_news_delete", methods={"DELETE"})
  134.      *
  135.      * @param Request $request
  136.      * @param News $News
  137.      *
  138.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  139.      */
  140.     public function delete(Request $requestNews $NewsCacheUtil $cacheUtil)
  141.     {
  142.         $this->isTokenValid();
  143.         log_info('新着情報削除開始', [$News->getId()]);
  144.         try {
  145.             $this->newsRepository->delete($News);
  146.             $event = new EventArgs(['News' => $News], $request);
  147.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_NEWS_DELETE_COMPLETE);
  148.             $this->addSuccess('admin.common.delete_complete''admin');
  149.             log_info('新着情報削除完了', [$News->getId()]);
  150.             // キャッシュの削除
  151.             $cacheUtil->clearDoctrineCache();
  152.         } catch (\Exception $e) {
  153.             $message trans('admin.common.delete_error_foreign_key', ['%name%' => $News->getTitle()]);
  154.             $this->addError($message'admin');
  155.             log_error('新着情報削除エラー', [$News->getId(), $e]);
  156.         }
  157.         return $this->redirectToRoute('admin_content_news');
  158.     }
  159. }