app/Plugin/CustomerRank42/Event/ProductEvent.php line 94

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : CustomerRank
  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\CustomerRank42\Event;
  12. use Eccube\Event\TemplateEvent;
  13. use Plugin\CustomerRank42\Entity\CustomerRankConfig;
  14. use Plugin\CustomerRank42\Repository\CustomerRankRepository;
  15. use Plugin\CustomerRank42\Service\CustomerRankService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ProductEvent implements EventSubscriberInterface
  18. {
  19.     private $customerRankService;
  20.     /**
  21.      * CustomerRankController constructor.
  22.      * @param CustomerRankRepository $customerRankRepository
  23.      */
  24.     public function __construct(
  25.             CustomerRankService $customerRankService
  26.             )
  27.     {
  28.         $this->customerRankService $customerRankService;
  29.     }
  30.     /**
  31.      * @return array
  32.      */
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             'Product/list.twig' => 'onTemplateProductList',
  37.             'Product/detail.twig' => 'onTemplateProductDetail',
  38.         ];
  39.     }
  40.     public function onTemplateProductDetail(TemplateEvent $event)
  41.     {
  42.         $this->customerRankService->rankCheckForFront();
  43.         $parameters $event->getParameters();
  44.         $Product $parameters['Product'];
  45.         $class_categories = [];
  46.         $loginDisp $this->customerRankService->getConfig('login_disp');
  47.         $initFlag $loginDisp == CustomerRankConfig::DISABLED true false;
  48.         $CustomerRank $this->customerRankService->getCustomerRank($initFlag);
  49.         if(!is_null($CustomerRank)){
  50.             $class_categories[$Product->getId()]['__unselected']['#'] = [
  51.                 'customer_rank_price' => '',
  52.                 'customer_rank_price_inc_tax' => '',
  53.             ];
  54.             foreach($Product->getProductClasses() as $ProductClass){
  55.                 if(!$ProductClass->isVisible())continue;
  56.                 $ClassCategory1 $ProductClass->getClassCategory1();
  57.                 $ClassCategory2 $ProductClass->getClassCategory2();
  58.                 if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
  59.                     continue;
  60.                 }
  61.                 $class_category_id1 $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
  62.                 $class_category_id2 $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
  63.                 $class_categories[$Product->getId()][$class_category_id1]['#'] = [
  64.                     'customer_rank_price' => '',
  65.                     'customer_rank_price_inc_tax' => '',
  66.                 ];
  67.                 $class_categories[$Product->getId()][$class_category_id1]['#'.$class_category_id2] = [
  68.                     'customer_rank_price' => number_format($ProductClass->getCustomerRankPrice($CustomerRank->getId())),
  69.                     'customer_rank_price_inc_tax' => number_format($ProductClass->getCustomerRankPriceIncTax($CustomerRank->getId())),
  70.                 ];
  71.             }
  72.         }
  73.         $parameters['CustomerRank'] = $CustomerRank;
  74.         $parameters['CustomerPrices'] = $class_categories;
  75.         $event->setParameters($parameters);
  76.         $twig '@CustomerRank42/default/Product/product_js.twig';
  77.         $event->addSnippet($twig);
  78.     }
  79.     public function onTemplateProductList(TemplateEvent $event)
  80.     {
  81.         $this->customerRankService->rankCheckForFront();
  82.         $parameters $event->getParameters();
  83.         $pagination $parameters['pagination'];
  84.         $class_categories = [];
  85.         $loginDisp $this->customerRankService->getConfig('login_disp');
  86.         $initFlag $loginDisp == CustomerRankConfig::DISABLED true false;
  87.         $CustomerRank $this->customerRankService->getCustomerRank($initFlag);
  88.         if(!is_null($CustomerRank)){
  89.             foreach($pagination as $Product){
  90.                 $class_categories[$Product->getId()]['__unselected']['#'] = [
  91.                     'customer_rank_price' => '',
  92.                     'customer_rank_price_inc_tax' => '',
  93.                 ];
  94.                 foreach($Product->getProductClasses() as $ProductClass){
  95.                     if(!$ProductClass->isVisible())continue;
  96.                     $ClassCategory1 $ProductClass->getClassCategory1();
  97.                     $ClassCategory2 $ProductClass->getClassCategory2();
  98.                     if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
  99.                         continue;
  100.                     }
  101.                     $class_category_id1 $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
  102.                     $class_category_id2 $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
  103.                     $class_categories[$Product->getId()][$class_category_id1]['#'] = [
  104.                         'customer_rank_price' => '',
  105.                         'customer_rank_price_inc_tax' => '',
  106.                     ];
  107.                     $class_categories[$Product->getId()][$class_category_id1]['#'.$class_category_id2] = [
  108.                         'customer_rank_price' => number_format($ProductClass->getCustomerRankPrice($CustomerRank->getId())),
  109.                         'customer_rank_price_inc_tax' => number_format($ProductClass->getCustomerRankPriceIncTax($CustomerRank->getId())),
  110.                     ];
  111.                 }
  112.             }
  113.         }
  114.         $parameters['CustomerRank'] = $CustomerRank;
  115.         $parameters['CustomerPrices'] = $class_categories;
  116.         $event->setParameters($parameters);
  117.         $twig '@CustomerRank42/default/Product/product_js.twig';
  118.         $event->addSnippet($twig);
  119.     }
  120. }