app/Customize/EventListener/MPEventListener.php line 508

Open in your IDE?
  1. <?php
  2. namespace Customize\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Eccube\Event\EccubeEvents;
  7. use Eccube\Event\EventArgs;
  8. use Eccube\Event\TemplateEvent;
  9. use Customize\Service\MailService;
  10. use Eccube\Service\CartService;
  11. use Eccube\Common\EccubeConfig;
  12. use Eccube\Service\PurchaseFlow\PurchaseContext;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Customize\Repository\SlnRegularOrderSubRepository;
  15. use Customize\Repository\SlnRegularSaleTypeSubRepository;
  16. use Eccube\Repository\Master\SaleTypeRepository;
  17. use Customize\Repository\SaleTypeSubRepository;
  18. use Customize\Repository\MPCustomConfigRepository;
  19. use Customize\Repository\CustomSaleTypeRepository;
  20. use Customize\Service\Method\MethodUtils;
  21. use Customize\Service\Util;
  22. use Twig_Environment;
  23. // 
  24. class MPEventListener implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var EccubeConfig
  28.      */
  29.     protected $eccubeConfig;
  30.     /**
  31.      * @var CartService
  32.      */
  33.     private $cartService;
  34.     /**
  35.      * @var TokenStorageInterface
  36.      */
  37.     private $tokenStorage;
  38.     /**
  39.      * @var SlnRegularOrderSubRepository
  40.      */
  41.     private $regularOrderSubRepository;
  42.     /**
  43.      * @var SlnRegularSaleTypeSubRepository
  44.      */
  45.     protected $regularSaleTypeSubRepository;
  46.     /**
  47.      * @var SaleTypeSubRepository
  48.      */
  49.     protected $saleTypeSubRepository;
  50.     /**
  51.      * @var MPCustomConfigRepository
  52.      */
  53.     protected $mpCustomConfigRepository;
  54.     /**
  55.      * @var Twig_Environment
  56.      */
  57.     protected $twig;
  58.     /**
  59.      * @var CustomSaleTypeRepository
  60.      */
  61.     protected $customSaleTypeRepository;
  62.     /**
  63.      * @var Util
  64.      */
  65.     protected $util;
  66.     /**
  67.      * コンストラクタ
  68.      */
  69.     public function __construct(
  70.         SlnRegularOrderSubRepository $regularOrderSubRepository,
  71.         SlnRegularSaleTypeSubRepository $regularSaleTypeSubRepository,
  72.         MPCustomConfigRepository $mpCustomConfigRepository,
  73.         SaleTypeSubRepository $saleTypeSubRepository,
  74.         EccubeConfig $eccubeConfig,
  75.         CartService $cartService,
  76.         TokenStorageInterface $tokenStorage,
  77.         Twig_Environment $twig,
  78.         CustomSaleTypeRepository $customSaleTypeRepository,
  79.         Util $util
  80.     ) {
  81.         $this->regularOrderSubRepository $regularOrderSubRepository;
  82.         $this->regularSaleTypeSubRepository $regularSaleTypeSubRepository;
  83.         $this->saleTypeSubRepository $saleTypeSubRepository;
  84.         $this->mpCustomConfigRepository $mpCustomConfigRepository;
  85.         $this->eccubeConfig $eccubeConfig;
  86.         $this->cartService $cartService;
  87.         $this->tokenStorage $tokenStorage;
  88.         $this->twig $twig;
  89.         $this->customSaleTypeRepository $customSaleTypeRepository;
  90.         $this->util $util;
  91.     }
  92.     /**
  93.      * リッスンしたいサブスクライバのイベント名の配列を返します。
  94.      * 配列のキーはイベント名、値は以下のどれかをしてします。
  95.      * - 呼び出すメソッド名
  96.      * - 呼び出すメソッド名と優先度の配列
  97.      * - 呼び出すメソッド名と優先度の配列の配列
  98.      * 優先度を省略した場合は0
  99.      *
  100.      * 例:
  101.      * - array('eventName' => 'methodName')
  102.      * - array('eventName' => array('methodName', $priority))
  103.      * - array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  104.      *
  105.      * {@inheritdoc}
  106.      */
  107.     public static function getSubscribedEvents()
  108.     {
  109.         return [
  110.             // // マイページから、会員情報を更新た場合のイベントをフック
  111.             // EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_COMPLETE => 'onMypageChangeIndexComplete',
  112.             // // マイページの会員情報を表示したときのイベントをフック
  113.             // EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_INITIALIZE => 'onMypageChangeIndexInitialize',
  114.             // ↓EC-CUBEの標準(Twigへの介入) 
  115.             '@admin/Product/product.twig' => 'onRenderAdminProduct',
  116.             'Mypage/withdraw.twig' => 'onMypageTwig',
  117.             'Cart/index.twig' => 'onCartindexTwig',
  118.             'Shopping/index.twig' => 'onShoppingindexTwig',
  119.             'Shopping/confirm.twig' => 'onShoppingconfirmTwig',
  120.             'admin/Customer/index.twig' => 'onAdminCustomerIndexTwig',
  121.             'admin/Customer/edit.twig' => 'onAdminCustomerEditTwig',
  122.             'Product/detail.twig' => 'onProductDetailTwig',
  123.             'Product/list.twig' => 'onProductListTwig',
  124.             'Mypage/history.twig' => 'onMypageHistoryTwig',
  125.             // ↓は定期受注プラグインに介入するためのもの
  126.             '@SlnRegular4/admin/sale_type.twig' => 'onRegular4SaleTypeTwig',
  127.             '@SlnRegular4/admin/Order/regular_edit.twig' => 'onRegular4AdminOrderEditTwig',
  128.             '@SlnRegular4/Mypage/regular_order.twig' => 'onRegular4MypageRegularOrderTwig',
  129.             '@SlnRegular4/Mypage/regular_history.twig' => 'onRegular4MypageRegularHistoryTwig',
  130.             // 以下のTwigには介入できなかった
  131.             // '@SlnRegular4/Shopping/index.twig' => 'onRegular4ShoppingIndexTwig',
  132.             '@admin/Order/edit.twig' => 'onAdminOrderEditTwig',
  133.             '@admin/Setting/Shop/shop_master.twig' => 'onAdminSettingShopMasterTwig',
  134.             '@admin/Setting/Shop/payment_edit.twig' => 'onAdminSettingShopPaymentEditTwig'
  135.         ];
  136.     }
  137.     
  138.     /**
  139.      * 【フロント】マイページ画面介入
  140.      */
  141.     public function onMypageTwig(TemplateEvent $event) {
  142.         log_info('[onMypageTwig]を検出しました');
  143.         $event->addSnippet('Mypage/withdraw_custom.twig');
  144.     }
  145.     /**
  146.      * フロント画面 - 商品詳細画面介入
  147.      */
  148.     public function onProductDetailTwig(TemplateEvent $event) {
  149.         $event->addSnippet('Product/detail_sub.twig');
  150.         $parameters $event->getParameters();
  151.         $product $event->getParameter('Product');
  152.         $event->setParameter('parameters'$parameters);
  153.         // $customSaleType = null;
  154.         // 「カートリッジ交換お問い合わせ設定」を取得する。
  155.         $customSaleTypeExchageCartridge $this->util->isExchageCartridgeByProduct($product);
  156.         // 「レンタル申し込み対象設定」を取得する。
  157.         $customSaleTypeRental $this->util->isRentalByProduct($product);
  158.         // $event->setParameter('customSaleType', $customSaleType);
  159.         $event->setParameter('customSaleTypeExchageCartridge'$customSaleTypeExchageCartridge);
  160.         $event->setParameter('customSaleTypeRental'$customSaleTypeRental);
  161.     }
  162.     /**
  163.      * フロント画面 - 商品一覧画面介入
  164.      */
  165.     public function onProductListTwig(TemplateEvent $event) {
  166.         // 連携用「販売種別」情報を配列として取得し、Twig内で確認する。
  167.         $setCustomsaletype = [];
  168.         $customsaletypes $this->customSaleTypeRepository->findAll();
  169.         if(!$customsaletypes) {
  170.             $setCustomsaletype = [];
  171.         } else {
  172.             foreach ($customsaletypes as $customsaletype) {
  173.                 $setCustomsaletype[$customsaletype->saleTypeId] = $customsaletype;
  174.             }
  175.         }
  176.         $event->setParameter('CustomSaleType'$setCustomsaletype);
  177.     }
  178.     // 【フロント】カート画面 - ショッピングカート介入
  179.     public function onCartindexTwig(TemplateEvent $event) {
  180.         log_info('[onCartindexTwig]を検出しました');
  181.         $Order null;
  182.         $Carts $event->getParameter('Carts');
  183.         $isRegularOrder $this->isRegularOrderByCarts($Carts);
  184.         $event->addSnippet('Shopping/autoshipment.twig');
  185.         $event->setParameter('isRegularOrder'$isRegularOrder);
  186.     }
  187.     // 【フロント】カート画面 - ご注文手続き介入
  188.     public function onShoppingindexTwig(TemplateEvent $event) {
  189.         log_info('[onShoppingindexTwig]を検出しました');
  190.         $Order $event->getParameter('Order');
  191.         
  192.         $isRegularOrder $this->isRegularOrderByOrder($Order);
  193.         $event->addSnippet('Shopping/autoshipment.twig');
  194.         // $event->addSnippet('Shopping/autoshipment_info.twig');
  195.         // 定期受注対象の注文情報か確認
  196.         $event->setParameter('isRegularOrder'$isRegularOrder);
  197.         // 本体(取付工事依頼表示)設定の有無を確認
  198.         $event->setParameter('IsInstallationWorkRequest',$this->util->isInstallationWorkRequestByOrder($Order));
  199.         // カートリッジ交換サポート申し込み対象か確認
  200.         $event->setParameter('IsChangeCartridge'$this->util->isChangeCartridgeByOrder($Order));
  201.         // レンタル申し込み対象か確認
  202.         $event->setParameter('IsRentalItem'$this->util->isRentalByOrder($Order));
  203.         // オートシップメント申込対象か確認
  204.         $event->setParameter('IsAutoShipment'$this->util->isAutoShipmentByOrder($Order));
  205.     }
  206.     // 【フロント】カート画面 - ご注文内容のご確認介入
  207.     public function onShoppingconfirmTwig(TemplateEvent $event) {
  208.         log_info('[onShoppingconfirmTwig]を検出しました');
  209.         $Order $event->getParameter('Order');
  210.         $isRegularOrder $this->isRegularOrderByOrder($Order);
  211.         $event->addSnippet('Shopping/autoshipment.twig');
  212.         // 定期受注対象の注文情報か確認
  213.         $event->setParameter('isRegularOrder'$isRegularOrder);
  214.         // 本体(取付工事依頼表示)設定の有無を確認
  215.         $event->setParameter('IsInstallationWorkRequest'$this->util->isInstallationWorkRequestByOrder($Order));
  216.         // カートリッジ交換サポート申し込み対象か確認
  217.         $event->setParameter('IsChangeCartridge'$this->util->isChangeCartridgeByOrder($Order));
  218.         // レンタル申し込み対象か確認
  219.         $event->setParameter('IsRentalItem'$this->util->isRentalByOrder($Order));
  220.         // オートシップメント申込対象か確認
  221.         $event->setParameter('IsAutoShipment'$this->util->isAutoShipmentByOrder($Order));
  222.         // 「注文する」ボタンの文字列を変更する
  223.         if ($Order) {
  224.             $methodClass $Order->getPayment()->getMethodClass();
  225.             if (MethodUtils::isJaccsWebbyPaymentMethodByOrder($Order)){
  226.                 // 支払方法が、「JACCS:分割(金利手数料弊社負担)」の場合
  227.                 $event->addSnippet('JACCS_WeBBy/jaccs_webby_shopping_confirm.twig');
  228.             }
  229.         }
  230.     }
  231.     // 定期受注プラグインの設定画面(編集)に介入
  232.     public function onRegular4SaleTypeTwig(TemplateEvent $event){
  233.         // 定期受注間隔:毎年を追加(カスタマイズ)
  234.         $event->addSnippet('admin/sale_type_yearly.twig');
  235.     }
  236.     // 管理画面の会員一覧に介入
  237.     public function onAdminCustomerIndexTwig(TemplateEvent $event){
  238.         // $event->addSnippet('admin/sale_type_yearly.twig');
  239.     }
  240.     
  241.     // 管理画面の会員編集に介入
  242.     public function onAdminCustomerEditTwig(TemplateEvent $event){
  243.         $event->addSnippet('admin/info.twig');
  244.     }
  245.     /**
  246.      * 管理画面:商品登録画面に「本体(取付工事依頼表示)」項目を表示する.
  247.      *
  248.      * @param TemplateEvent $event
  249.      */
  250.     public function onRenderAdminProduct(TemplateEvent $event)
  251.     {
  252.         $product $event->getParameter('Product');
  253.         // // 「カートリッジ交換お問い合わせ設定」を取得する。
  254.         // $isExchageCartridgeByProduct = $this->util->isExchageCartridgeByProduct($product);
  255.         // 「レンタル申し込み対象設定」を取得する。
  256.         $event->setParameter('IsRental'$this->util->isRentalByProduct($product));
  257.         // 追加する順番に注意!!!
  258.         // カートリッジ交換サポート申し込み対象項目は非表示
  259.         // $event->addSnippet('admin/Product/add_change_cartridge_flg.twig');
  260.         $event->addSnippet('admin/Product/add_installation_work_request_flg.twig');
  261.         // レンタル対象項目は非表示
  262.         // $event->addSnippet('admin/Product/add_rental_flg.twig');
  263.         $event->addSnippet('admin/Product/add_rental_url.twig');
  264.         // $event->addSnippet('admin/Product/add_sort_no.twig');
  265.         // $event->addSnippet('admin/Product/add_sale_type_change_event.twig');
  266.         $event->addSnippet('admin/Product/add_non_cartridge_item_flg.twig');
  267.     }
  268.     // 【追加プラグイン】定期受注編集画面に介入
  269.     // ※コントローラーの修正が必要なので要確認です
  270.     public function onRegular4AdminOrderEditTwig(TemplateEvent $event){
  271.         $event->addSnippet('admin/info_regular_order.twig');
  272.         $event->addSnippet('admin/info_regular_order_log.twig');
  273.         $event->addSnippet('admin/info_regular_order_edit_history.twig');
  274.     }
  275.     // 【追加プラグイン】<フロント>マイページの定期受注一覧詳細画面に介入
  276.     // ※コントローラーの修正が必要なので要確認です
  277.     public function onRegular4MypageRegularOrderTwig(TemplateEvent $event){
  278.         // 対象のTwigファイルを差し替える
  279.         $source $this->twig->getLoader()
  280.             ->getSourceContext("Mypage/regular_order.twig")
  281.             ->getCode();
  282.                 
  283.         $event->setSource($source);
  284.     }
  285.     // 【追加プラグイン】<フロント>マイページの定期受注詳細画面に介入
  286.     // ※コントローラーの修正が必要なので要確認です
  287.     public function onRegular4MypageRegularHistoryTwig(TemplateEvent $event){
  288.         // 対象のTwigファイルを差し替える
  289.         $source $this->twig->getLoader()
  290.             ->getSourceContext("Mypage/regular_history.twig")
  291.             ->getCode();
  292.                 
  293.         $event->setSource($source);
  294.     }
  295.     // // 【追加プラグイン】<フロント>マイページの定期受注詳細画面に介入
  296.     // // ※コントローラーの修正が必要なので要確認です
  297.     // public function onRegular4ShoppingIndexTwig(TemplateEvent $event){
  298.     //         $event->addSnippet('@SlnRegular4/Shopping/index.twig.twig');
  299.     //         // // 対象のTwigファイルを差し替える
  300.     //     // $source = $this->twig->getLoader()
  301.     //     //     ->getSourceContext("Mypage/regular_history.twig")
  302.     //     //     ->getCode();
  303.                 
  304.     //     // $event->setSource($source);
  305.     // }
  306.     // ログインしているユーザーの情報を取得
  307.     private function getUser() {
  308.         $Customer null;
  309.         if (null !== $token $this->tokenStorage->getToken()) {
  310.             $Customer $token->getUser();
  311.         }
  312.         if (!is_object($Customer $token->getUser())) {
  313.             // e.g. anonymous authentication
  314.             return;
  315.         }
  316.         return $Customer;
  317.     }
  318.     // 「オートシップメント販売」の商品を購入中に、「定期受注履歴」を確認し、
  319.     // 「継続中」の販売種別が「オートシップメント販売」の情報があるかどうか確認する
  320.     private function isRegularOrderByOrder($Order) {
  321.         $Ids = []; 
  322.         // オートシップメントIDの取得
  323.         $config $this->mpCustomConfigRepository->findAll();
  324.         if ($config){
  325.             $config $config[0];
  326.         } else {
  327.             log_info('設定情報が登録されていません:ERROR:レコードの登録なし');
  328.             return false;
  329.         }
  330.         $saleTypeId $config->autoshipment_id;
  331.         $regularTypes $this->saleTypeSubRepository->getSaleTypeById($saleTypeId);
  332.         if ($regularTypes) {
  333.             $OrderItems $Order->getOrderItems();
  334.             foreach ($OrderItems as $OrderItem) {
  335.                 if ($OrderItem->getProductClass()){
  336.                     if ($OrderItem->getProductClass()->getSaleType()->getId() == $regularTypes[0]->getId()){
  337.                         if ($OrderItem->getProductClass()->getSaleType()){
  338.                             $Ids[] = $OrderItem->getProductClass()->getSaleType()->getId();
  339.                         }
  340.                     }
  341.                 }
  342.             }
  343.         }
  344.         // ログインユーザの取得
  345.         $Customer $this->getUser();
  346.         $isRegularOrder false;
  347.         $Orders null;
  348.         // 販売種別が、オートシップメント販売の場合、定期受注情報を確認する。
  349.         if (count($Ids)>0){
  350.             if ($Customer != null){
  351.                 $Orders $this->regularOrderSubRepository->getRegularOrderByCustomer($Customer);
  352.                 if (count($Orders)>0){
  353.                     $Ids = []; 
  354.                     $saleTypeId $config->autoshipment_id;
  355.                     $regularTypes $this->saleTypeSubRepository->getSaleTypeById($saleTypeId);
  356.                     foreach ($Orders as $Order) {
  357.                         $sale_types $Order->getSaleTypes();
  358.                         foreach ($sale_types as $sale_type) {
  359.                             if ($sale_type->getId() == $regularTypes[0]->getId()){
  360.                                 $Ids[] = $sale_type->getId();
  361.                             }
  362.                         }
  363.                     }
  364.                     if (count($Ids)>0){
  365.                         $isRegularOrder true;
  366.                     }
  367.                 }
  368.             }
  369.         }
  370.         return $isRegularOrder;
  371.     }
  372.     // 「オートシップメント販売」の商品を購入中に、「定期受注履歴」を確認し、
  373.     // 「継続中」の販売種別が「オートシップメント販売」の情報があるかどうか確認する
  374.     private function isRegularOrderByCarts($Carts) {
  375.         $Ids = []; 
  376.         // オートシップメントIDの取得
  377.         $config $this->mpCustomConfigRepository->findAll();
  378.         if ($config){
  379.             $config $config[0];
  380.         } else {
  381.             log_info('設定情報が登録されていません:ERROR:レコードの登録なし');
  382.             return "";
  383.         }
  384.         $saleTypeId $config->autoshipment_id;
  385.         $regularTypes $this->saleTypeSubRepository->getSaleTypeById($saleTypeId);
  386.         if ($regularTypes) {
  387.             foreach ($Carts as $Cart) {
  388.                 $CartItems $Cart->getCartItems();
  389.                 foreach ($CartItems as $CartItem) {
  390.                     if ($CartItem->getProductClass()->getSaleType()->getId() == $regularTypes[0]->getId()){
  391.                         $Ids[] = $CartItem->getProductClass()->getSaleType()->getId();
  392.                     }
  393.                 }
  394.             }
  395.         }
  396.         // ログインユーザの取得
  397.         $Customer $this->getUser();
  398.         $isRegularOrder false;
  399.         $Orders null;
  400.         // 販売種別が、オートシップメント販売の場合、定期受注情報を確認する。
  401.         if (count($Ids)>0){
  402.             if ($Customer != null){
  403.                 $Orders $this->regularOrderSubRepository->getRegularOrderByCustomer($Customer);
  404.                 if (count($Orders)>0){
  405.                     $Ids = []; 
  406.                     $saleTypeId $config->autoshipment_id;
  407.                     $regularTypes $this->saleTypeSubRepository->getSaleTypeById($saleTypeId);
  408.                     foreach ($Orders as $Order) {
  409.                         $sale_types $Order->getSaleTypes();
  410.                         foreach ($sale_types as $sale_type) {
  411.                             if ($sale_type->getId() == $regularTypes[0]->getId()){
  412.                                 $Ids[] = $sale_type->getId();
  413.                             }
  414.                         }
  415.                     }
  416.                     if (count($Ids)>0){
  417.                         $isRegularOrder true;
  418.                     }
  419.                 }
  420.             }
  421.         }
  422.         return $isRegularOrder;
  423.     }
  424.     /**
  425.      * 【管理画面】受注登録画面介入
  426.      */
  427.     public function onAdminOrderEditTwig(TemplateEvent $event) {
  428.         $Order $event->getParameter('Order');
  429.         $event->addSnippet('admin/Order/cartridg_replacement_support.twig');
  430.         $event->addSnippet('admin/Order/work_request.twig');
  431.         $event->addSnippet('admin/Order/rental_attachment.twig');
  432.         $event->addSnippet('admin/Order/autoshipment_entry.twig');
  433.         $event->setParameter('IsInstallationWorkRequest',$this->util->isInstallationWorkRequestByOrder($Order));
  434.         $event->setParameter('IsChangeCartridge'$this->util->isChangeCartridgeByOrder($Order));
  435.         $event->setParameter('IsRental'$this->util->isRentalByOrder($Order));
  436.         $event->setParameter('IsAutoShipment'$this->util->isAutoShipmentByOrder($Order));
  437.     }
  438.     /**
  439.      * 【フロント】マイページ>ご注文履歴画面介入
  440.      */
  441.     public function onMypageHistoryTwig(TemplateEvent $event) {
  442.         $Order $event->getParameter('Order');
  443.         $event->setParameter('IsChangeCartridge'$this->util->isChangeCartridgeByOrder($Order));
  444.     }
  445.     /**
  446.      * 【管理画面】設定>店舗設定>基本設定 画面介入
  447.      */
  448.     public function onAdminSettingShopMasterTwig(TemplateEvent $event) {
  449.         $event->addSnippet('admin/shop_master.twig');
  450.     }
  451.     /**
  452.      * 【管理画面】設定>店舗設定>支払方法設定:編集画面介入
  453.      */
  454.     public function onAdminSettingShopPaymentEditTwig(TemplateEvent $event) {
  455.         $event->addSnippet('admin/Setting/Shop/payment_edit_charge_amount.twig');
  456.     }
  457. }