- <?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 Eccube\Service\TaxRuleService;
- use Plugin\ProductOption42\Entity\Option;
- use Plugin\ProductOption42\Repository\OptionCategoryRepository;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- class MypageEvent implements EventSubscriberInterface
- {
-     private $optionCategoryRepository;
-     private $taxRuleService;
-     public function __construct(
-             OptionCategoryRepository $optionCategoryRepository,
-             TaxRuleService $taxRuleService
-             )
-     {
-         $this->optionCategoryRepository = $optionCategoryRepository;
-         $this->taxRuleService = $taxRuleService;
-     }
-     /**
-      * @return array
-      */
-     public static function getSubscribedEvents()
-     {
-         return [
-             'Mypage/index.twig' => 'onTemplateMypageIndex',
-             'Mypage/history.twig' => 'onTemplateMypageHistory',
-         ];
-     }
-     public function onTemplateMypageIndex(TemplateEvent $event)
-     {
-         $source = $event->getSource();
-         if(preg_match("/Order\.MergedProductOrderItems/",$source, $result)){
-             $search = $result[0];
-             $replace = "Order.MergedProductOptionOrderItems";
-             $source = str_replace($search, $replace, $source);
-         }
-         if(preg_match("/\<p\sclass\=\"ec\-historyRole\_\_detailPrice\"\>/",$source, $result)){
-             $search = $result[0];
-             $replace = $search . "{{ include('@ProductOption42/default/Mypage/orderitem_option.twig') }}";
-             $source = str_replace($search, $replace, $source);
-         }
-         $event->setSource($source);
-     }
-     public function onTemplateMypageHistory(TemplateEvent $event)
-     {
-         $parameters = $event->getParameters();
-         $Order = $parameters['Order'];
-         foreach($Order->getProductOrderItems() as $OrderItem){
-             $ProductClass = $OrderItem->getProductClass();
-             $current_price = 0;
-             foreach($OrderItem->getOrderItemOptions() as $OrderItemOption){
-                 foreach($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategory){
-                     $optionCategoryId = $OrderItemOptionCategory->getOptionCategoryId();
-                     if($optionCategoryId > 0){
-                         $OptionCategory = $this->optionCategoryRepository->find($optionCategoryId);
-                         if(!is_null($OptionCategory)){
-                             $option_price = $OptionCategory->getValue();
-                             if($OptionCategory->getOption()->getType() == Option::NUMBER_TYPE && $OptionCategory->getMultipleFlg()){
-                                 $option_price *= $OrderItemOptionCategory->getValue();
-                             }
-                             $current_price += $option_price;
-                         }
-                     }
-                 }
-             }
-             $OrderItem->setCurrentPrice($current_price);
-             $OrderItem->setCurrentTax($this->taxRuleService->getTax($OrderItem->getCurrentPrice(),$ProductClass->getProduct(),$ProductClass));
-         }
-         $event->setParameters($parameters);
-         $source = $event->getSource();
-         if(preg_match("/=\s*orderItem\.productClass\.price02IncTax|=\s*orderItem\.productClass\.customer\_rank\_priceIncTax/",$source, $result)){
-             $search = $result[0];
-             $replace = $search . "+ orderItem.CurrentPriceIncTax";
-             $source = str_replace($search, $replace, $source);
-         }
-         if(preg_match("/\{\{\s*orderItem\.productClass\.price02IncTax\|price\s*\}\}/",$source, $result)){
-             $search = $result[0];
-             $replace = "{% set currentPriceIncTax = orderItem.productClass.price02IncTax+orderItem.CurrentPriceIncTax %}{{ currentPriceIncTax|price }}";
-             $source = str_replace($search, $replace, $source);
-         }
-         if(preg_match("/\{\{\s*orderItem\.productClass\.customer_rank_priceIncTax\|price\s*\}\}/",$source, $result)){
-             $search = $result[0];
-             $replace = "{% set currentPriceIncTax = orderItem.productClass.customer_rank_priceIncTax+orderItem.CurrentPriceIncTax %}{{ currentPriceIncTax|price }}";
-             $source = str_replace($search, $replace, $source);
-         }
-         if(preg_match("/\<p\>\{\{\sorderItem\.price\_inc\_tax\|price\s\}\}/",$source, $result)){
-             $search = $result[0];
-             $replace = "{{ include('@ProductOption42/default/Shopping/orderitem_option.twig') }}" . $search;
-             $source = str_replace($search, $replace, $source);
-         }
-         $event->setSource($source);
-     }
- }
-