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

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.         ];
  37.     }
  38.     public function onTemplateProductList(TemplateEvent $event)
  39.     {
  40.         $parameters $event->getParameters();
  41.         $Products $parameters['pagination'];
  42.         $source $event->getSource();
  43.         // カート追加のフォームが存在しない場合は処理を行わない
  44.         if(!preg_match('/url\(\'product_add_cart\'/',$source$result)){
  45.             return;
  46.         }
  47.         $optionParameters $this->productOptionService->getOptionParameters($Products);
  48.         $parameters array_merge($parameters$optionParameters);
  49.         $parameters['ProductOptions'] = $this->productOptionRepository->getListByProducts($Products);
  50.         $event->setParameters($parameters);
  51.         $twig 'Product/option_css.twig';
  52.         $event->addAsset($twig);
  53.         $twig '@ProductOption42/default/Product/option_js.twig';
  54.         $event->addSnippet($twig);
  55.         $twig '@ProductOption42/default/Product/list_js.twig';
  56.         $event->addSnippet($twig);
  57.         if(!preg_match('/include\(\s*\'Product\/option\.twig/',$source$result)){
  58.             if(preg_match("/\<div\sclass\=\"ec\-numberInput\"\>/",$source$result)){
  59.                 $search $result[0];
  60.                 $replace "{{ include('Product/option.twig') }}" $search;
  61.                 $source str_replace($search$replace$source);
  62.             }
  63.         }
  64.         if(!preg_match('/include\(\s*\'Product\/option\_description\.twig/',$source$result)){
  65.             if(preg_match("/\<div\sclass\=\"ec\-modal\"\>/",$source$result)){
  66.                 $search $result[0];
  67.                 $replace "{{ include('Product/option_description.twig') }}" $search;
  68.                 $source str_replace($search$replace$source);
  69.             }
  70.         }
  71.         $event->setSource($source);
  72.     }
  73.     public function onTemplateProductDetail(TemplateEvent $event)
  74.     {
  75.         $parameters $event->getParameters();
  76.         $Product $parameters['Product'];
  77.         $Products = [$Product];
  78.         $optionParameters $this->productOptionService->getOptionParameters($Products);
  79.         $parameters array_merge($parameters$optionParameters);
  80.         $parameters['ProductOptions'] = $Product->getProductOptions();
  81.         $event->setParameters($parameters);
  82.         $twig 'Product/option_css.twig';
  83.         $event->addAsset($twig);
  84.         $twig '@ProductOption42/default/Product/option_js.twig';
  85.         $event->addSnippet($twig);
  86.         $twig '@ProductOption42/default/Product/detail_js.twig';
  87.         $event->addSnippet($twig);
  88.         $source $event->getSource();
  89.         if(!preg_match('/include\(\s*\'Product\/option\.twig/',$source$result)){
  90.             if(preg_match("/\<div\sclass\=\"ec\-numberInput\"\>/",$source$result)){
  91.                 $search $result[0];
  92.                 $replace "{{ include('Product/option.twig') }}" $search;
  93.                 $source str_replace($search$replace$source);
  94.             }
  95.         }
  96.         if(!preg_match('/include\(\s*\'Product\/option\_description\.twig/',$source$result)){
  97.             if(preg_match("/\<div\sclass\=\"ec\-modal\"\>/",$source$result)){
  98.                 $search $result[0];
  99.                 $replace "{{ include('Product/option_description.twig') }}" $search;
  100.                 $source str_replace($search$replace$source);
  101.             }
  102.         }
  103.         $event->setSource($source);
  104.     }
  105. }