<?php
namespace Customize\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Customize\Service\MailService;
use Eccube\Service\CartService;
use Eccube\Common\EccubeConfig;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Customize\Repository\SlnRegularOrderSubRepository;
use Customize\Repository\SlnRegularSaleTypeSubRepository;
use Eccube\Repository\Master\SaleTypeRepository;
use Customize\Repository\SaleTypeSubRepository;
use Customize\Repository\MPCustomConfigRepository;
use Customize\Repository\CustomSaleTypeRepository;
use Customize\Service\Method\MethodUtils;
use Customize\Service\Util;
use Twig_Environment;
//
class MPEventListener implements EventSubscriberInterface
{
/**
* @var EccubeConfig
*/
protected $eccubeConfig;
/**
* @var CartService
*/
private $cartService;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @var SlnRegularOrderSubRepository
*/
private $regularOrderSubRepository;
/**
* @var SlnRegularSaleTypeSubRepository
*/
protected $regularSaleTypeSubRepository;
/**
* @var SaleTypeSubRepository
*/
protected $saleTypeSubRepository;
/**
* @var MPCustomConfigRepository
*/
protected $mpCustomConfigRepository;
/**
* @var Twig_Environment
*/
protected $twig;
/**
* @var CustomSaleTypeRepository
*/
protected $customSaleTypeRepository;
/**
* @var Util
*/
protected $util;
/**
* コンストラクタ
*/
public function __construct(
SlnRegularOrderSubRepository $regularOrderSubRepository,
SlnRegularSaleTypeSubRepository $regularSaleTypeSubRepository,
MPCustomConfigRepository $mpCustomConfigRepository,
SaleTypeSubRepository $saleTypeSubRepository,
EccubeConfig $eccubeConfig,
CartService $cartService,
TokenStorageInterface $tokenStorage,
Twig_Environment $twig,
CustomSaleTypeRepository $customSaleTypeRepository,
Util $util
) {
$this->regularOrderSubRepository = $regularOrderSubRepository;
$this->regularSaleTypeSubRepository = $regularSaleTypeSubRepository;
$this->saleTypeSubRepository = $saleTypeSubRepository;
$this->mpCustomConfigRepository = $mpCustomConfigRepository;
$this->eccubeConfig = $eccubeConfig;
$this->cartService = $cartService;
$this->tokenStorage = $tokenStorage;
$this->twig = $twig;
$this->customSaleTypeRepository = $customSaleTypeRepository;
$this->util = $util;
}
/**
* リッスンしたいサブスクライバのイベント名の配列を返します。
* 配列のキーはイベント名、値は以下のどれかをしてします。
* - 呼び出すメソッド名
* - 呼び出すメソッド名と優先度の配列
* - 呼び出すメソッド名と優先度の配列の配列
* 優先度を省略した場合は0
*
* 例:
* - array('eventName' => 'methodName')
* - array('eventName' => array('methodName', $priority))
* - array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
// // マイページから、会員情報を更新た場合のイベントをフック
// EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_COMPLETE => 'onMypageChangeIndexComplete',
// // マイページの会員情報を表示したときのイベントをフック
// EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_INITIALIZE => 'onMypageChangeIndexInitialize',
// ↓EC-CUBEの標準(Twigへの介入)
'@admin/Product/product.twig' => 'onRenderAdminProduct',
'Mypage/withdraw.twig' => 'onMypageTwig',
'Cart/index.twig' => 'onCartindexTwig',
'Shopping/index.twig' => 'onShoppingindexTwig',
'Shopping/confirm.twig' => 'onShoppingconfirmTwig',
'admin/Customer/index.twig' => 'onAdminCustomerIndexTwig',
'admin/Customer/edit.twig' => 'onAdminCustomerEditTwig',
'Product/detail.twig' => 'onProductDetailTwig',
'Product/list.twig' => 'onProductListTwig',
'Mypage/history.twig' => 'onMypageHistoryTwig',
// ↓は定期受注プラグインに介入するためのもの
'@SlnRegular4/admin/sale_type.twig' => 'onRegular4SaleTypeTwig',
'@SlnRegular4/admin/Order/regular_edit.twig' => 'onRegular4AdminOrderEditTwig',
'@SlnRegular4/Mypage/regular_order.twig' => 'onRegular4MypageRegularOrderTwig',
'@SlnRegular4/Mypage/regular_history.twig' => 'onRegular4MypageRegularHistoryTwig',
// 以下のTwigには介入できなかった
// '@SlnRegular4/Shopping/index.twig' => 'onRegular4ShoppingIndexTwig',
'@admin/Order/edit.twig' => 'onAdminOrderEditTwig',
'@admin/Setting/Shop/shop_master.twig' => 'onAdminSettingShopMasterTwig',
'@admin/Setting/Shop/payment_edit.twig' => 'onAdminSettingShopPaymentEditTwig'
];
}
/**
* 【フロント】マイページ画面介入
*/
public function onMypageTwig(TemplateEvent $event) {
log_info('[onMypageTwig]を検出しました');
$event->addSnippet('Mypage/withdraw_custom.twig');
}
/**
* フロント画面 - 商品詳細画面介入
*/
public function onProductDetailTwig(TemplateEvent $event) {
$event->addSnippet('Product/detail_sub.twig');
$parameters = $event->getParameters();
$product = $event->getParameter('Product');
$event->setParameter('parameters', $parameters);
// $customSaleType = null;
// 「カートリッジ交換お問い合わせ設定」を取得する。
$customSaleTypeExchageCartridge = $this->util->isExchageCartridgeByProduct($product);
// 「レンタル申し込み対象設定」を取得する。
$customSaleTypeRental = $this->util->isRentalByProduct($product);
// $event->setParameter('customSaleType', $customSaleType);
$event->setParameter('customSaleTypeExchageCartridge', $customSaleTypeExchageCartridge);
$event->setParameter('customSaleTypeRental', $customSaleTypeRental);
}
/**
* フロント画面 - 商品一覧画面介入
*/
public function onProductListTwig(TemplateEvent $event) {
// 連携用「販売種別」情報を配列として取得し、Twig内で確認する。
$setCustomsaletype = [];
$customsaletypes = $this->customSaleTypeRepository->findAll();
if(!$customsaletypes) {
$setCustomsaletype = [];
} else {
foreach ($customsaletypes as $customsaletype) {
$setCustomsaletype[$customsaletype->saleTypeId] = $customsaletype;
}
}
$event->setParameter('CustomSaleType', $setCustomsaletype);
}
// 【フロント】カート画面 - ショッピングカート介入
public function onCartindexTwig(TemplateEvent $event) {
log_info('[onCartindexTwig]を検出しました');
$Order = null;
$Carts = $event->getParameter('Carts');
$isRegularOrder = $this->isRegularOrderByCarts($Carts);
$event->addSnippet('Shopping/autoshipment.twig');
$event->setParameter('isRegularOrder', $isRegularOrder);
}
// 【フロント】カート画面 - ご注文手続き介入
public function onShoppingindexTwig(TemplateEvent $event) {
log_info('[onShoppingindexTwig]を検出しました');
$Order = $event->getParameter('Order');
$isRegularOrder = $this->isRegularOrderByOrder($Order);
$event->addSnippet('Shopping/autoshipment.twig');
// $event->addSnippet('Shopping/autoshipment_info.twig');
// 定期受注対象の注文情報か確認
$event->setParameter('isRegularOrder', $isRegularOrder);
// 本体(取付工事依頼表示)設定の有無を確認
$event->setParameter('IsInstallationWorkRequest',$this->util->isInstallationWorkRequestByOrder($Order));
// カートリッジ交換サポート申し込み対象か確認
$event->setParameter('IsChangeCartridge', $this->util->isChangeCartridgeByOrder($Order));
// レンタル申し込み対象か確認
$event->setParameter('IsRentalItem', $this->util->isRentalByOrder($Order));
// オートシップメント申込対象か確認
$event->setParameter('IsAutoShipment', $this->util->isAutoShipmentByOrder($Order));
}
// 【フロント】カート画面 - ご注文内容のご確認介入
public function onShoppingconfirmTwig(TemplateEvent $event) {
log_info('[onShoppingconfirmTwig]を検出しました');
$Order = $event->getParameter('Order');
$isRegularOrder = $this->isRegularOrderByOrder($Order);
$event->addSnippet('Shopping/autoshipment.twig');
// 定期受注対象の注文情報か確認
$event->setParameter('isRegularOrder', $isRegularOrder);
// 本体(取付工事依頼表示)設定の有無を確認
$event->setParameter('IsInstallationWorkRequest', $this->util->isInstallationWorkRequestByOrder($Order));
// カートリッジ交換サポート申し込み対象か確認
$event->setParameter('IsChangeCartridge', $this->util->isChangeCartridgeByOrder($Order));
// レンタル申し込み対象か確認
$event->setParameter('IsRentalItem', $this->util->isRentalByOrder($Order));
// オートシップメント申込対象か確認
$event->setParameter('IsAutoShipment', $this->util->isAutoShipmentByOrder($Order));
// 「注文する」ボタンの文字列を変更する
if ($Order) {
$methodClass = $Order->getPayment()->getMethodClass();
if (MethodUtils::isJaccsWebbyPaymentMethodByOrder($Order)){
// 支払方法が、「JACCS:分割(金利手数料弊社負担)」の場合
$event->addSnippet('JACCS_WeBBy/jaccs_webby_shopping_confirm.twig');
}
}
}
// 定期受注プラグインの設定画面(編集)に介入
public function onRegular4SaleTypeTwig(TemplateEvent $event){
// 定期受注間隔:毎年を追加(カスタマイズ)
$event->addSnippet('admin/sale_type_yearly.twig');
}
// 管理画面の会員一覧に介入
public function onAdminCustomerIndexTwig(TemplateEvent $event){
// $event->addSnippet('admin/sale_type_yearly.twig');
}
// 管理画面の会員編集に介入
public function onAdminCustomerEditTwig(TemplateEvent $event){
$event->addSnippet('admin/info.twig');
}
/**
* 管理画面:商品登録画面に「本体(取付工事依頼表示)」項目を表示する.
*
* @param TemplateEvent $event
*/
public function onRenderAdminProduct(TemplateEvent $event)
{
$product = $event->getParameter('Product');
// // 「カートリッジ交換お問い合わせ設定」を取得する。
// $isExchageCartridgeByProduct = $this->util->isExchageCartridgeByProduct($product);
// 「レンタル申し込み対象設定」を取得する。
$event->setParameter('IsRental', $this->util->isRentalByProduct($product));
// 追加する順番に注意!!!
// カートリッジ交換サポート申し込み対象項目は非表示
// $event->addSnippet('admin/Product/add_change_cartridge_flg.twig');
$event->addSnippet('admin/Product/add_installation_work_request_flg.twig');
// レンタル対象項目は非表示
// $event->addSnippet('admin/Product/add_rental_flg.twig');
$event->addSnippet('admin/Product/add_rental_url.twig');
// $event->addSnippet('admin/Product/add_sort_no.twig');
// $event->addSnippet('admin/Product/add_sale_type_change_event.twig');
$event->addSnippet('admin/Product/add_non_cartridge_item_flg.twig');
}
// 【追加プラグイン】定期受注編集画面に介入
// ※コントローラーの修正が必要なので要確認です
public function onRegular4AdminOrderEditTwig(TemplateEvent $event){
$event->addSnippet('admin/info_regular_order.twig');
$event->addSnippet('admin/info_regular_order_log.twig');
$event->addSnippet('admin/info_regular_order_edit_history.twig');
}
// 【追加プラグイン】<フロント>マイページの定期受注一覧詳細画面に介入
// ※コントローラーの修正が必要なので要確認です
public function onRegular4MypageRegularOrderTwig(TemplateEvent $event){
// 対象のTwigファイルを差し替える
$source = $this->twig->getLoader()
->getSourceContext("Mypage/regular_order.twig")
->getCode();
$event->setSource($source);
}
// 【追加プラグイン】<フロント>マイページの定期受注詳細画面に介入
// ※コントローラーの修正が必要なので要確認です
public function onRegular4MypageRegularHistoryTwig(TemplateEvent $event){
// 対象のTwigファイルを差し替える
$source = $this->twig->getLoader()
->getSourceContext("Mypage/regular_history.twig")
->getCode();
$event->setSource($source);
}
// // 【追加プラグイン】<フロント>マイページの定期受注詳細画面に介入
// // ※コントローラーの修正が必要なので要確認です
// public function onRegular4ShoppingIndexTwig(TemplateEvent $event){
// $event->addSnippet('@SlnRegular4/Shopping/index.twig.twig');
// // // 対象のTwigファイルを差し替える
// // $source = $this->twig->getLoader()
// // ->getSourceContext("Mypage/regular_history.twig")
// // ->getCode();
// // $event->setSource($source);
// }
// ログインしているユーザーの情報を取得
private function getUser() {
$Customer = null;
if (null !== $token = $this->tokenStorage->getToken()) {
$Customer = $token->getUser();
}
if (!is_object($Customer = $token->getUser())) {
// e.g. anonymous authentication
return;
}
return $Customer;
}
// 「オートシップメント販売」の商品を購入中に、「定期受注履歴」を確認し、
// 「継続中」の販売種別が「オートシップメント販売」の情報があるかどうか確認する
private function isRegularOrderByOrder($Order) {
$Ids = [];
// オートシップメントIDの取得
$config = $this->mpCustomConfigRepository->findAll();
if ($config){
$config = $config[0];
} else {
log_info('設定情報が登録されていません:ERROR:レコードの登録なし');
return false;
}
$saleTypeId = $config->autoshipment_id;
$regularTypes = $this->saleTypeSubRepository->getSaleTypeById($saleTypeId);
if ($regularTypes) {
$OrderItems = $Order->getOrderItems();
foreach ($OrderItems as $OrderItem) {
if ($OrderItem->getProductClass()){
if ($OrderItem->getProductClass()->getSaleType()->getId() == $regularTypes[0]->getId()){
if ($OrderItem->getProductClass()->getSaleType()){
$Ids[] = $OrderItem->getProductClass()->getSaleType()->getId();
}
}
}
}
}
// ログインユーザの取得
$Customer = $this->getUser();
$isRegularOrder = false;
$Orders = null;
// 販売種別が、オートシップメント販売の場合、定期受注情報を確認する。
if (count($Ids)>0){
if ($Customer != null){
$Orders = $this->regularOrderSubRepository->getRegularOrderByCustomer($Customer);
if (count($Orders)>0){
$Ids = [];
$saleTypeId = $config->autoshipment_id;
$regularTypes = $this->saleTypeSubRepository->getSaleTypeById($saleTypeId);
foreach ($Orders as $Order) {
$sale_types = $Order->getSaleTypes();
foreach ($sale_types as $sale_type) {
if ($sale_type->getId() == $regularTypes[0]->getId()){
$Ids[] = $sale_type->getId();
}
}
}
if (count($Ids)>0){
$isRegularOrder = true;
}
}
}
}
return $isRegularOrder;
}
// 「オートシップメント販売」の商品を購入中に、「定期受注履歴」を確認し、
// 「継続中」の販売種別が「オートシップメント販売」の情報があるかどうか確認する
private function isRegularOrderByCarts($Carts) {
$Ids = [];
// オートシップメントIDの取得
$config = $this->mpCustomConfigRepository->findAll();
if ($config){
$config = $config[0];
} else {
log_info('設定情報が登録されていません:ERROR:レコードの登録なし');
return "";
}
$saleTypeId = $config->autoshipment_id;
$regularTypes = $this->saleTypeSubRepository->getSaleTypeById($saleTypeId);
if ($regularTypes) {
foreach ($Carts as $Cart) {
$CartItems = $Cart->getCartItems();
foreach ($CartItems as $CartItem) {
if ($CartItem->getProductClass()->getSaleType()->getId() == $regularTypes[0]->getId()){
$Ids[] = $CartItem->getProductClass()->getSaleType()->getId();
}
}
}
}
// ログインユーザの取得
$Customer = $this->getUser();
$isRegularOrder = false;
$Orders = null;
// 販売種別が、オートシップメント販売の場合、定期受注情報を確認する。
if (count($Ids)>0){
if ($Customer != null){
$Orders = $this->regularOrderSubRepository->getRegularOrderByCustomer($Customer);
if (count($Orders)>0){
$Ids = [];
$saleTypeId = $config->autoshipment_id;
$regularTypes = $this->saleTypeSubRepository->getSaleTypeById($saleTypeId);
foreach ($Orders as $Order) {
$sale_types = $Order->getSaleTypes();
foreach ($sale_types as $sale_type) {
if ($sale_type->getId() == $regularTypes[0]->getId()){
$Ids[] = $sale_type->getId();
}
}
}
if (count($Ids)>0){
$isRegularOrder = true;
}
}
}
}
return $isRegularOrder;
}
/**
* 【管理画面】受注登録画面介入
*/
public function onAdminOrderEditTwig(TemplateEvent $event) {
$Order = $event->getParameter('Order');
$event->addSnippet('admin/Order/cartridg_replacement_support.twig');
$event->addSnippet('admin/Order/work_request.twig');
$event->addSnippet('admin/Order/rental_attachment.twig');
$event->addSnippet('admin/Order/autoshipment_entry.twig');
$event->setParameter('IsInstallationWorkRequest',$this->util->isInstallationWorkRequestByOrder($Order));
$event->setParameter('IsChangeCartridge', $this->util->isChangeCartridgeByOrder($Order));
$event->setParameter('IsRental', $this->util->isRentalByOrder($Order));
$event->setParameter('IsAutoShipment', $this->util->isAutoShipmentByOrder($Order));
}
/**
* 【フロント】マイページ>ご注文履歴画面介入
*/
public function onMypageHistoryTwig(TemplateEvent $event) {
$Order = $event->getParameter('Order');
$event->setParameter('IsChangeCartridge', $this->util->isChangeCartridgeByOrder($Order));
}
/**
* 【管理画面】設定>店舗設定>基本設定 画面介入
*/
public function onAdminSettingShopMasterTwig(TemplateEvent $event) {
$event->addSnippet('admin/shop_master.twig');
}
/**
* 【管理画面】設定>店舗設定>支払方法設定:編集画面介入
*/
public function onAdminSettingShopPaymentEditTwig(TemplateEvent $event) {
$event->addSnippet('admin/Setting/Shop/payment_edit_charge_amount.twig');
}
}