src/Eccube/Controller/Admin/Content/PageController.php line 71

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\Page;
  15. use Eccube\Entity\PageLayout;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Form\Type\Admin\MainEditType;
  19. use Eccube\Repository\Master\DeviceTypeRepository;
  20. use Eccube\Repository\PageLayoutRepository;
  21. use Eccube\Repository\PageRepository;
  22. use Eccube\Util\CacheUtil;
  23. use Eccube\Util\StringUtil;
  24. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  25. use Symfony\Component\Filesystem\Filesystem;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Component\Routing\RouterInterface;
  29. use Twig\Environment;
  30. class PageController extends AbstractController
  31. {
  32.     /**
  33.      * @var PageRepository
  34.      */
  35.     protected $pageRepository;
  36.     /**
  37.      * @var PageLayoutRepository
  38.      */
  39.     protected $pageLayoutRepository;
  40.     /**
  41.      * @var DeviceTypeRepository
  42.      */
  43.     protected $deviceTypeRepository;
  44.     /**
  45.      * PageController constructor.
  46.      *
  47.      * @param PageRepository $pageRepository
  48.      * @param DeviceTypeRepository $deviceTypeRepository
  49.      */
  50.     public function __construct(
  51.         PageRepository $pageRepository,
  52.         PageLayoutRepository $pageLayoutRepository,
  53.         DeviceTypeRepository $deviceTypeRepository
  54.     ) {
  55.         $this->pageRepository $pageRepository;
  56.         $this->pageLayoutRepository $pageLayoutRepository;
  57.         $this->deviceTypeRepository $deviceTypeRepository;
  58.     }
  59.     /**
  60.      * @Route("/%eccube_admin_route%/content/page", name="admin_content_page", methods={"GET"})
  61.      * @Template("@admin/Content/page.twig")
  62.      */
  63.     public function index(Request $requestRouterInterface $router)
  64.     {
  65.         $Pages $this->pageRepository->getPageList();
  66.         $event = new EventArgs(
  67.             [
  68.                 'Pages' => $Pages,
  69.             ],
  70.             $request
  71.         );
  72.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_PAGE_INDEX_COMPLETE);
  73.         return [
  74.             'Pages' => $Pages,
  75.             'router' => $router,
  76.         ];
  77.     }
  78.     /**
  79.      * @Route("/%eccube_admin_route%/content/page/new", name="admin_content_page_new", methods={"GET", "POST"})
  80.      * @Route("/%eccube_admin_route%/content/page/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_page_edit", methods={"GET", "POST"})
  81.      * @Template("@admin/Content/page_edit.twig")
  82.      */
  83.     public function edit(Request $requestEnvironment $twigRouterInterface $routerCacheUtil $cacheUtil$id null)
  84.     {
  85.         $this->addInfoOnce('admin.common.restrict_file_upload_info''admin');
  86.         if (null === $id) {
  87.             $Page $this->pageRepository->newPage();
  88.         } else {
  89.             $Page $this->pageRepository->find($id);
  90.         }
  91.         $isUserDataPage true;
  92.         $builder $this->formFactory
  93.             ->createBuilder(MainEditType::class, $Page);
  94.         $event = new EventArgs(
  95.             [
  96.                 'builder' => $builder,
  97.                 'Page' => $Page,
  98.             ],
  99.             $request
  100.         );
  101.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_PAGE_EDIT_INITIALIZE);
  102.         $form $builder->getForm();
  103.         // 更新時
  104.         $fileName null;
  105.         $namespace '@user_data/';
  106.         $PrevPage = clone $Page;
  107.         if ($id) {
  108.             // 編集不可ページはURL、ページ名、ファイル名を保持
  109.             if ($Page->getEditType() >= Page::EDIT_TYPE_DEFAULT) {
  110.                 $isUserDataPage false;
  111.                 $namespace '';
  112.             }
  113.             // テンプレートファイルの取得
  114.             $source $twig->getLoader()
  115.                 ->getSourceContext($namespace.$Page->getFileName().'.twig')
  116.                 ->getCode();
  117.             $form->get('tpl_data')->setData($source);
  118.             $fileName $Page->getFileName();
  119.         } elseif ($request->getMethod() === 'GET' && !$form->isSubmitted()) {
  120.             $source $twig->getLoader()
  121.                 ->getSourceContext('@admin/empty_page.twig')
  122.                 ->getCode();
  123.             $form->get('tpl_data')->setData($source);
  124.         }
  125.         $form->handleRequest($request);
  126.         if ($form->isSubmitted() && $form->isValid()) {
  127.             $Page $form->getData();
  128.             if (!$isUserDataPage) {
  129.                 $Page
  130.                     ->setUrl($PrevPage->getUrl())
  131.                     ->setFileName($PrevPage->getFileName())
  132.                     ->setName($Page->getName());
  133.             }
  134.             // DB登録
  135.             $this->entityManager->persist($Page);
  136.             $this->entityManager->flush();
  137.             // ファイル生成・更新
  138.             if ($isUserDataPage) {
  139.                 $templatePath $this->getParameter('eccube_theme_user_data_dir');
  140.             } else {
  141.                 $templatePath $this->getParameter('eccube_theme_front_dir');
  142.             }
  143.             $filePath $templatePath.'/'.$Page->getFileName().'.twig';
  144.             $fs = new Filesystem();
  145.             $pageData $form->get('tpl_data')->getData();
  146.             $pageData StringUtil::convertLineFeed($pageData);
  147.             $fs->dumpFile($filePath$pageData);
  148.             // 更新でファイル名を変更した場合、以前のファイルを削除
  149.             if ($Page->getFileName() != $fileName && !is_null($fileName)) {
  150.                 $oldFilePath $templatePath.'/'.$fileName.'.twig';
  151.                 if ($fs->exists($oldFilePath)) {
  152.                     $fs->remove($oldFilePath);
  153.                 }
  154.             }
  155.             foreach ($Page->getPageLayouts() as $PageLayout) {
  156.                 $Page->removePageLayout($PageLayout);
  157.                 $this->entityManager->remove($PageLayout);
  158.                 $this->entityManager->flush();
  159.             }
  160.             $Layout $form['PcLayout']->getData();
  161.             $LastPageLayout $this->pageLayoutRepository->findOneBy([], ['sort_no' => 'DESC']);
  162.             $sortNo $LastPageLayout->getSortNo();
  163.             if ($Layout) {
  164.                 $PageLayout = new PageLayout();
  165.                 $PageLayout->setLayoutId($Layout->getId());
  166.                 $PageLayout->setLayout($Layout);
  167.                 $PageLayout->setPageId($Page->getId());
  168.                 $PageLayout->setSortNo($sortNo++);
  169.                 $PageLayout->setPage($Page);
  170.                 $this->entityManager->persist($PageLayout);
  171.                 $this->entityManager->flush();
  172.             }
  173.             $Layout $form['SpLayout']->getData();
  174.             if ($Layout) {
  175.                 $PageLayout = new PageLayout();
  176.                 $PageLayout->setLayoutId($Layout->getId());
  177.                 $PageLayout->setLayout($Layout);
  178.                 $PageLayout->setPageId($Page->getId());
  179.                 $PageLayout->setSortNo($sortNo++);
  180.                 $PageLayout->setPage($Page);
  181.                 $this->entityManager->persist($PageLayout);
  182.                 $this->entityManager->flush();
  183.             }
  184.             $event = new EventArgs(
  185.                 [
  186.                     'form' => $form,
  187.                     'Page' => $Page,
  188.                     'templatePath' => $templatePath,
  189.                     'filePath' => $filePath,
  190.                 ],
  191.                 $request
  192.             );
  193.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_PAGE_EDIT_COMPLETE);
  194.             $this->addSuccess('admin.common.save_complete''admin');
  195.             // キャッシュの削除
  196.             $cacheUtil->clearTwigCache();
  197.             $cacheUtil->clearDoctrineCache();
  198.             return $this->redirectToRoute('admin_content_page_edit', ['id' => $Page->getId()]);
  199.         }
  200.         if ($isUserDataPage) {
  201.             $templatePath $this->getParameter('eccube_theme_user_data_dir');
  202.             $url '';
  203.         } else {
  204.             $templatePath $this->getParameter('eccube_theme_front_dir');
  205.             $url $router->getRouteCollection()->get($PrevPage->getUrl())->getPath();
  206.         }
  207.         $projectDir $this->getParameter('kernel.project_dir');
  208.         $templatePath str_replace($projectDir.'/'''$templatePath);
  209.         return [
  210.             'form' => $form->createView(),
  211.             'page_id' => $Page->getId(),
  212.             'is_user_data_page' => $isUserDataPage,
  213.             'is_confirm_page' => $Page->getEditType() == Page::EDIT_TYPE_DEFAULT_CONFIRM,
  214.             'template_path' => $templatePath,
  215.             'url' => $url,
  216.         ];
  217.     }
  218.     /**
  219.      * @Route("/%eccube_admin_route%/content/page/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_page_delete", methods={"DELETE"})
  220.      */
  221.     public function delete(Request $requestCacheUtil $cacheUtil$id null)
  222.     {
  223.         $this->isTokenValid();
  224.         $Page $this->pageRepository
  225.             ->findOneBy([
  226.                 'id' => $id,
  227.             ]);
  228.         if (!$Page) {
  229.             $this->deleteMessage();
  230.             return $this->redirectToRoute('admin_content_page');
  231.         }
  232.         // ユーザーが作ったページのみ削除する
  233.         if ($Page->getEditType() == Page::EDIT_TYPE_USER) {
  234.             $templatePath $this->getParameter('eccube_theme_user_data_dir');
  235.             $file $templatePath.'/'.$Page->getFileName().'.twig';
  236.             $fs = new Filesystem();
  237.             if ($fs->exists($file)) {
  238.                 $fs->remove($file);
  239.             }
  240.             $this->entityManager->remove($Page);
  241.             $this->entityManager->flush();
  242.             $event = new EventArgs(
  243.                 [
  244.                     'Page' => $Page,
  245.                 ],
  246.                 $request
  247.             );
  248.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_PAGE_DELETE_COMPLETE);
  249.             $this->addSuccess('admin.common.delete_complete''admin');
  250.             // キャッシュの削除
  251.             $cacheUtil->clearTwigCache();
  252.             $cacheUtil->clearDoctrineCache();
  253.         }
  254.         return $this->redirectToRoute('admin_content_page');
  255.     }
  256. }