src/Eccube/Controller/Admin/Content/LayoutController.php line 155

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 Doctrine\ORM\NoResultException;
  14. use Eccube\Controller\AbstractController;
  15. use Eccube\Entity\Layout;
  16. use Eccube\Entity\Master\ProductStatus;
  17. use Eccube\Form\Type\Admin\LayoutType;
  18. use Eccube\Repository\BlockPositionRepository;
  19. use Eccube\Repository\BlockRepository;
  20. use Eccube\Repository\LayoutRepository;
  21. use Eccube\Repository\Master\DeviceTypeRepository;
  22. use Eccube\Repository\PageLayoutRepository;
  23. use Eccube\Repository\PageRepository;
  24. use Eccube\Repository\ProductRepository;
  25. use Eccube\Util\CacheUtil;
  26. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  27. use Symfony\Component\HttpFoundation\JsonResponse;
  28. use Symfony\Component\HttpFoundation\RedirectResponse;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  31. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  32. use Symfony\Component\Routing\Annotation\Route;
  33. use Twig\Environment as Twig;
  34. class LayoutController extends AbstractController
  35. {
  36.     public const DUMMY_BLOCK_ID 9999999999;
  37.     /**
  38.      * @var BlockRepository
  39.      */
  40.     protected $blockRepository;
  41.     /**
  42.      * @var BlockPositionRepository
  43.      */
  44.     protected $blockPositionRepository;
  45.     /**
  46.      * @var LayoutRepository
  47.      */
  48.     protected $layoutRepository;
  49.     /**
  50.      * @var PageLayoutRepository
  51.      */
  52.     protected $pageLayoutRepository;
  53.     /**
  54.      * @var PageRepository
  55.      */
  56.     protected $pageRepository;
  57.     /**
  58.      * @var ProductRepository
  59.      */
  60.     protected $productRepository;
  61.     /**
  62.      * @var DeviceTypeRepository
  63.      */
  64.     protected $deviceTypeRepository;
  65.     /**
  66.      * @var boolean
  67.      */
  68.     protected $isPreview false;
  69.     /**
  70.      * LayoutController constructor.
  71.      *
  72.      * @param BlockRepository $blockRepository
  73.      * @param LayoutRepository $layoutRepository
  74.      * @param PageLayoutRepository $pageLayoutRepository
  75.      * @param pageRepository $pageRepository
  76.      * @param ProductRepository $productRepository
  77.      * @param DeviceTypeRepository $deviceTypeRepository
  78.      */
  79.     public function __construct(BlockRepository $blockRepositoryBlockPositionRepository $blockPositionRepositoryLayoutRepository $layoutRepositoryPageLayoutRepository $pageLayoutRepositoryPageRepository $pageRepositoryProductRepository $productRepositoryDeviceTypeRepository $deviceTypeRepository)
  80.     {
  81.         $this->blockRepository $blockRepository;
  82.         $this->blockPositionRepository $blockPositionRepository;
  83.         $this->layoutRepository $layoutRepository;
  84.         $this->pageLayoutRepository $pageLayoutRepository;
  85.         $this->pageRepository $pageRepository;
  86.         $this->productRepository $productRepository;
  87.         $this->deviceTypeRepository $deviceTypeRepository;
  88.     }
  89.     /**
  90.      * @Route("/%eccube_admin_route%/content/layout", name="admin_content_layout", methods={"GET"})
  91.      * @Template("@admin/Content/layout_list.twig")
  92.      */
  93.     public function index()
  94.     {
  95.         $qb $this->layoutRepository->createQueryBuilder('l');
  96.         $Layouts $qb->where('l.id != :DefaultLayoutPreviewPage')
  97.                     ->orderBy('l.DeviceType''DESC')
  98.                     ->addOrderBy('l.id''ASC')
  99.                     ->setParameter('DefaultLayoutPreviewPage'Layout::DEFAULT_LAYOUT_PREVIEW_PAGE)
  100.                     ->getQuery()
  101.                     ->getResult();
  102.         return [
  103.             'Layouts' => $Layouts,
  104.         ];
  105.     }
  106.     /**
  107.      * @Route("/%eccube_admin_route%/content/layout/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_layout_delete", methods={"DELETE"})
  108.      *
  109.      * @param Layout $Layout
  110.      *
  111.      * @return RedirectResponse
  112.      */
  113.     public function delete(Layout $LayoutCacheUtil $cacheUtil)
  114.     {
  115.         $this->isTokenValid();
  116.         /** @var Layout $Layout */
  117.         if (!$Layout->isDeletable()) {
  118.             $this->addWarning(trans('admin.common.delete_error_foreign_key', ['%name%' => $Layout->getName()]), 'admin');
  119.             return $this->redirectToRoute('admin_content_layout');
  120.         }
  121.         $this->entityManager->remove($Layout);
  122.         $this->entityManager->flush();
  123.         $this->addSuccess('admin.common.delete_complete''admin');
  124.         // キャッシュの削除
  125.         $cacheUtil->clearDoctrineCache();
  126.         return $this->redirectToRoute('admin_content_layout');
  127.     }
  128.     /**
  129.      * @Route("/%eccube_admin_route%/content/layout/new", name="admin_content_layout_new", methods={"GET", "POST"})
  130.      * @Route("/%eccube_admin_route%/content/layout/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_layout_edit", methods={"GET", "POST"})
  131.      * @Template("@admin/Content/layout.twig")
  132.      */
  133.     public function edit(Request $requestCacheUtil $cacheUtil$id null$previewPageId null)
  134.     {
  135.         if (is_null($id)) {
  136.             $Layout = new Layout();
  137.         } else {
  138.             $Layout $this->layoutRepository->get($this->isPreview $id);
  139.             if (is_null($Layout)) {
  140.                 throw new NotFoundHttpException();
  141.             }
  142.         }
  143.         // 未使用ブロックの取得
  144.         $Blocks $Layout->getBlocks();
  145.         if (empty($Blocks)) {
  146.             $UnusedBlocks $this->blockRepository->findAll();
  147.         } else {
  148.             $UnusedBlocks $this->blockRepository->getUnusedBlocks($Blocks);
  149.         }
  150.         $builder $this->formFactory->createBuilder(LayoutType::class, $Layout, ['layout_id' => $id]);
  151.         $form $builder->getForm();
  152.         $form->handleRequest($request);
  153.         if ($form->isSubmitted() && $form->isValid()) {
  154.             // Layoutの更新
  155.             $Layout $form->getData();
  156.             $this->entityManager->persist($Layout);
  157.             $this->entityManager->flush();
  158.             // BlockPositionの更新
  159.             // delete/insertのため、一度削除する.
  160.             $BlockPositions $Layout->getBlockPositions();
  161.             foreach ($BlockPositions as $BlockPosition) {
  162.                 $Layout->removeBlockPosition($BlockPosition);
  163.                 $this->entityManager->remove($BlockPosition);
  164.                 $this->entityManager->flush();
  165.             }
  166.             // ブロックの個数分登録を行う.
  167.             $data $request->request->all();
  168.             $this->blockPositionRepository->register($data$Blocks$UnusedBlocks$Layout);
  169.             // キャッシュの削除
  170.             $cacheUtil->clearDoctrineCache();
  171.             // プレビューモード
  172.             if ($this->isPreview) {
  173.                 // プレビューする画面を取得
  174.                 try {
  175.                     $Page $this->pageRepository->find($previewPageId);
  176.                 } catch (NoResultException $e) {
  177.                     throw new NotFoundHttpException();
  178.                 }
  179.                 if ($Page->getEditType() >= \Eccube\Entity\Page::EDIT_TYPE_DEFAULT) {
  180.                     if ($Page->getUrl() === 'product_detail') {
  181.                         $product $this->productRepository->findOneBy(['Status' => ProductStatus::DISPLAY_SHOW]);
  182.                         if (is_null($product)) {
  183.                             throw new NotFoundHttpException();
  184.                         }
  185.                         return $this->redirectToRoute($Page->getUrl(), ['preview' => 1'id' => $product->getId()]);
  186.                     } else {
  187.                         return $this->redirectToRoute($Page->getUrl(), ['preview' => 1]);
  188.                     }
  189.                 }
  190.                 return $this->redirectToRoute('user_data', ['route' => $Page->getUrl(), 'preview' => 1]);
  191.             }
  192.             $this->addSuccess('admin.common.save_complete''admin');
  193.             return $this->redirectToRoute('admin_content_layout_edit', ['id' => $Layout->getId()]);
  194.         }
  195.         return [
  196.             'form' => $form->createView(),
  197.             'Layout' => $Layout,
  198.             'UnusedBlocks' => $UnusedBlocks,
  199.         ];
  200.     }
  201.     /**
  202.      * @Route("/%eccube_admin_route%/content/layout/view_block", name="admin_content_layout_view_block", methods={"GET"})
  203.      *
  204.      * @param Request $request
  205.      * @param Twig $twig
  206.      *
  207.      * @return JsonResponse
  208.      */
  209.     public function viewBlock(Request $requestTwig $twig)
  210.     {
  211.         if (!$request->isXmlHttpRequest()) {
  212.             throw new BadRequestHttpException();
  213.         }
  214.         $id $request->get('id');
  215.         if (is_null($id)) {
  216.             throw new BadRequestHttpException();
  217.         }
  218.         $Block $this->blockRepository->find($id);
  219.         if (null === $Block) {
  220.             throw new NotFoundHttpException();
  221.         }
  222.         $source $twig->getLoader()
  223.             ->getSourceContext('Block/'.$Block->getFileName().'.twig')
  224.             ->getCode();
  225.         return $this->json([
  226.             'id' => $Block->getId(),
  227.             'source' => $source,
  228.         ]);
  229.     }
  230.     /**
  231.      * @Route("/%eccube_admin_route%/content/layout/{id}/preview", requirements={"id" = "\d+"}, name="admin_content_layout_preview", methods={"POST"})
  232.      */
  233.     public function preview(Request $request$idCacheUtil $cacheUtil)
  234.     {
  235.         $form $request->get('admin_layout');
  236.         $this->isPreview true;
  237.         return $this->edit($request$cacheUtil$id$form['Page']);
  238.     }
  239. }