<?php
/*
* Plugin Name : ProductOption
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\ProductOption42\Event;
use Eccube\Event\TemplateEvent;
use Plugin\ProductOption42\Repository\ProductOptionRepository;
use Plugin\ProductOption42\Service\ProductOptionService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductEvent implements EventSubscriberInterface
{
private $productOptionRepository;
private $productOptionService;
public function __construct(
ProductOptionRepository $productOptionRepository,
ProductOptionService $productOptionService
)
{
$this->productOptionRepository = $productOptionRepository;
$this->productOptionService = $productOptionService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Product/list.twig' => 'onTemplateProductList',
'Product/detail.twig' => 'onTemplateProductDetail',
];
}
public function onTemplateProductList(TemplateEvent $event)
{
$parameters = $event->getParameters();
$Products = $parameters['pagination'];
$source = $event->getSource();
// カート追加のフォームが存在しない場合は処理を行わない
if(!preg_match('/url\(\'product_add_cart\'/',$source, $result)){
return;
}
$optionParameters = $this->productOptionService->getOptionParameters($Products);
$parameters = array_merge($parameters, $optionParameters);
$parameters['ProductOptions'] = $this->productOptionRepository->getListByProducts($Products);
$event->setParameters($parameters);
$twig = 'Product/option_css.twig';
$event->addAsset($twig);
$twig = '@ProductOption42/default/Product/option_js.twig';
$event->addSnippet($twig);
$twig = '@ProductOption42/default/Product/list_js.twig';
$event->addSnippet($twig);
if(!preg_match('/include\(\s*\'Product\/option\.twig/',$source, $result)){
if(preg_match("/\<div\sclass\=\"ec\-numberInput\"\>/",$source, $result)){
$search = $result[0];
$replace = "{{ include('Product/option.twig') }}" . $search;
$source = str_replace($search, $replace, $source);
}
}
if(!preg_match('/include\(\s*\'Product\/option\_description\.twig/',$source, $result)){
if(preg_match("/\<div\sclass\=\"ec\-modal\"\>/",$source, $result)){
$search = $result[0];
$replace = "{{ include('Product/option_description.twig') }}" . $search;
$source = str_replace($search, $replace, $source);
}
}
$event->setSource($source);
}
public function onTemplateProductDetail(TemplateEvent $event)
{
$parameters = $event->getParameters();
$Product = $parameters['Product'];
$Products = [$Product];
$optionParameters = $this->productOptionService->getOptionParameters($Products);
$parameters = array_merge($parameters, $optionParameters);
$parameters['ProductOptions'] = $Product->getProductOptions();
$event->setParameters($parameters);
$twig = 'Product/option_css.twig';
$event->addAsset($twig);
$twig = '@ProductOption42/default/Product/option_js.twig';
$event->addSnippet($twig);
$twig = '@ProductOption42/default/Product/detail_js.twig';
$event->addSnippet($twig);
$source = $event->getSource();
if(!preg_match('/include\(\s*\'Product\/option\.twig/',$source, $result)){
if(preg_match("/\<div\sclass\=\"ec\-numberInput\"\>/",$source, $result)){
$search = $result[0];
$replace = "{{ include('Product/option.twig') }}" . $search;
$source = str_replace($search, $replace, $source);
}
}
if(!preg_match('/include\(\s*\'Product\/option\_description\.twig/',$source, $result)){
if(preg_match("/\<div\sclass\=\"ec\-modal\"\>/",$source, $result)){
$search = $result[0];
$replace = "{{ include('Product/option_description.twig') }}" . $search;
$source = str_replace($search, $replace, $source);
}
}
$event->setSource($source);
}
}