<?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\Repository\CustomerPriceRepository;
use Plugin\CustomerRank42\Repository\CustomerRankRepository;
use Plugin\CustomerRank42\Service\CustomerRankService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MypageEvent implements EventSubscriberInterface
{
private $customerPriceRepository;
private $customerRankService;
/**
* CustomerRankController constructor.
* @param CustomerRankRepository $customerRankRepository
*/
public function __construct(
CustomerPriceRepository $customerPriceRepository,
CustomerRankService $customerRankService
)
{
$this->customerPriceRepository = $customerPriceRepository;
$this->customerRankService = $customerRankService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Mypage/history.twig' => 'onTemplateMypageHistory',
'Mypage/favorite.twig' => 'onTemplateMypageFavorite',
];
}
public function onTemplateMypageHistory(TemplateEvent $event)
{
$this->customerRankService->rankCheckForFront();
$parameters = $event->getParameters();
$Order = $parameters['Order'];
$CustomerRank = $this->customerRankService->getCustomerRank(true);
foreach($Order->getOrderItems() as $OrderItem){
$ProductClass = $OrderItem->getProductClass();
if(!is_null($ProductClass)){
if(!$ProductClass->isVisible())continue;
$ProductClass->setCustomerRankPrice($this->customerPriceRepository->getCustomerPriceByProductClass($CustomerRank, $ProductClass));
$ProductClass->setCustomerRankPriceIncTax($this->customerPriceRepository->getCustomerPriceIncTaxByProductClass($CustomerRank, $ProductClass));
}
}
$event->setParameters($parameters);
$source = $event->getSource();
$source = preg_replace("/price02/","customer_rank_price",$source);
$event->setSource($source);
}
public function onTemplateMypageFavorite(TemplateEvent $event)
{
$this->customerRankService->rankCheckForFront();
$parameters = $event->getParameters();
$pagination = $parameters['pagination'];
$CustomerRank = $this->customerRankService->getCustomerRank(true);
foreach($pagination as $FavoriteItem){
$Product = $FavoriteItem->getProduct();
foreach($Product->getProductClasses() as $ProductClass){
if(!is_null($ProductClass)){
if(!$ProductClass->isVisible())continue;
$ProductClass->setCustomerRankPrice($this->customerPriceRepository->getCustomerPriceByProductClass($CustomerRank, $ProductClass));
$ProductClass->setCustomerRankPriceIncTax($this->customerPriceRepository->getCustomerPriceIncTaxByProductClass($CustomerRank, $ProductClass));
}
}
}
$parameters['CustomerRank'] = $CustomerRank;
$event->setParameters($parameters);
}
}