<?php
/*
* Plugin Name : CustomerRank
*
* 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\CustomerRank42\Event;
use Eccube\Event\TemplateEvent;
use Plugin\CustomerRank42\Entity\CustomerRankConfig;
use Plugin\CustomerRank42\Repository\CustomerRankRepository;
use Plugin\CustomerRank42\Service\CustomerRankService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductEvent implements EventSubscriberInterface
{
private $customerRankService;
/**
* CustomerRankController constructor.
* @param CustomerRankRepository $customerRankRepository
*/
public function __construct(
CustomerRankService $customerRankService
)
{
$this->customerRankService = $customerRankService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Product/list.twig' => 'onTemplateProductList',
'Product/detail.twig' => 'onTemplateProductDetail',
];
}
public function onTemplateProductDetail(TemplateEvent $event)
{
$this->customerRankService->rankCheckForFront();
$parameters = $event->getParameters();
$Product = $parameters['Product'];
$class_categories = [];
$loginDisp = $this->customerRankService->getConfig('login_disp');
$initFlag = $loginDisp == CustomerRankConfig::DISABLED ? true : false;
$CustomerRank = $this->customerRankService->getCustomerRank($initFlag);
if(!is_null($CustomerRank)){
$class_categories[$Product->getId()]['__unselected']['#'] = [
'customer_rank_price' => '',
'customer_rank_price_inc_tax' => '',
];
foreach($Product->getProductClasses() as $ProductClass){
if(!$ProductClass->isVisible())continue;
$ClassCategory1 = $ProductClass->getClassCategory1();
$ClassCategory2 = $ProductClass->getClassCategory2();
if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
continue;
}
$class_category_id1 = $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
$class_category_id2 = $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
$class_categories[$Product->getId()][$class_category_id1]['#'] = [
'customer_rank_price' => '',
'customer_rank_price_inc_tax' => '',
];
$class_categories[$Product->getId()][$class_category_id1]['#'.$class_category_id2] = [
'customer_rank_price' => number_format($ProductClass->getCustomerRankPrice($CustomerRank->getId())),
'customer_rank_price_inc_tax' => number_format($ProductClass->getCustomerRankPriceIncTax($CustomerRank->getId())),
];
}
}
$parameters['CustomerRank'] = $CustomerRank;
$parameters['CustomerPrices'] = $class_categories;
$event->setParameters($parameters);
$twig = '@CustomerRank42/default/Product/product_js.twig';
$event->addSnippet($twig);
}
public function onTemplateProductList(TemplateEvent $event)
{
$this->customerRankService->rankCheckForFront();
$parameters = $event->getParameters();
$pagination = $parameters['pagination'];
$class_categories = [];
$loginDisp = $this->customerRankService->getConfig('login_disp');
$initFlag = $loginDisp == CustomerRankConfig::DISABLED ? true : false;
$CustomerRank = $this->customerRankService->getCustomerRank($initFlag);
if(!is_null($CustomerRank)){
foreach($pagination as $Product){
$class_categories[$Product->getId()]['__unselected']['#'] = [
'customer_rank_price' => '',
'customer_rank_price_inc_tax' => '',
];
foreach($Product->getProductClasses() as $ProductClass){
if(!$ProductClass->isVisible())continue;
$ClassCategory1 = $ProductClass->getClassCategory1();
$ClassCategory2 = $ProductClass->getClassCategory2();
if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
continue;
}
$class_category_id1 = $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
$class_category_id2 = $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
$class_categories[$Product->getId()][$class_category_id1]['#'] = [
'customer_rank_price' => '',
'customer_rank_price_inc_tax' => '',
];
$class_categories[$Product->getId()][$class_category_id1]['#'.$class_category_id2] = [
'customer_rank_price' => number_format($ProductClass->getCustomerRankPrice($CustomerRank->getId())),
'customer_rank_price_inc_tax' => number_format($ProductClass->getCustomerRankPriceIncTax($CustomerRank->getId())),
];
}
}
}
$parameters['CustomerRank'] = $CustomerRank;
$parameters['CustomerPrices'] = $class_categories;
$event->setParameters($parameters);
$twig = '@CustomerRank42/default/Product/product_js.twig';
$event->addSnippet($twig);
}
}