app/Plugin/PinpointSale/EventSubscriber/AdminProductEventSubscriber.php line 40

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