app/Plugin/SheebDlc4/Event.php line 218

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Customize of EC-CUBE by Kenji Nakanishi
  4.  *
  5.  * Copyright(c) 2021 Kenji Nakanishi. All Rights Reserved.
  6.  *
  7.  * https://www.facebook.com/web.kenji.nakanishi
  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 Plugin\SheebDlc4;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Common\EccubeConfig;
  15. use Eccube\Entity\Product;
  16. use Eccube\Entity\ProductClass;
  17. use Eccube\Event\EccubeEvents;
  18. use Eccube\Event\EventArgs;
  19. use Eccube\Event\TemplateEvent;
  20. use Eccube\Repository\ProductRepository;
  21. use Plugin\SheebDlc4\Entity\Config;
  22. use Plugin\SheebDlc4\Entity\DownloadContent;
  23. use Plugin\SheebDlc4\Form\Extension\ProductType;
  24. use Plugin\SheebDlc4\Repository\ConfigRepository;
  25. use Plugin\SheebDlc4\Service\SaveFile\SaveFileModuleFactory;
  26. use Symfony\Component\Asset\Packages;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. use Symfony\Component\Filesystem\Filesystem;
  29. class Event implements EventSubscriberInterface
  30. {
  31.     /**
  32.      * @return array
  33.      */
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             // 管理画面「商品詳細画面」読み込み・描画処理への介入
  38.             '@admin/Product/product.twig' => ['onTemplateProductEdit'10],
  39.             // 管理画面「商品詳細画面」保存処理への介入
  40.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => ['onProductEditComplete'10],
  41.             // 管理画面「商品詳細画面」削除処理への介入
  42.             EccubeEvents::ADMIN_PRODUCT_DELETE_COMPLETE => ['onProductEditDelete'10],
  43.             // フロント画面 会員ログイン画面
  44.             'Shopping/login.twig' => ['onTemplateShoppingLogin'10],
  45.             // フロント画面 注文手続画面
  46.             'Shopping/index.twig' => ['onTemplateShoppingIndex'10],
  47.             // フロント画面 注文確認画面
  48.             'Shopping/confirm.twig' => ['onTemplateShoppingConfirm'10],
  49.             // フロント画面 マイページ 注文履歴詳細
  50.             'Mypage/history.twig' => ['onTemplateMypageHistory'10],
  51.             // フロント画面 マイページのその他全ページ(共通ヘッダを修正するだけ)
  52.             'Mypage/index.twig' => ['onTemplateMypageNavi'10],
  53.             'Mypage/change.twig' => ['onTemplateMypageNavi'10],
  54.             'Mypage/change_complete.twig' => ['onTemplateMypageNavi'10],
  55.             'Mypage/delivery.twig' => ['onTemplateMypageNavi'10],
  56.             'Mypage/delivery_edit.twig' => ['onTemplateMypageNavi'10],
  57.             'Mypage/favorite.twig' => ['onTemplateMypageNavi'10],
  58.             'Mypage/withdraw.twig' => ['onTemplateMypageNavi'10],
  59.             'Mypage/withdraw_confirm.twig' => ['onTemplateMypageNavi'10],
  60.             'Mypage/withdraw_complete.twig' => ['onTemplateMypageNavi'10],
  61.         ];
  62.     }
  63.     /**
  64.      * @var EccubeConfig
  65.      */
  66.     private $eccubeConfig;
  67.     /**
  68.      * @var ProductRepository
  69.      */
  70.     private $product_repository;
  71.     /**
  72.      * @var EntityManagerInterface
  73.      */
  74.     private $em;
  75.     /**
  76.      * @var Packages
  77.      */
  78.     private $assets;
  79.     public function __construct(EccubeConfig $eccubeConfigProductRepository $product_repositoryEntityManagerInterface $emPackages $package)
  80.     {
  81.         $this->eccubeConfig $eccubeConfig;
  82.         $this->product_repository $product_repository;
  83.         $this->em $em;
  84.         $this->assets $package;
  85.     }
  86.     /**
  87.      * 管理画面 商品管理 詳細画面
  88.      */
  89.     public function onTemplateProductEdit(TemplateEvent $templateEvent)
  90.     {
  91.         // フロントエンド系
  92.         $templateEvent->addSnippet('@SheebDlc4/Modules/content_view.twig');
  93.         $templateEvent->addSnippet('@SheebDlc4/Admin/Product/product.twig');
  94.     }
  95.     /**
  96.      * 管理画面 商品管理 詳細画面 保存時
  97.      *
  98.      * @throws \Exception
  99.      */
  100.     public function onProductEditComplete(EventArgs $event)
  101.     {
  102.         /**
  103.          * @var $Product Product
  104.          * @var $ProductClass ProductClass
  105.          * @var $configRepository ConfigRepository
  106.          */
  107.         $Product $event->getArgument('Product');
  108.         // 販売種別「ダウンロードコンテンツ」以外では何もしない
  109.         $ProductClass $Product->getProductClasses()->current();
  110.         if ($ProductClass->getSaleType()->getId() !== PluginManager::getDlcSaleType($this->em)->getId()) {
  111.             return;
  112.         }
  113.         /*
  114.          * 数値系はデフォルトは0
  115.          */
  116.         switch ($Product->getSheebDlc4SettingType()) {
  117.             case ProductType::TERM:
  118.                 $Product->setSheebDlc4Startline(null);
  119.                 $Product->setSheebDlc4Deadline(null);
  120.                 if (empty($Product->getSheebDlc4DownloadDueDays())) {
  121.                     $Product->setSheebDlc4DownloadDueDays(PluginManager::DEFAULT_DOWNLOAD_DUE_DAYS);
  122.                 }
  123.                 if (empty($Product->getSheebDlc4ViewingDays())) {
  124.                     $Product->setSheebDlc4ViewingDays(PluginManager::DEFAULT_VIEWING_DAYS);
  125.                 }
  126.                 break;
  127.             case ProductType::DATETIME:
  128.                 $Product->setSheebDlc4DownloadDueDays(0);
  129.                 $Product->setSheebDlc4ViewingDays(0);
  130.                 if (empty($Product->getSheebDlc4Startline())) {
  131.                     $Product->setSheebDlc4Startline(new \DateTime());
  132.                 }
  133.                 if (empty($Product->getSheebDlc4Deadline())) {
  134.                     $Product->setSheebDlc4Deadline(null);
  135.                 }
  136.                 break;
  137.         }
  138.         $this->em->persist($Product);
  139.         $this->em->flush();
  140.     }
  141.     /**
  142.      * 管理画面 商品管理 削除時
  143.      *
  144.      * @throws \Exception
  145.      */
  146.     public function onProductEditDelete(EventArgs $event)
  147.     {
  148.         $configRepository $this->em->getRepository(Config::class);
  149.         /**
  150.          * @var $Product Product
  151.          * @var $configRepository ConfigRepository
  152.          */
  153.         $Product $event->getArgument('Product');
  154.         $Contents $Product->getContents();
  155.         /* @var $DownloadContent DownloadContent */
  156.         foreach ($Contents as $DownloadContent) {
  157.             // ファイル削除
  158.             $module SaveFileModuleFactory::get($this->eccubeConfig$configRepository->get(), $this->assets$DownloadContent->getSheebDownloadContent());
  159.             $module->remove($DownloadContent);
  160.         }
  161.     }
  162.     /**
  163.      * フロント画面 会員ログイン画面
  164.      */
  165.     public function onTemplateShoppingLogin(TemplateEvent $templateEvent)
  166.     {
  167.         $templateEvent->addSnippet('@SheebDlc4/Shopping/login.twig');
  168.     }
  169.     /**
  170.      * フロント画面 注文手続画面
  171.      */
  172.     public function onTemplateShoppingIndex(TemplateEvent $templateEvent)
  173.     {
  174.         if (defined('EXIST_DOWNLOAD_CONTENT')) {
  175.             $templateEvent->addSnippet('@SheebDlc4/Shopping/index.twig');
  176.         }
  177.     }
  178.     /**
  179.      * フロント画面 注文確認画面
  180.      */
  181.     public function onTemplateShoppingConfirm(TemplateEvent $templateEvent)
  182.     {
  183.         if (defined('EXIST_DOWNLOAD_CONTENT')) {
  184.             $templateEvent->addSnippet('@SheebDlc4/Shopping/confirm.twig');
  185.         }
  186.     }
  187.     public function onTemplateMypageNavi(TemplateEvent $templateEvent)
  188.     {
  189.         $templateEvent->addSnippet('@SheebDlc4/Mypage/navi.twig');
  190.     }
  191.     public function onTemplateMypageHistory(TemplateEvent $templateEvent)
  192.     {
  193.         $templateEvent->addSnippet('@SheebDlc4/Mypage/history.twig');
  194.     }
  195. }