app/Plugin/ProductOption42/Event/ProductEvent.php line 91

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : ProductOption
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Plugin\ProductOption42\Event;
  12. use Eccube\Event\TemplateEvent;
  13. use Plugin\ProductOption42\Repository\ProductOptionRepository;
  14. use Plugin\ProductOption42\Service\ProductOptionService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ProductEvent implements EventSubscriberInterface
  17. {
  18.     private $productOptionRepository;
  19.     private $productOptionService;
  20.     public function __construct(
  21.             ProductOptionRepository $productOptionRepository,
  22.             ProductOptionService $productOptionService
  23.             )
  24.     {
  25.         $this->productOptionRepository $productOptionRepository;
  26.         $this->productOptionService $productOptionService;
  27.     }
  28.     /**
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             //'Product/list.twig' => 'onTemplateProductList',
  35.             'Product/detail.twig' => 'onTemplateProductDetail',
  36.             'Product/detail_app.twig' => 'onTemplateProductDetail',
  37.         ];
  38.     }
  39.     public function onTemplateProductList(TemplateEvent $event)
  40.     {
  41.         $parameters $event->getParameters();
  42.         $Products $parameters['pagination'];
  43.         $source $event->getSource();
  44.         // カート追加のフォームが存在しない場合は処理を行わない
  45.         if(!preg_match('/url\(\'product_add_cart\'/',$source$result)){
  46.             return;
  47.         }
  48.         $optionParameters $this->productOptionService->getOptionParameters($Products);
  49.         $parameters array_merge($parameters$optionParameters);
  50.         $parameters['ProductOptions'] = $this->productOptionRepository->getListByProducts($Products);
  51.         $event->setParameters($parameters);
  52.         $twig 'Product/option_css.twig';
  53.         $event->addAsset($twig);
  54.         $twig '@ProductOption42/default/Product/option_js.twig';
  55.         $event->addSnippet($twig);
  56.         $twig '@ProductOption42/default/Product/list_js.twig';
  57.         $event->addSnippet($twig);
  58.         if(!preg_match('/include\(\s*\'Product\/option\.twig/',$source$result)){
  59.             if(preg_match("/\<div\sclass\=\"ec\-numberInput\"\>/",$source$result)){
  60.                 $search $result[0];
  61.                 $replace "{{ include('Product/option.twig') }}" $search;
  62.                 $source str_replace($search$replace$source);
  63.             }
  64.         }
  65.         if(!preg_match('/include\(\s*\'Product\/option\_description\.twig/',$source$result)){
  66.             if(preg_match("/\<div\sclass\=\"ec\-modal\"\>/",$source$result)){
  67.                 $search $result[0];
  68.                 $replace "{{ include('Product/option_description.twig') }}" $search;
  69.                 $source str_replace($search$replace$source);
  70.             }
  71.         }
  72.         $event->setSource($source);
  73.     }
  74.     public function onTemplateProductDetail(TemplateEvent $event)
  75.     {
  76.         $parameters $event->getParameters();
  77.         $Product $parameters['Product'];
  78.         $Products = [$Product];
  79.         $optionParameters $this->productOptionService->getOptionParameters($Products);
  80.         $parameters array_merge($parameters$optionParameters);
  81.         $parameters['ProductOptions'] = $Product->getProductOptions();
  82.         $event->setParameters($parameters);
  83.         $twig 'Product/option_css.twig';
  84.         $event->addAsset($twig);
  85.         $twig '@ProductOption42/default/Product/option_js.twig';
  86.         $event->addSnippet($twig);
  87.         $twig '@ProductOption42/default/Product/detail_js.twig';
  88.         $event->addSnippet($twig);
  89.         $source $event->getSource();
  90.         if(!preg_match('/include\(\s*\'Product\/option\.twig/',$source$result)){
  91.             if(preg_match("/\<div\sclass\=\"ec\-numberInput\"\>/",$source$result)){
  92.                 $search $result[0];
  93.                 $replace "{{ include('Product/option.twig') }}" $search;
  94.                 $source str_replace($search$replace$source);
  95.             }
  96.         }
  97.         if(!preg_match('/include\(\s*\'Product\/option\_description\.twig/',$source$result)){
  98.             if(preg_match("/\<div\sclass\=\"ec\-modal\"\>/",$source$result)){
  99.                 $search $result[0];
  100.                 $replace "{{ include('Product/option_description.twig') }}" $search;
  101.                 $source str_replace($search$replace$source);
  102.             }
  103.         }
  104.         $event->setSource($source);
  105.     }
  106. }