app/Plugin/PointEx/EventSubscriber/AdminProductEventSubscriber.php line 177

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2019 SYSTEM_KD
  4.  * Date: 2019/06/23
  5.  */
  6. namespace Plugin\PointEx\EventSubscriber;
  7. use Eccube\Entity\Product;
  8. use Eccube\Event\TemplateEvent;
  9. use Plugin\PointEx\Service\FormViewHelper;
  10. use Plugin\PointEx\Service\PointExHelper;
  11. use Plugin\PointEx\Service\TwigRenderService\TwigRenderService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Form\FormView;
  14. class AdminProductEventSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var TwigRenderService */
  17.     protected $twigRenderService;
  18.     /** @var FormViewHelper */
  19.     protected $formViewHelper;
  20.     protected $pointExHelper;
  21.     public function __construct(
  22.         TwigRenderService $twigRenderService,
  23.         FormViewHelper $formViewHelper,
  24.         PointExHelper $pointExHelper
  25.     )
  26.     {
  27.         $this->twigRenderService $twigRenderService;
  28.         $this->formViewHelper $formViewHelper;
  29.         $this->pointExHelper $pointExHelper;
  30.     }
  31.     /**
  32.      * 商品登録
  33.      *
  34.      * @param TemplateEvent $event
  35.      */
  36.     public function onTemplateProductProduct(TemplateEvent $event)
  37.     {
  38.         if (!$this->pointExHelper->isPointUse()) {
  39.             // ポイント利用が無効の場合は追加しない
  40.             return;
  41.         }
  42.         $this->twigRenderService->initRenderService($event);
  43.         /** @var Product $Product */
  44.         $Product $event->getParameter('Product');
  45.         if ($Product->hasProductClass()) {
  46.             // 規格あり商品
  47.         } else {
  48.             // 規格なし商品
  49.             $this->twigRenderService
  50.                 ->insertBuilder()
  51.                 ->find('.c-primaryCol > div')
  52.                 ->eq(0)
  53.                 ->setTemplate('@PointEx/admin/Product/point_ex_area.twig')
  54.                 ->setTargetId('plugin_point_ex_block')
  55.                 ->setInsertModeAfter();
  56.             $this->twigRenderService->addSupportSnippet();
  57.         }
  58.     }
  59.     /**
  60.      * 商品規格登録
  61.      *
  62.      * @param TemplateEvent $event
  63.      */
  64.     public function onTemplateProductClassEdit(TemplateEvent $event)
  65.     {
  66.         if (!$this->pointExHelper->isPointUse()) {
  67.             // ポイント利用が無効の場合は追加しない
  68.             return;
  69.         }
  70.         $this->twigRenderService->initRenderService($event);
  71.         /** @var FormView $form */
  72.         $form $event->getParameter('form');
  73.         // エラー状態の判定
  74.         $formProductClasses $form['product_classes'];
  75.         $list = [];
  76.         /** @var FormView $formRow */
  77.         foreach ($formProductClasses as $formRow) {
  78.             $this->formViewHelper->validList($list$formRow);
  79.         }
  80.         $pointExInputValidOther true;
  81.         $pointExInputValid true;
  82.         foreach ($list as $item) {
  83.             if ('PointExProductClasses' ==
  84.                 $this->formViewHelper->getParentName($item)) {
  85.                 $pointExInputValid false;
  86.             } else {
  87.                 $pointExInputValidOther false;
  88.             }
  89.         }
  90.         $event->setParameter('pointExInputValidOther'$pointExInputValidOther);
  91.         $event->setParameter('pointExInputValid'$pointExInputValid);
  92.         /** @var Product $Product */
  93.         $Product $event->getParameter('Product');
  94.         // 切り替えボタン
  95.         $this->twigRenderService
  96.             ->insertBuilder()
  97.             ->find('form')
  98.             ->find('.justify-content-between')
  99.             ->find('div > span')
  100.             ->eq(0)
  101.             ->setTargetId('point_ex_change')
  102.             ->setInsertModeAppend();
  103.         if (!$pointExInputValidOther
  104.             && !$pointExInputValid) {
  105.             // エラーメッセージ
  106.             $this->twigRenderService
  107.                 ->insertBuilder()
  108.                 ->find('form')
  109.                 ->find('.justify-content-between')
  110.                 ->find('div')
  111.                 ->eq(0)
  112.                 ->setTargetId('point_ex_msg')
  113.                 ->setInsertModeAppend();
  114.         }
  115.         // タイトル
  116.         $this->twigRenderService
  117.             ->insertBuilder()
  118.             ->find('#ex-product_class > table > thead')
  119.             ->eq(0)
  120.             ->setTargetId('point_ex_thead')
  121.             ->setInsertModeAfter();
  122.         // リスト
  123.         $eachChild $this->twigRenderService->eachChildBuilder();
  124.         $eachChild
  125.             ->findAndDataKey('#ex-product_class-''point_ex_product_class_name')
  126.             ->targetFindThis()
  127.             ->setInsertModeAfter();
  128.         $this->twigRenderService
  129.             ->eachBuilder()
  130.             ->find('.product_class_point_ex_target')
  131.             ->each($eachChild->build());
  132.         $this->twigRenderService->addSupportSnippetSlow(
  133.             '@PointEx/admin/Product/point_ex_product_class.twig',
  134.             '@PointEx/admin/Product/point_ex_product_class_js.twig'
  135.         );
  136.     }
  137.     /**
  138.      * 商品一覧テンプレート
  139.      *
  140.      * @param TemplateEvent $event
  141.      */
  142.     public function onTemplateProductIndex(TemplateEvent $event)
  143.     {
  144.         $this->twigRenderService->initRenderService($event);
  145.         $child $this->twigRenderService
  146.             ->eachChildBuilder()
  147.             ->findAndDataKey('#ex-product-''point_ex_product_id')
  148.             ->find('td')
  149.             ->eq(5)
  150.             ->targetFindThis()
  151.             ->setInsertModeAppend();
  152.         $this->twigRenderService
  153.             ->eachBuilder()
  154.             ->find('.point_ex_bonus')
  155.             ->each($child->build());
  156.         $this->twigRenderService->addSupportSnippet(
  157.             '@PointEx/admin/Product/index_add.twig'
  158.         );
  159.     }
  160.     /**
  161.      * Returns an array of event names this subscriber wants to listen to.
  162.      *
  163.      * The array keys are event names and the value can be:
  164.      *
  165.      *  * The method name to call (priority defaults to 0)
  166.      *  * An array composed of the method name to call and the priority
  167.      *  * An array of arrays composed of the method names to call and respective
  168.      *    priorities, or 0 if unset
  169.      *
  170.      * For instance:
  171.      *
  172.      *  * ['eventName' => 'methodName']
  173.      *  * ['eventName' => ['methodName', $priority]]
  174.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  175.      *
  176.      * @return array The event names to listen to
  177.      */
  178.     public static function getSubscribedEvents()
  179.     {
  180.         return [
  181.             // 商品詳細
  182.             '@admin/Product/product.twig' => ['onTemplateProductProduct'],
  183.             // 規格登録
  184.             '@admin/Product/product_class.twig' => ['onTemplateProductClassEdit', -10],
  185.             // 商品一覧
  186.             '@admin/Product/index.twig' => ['onTemplateProductIndex'10],
  187.         ];
  188.     }
  189. }