app/Plugin/QuantityDiscountDx/EventSubscriber/AdminProductEventSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2019 SYSTEM_KD
  4.  * Date: 2019/03/09
  5.  */
  6. namespace Plugin\QuantityDiscountDx\EventSubscriber;
  7. use Eccube\Entity\Product;
  8. use Eccube\Event\EccubeEvents;
  9. use Eccube\Event\EventArgs;
  10. use Eccube\Event\TemplateEvent;
  11. use Plugin\QuantityDiscountDx\Service\QuantityDiscountService;
  12. use Plugin\QuantityDiscountDx\Service\TwigRenderService\TwigRenderService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class AdminProductEventSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var TwigRenderService */
  17.     protected $twigRenderService;
  18.     /** @var QuantityDiscountService */
  19.     protected $quantityDiscountService;
  20.     public function __construct(
  21.         TwigRenderService $twigRenderService,
  22.         QuantityDiscountService $quantityDiscountService
  23.     )
  24.     {
  25.         $this->twigRenderService $twigRenderService;
  26.         $this->quantityDiscountService $quantityDiscountService;
  27.     }
  28.     /**
  29.      * 商品詳細テンプレート
  30.      *
  31.      * @param TemplateEvent $event
  32.      */
  33.     public function onTemplateProductProduct(TemplateEvent $event)
  34.     {
  35.         $this->twigRenderService->initRenderService($event);
  36.         /** @var Product $Product */
  37.         $Product $event->getParameter('Product');
  38.         if ($Product->hasProductClass()) {
  39.             // 規格あり商品
  40.         } else {
  41.             // 規格なし商品
  42.             $this->twigRenderService
  43.                 ->insertBuilder()
  44.                 ->find('.c-primaryCol > div')
  45.                 ->eq(0)
  46.                 ->setTemplate('@QuantityDiscountDx/admin/Product/qd_area.twig')
  47.                 ->setTargetId('plugin_qd_block')
  48.                 ->setInsertModeAfter()
  49.                 ->setScript('@QuantityDiscountDx/admin/Product/qd_area_js.twig');
  50.             $this->twigRenderService->addSupportSnippet();
  51.         }
  52.     }
  53.     /**
  54.      * 商品コピー完了
  55.      *
  56.      * @param EventArgs $event
  57.      */
  58.     public function onAdminProductCopyComplete(EventArgs $event)
  59.     {
  60.         // 商品コピー完了時
  61.         $CopyProductClasses $event->getArgument('CopyProductClasses');
  62.         $this->quantityDiscountService->copyQdProductClasses($CopyProductClasses);
  63.     }
  64.     /**
  65.      * Returns an array of event names this subscriber wants to listen to.
  66.      *
  67.      * The array keys are event names and the value can be:
  68.      *
  69.      * * The method name to call (priority defaults to 0)
  70.      * * An array composed of the method name to call and the priority
  71.      * * An array of arrays composed of the method names to call and respective
  72.      *   priorities, or 0 if unset
  73.      *
  74.      * For instance:
  75.      *
  76.      * * array('eventName' => 'methodName')
  77.      * * array('eventName' => array('methodName', $priority))
  78.      * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  79.      *
  80.      * @return array The event names to listen to
  81.      */
  82.     public static function getSubscribedEvents()
  83.     {
  84.         return [
  85.             // 商品詳細
  86.             '@admin/Product/product.twig' => ['onTemplateProductProduct'],
  87.             // 商品コピー完了
  88.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => ['onAdminProductCopyComplete'],
  89.         ];
  90.     }
  91. }