<?php
/*
* This file is part of Customize of EC-CUBE by Kenji Nakanishi
*
* Copyright(c) 2021 Kenji Nakanishi. All Rights Reserved.
*
* https://www.facebook.com/web.kenji.nakanishi
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\SheebDlc42;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Common\EccubeConfig;
use Eccube\Entity\Product;
use Eccube\Entity\ProductClass;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\ProductRepository;
use Plugin\SheebDlc42\Entity\Config;
use Plugin\SheebDlc42\Entity\DownloadContent;
use Plugin\SheebDlc42\Entity\ProductClassContent;
use Plugin\SheebDlc42\Form\Extension\ProductType;
use Plugin\SheebDlc42\Repository\ConfigRepository;
use Plugin\SheebDlc42\Service\SaveFile\SaveFileModuleFactory;
use Symfony\Component\Asset\Packages;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Filesystem\Filesystem;
class Event implements EventSubscriberInterface
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
// 管理画面「商品詳細画面」読み込み・描画処理への介入
'@admin/Product/product.twig' => ['onTemplateProductEdit', 10],
// 管理画面「受注詳細画面」項目追加「手動利用可能基準日時」
'@admin/Order/edit.twig' => ['onTemplateOrderEdit', 10],
// 管理画面「商品詳細画面」保存処理への介入
EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => ['onProductEditComplete', 10],
// 管理画面「商品詳細画面」削除処理への介入
EccubeEvents::ADMIN_PRODUCT_DELETE_COMPLETE => ['onProductEditDelete', 10],
// フロント画面 会員ログイン画面
'Shopping/login.twig' => ['onTemplateShoppingLogin', 10],
// フロント画面 注文手続画面
'Shopping/index.twig' => ['onTemplateShoppingIndex', 10],
// フロント画面 注文確認画面
'Shopping/confirm.twig' => ['onTemplateShoppingConfirm', 10],
// フロント画面 マイページ 注文履歴詳細
'Mypage/history.twig' => ['onTemplateMypageHistory', 10],
// フロント画面 マイページのその他全ページ(共通ヘッダを修正するだけ)
'Mypage/index.twig' => ['onTemplateMypageNavi', 10],
'Mypage/change.twig' => ['onTemplateMypageNavi', 10],
'Mypage/change_complete.twig' => ['onTemplateMypageNavi', 10],
'Mypage/delivery.twig' => ['onTemplateMypageNavi', 10],
'Mypage/delivery_edit.twig' => ['onTemplateMypageNavi', 10],
'Mypage/favorite.twig' => ['onTemplateMypageNavi', 10],
'Mypage/withdraw.twig' => ['onTemplateMypageNavi', 10],
'Mypage/withdraw_confirm.twig' => ['onTemplateMypageNavi', 10],
'Mypage/withdraw_complete.twig' => ['onTemplateMypageNavi', 10],
];
}
/**
* @var EccubeConfig
*/
private $eccubeConfig;
/**
* @var ProductRepository
*/
private $product_repository;
/**
* @var ProductClassContentRepository
*/
private $product_class_content_repository;
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var Packages
*/
private $assets;
public function __construct(EccubeConfig $eccubeConfig, ProductRepository $product_repository, EntityManagerInterface $em, Packages $package)
{
$this->eccubeConfig = $eccubeConfig;
$this->product_repository = $product_repository;
$this->em = $em;
$this->assets = $package;
$this->product_class_content_repository = $em->getRepository(ProductClassContent::class);
}
/**
* 管理画面 商品管理 詳細画面
*/
public function onTemplateProductEdit(TemplateEvent $templateEvent)
{
// フロントエンド系
$templateEvent->addSnippet('@SheebDlc42/Modules/content_view.twig');
$templateEvent->addSnippet('@SheebDlc42/Admin/Product/product.twig');
}
/**
* 管理画面「受注詳細画面」項目追加「手動利用可能基準日時」
* @param TemplateEvent $templateEvent
*/
public function onTemplateOrderEdit(TemplateEvent $templateEvent)
{
// フロントエンド系
$templateEvent->addSnippet('@SheebDlc42/Admin/Order/edit.twig');
}
/**
* 管理画面 商品管理 詳細画面 保存時
*
* @throws \Exception
*/
public function onProductEditComplete(EventArgs $event)
{
/**
* @var $Product Product
* @var $ProductClass ProductClass
* @var $configRepository ConfigRepository
*/
$Product = $event->getArgument('Product');
// 販売種別「ダウンロードコンテンツ」以外では何もしない
$ProductClass = $Product->getProductClasses()->current();
if ($ProductClass->getSaleType()->getId() !== PluginManager::getDlcSaleType($this->em)->getId()) {
return;
}
/*
* 数値系はデフォルトは0
*/
switch ($Product->getSheebDlc42SettingType()) {
case ProductType::TERM:
$Product->setSheebDlc42Startline(null);
$Product->setSheebDlc42Deadline(null);
if (empty($Product->getSheebDlc42DownloadDueDays())) {
$Product->setSheebDlc42DownloadDueDays(PluginManager::DEFAULT_DOWNLOAD_DUE_DAYS);
}
if (empty($Product->getSheebDlc42ViewingDays())) {
$Product->setSheebDlc42ViewingDays(PluginManager::DEFAULT_VIEWING_DAYS);
}
break;
case ProductType::DATETIME:
$Product->setSheebDlc42DownloadDueDays(0);
$Product->setSheebDlc42ViewingDays(0);
if (empty($Product->getSheebDlc42Startline())) {
$Product->setSheebDlc42Startline(new \DateTime());
}
if (empty($Product->getSheebDlc42Deadline())) {
$Product->setSheebDlc42Deadline(null);
}
break;
}
$this->em->persist($Product);
$this->em->flush();
}
/**
* 管理画面 商品管理 削除時
*
* @throws \Exception
*/
public function onProductEditDelete(EventArgs $event)
{
$configRepository = $this->em->getRepository(Config::class);
/**
* @var $Product Product
* @var $configRepository ConfigRepository
*/
$Product = $event->getArgument('Product');
foreach ($Product->getProductClasses() as $ProductClass) {
foreach ($ProductClass->getProductClassContent() as $ProductClassContent) {
/* @var $DownloadContent DownloadContent */
$DownloadContent = $ProductClassContent->getContent();
if (!empty($DownloadContent)) {
// ファイル削除
$module = SaveFileModuleFactory::get($this->eccubeConfig, $configRepository->get(), $this->assets, $DownloadContent->getSheebDownloadContent());
$module->remove($DownloadContent);
}
}
}
}
/**
* フロント画面 会員ログイン画面
*/
public function onTemplateShoppingLogin(TemplateEvent $templateEvent)
{
$templateEvent->addSnippet('@SheebDlc42/Shopping/login.twig');
}
/**
* フロント画面 注文手続画面
*/
public function onTemplateShoppingIndex(TemplateEvent $templateEvent)
{
if (defined('EXIST_DOWNLOAD_CONTENT')) {
$templateEvent->addSnippet('@SheebDlc42/Shopping/index.twig');
}
}
/**
* フロント画面 注文確認画面
*/
public function onTemplateShoppingConfirm(TemplateEvent $templateEvent)
{
if (defined('EXIST_DOWNLOAD_CONTENT')) {
$templateEvent->addSnippet('@SheebDlc42/Shopping/confirm.twig');
}
}
public function onTemplateMypageNavi(TemplateEvent $templateEvent)
{
$templateEvent->addSnippet('@SheebDlc42/Mypage/navi.twig');
}
public function onTemplateMypageHistory(TemplateEvent $templateEvent)
{
$templateEvent->addSnippet('@SheebDlc42/Mypage/history.twig');
$contentMap = [];
$Order = $templateEvent->getParameter('Order');
foreach ($Order->getProductOrderItems() as $OrderItem) {
$oid = $OrderItem->getId();
$ProductClass = $OrderItem->getProductClass();
$contentMap[$oid] = [
'name' => $OrderItem->getProductName(),
'contents' => $this->product_class_content_repository->findBy(
[ 'ProductClass' => $ProductClass ],
[ 'sheeb_dlc42_sort_number' => 'ASC' ],
)
];
}
$templateEvent->setParameter('contentHolders', $contentMap);
}
}