app/Plugin/TaxManagement42/TemplateLayoutListener.php line 56

Open in your IDE?
  1. <?php
  2. namespace Plugin\TaxManagement42;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Twig\Environment;
  8. use Plugin\TaxManagement42\Services\Simple_html_dom;
  9. use Exception;
  10. use Plugin\TaxManagement42\Services\simple_html_dom_node;
  11. /**
  12.  * Class TemplateLayoutListener
  13.  * @package Plugin\TaxManagement42
  14.  */
  15. class TemplateLayoutListener implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var Environment
  19.      */
  20.     protected $twig;
  21.     const TAX_MANAGEMENT_COMMENT_REGEX '/(<!-- tax_management_通常価格 -->)[\s\S]+?(<!-- tax_management_商品コード -->)/s';
  22.     const TAX_MANAGEMENT_SALE_PRICE_CLASSNAME 'tax_management-sale_price';
  23.     const TAX_MANAGEMENT_LIST_PRICE_CLASSNAME 'tax_management-list_price';
  24.     const TAX_MANAGEMENT_DISCOUNT_RATE_CLASSNAME 'tax_management-discount_rate';
  25.     const TAX_MANAGEMENT_CHECK_CLASSNAME 'tax_management-check';
  26.     const TAX_MANAGEMENT_DETAIL_PAGE_CLASSNAME 'tax_management-detail_page';
  27.     const TAX_MANAGEMENT_LIST_PAGE_CLASSNAME 'tax_management-list_page';
  28.     const DETAIL_PAGE_SALE_PRICE_DEFAULT_TAG_PARENT_CLASSNAME 'ec-productRole__price > .ec-price';
  29.     const DETAIL_PAGE_SALE_PRICE_DEFAULT_TAG_PRICE_CLASSNAME 'ec-price__price';
  30.     const DETAIL_PAGE_SALE_PRICE_DEFAULT_TAG_TAX_CLASSNAME 'ec-price__tax';
  31.     const DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_PARENT_CLASSNAME 'ec-productRole__priceRegular';
  32.     const DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_PRICE_CLASSNAME 'ec-productRole__priceRegularPrice';
  33.     const DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_PRICE_WITH_CLASS_CLASSNAME 'price01-default';
  34.     const DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_TAX_CLASSNAME 'ec-productRole__priceRegularTax';
  35.     const LIST_PAGE_SALE_PRICE_DEFAULT_TAG_PARENT_CLASSNAME 'price02-default';
  36.     const CSS_URL '/../../plugin/TaxManagement42/assets/css/demo.css';
  37.     /**
  38.      * TemplateLayoutListener constructor.
  39.      * @param Environment $twig
  40.      */
  41.     public function __construct(
  42.         Environment $twig
  43.     ) {
  44.         $this->twig $twig;
  45.     }
  46.     public function onKernelResponse(ResponseEvent $event)
  47.     {
  48.         if (!$event->isMasterRequest()) {
  49.             return;
  50.         }
  51.         /** @var \Symfony\Component\HttpFoundation\ParameterBag $attributes */
  52.         $request $event->getRequest();
  53.         $attributes $request->attributes;
  54.         $route $attributes->get('_route');
  55.         $TaxManagementConfig $request->get('TaxManagementConfig');
  56.         if (is_null($TaxManagementConfig)) {
  57.             log_error('Can’t get TaxManagement config from database plg_tax_management_config table');
  58.             return;
  59.         }
  60.         else{
  61.             $entityName get_class($TaxManagementConfig);
  62.             if ($entityName != 'Plugin\TaxManagement42\Entity\Config') {
  63.                 log_error('Can’t get TaxManagement config from database plg_tax_management_config table');
  64.                 return;
  65.             }
  66.         }
  67.         $response $event->getResponse();
  68.         $data '';
  69.         if ($response instanceof Response) {
  70.             $data $response->getContent();
  71.         }
  72.         if ($route == "product_detail") {
  73.             $Product $request->get('Product');
  74.             $price01 '';
  75.             $price02 '';
  76.             if ($response instanceof Response && $Product) {
  77.                 $doc = new Simple_html_dom($datanullnullnullfalse);
  78.                 // get custom tag sale price
  79.                 $price01customElements $doc->find('.'.self::TAX_MANAGEMENT_LIST_PRICE_CLASSNAME);
  80.                 $price02customElements $doc->find('.'.self::TAX_MANAGEMENT_SALE_PRICE_CLASSNAME);
  81.                 $isCommentTagExist preg_match(self::TAX_MANAGEMENT_COMMENT_REGEX$data);
  82.                 
  83.                 $isTaxListPriceTagExist count($price01customElements) > ;
  84.                 $isTaxSalePriceTagExist count($price02customElements) > ;
  85.                 if($isCommentTagExist && !$isTaxListPriceTagExist && !$isTaxSalePriceTagExist) {
  86.                     try {
  87.                         $template_price01 $this->twig->loadTemplate('@TaxManagement42/default/Product/detail_list_price.twig');
  88.                         $price01 $template_price01->render(array('Product' => $Product'isCommentTagExist' => $isCommentTagExist));
  89.                         $template_price02 $this->twig->loadTemplate('@TaxManagement42/default/Product/detail_sale_price.twig');
  90.                         $price02 $template_price02->render(array('Product' => $Product'TaxManagementConfig' => $TaxManagementConfig'isCommentTagExist' => $isCommentTagExist'isIncludeDiscountRate' => $TaxManagementConfig->isDiscountRateFlag()));
  91.                         
  92.                         $data preg_replace(self::TAX_MANAGEMENT_COMMENT_REGEX$price01$price02$data1);
  93.                     } catch (Exception $e) {
  94.                     }
  95.                 } else {
  96.                     try {
  97.                         $price02Value $this->_getPriceValue($Product); // price02 values with number_format
  98.                         $price01Price $this->_getPriceValue($Product1); // price01 values with number_format
  99.                         $price02NoTaxClass $this->_getPrice02AdditionalClass($TaxManagementConfig); // additional class in case default tag
  100.                         $price02IncTaxClass $this->_getPrice02AdditionalClass($TaxManagementConfig1); // additional class in case default tag
  101.                         $price01IncTax $this->_getPrice01IncludeTaxText($TaxManagementConfig$Product);
  102.                         $price02NoTax $this->_getPrice02NoTaxText($TaxManagementConfig$Product);
  103.                         $price02IncTax $this->_getPrice02IncludeTaxText($TaxManagementConfig$Product);
  104.                         // custom tag process
  105.                         $template_price01 $this->twig->loadTemplate('@TaxManagement42/default/Product/detail_list_price.twig');
  106.                         if ($isTaxListPriceTagExist) {
  107.                             foreach($price01customElements as $price01custom) {
  108.                                 if(!$this->_hasClass(self::TAX_MANAGEMENT_DETAIL_PAGE_CLASSNAME$price01custom)) {
  109.                                     $price01custom->setAttribute('class'$price01custom->getAttribute('class'). ' ' self::TAX_MANAGEMENT_DETAIL_PAGE_CLASSNAME);
  110.                                 }
  111.                                 
  112.                                 $price01 $template_price01->render(array('Product' => $Product'isCommentTagExist' => $isCommentTagExist));
  113.                                 $price01custom->innertext $price01;
  114.                                 $defaultPriceValueElement $price01custom->find('.' self::DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_PRICE_CLASSNAME);
  115.                                 $defaultPriceTaxElement $price01custom->find('.' self::DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_TAX_CLASSNAME);
  116.                                 $this->_addCheckClassToDefaultElement($defaultPriceValueElement);
  117.                                 $this->_addCheckClassToDefaultElement($defaultPriceTaxElement);
  118.                             }
  119.                         }
  120.                     } catch (\Throwable $th) {
  121.                         log_error($th);
  122.                     }
  123.                     
  124.                     // default tag process
  125.                     if ($Product->hasProductClass()) {
  126.                         try {
  127.                             $price01DefaultElements $doc->find('.' self::DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_PARENT_CLASSNAME);
  128.                             if(count($price01DefaultElements)) {
  129.                                 foreach($price01DefaultElements as $price01Default) {
  130.                                     
  131.                                     if(count($price01Default->find('.'.self::TAX_MANAGEMENT_LIST_PRICE_CLASSNAME)) > || 
  132.                                     $this->_hasClass(self::TAX_MANAGEMENT_LIST_PRICE_CLASSNAME$price01Default)) {
  133.                                         continue;
  134.                                     }
  135.                                     
  136.                                     $price01Value $price01Default->find('.' self::DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_PRICE_CLASSNAME);
  137.                                     if(
  138.                                         !$price01Value||
  139.                                         count($price01Value) > ||
  140.                                         $this->_hasClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$price01Value[0])) {
  141.                                         continue;
  142.                                     }
  143.                                     
  144.     
  145.                                     $price01ValueElement $price01Value[0]->find('.' self::DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_PRICE_WITH_CLASS_CLASSNAME);
  146.                                     if(
  147.                                         !$price01ValueElement||
  148.                                         count($price01ValueElement) > ||
  149.                                         $this->_hasClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$price01ValueElement[0])) {
  150.                                         continue;
  151.                                     }
  152.     
  153.                                     $price01ValueElement $price01ValueElement[0];
  154.                                     $price01Tax $price01Value[0]->next_sibling();
  155.                                     if(
  156.                                         !$price01Tax || 
  157.                                         !$this->_hasClass(self::DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_TAX_CLASSNAME$price01Tax) || 
  158.                                         $this->_hasClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$price01Tax)) {
  159.                                         continue;
  160.                                     }
  161.                                     $price01ValueElement->innertext $price01IncTax;
  162.                                     $price01Tax->innertext '税込';
  163.                                     $price01Default->setAttribute('class'$price01Default->getAttribute('class'). ' 'self::TAX_MANAGEMENT_LIST_PRICE_CLASSNAME ' ' self::TAX_MANAGEMENT_DETAIL_PAGE_CLASSNAME);
  164.                                 }
  165.                             }
  166.                         } catch (\Throwable $th) {
  167.                             log_error($th);
  168.                         }
  169.                     } else {
  170.                         try {
  171.                             $price01Value $doc->find('.' self::DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_PRICE_CLASSNAME);
  172.                             if(count($price01Value)) {
  173.                                 foreach($price01Value as $price01ValueElement) {
  174.                                     if(
  175.                                         count($price01ValueElement->find('.'.self::TAX_MANAGEMENT_LIST_PRICE_CLASSNAME)) > || 
  176.                                         $this->_hasClass(self::TAX_MANAGEMENT_LIST_PRICE_CLASSNAME$price01ValueElement) || 
  177.                                         $this->_hasClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$price01ValueElement)) {
  178.                                         continue;
  179.                                     }
  180.     
  181.                                     $price01TaxElement $price01ValueElement->next_sibling();
  182.                                     if(
  183.                                         !$price01TaxElement || 
  184.                                         !$this->_hasClass(self::DETAIL_PAGE_LIST_PRICE_DEFAULT_TAG_TAX_CLASSNAME$price01TaxElement)) {
  185.                                         continue;
  186.                                     }
  187.                                     if(
  188.                                         count($price01TaxElement->find('.'.self::TAX_MANAGEMENT_LIST_PRICE_CLASSNAME)) > || 
  189.                                         $this->_hasClass(self::TAX_MANAGEMENT_LIST_PRICE_CLASSNAME$price01TaxElement) ||
  190.                                         $this->_hasClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$price01TaxElement)) {
  191.                                         continue;
  192.                                     }
  193.     
  194.                                     $price01ValueElement->innertext '通常価格:' $price01IncTax;
  195.                                     $price01TaxElement->innertext '税込';
  196.                                     $price01ValueElement->setAttribute('class'$price01ValueElement->getAttribute('class'). ' 'self::TAX_MANAGEMENT_LIST_PRICE_CLASSNAME ' ' self::TAX_MANAGEMENT_DETAIL_PAGE_CLASSNAME);
  197.                                     $price01TaxElement->setAttribute('class'$price01TaxElement->getAttribute('class'). ' 'self::TAX_MANAGEMENT_LIST_PRICE_CLASSNAME ' ' self::TAX_MANAGEMENT_DETAIL_PAGE_CLASSNAME);
  198.                                 }
  199.                             }
  200.                         } catch (\Throwable $th) {
  201.                             log_error($th);
  202.                         }
  203.                     }
  204.                     try {
  205.                         $checkClassElements $doc->find('.'.self::TAX_MANAGEMENT_CHECK_CLASSNAME);
  206.                         if($checkClassElements) {
  207.                             foreach($checkClassElements as $checkClassElement) {
  208.                                 $this->_removeClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$checkClassElement);
  209.                             }
  210.                         }
  211.                     } catch (\Throwable $th) {
  212.                         log_error($th);
  213.                     }
  214.                     $data $doc->save();
  215.                     $doc->clear();
  216.                     unset($doc);
  217.                     $doc = new Simple_html_dom($datanullnullnullfalse);
  218.                     try {
  219.                         $price02customElements $doc->find('.'.self::TAX_MANAGEMENT_SALE_PRICE_CLASSNAME);
  220.                         $isTaxSalePriceTagExist count($price02customElements) > ;
  221.     
  222.                         $template_price02 $this->twig->loadTemplate('@TaxManagement42/default/Product/detail_sale_price.twig');
  223.                         if ($isTaxSalePriceTagExist) {
  224.                             foreach($price02customElements as $price02custom) {
  225.                                 if(!$this->_hasClass(self::TAX_MANAGEMENT_DETAIL_PAGE_CLASSNAME$price02custom)) {
  226.                                     $price02custom->setAttribute('class'$price02custom->getAttribute('class'). ' ' self::TAX_MANAGEMENT_DETAIL_PAGE_CLASSNAME);
  227.                                 }
  228.     
  229.                                 $price02 $template_price02->render(array('Product' => $Product'TaxManagementConfig' => $TaxManagementConfig'isCommentTagExist' => false'isIncludeDiscountRate' => true));
  230.                                 $price02custom->innertext $price02;
  231.                                 $defaultPriceValueElement $price02custom->find('.' self::DETAIL_PAGE_SALE_PRICE_DEFAULT_TAG_PRICE_CLASSNAME);
  232.                                 $defaultPriceTaxElement $price02custom->find('.' self::DETAIL_PAGE_SALE_PRICE_DEFAULT_TAG_TAX_CLASSNAME);
  233.                                 $this->_addCheckClassToDefaultElement($defaultPriceValueElement);
  234.                                 $this->_addCheckClassToDefaultElement($defaultPriceTaxElement);
  235.                             }
  236.                         }
  237.                     } catch (\Throwable $th) {
  238.                         log_error($th);
  239.                     }
  240.                     try {
  241.                         $price02DefaultElements $doc->find('.'.self::DETAIL_PAGE_SALE_PRICE_DEFAULT_TAG_PARENT_CLASSNAME);
  242.                         if(count($price02DefaultElements)) {
  243.                             foreach($price02DefaultElements as $price02Default) {
  244.                                 if(count($price02Default->find('.'.self::TAX_MANAGEMENT_SALE_PRICE_CLASSNAME)) > || 
  245.                                 $this->_hasClass(self::TAX_MANAGEMENT_SALE_PRICE_CLASSNAME$price02Default)) {
  246.                                     continue;
  247.                                 }
  248.     
  249.                                 $price02ValueElement $price02Default->find('.'self::DETAIL_PAGE_SALE_PRICE_DEFAULT_TAG_PRICE_CLASSNAME);
  250.                                 if(
  251.                                     !$price02ValueElement||
  252.                                     count($price02ValueElement) > ||
  253.                                     $this->_hasClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$price02ValueElement[0])) 
  254.                                 {
  255.                                     continue;
  256.                                 }
  257.     
  258.                                 $price02ValueElement $price02ValueElement[0];
  259.                                 $price02TaxElement $price02ValueElement->next_sibling();
  260.                                 if(
  261.                                     !$price02TaxElement || 
  262.                                     !$this->_hasClass(self::DETAIL_PAGE_SALE_PRICE_DEFAULT_TAG_TAX_CLASSNAME$price02TaxElement) || 
  263.                                     $this->_hasClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$price02TaxElement)) 
  264.                                 {
  265.                                     continue;
  266.                                 }
  267.                                 if($price02ValueElement && $price02TaxElement) {
  268.                                     foreach(explode(' '$price02NoTaxClass) as $className) {
  269.                                         if(!$this->_hasClass($className$price02ValueElement)) {
  270.                                             $price02ValueElement->setAttribute('class'$price02ValueElement->getAttribute('class'). ' '$className);
  271.                                         }
  272.                                     }
  273.                                     foreach(explode(' '$price02IncTaxClass) as $className) {
  274.                                         if(!$this->_hasClass($className$price02TaxElement)) {
  275.                                             $price02TaxElement->setAttribute('class'$price02TaxElement->getAttribute('class'). ' '$className);
  276.                                         }
  277.                                     }
  278.     
  279.                                     $discount_rate '';
  280.                                     try {
  281.                                         $template_discount_rate $this->twig->loadTemplate('@TaxManagement42/default/Product/detail_discount_rate.twig');
  282.                                         $discount_rate $template_discount_rate->render(array('Product' => $Product'TaxManagementConfig' => $TaxManagementConfig));
  283.                                     } catch (Exception $e) {
  284.     
  285.                                     }
  286.                                     try {
  287.                                         if ($TaxManagementConfig->isIncludeTaxFlag() === false) {
  288.                                             $price02ValueElement->innertext $price02NoTax;
  289.                                             $price02TaxElement->innertext '('$price02IncTax '税込)';
  290.                                             $price02TaxElement->outertext .= $discount_rate;
  291.                                         } else {
  292.                                             $price02ValueElement->innertext $price02IncTax;
  293.                                             $price02TaxElement->innertext '税込';
  294.                                             $price02TaxElement->outertext .= $discount_rate;
  295.                                         }
  296.                                     } catch (\Throwable $th) {
  297.                                         log_error($th);
  298.                                     }
  299.                                     
  300.                                     $price02Default->setAttribute('class'$price02Default->getAttribute('class'). ' 'self::TAX_MANAGEMENT_SALE_PRICE_CLASSNAME ' ' self::TAX_MANAGEMENT_DETAIL_PAGE_CLASSNAME);
  301.                                 }
  302.                             }
  303.                         }
  304.                     } catch (\Throwable $th) {
  305.                         log_error($th);
  306.                     }
  307.                     try {
  308.                         // remove check class
  309.                         $checkClassElements $doc->find('.'.self::TAX_MANAGEMENT_CHECK_CLASSNAME);
  310.                         if($checkClassElements) {
  311.                             foreach($checkClassElements as $checkClassElement) {
  312.                                 $this->_removeClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$checkClassElement);
  313.                             }
  314.                         }
  315.                     } catch (\Throwable $th) {
  316.                         log_error($th);
  317.                     }
  318.                     $data $doc->save();
  319.                     $doc->clear();
  320.                     unset($doc);
  321.                 }
  322.                 
  323.                 $response->setContent($data);
  324.                 $event->setResponse($response);
  325.             }
  326.         } else if ($route == "product_list") {
  327.             $Products $request->get('tax_management_products');
  328.             $indexArr = [];
  329.             $price01 '';
  330.             $price02 '';
  331.             if ($response instanceof Response && $Products) {
  332.                 foreach($Products as $key => $Product) {
  333.                     $indexArr[$Product->getId()] = $key;
  334.                 }
  335.                 $doc = new Simple_html_dom($datanullnullnullfalse);
  336.                 try {
  337.                     $price02customElements $doc->find('.'.self::TAX_MANAGEMENT_SALE_PRICE_CLASSNAME);
  338.                     if (count($price02customElements)) {
  339.                         foreach($price02customElements as $price02custom) {
  340.                             // get product id
  341.                             $classes explode(' '$price02custom->getAttribute('class'));
  342.                             
  343.                             $product_id 0;
  344.                             foreach($classes as $class) {
  345.                                 if(preg_match("/^tax_management_id_([1-9][0-9]*)$/i"$class$matches)) {
  346.                                     $product_id $matches[1];
  347.                                     break;
  348.                                 }
  349.                             }
  350.                             if(!$product_id) {
  351.                                 continue;
  352.                             }
  353.     
  354.                             if(!isset($indexArr[$product_id])) {
  355.                                 continue;
  356.                             }
  357.     
  358.                             $index $indexArr[$product_id];
  359.                             $Product $Products[$index];
  360.                             $price02Value $this->_getPriceValue($Product);
  361.                             $price02IncTax $this->_getPrice02IncludeTaxText($TaxManagementConfig$Product);
  362.     
  363.                             $defaultPriceValueElement $price02custom->find('.' self::LIST_PAGE_SALE_PRICE_DEFAULT_TAG_PARENT_CLASSNAME);
  364.                             $this->_addCheckClassToDefaultElement($defaultPriceValueElement);
  365.     
  366.                             $price02 '';
  367.                             try {
  368.                                 $template_price02 $this->twig->loadTemplate('@TaxManagement42/default/Product/list_sale_price.twig');
  369.                                 $price02 $template_price02->render(array('Product' => $Product'TaxManagementConfig' => $TaxManagementConfig'isIncludeDiscountRate' => false));
  370.                             } catch(Exception $e) {
  371.     
  372.                             }
  373.                             if(!$this->_hasClass(self::TAX_MANAGEMENT_LIST_PAGE_CLASSNAME$price02custom)) {
  374.                                 $price02custom->setAttribute('class'$price02custom->getAttribute('class'). ' ' self::TAX_MANAGEMENT_LIST_PAGE_CLASSNAME);
  375.                             }
  376.     
  377.                             if($price02) {
  378.                                 $discount_rate '';
  379.                                 try {
  380.                                     $template_discount_rate $this->twig->loadTemplate('@TaxManagement42/default/Product/list_discount_rate.twig');
  381.                                     $discount_rate $template_discount_rate->render(array('Product' => $Product'TaxManagementConfig' => $TaxManagementConfig));
  382.                                 } catch(Exception $e) {
  383.     
  384.                                 }
  385.                                 $price02custom->innertext $price02;
  386.                                 $price02custom->outertext .= $discount_rate;
  387.                             }
  388.                         }
  389.                     }
  390.                 } catch (\Throwable $th) {
  391.                     log_error($th);
  392.                 }
  393.                 try {
  394.                     $price02DefaultElements $doc->find('.'.self::LIST_PAGE_SALE_PRICE_DEFAULT_TAG_PARENT_CLASSNAME);
  395.                     if(count($price02DefaultElements)) {
  396.                         foreach($price02DefaultElements as $price02Default) {
  397.                             if(
  398.                                 count($price02Default->find('.'.self::TAX_MANAGEMENT_SALE_PRICE_CLASSNAME)) > || 
  399.                                 $this->_hasClass(self::TAX_MANAGEMENT_SALE_PRICE_CLASSNAME$price02Default) ||
  400.                                 $this->_hasClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$price02Default)) {
  401.                                 continue;
  402.                             }
  403.                             $classes explode(' '$price02Default->getAttribute('class'));
  404.                             
  405.                             $product_id 0;
  406.                             foreach($classes as $class) {
  407.                                 if(preg_match("/^tax_management_id_([1-9][0-9]*)$/i"$class$matches)) {
  408.                                     $product_id $matches[1];
  409.                                     break;
  410.                                 }
  411.                             }
  412.                             if(!$product_id) {
  413.                                 continue;
  414.                             }
  415.     
  416.                             if(!isset($indexArr[$product_id])) {
  417.                                 continue;
  418.                             }
  419.     
  420.                             $index $indexArr[$product_id];
  421.                             $Product $Products[$index];
  422.                             $price02Value $this->_getPriceValue($Product);
  423.                             $price02NoTax $this->_getPrice02NoTaxText($TaxManagementConfig$Product);
  424.                             $price02IncTax $this->_getPrice02IncludeTaxText($TaxManagementConfig$Product);
  425.     
  426.                             $discount_rate '';
  427.                             try {
  428.                                 $template_discount_rate $this->twig->loadTemplate('@TaxManagement42/default/Product/list_discount_rate.twig');
  429.                                 $discount_rate $template_discount_rate->render(array('Product' => $Product'TaxManagementConfig' => $TaxManagementConfig));
  430.                             } catch(Exception $e) {
  431.     
  432.                             }
  433.                             
  434.                             if ($TaxManagementConfig->isIncludeTaxFlag() === false) {
  435.                                 $price02Default->innertext $price02NoTax;
  436.                             } else {
  437.                                 $price02Default->innertext $price02IncTax;
  438.                             }
  439.                             $price02Default->setAttribute('class'$price02Default->getAttribute('class'). ' 'self::TAX_MANAGEMENT_SALE_PRICE_CLASSNAME ' ' self::TAX_MANAGEMENT_LIST_PAGE_CLASSNAME);
  440.                             if($discount_rate) {
  441.                                 $price02Default->outertext .= $discount_rate;
  442.                             }
  443.                         }
  444.                     }
  445.                 } catch (\Throwable $th) {
  446.                     log_error($th);
  447.                 }
  448.                 try {
  449.                     // remove check class
  450.                     $checkClassElements $doc->find('.'.self::TAX_MANAGEMENT_CHECK_CLASSNAME);
  451.                     if($checkClassElements) {
  452.                         foreach($checkClassElements as &$checkClassElement) {
  453.                             $this->_removeClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$checkClassElement);
  454.                         }
  455.                     }
  456.                 } catch (\Throwable $th) {
  457.                     log_error($th);
  458.                 }
  459.                 $data $doc->save();
  460.                 $doc->clear();
  461.                 unset($doc);
  462.                 
  463.                 $response->setContent($data);
  464.                 $event->setResponse($response);
  465.             }
  466.         }
  467.     }
  468.     /**
  469.      * @return array
  470.      */
  471.     public static function getSubscribedEvents()
  472.     {
  473.         return [
  474.             KernelEvents::RESPONSE => ['onKernelResponse'1],
  475.         ];
  476.     }
  477.     private function _hasClass($className$node)
  478.     {
  479.         if(!$node instanceof simple_html_dom_node) {
  480.             return false;
  481.         }
  482.         $classes explode(' '$node->getAttribute('class'));
  483.         if(!$className) {
  484.             return false;
  485.         }
  486.         return in_array($className$classes);
  487.     }
  488.     private function _removeClass($className, &$node)
  489.     {
  490.         if(!$node instanceof simple_html_dom_node) {
  491.             return false;
  492.         }
  493.         $classes explode(' '$node->getAttribute('class'));
  494.         if(!$className) {
  495.             return false;
  496.         }
  497.         foreach($classes as $key => $class) {
  498.             if($className == $class) {
  499.                 unset($classes[$key]);
  500.                 $classesText implode(' '$classes);
  501.                 $node->setAttribute('class'$classesText);
  502.             }
  503.         }
  504.     }
  505.     private function _getPrice01IncludeTaxText($TaxManagementConfig$Product) {
  506.         $price01Value $this->_getPriceValue($Product1false);
  507.         $price01IncTax '';
  508.         if($price01Value['price01IncTaxMin'] === null) {
  509.             return '';
  510.         }
  511.         if ($Product->hasProductClass()) {
  512.             if ($Product->getPrice01Min() == $Product->getPrice01Max()) {
  513.                 $price01IncTax $price01Value['price01IncTaxMin'];
  514.             } else {
  515.                 $price01IncTax $price01Value['price01IncTaxMin'] . ' ~ ' $price01Value['price01IncTaxMax'];
  516.             }
  517.         } else {
  518.             $price01IncTax $price01Value['price01IncTaxMin'];
  519.         }
  520.         return $price01IncTax;
  521.     }
  522.     private function _getPrice02IncludeTaxText($TaxManagementConfig$Product) {
  523.         $price02Value $this->_getPriceValue($Product2false);
  524.         $price02IncTax '';
  525.         if ($Product->hasProductClass()) {
  526.             if ($Product->getPrice02Min() == $Product->getPrice02Max()) {
  527.                 $price02IncTax $price02Value['price02IncTaxMin'];
  528.             } else {
  529.                 $price02IncTax $price02Value['price02IncTaxMin'] . ' ~ ' $price02Value['price02IncTaxMax'];
  530.             }
  531.         } else {
  532.             $price02IncTax $price02Value['price02IncTaxMin'];
  533.         }
  534.         return $price02IncTax;
  535.     }
  536.     private function _getPrice02NoTaxText($TaxManagementConfig$Product) {
  537.         $price02Value $this->_getPriceValue($Product2false);
  538.         $price02NoTax '';
  539.         if ($Product->hasProductClass()) {
  540.             if ($Product->getPrice02Min() == $Product->getPrice02Max()) {
  541.                 $price02NoTax $price02Value['price02NoTaxMin'];
  542.             } else {
  543.                 $price02NoTax $price02Value['price02NoTaxMin'] . ' ~ ' $price02Value['price02NoTaxMax'];
  544.             }
  545.         } else {
  546.             $price02NoTax $price02Value['price02NoTaxMin'];
  547.         }
  548.         return $price02NoTax;
  549.     }
  550.     private function _getPrice02AdditionalClass($TaxManagementConfig$type 0) {
  551.         if ($TaxManagementConfig->isIncludeTaxFlag() === false) {
  552.             $price02NoTaxClass 'price02-default price02-default-no-tax';
  553.             $price02IncTaxClass '';
  554.         } else {
  555.             $price02NoTaxClass 'price02-default';
  556.             $price02IncTaxClass '';
  557.         }
  558.         if($type == 0) {
  559.             return $price02NoTaxClass;
  560.         } 
  561.         if($type == 1) {
  562.             return $price02IncTaxClass;
  563.         }
  564.     }
  565.     /**
  566.      * Calculate prices value of $Product based on $kind includes
  567.      *
  568.      * @param Eccube\Entity\Product $Product
  569.      * @param int $kind default 2: price02 or sale price
  570.      *                          1: price01 or list price
  571.      * @return array(
  572.      *      'price{$kind}NoTaxMin' => price base on kind not including tax 
  573.      *      'price{$kind}NoTaxMax' =>
  574.      *      'price{$kind}IncTaxMin'
  575.      *      'price{$kind}IncTaxMax'
  576.      *  )
  577.      * 
  578.      */
  579.     private function _getPriceValue($Product$kind 2$number_format true) {
  580.         if($kind == 2) {
  581.             if($number_format) {
  582.                 return array(
  583.                     'price02NoTaxMin' => number_format($Product->getPrice02Min()),
  584.                     'price02NoTaxMax' => number_format($Product->getPrice02Max()),
  585.                     'price02IncTaxMin' => number_format($Product->getPrice02IncTaxMin()),
  586.                     'price02IncTaxMax' => number_format($Product->getPrice02IncTaxMax()),
  587.                 );
  588.             }
  589.             return array(
  590.                 'price02NoTaxMin' => $this->_convertPriceToString($Product->getPrice02Min()),
  591.                 'price02NoTaxMax' => $this->_convertPriceToString($Product->getPrice02Max()),
  592.                 'price02IncTaxMin' => $this->_convertPriceToString($Product->getPrice02IncTaxMin()),
  593.                 'price02IncTaxMax' => $this->_convertPriceToString($Product->getPrice02IncTaxMax()),
  594.             );
  595.         }
  596.         if($kind == 1) {
  597.             if($number_format) {
  598.                 return array(
  599.                     'price01NoTaxMin' => $Product->getPrice01Min() !== null number_format($Product->getPrice01Min()): null,
  600.                     'price01NoTaxMax' => $Product->getPrice01Min() !== null number_format($Product->getPrice01Max()): null,
  601.                     'price01IncTaxMin' => $Product->getPrice01Min() !== null number_format($Product->getPrice01IncTaxMin()): null,
  602.                     'price01IncTaxMax' => $Product->getPrice01Min() !== null number_format($Product->getPrice01IncTaxMax()): null,
  603.                 );
  604.             }
  605.             return array(
  606.                 'price01NoTaxMin' => $Product->getPrice01Min() !== null $this->_convertPriceToString($Product->getPrice01Min()): null,
  607.                 'price01NoTaxMax' => $Product->getPrice01Min() !== null $this->_convertPriceToString($Product->getPrice01Max()): null,
  608.                 'price01IncTaxMin' => $Product->getPrice01Min() !== null $this->_convertPriceToString($Product->getPrice01IncTaxMin()): null,
  609.                 'price01IncTaxMax' => $Product->getPrice01Min() !== null $this->_convertPriceToString($Product->getPrice01IncTaxMax()): null,
  610.             );
  611.         }
  612.     }
  613.     private function _addCheckClassToDefaultElement(&$elements) {
  614.         if(count($elements) != 0) {
  615.             foreach($elements as $element) {
  616.                 if(!$this->_hasClass(self::TAX_MANAGEMENT_CHECK_CLASSNAME$element)) {
  617.                     $element->setAttribute('class'$element->getAttribute('class'). ' ' self::TAX_MANAGEMENT_CHECK_CLASSNAME);
  618.                 } 
  619.             } 
  620.         }
  621.     }
  622.     private function _convertPriceToString($price)
  623.     {
  624.         if (!(int)$price) {
  625.             return '';
  626.         }
  627.         $priceValueTemplate $this->twig->loadTemplate('@TaxManagement42/default/Product/sale_price_value.twig');
  628.         $result $priceValueTemplate->render(array('price' => $price));
  629.         return $result;
  630.     }
  631. }