app/Plugin/PointEx/EventSubscriber/ProductDetailEventSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2019 SYSTEM_KD
  4.  * Date: 2019/07/20
  5.  */
  6. namespace Plugin\PointEx\EventSubscriber;
  7. use Eccube\Entity\Product;
  8. use Eccube\Event\TemplateEvent;
  9. use Plugin\PointEx\Config\ConfigSetting;
  10. use Plugin\PointEx\Service\PlgConfigService\ConfigService;
  11. use Plugin\PointEx\Service\PointExHelper;
  12. use Plugin\PointEx\Service\TwigRenderService\TwigRenderService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ProductDetailEventSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var TwigRenderService */
  17.     protected $twigRenderService;
  18.     /** @var ConfigService */
  19.     protected $configService;
  20.     /** @var PointExHelper */
  21.     protected $pointExHelper;
  22.     public function __construct(
  23.         TwigRenderService $twigRenderService,
  24.         ConfigService $configService,
  25.         PointExHelper $pointExHelper
  26.     )
  27.     {
  28.         $this->twigRenderService $twigRenderService;
  29.         $this->configService $configService;
  30.         $this->pointExHelper $pointExHelper;
  31.     }
  32.     /**
  33.      * 商品詳細テンプレート
  34.      *
  35.      * @param TemplateEvent $event
  36.      */
  37.     public function onTemplateProductDetail(TemplateEvent $event)
  38.     {
  39.         if (!$this->pointExHelper->isPointUse()) {
  40.             // ポイント利用が無効の場合表示しない
  41.             return;
  42.         }
  43.         if ($this->configService->isKeyBool(ConfigSetting::KEY_DETAIL_VIEW)) {
  44.             $this->twigRenderService->initRenderService($event);
  45.             /** @var Product $Product */
  46.             $Product $event->getParameter('Product');
  47.             if ($Product->getCodeMin()) {
  48.                 // 商品コード下にボーナスポイント表示追加
  49.                 $findKey '.ec-productRole__code';
  50.             } else {
  51.                 // 販売価格の下にボーナスポイント表示追加
  52.                 $findKey '.ec-productRole__price';
  53.             }
  54.             $this->twigRenderService
  55.                 ->insertBuilder()
  56.                 ->find($findKey)
  57.                 ->eq(0)
  58.                 ->setTargetId('ec-productRole__bonus_point')
  59.                 ->setInsertModeAfter();
  60.             $this->twigRenderService->addSupportSnippet(
  61.                 '@PointEx/default/Product/detail_add.twig',
  62.                 '@PointEx/default/Product/detail_add_js.twig'
  63.             );
  64.             $event->addAsset('@PointEx/default/Product/detail_add_css.twig');
  65.         }
  66.     }
  67.     /**
  68.      * Returns an array of event names this subscriber wants to listen to.
  69.      *
  70.      * The array keys are event names and the value can be:
  71.      *
  72.      *  * The method name to call (priority defaults to 0)
  73.      *  * An array composed of the method name to call and the priority
  74.      *  * An array of arrays composed of the method names to call and respective
  75.      *    priorities, or 0 if unset
  76.      *
  77.      * For instance:
  78.      *
  79.      *  * ['eventName' => 'methodName']
  80.      *  * ['eventName' => ['methodName', $priority]]
  81.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  82.      *
  83.      * @return array The event names to listen to
  84.      */
  85.     public static function getSubscribedEvents()
  86.     {
  87.         return [
  88.             "Product/detail.twig" => ['onTemplateProductDetail'],
  89.         ];
  90.     }
  91. }