src/Eccube/Controller/Admin/Content/BlockController.php line 57

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\Block;
  15. use Eccube\Entity\Master\DeviceType;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Form\Type\Admin\BlockType;
  19. use Eccube\Repository\BlockRepository;
  20. use Eccube\Repository\Master\DeviceTypeRepository;
  21. use Eccube\Util\CacheUtil;
  22. use Eccube\Util\StringUtil;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  24. use Symfony\Component\Filesystem\Filesystem;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Twig\Environment;
  29. class BlockController extends AbstractController
  30. {
  31.     /**
  32.      * @var BlockRepository
  33.      */
  34.     protected $blockRepository;
  35.     /**
  36.      * @var DeviceTypeRepository
  37.      */
  38.     protected $deviceTypeRepository;
  39.     public function __construct(
  40.         BlockRepository $blockRepository,
  41.         DeviceTypeRepository $deviceTypeRepository
  42.     ) {
  43.         $this->blockRepository $blockRepository;
  44.         $this->deviceTypeRepository $deviceTypeRepository;
  45.     }
  46.     /**
  47.      * @Route("/%eccube_admin_route%/content/block", name="admin_content_block", methods={"GET"})
  48.      * @Template("@admin/Content/block.twig")
  49.      */
  50.     public function index(Request $request)
  51.     {
  52.         $DeviceType $this->deviceTypeRepository
  53.             ->find(DeviceType::DEVICE_TYPE_PC);
  54.         // 登録されているブロック一覧の取得
  55.         $Blocks $this->blockRepository->getList($DeviceType);
  56.         $event = new EventArgs(
  57.             [
  58.                 'DeviceType' => $DeviceType,
  59.                 'Blocks' => $Blocks,
  60.             ],
  61.             $request
  62.         );
  63.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_BLOCK_INDEX_COMPLETE);
  64.         return [
  65.             'Blocks' => $Blocks,
  66.         ];
  67.     }
  68.     /**
  69.      * @Route("/%eccube_admin_route%/content/block/new", name="admin_content_block_new", methods={"GET", "POST"})
  70.      * @Route("/%eccube_admin_route%/content/block/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_block_edit", methods={"GET", "POST"})
  71.      * @Template("@admin/Content/block_edit.twig")
  72.      */
  73.     public function edit(Request $requestEnvironment $twigFileSystem $fsCacheUtil $cacheUtil$id null)
  74.     {
  75.         $this->addInfoOnce('admin.common.restrict_file_upload_info''admin');
  76.         $DeviceType $this->deviceTypeRepository
  77.             ->find(DeviceType::DEVICE_TYPE_PC);
  78.         if (null === $id) {
  79.             $Block $this->blockRepository->newBlock($DeviceType);
  80.         } else {
  81.             $Block $this->blockRepository->findOneBy(
  82.                 [
  83.                     'id' => $id,
  84.                     'DeviceType' => $DeviceType,
  85.                 ]
  86.             );
  87.         }
  88.         if (!$Block) {
  89.             throw new NotFoundHttpException();
  90.         }
  91.         $builder $this->formFactory
  92.             ->createBuilder(BlockType::class, $Block);
  93.         $html '';
  94.         $previousFileName null;
  95.         if ($id) {
  96.             $previousFileName $Block->getFileName();
  97.             $html $twig->getLoader()
  98.                 ->getSourceContext('Block/'.$Block->getFileName().'.twig')
  99.                 ->getCode();
  100.         }
  101.         $event = new EventArgs(
  102.             [
  103.                 'builder' => $builder,
  104.                 'DeviceType' => $DeviceType,
  105.                 'Block' => $Block,
  106.                 'html' => $html,
  107.             ],
  108.             $request
  109.         );
  110.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_INITIALIZE);
  111.         $html $event->getArgument('html');
  112.         $form $builder->getForm();
  113.         $form->get('block_html')->setData($html);
  114.         $form->handleRequest($request);
  115.         if ($form->isSubmitted() && $form->isValid()) {
  116.             $Block $form->getData();
  117.             $this->entityManager->persist($Block);
  118.             $this->entityManager->flush();
  119.             $dir sprintf('%s/app/template/%s/Block',
  120.                 $this->getParameter('kernel.project_dir'),
  121.                 $this->getParameter('eccube.theme'));
  122.             $file $dir.'/'.$Block->getFileName().'.twig';
  123.             $source $form->get('block_html')->getData();
  124.             $source StringUtil::convertLineFeed($source);
  125.             $fs->dumpFile($file$source);
  126.             // 更新でファイル名を変更した場合、以前のファイルを削除
  127.             if (null !== $previousFileName && $Block->getFileName() !== $previousFileName) {
  128.                 $old $dir.'/'.$previousFileName.'.twig';
  129.                 if ($fs->exists($old)) {
  130.                     $fs->remove($old);
  131.                 }
  132.             }
  133.             // キャッシュの削除
  134.             $cacheUtil->clearTwigCache();
  135.             $cacheUtil->clearDoctrineCache();
  136.             $event = new EventArgs(
  137.                 [
  138.                     'form' => $form,
  139.                     'Block' => $Block,
  140.                 ],
  141.                 $request
  142.             );
  143.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_BLOCK_EDIT_COMPLETE);
  144.             $this->addSuccess('admin.common.save_complete''admin');
  145.             return $this->redirectToRoute('admin_content_block_edit', ['id' => $Block->getId()]);
  146.         }
  147.         return [
  148.             'form' => $form->createView(),
  149.             'block_id' => $id,
  150.             'deletable' => $Block->isDeletable(),
  151.         ];
  152.     }
  153.     /**
  154.      * @Route("/%eccube_admin_route%/content/block/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_block_delete", methods={"DELETE"})
  155.      */
  156.     public function delete(Request $requestBlock $BlockFilesystem $fsCacheUtil $cacheUtil)
  157.     {
  158.         $this->isTokenValid();
  159.         // ユーザーが作ったブロックのみ削除する
  160.         if ($Block->isDeletable()) {
  161.             $dir sprintf('%s/app/template/%s/Block',
  162.                 $this->getParameter('kernel.project_dir'),
  163.                 $this->getParameter('eccube.theme'));
  164.             $file $dir.'/'.$Block->getFileName().'.twig';
  165.             if ($fs->exists($file)) {
  166.                 $fs->remove($file);
  167.             }
  168.             $this->entityManager->remove($Block);
  169.             $this->entityManager->flush();
  170.             $event = new EventArgs(
  171.                 [
  172.                     'Block' => $Block,
  173.                 ],
  174.                 $request
  175.             );
  176.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_CONTENT_BLOCK_DELETE_COMPLETE);
  177.             $this->addSuccess('admin.common.delete_complete''admin');
  178.             // キャッシュの削除
  179.             $cacheUtil->clearTwigCache();
  180.             $cacheUtil->clearDoctrineCache();
  181.         }
  182.         return $this->redirectToRoute('admin_content_block');
  183.     }
  184. }