app/Plugin/SheebDlc42/Event.php line 212

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\SheebDlc42;
  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\SheebDlc42\Entity\Config;
  22. use Plugin\SheebDlc42\Entity\DownloadContent;
  23. use Plugin\SheebDlc42\Entity\ProductClassContent;
  24. use Plugin\SheebDlc42\Form\Extension\ProductType;
  25. use Plugin\SheebDlc42\Repository\ConfigRepository;
  26. use Plugin\SheebDlc42\Service\SaveFile\SaveFileModuleFactory;
  27. use Symfony\Component\Asset\Packages;
  28. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  29. use Symfony\Component\Filesystem\Filesystem;
  30. class Event implements EventSubscriberInterface
  31. {
  32.     /**
  33.      * @return array
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             // 管理画面「商品詳細画面」読み込み・描画処理への介入
  39.             '@admin/Product/product.twig' => ['onTemplateProductEdit'10],
  40.             // 管理画面「受注詳細画面」項目追加「手動利用可能基準日時」
  41.             '@admin/Order/edit.twig' => ['onTemplateOrderEdit'10],
  42.             // 管理画面「商品詳細画面」保存処理への介入
  43.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => ['onProductEditComplete'10],
  44.             // 管理画面「商品詳細画面」削除処理への介入
  45.             EccubeEvents::ADMIN_PRODUCT_DELETE_COMPLETE => ['onProductEditDelete'10],
  46.             // フロント画面 会員ログイン画面
  47.             'Shopping/login.twig' => ['onTemplateShoppingLogin'10],
  48.             // フロント画面 注文手続画面
  49.             'Shopping/index.twig' => ['onTemplateShoppingIndex'10],
  50.             // フロント画面 注文確認画面
  51.             'Shopping/confirm.twig' => ['onTemplateShoppingConfirm'10],
  52.             // フロント画面 マイページ 注文履歴詳細
  53.             'Mypage/history.twig' => ['onTemplateMypageHistory'10],
  54.             // フロント画面 マイページのその他全ページ(共通ヘッダを修正するだけ)
  55.             'Mypage/index.twig' => ['onTemplateMypageNavi'10],
  56.             'Mypage/change.twig' => ['onTemplateMypageNavi'10],
  57.             'Mypage/change_complete.twig' => ['onTemplateMypageNavi'10],
  58.             'Mypage/delivery.twig' => ['onTemplateMypageNavi'10],
  59.             'Mypage/delivery_edit.twig' => ['onTemplateMypageNavi'10],
  60.             'Mypage/favorite.twig' => ['onTemplateMypageNavi'10],
  61.             'Mypage/withdraw.twig' => ['onTemplateMypageNavi'10],
  62.             'Mypage/withdraw_confirm.twig' => ['onTemplateMypageNavi'10],
  63.             'Mypage/withdraw_complete.twig' => ['onTemplateMypageNavi'10],
  64.         ];
  65.     }
  66.     /**
  67.      * @var EccubeConfig
  68.      */
  69.     private $eccubeConfig;
  70.     /**
  71.      * @var ProductRepository
  72.      */
  73.     private $product_repository;
  74.     /**
  75.      * @var ProductClassContentRepository
  76.      */
  77.     private $product_class_content_repository;
  78.     /**
  79.      * @var EntityManagerInterface
  80.      */
  81.     private $em;
  82.     /**
  83.      * @var Packages
  84.      */
  85.     private $assets;
  86.     public function __construct(EccubeConfig $eccubeConfigProductRepository $product_repositoryEntityManagerInterface $emPackages $package)
  87.     {
  88.         $this->eccubeConfig $eccubeConfig;
  89.         $this->product_repository $product_repository;
  90.         $this->em $em;
  91.         $this->assets $package;
  92.         $this->product_class_content_repository $em->getRepository(ProductClassContent::class);
  93.     }
  94.     /**
  95.      * 管理画面 商品管理 詳細画面
  96.      */
  97.     public function onTemplateProductEdit(TemplateEvent $templateEvent)
  98.     {
  99.         // フロントエンド系
  100.         $templateEvent->addSnippet('@SheebDlc42/Modules/content_view.twig');
  101.         $templateEvent->addSnippet('@SheebDlc42/Admin/Product/product.twig');
  102.     }
  103.     /**
  104.      * 管理画面「受注詳細画面」項目追加「手動利用可能基準日時」
  105.      * @param TemplateEvent $templateEvent
  106.      */
  107.     public function onTemplateOrderEdit(TemplateEvent $templateEvent)
  108.     {
  109.         // フロントエンド系
  110.         $templateEvent->addSnippet('@SheebDlc42/Admin/Order/edit.twig');
  111.     }
  112.     /**
  113.      * 管理画面 商品管理 詳細画面 保存時
  114.      *
  115.      * @throws \Exception
  116.      */
  117.     public function onProductEditComplete(EventArgs $event)
  118.     {
  119.         /**
  120.          * @var $Product Product
  121.          * @var $ProductClass ProductClass
  122.          * @var $configRepository ConfigRepository
  123.          */
  124.         $Product $event->getArgument('Product');
  125.         // 販売種別「ダウンロードコンテンツ」以外では何もしない
  126.         $ProductClass $Product->getProductClasses()->current();
  127.         if ($ProductClass->getSaleType()->getId() !== PluginManager::getDlcSaleType($this->em)->getId()) {
  128.             return;
  129.         }
  130.         /*
  131.          * 数値系はデフォルトは0
  132.          */
  133.         switch ($Product->getSheebDlc42SettingType()) {
  134.             case ProductType::TERM:
  135.                 $Product->setSheebDlc42Startline(null);
  136.                 $Product->setSheebDlc42Deadline(null);
  137.                 if (empty($Product->getSheebDlc42DownloadDueDays())) {
  138.                     $Product->setSheebDlc42DownloadDueDays(PluginManager::DEFAULT_DOWNLOAD_DUE_DAYS);
  139.                 }
  140.                 if (empty($Product->getSheebDlc42ViewingDays())) {
  141.                     $Product->setSheebDlc42ViewingDays(PluginManager::DEFAULT_VIEWING_DAYS);
  142.                 }
  143.                 break;
  144.             case ProductType::DATETIME:
  145.                 $Product->setSheebDlc42DownloadDueDays(0);
  146.                 $Product->setSheebDlc42ViewingDays(0);
  147.                 if (empty($Product->getSheebDlc42Startline())) {
  148.                     $Product->setSheebDlc42Startline(new \DateTime());
  149.                 }
  150.                 if (empty($Product->getSheebDlc42Deadline())) {
  151.                     $Product->setSheebDlc42Deadline(null);
  152.                 }
  153.                 break;
  154.         }
  155.         $this->em->persist($Product);
  156.         $this->em->flush();
  157.     }
  158.     /**
  159.      * 管理画面 商品管理 削除時
  160.      *
  161.      * @throws \Exception
  162.      */
  163.     public function onProductEditDelete(EventArgs $event)
  164.     {
  165.         $configRepository $this->em->getRepository(Config::class);
  166.         /**
  167.          * @var $Product Product
  168.          * @var $configRepository ConfigRepository
  169.          */
  170.         $Product $event->getArgument('Product');
  171.         foreach ($Product->getProductClasses() as $ProductClass) {
  172.             foreach ($ProductClass->getProductClassContent() as $ProductClassContent) {
  173.                 /* @var $DownloadContent DownloadContent */
  174.                 $DownloadContent $ProductClassContent->getContent();
  175.                 if (!empty($DownloadContent)) {
  176.                     // ファイル削除
  177.                     $module SaveFileModuleFactory::get($this->eccubeConfig$configRepository->get(), $this->assets$DownloadContent->getSheebDownloadContent());
  178.                     $module->remove($DownloadContent);
  179.                 }
  180.             }
  181.         }
  182.     }
  183.     /**
  184.      * フロント画面 会員ログイン画面
  185.      */
  186.     public function onTemplateShoppingLogin(TemplateEvent $templateEvent)
  187.     {
  188.         $templateEvent->addSnippet('@SheebDlc42/Shopping/login.twig');
  189.     }
  190.     /**
  191.      * フロント画面 注文手続画面
  192.      */
  193.     public function onTemplateShoppingIndex(TemplateEvent $templateEvent)
  194.     {
  195.         if (defined('EXIST_DOWNLOAD_CONTENT')) {
  196.             $templateEvent->addSnippet('@SheebDlc42/Shopping/index.twig');
  197.         }
  198.     }
  199.     /**
  200.      * フロント画面 注文確認画面
  201.      */
  202.     public function onTemplateShoppingConfirm(TemplateEvent $templateEvent)
  203.     {
  204.         if (defined('EXIST_DOWNLOAD_CONTENT')) {
  205.             $templateEvent->addSnippet('@SheebDlc42/Shopping/confirm.twig');
  206.         }
  207.     }
  208.     public function onTemplateMypageNavi(TemplateEvent $templateEvent)
  209.     {
  210.         $templateEvent->addSnippet('@SheebDlc42/Mypage/navi.twig');
  211.     }
  212.     public function onTemplateMypageHistory(TemplateEvent $templateEvent)
  213.     {
  214.         $templateEvent->addSnippet('@SheebDlc42/Mypage/history.twig');
  215.         $contentMap = [];
  216.         $Order $templateEvent->getParameter('Order');
  217.         foreach ($Order->getProductOrderItems() as $OrderItem) {
  218.             $oid $OrderItem->getId();
  219.             $ProductClass $OrderItem->getProductClass();
  220.             $contentMap[$oid] = [
  221.                 'name' => $OrderItem->getProductName(),
  222.                 'contents' => $this->product_class_content_repository->findBy(
  223.                     [ 'ProductClass' => $ProductClass ],
  224.                     [ 'sheeb_dlc42_sort_number' => 'ASC' ],
  225.                 )
  226.             ];
  227.         }
  228.         $templateEvent->setParameter('contentHolders'$contentMap);
  229.     }
  230. }