<?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\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\CustomerRepository;
use Plugin\CustomerRank42\Repository\CustomerPriceRepository;
use Plugin\CustomerRank42\Repository\CustomerRankRepository;
use Plugin\CustomerRank42\Service\CustomerRankService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AdminOrderEvent implements EventSubscriberInterface
{
private $customerRepository;
private $customerPriceRepository;
private $customerRankService;
/**
* CustomerRankController constructor.
* @param CustomerRankRepository $customerRankRepository
*/
public function __construct(
CustomerRepository $customerRepository,
CustomerPriceRepository $customerPriceRepository,
CustomerRankService $customerRankService
)
{
$this->customerRepository = $customerRepository;
$this->customerPriceRepository = $customerPriceRepository;
$this->customerRankService = $customerRankService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Order/index.twig' => 'onTemplateAdminOrder',
'@admin/Order/edit.twig' => 'onTemplateAdminOrderEdit',
EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_COMPLETE => 'hookAdminOrderEditSearchProductComplete',
EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE => 'hookAdminOrderEditIndexComplete',
];
}
public function onTemplateAdminOrder(TemplateEvent $event)
{
$twig = '@CustomerRank42/admin/Order/order_index.twig';
$event->addSnippet($twig);
$js = '@CustomerRank42/admin/Order/order_index.js';
$event->addAsset($js);
}
public function onTemplateAdminOrderEdit(TemplateEvent $event)
{
$source = $event->getSource();
if(preg_match("/\\$\('\#admin\_search\_product\_id'\)\.val\(\),/",$source, $result)){
$search = $result[0];
$replace = $search . "\n'customer_id':$('#order_CustomerId').text(),";
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$twig = '@CustomerRank42/admin/Order/customer_rank.twig';
$event->addSnippet($twig);
$js = '@CustomerRank42/admin/Order/customer_rank.js';
$event->addAsset($js);
}
public function hookAdminOrderEditSearchProductComplete(EventArgs $event)
{
$request = $event->getRequest();
$session = $request->getSession();
$pagination = $event->getArgument('pagination');
if ('POST' === $request->getMethod()) {
$customer_id = $request->get('customer_id');
$session->set('eccube.cusstomerrank.order.product.search', $customer_id);
}else{
$customer_id = $session->get('eccube.cusstomerrank.order.product.search');
}
if(!is_null($customer_id) && is_numeric($customer_id) && $customer_id > 0){
$Customer = $this->customerRepository->find($customer_id);
if(!is_null($Customer)){
$CustomerRank = $Customer->getCustomerRank();
if(!is_null($CustomerRank)){
foreach($pagination as $Product){
foreach($Product->getProductClasses() as $ProductClass){
$ProductClass->setPrice02($this->customerPriceRepository->getCustomerPriceByProductClass($CustomerRank, $ProductClass));
}
}
}
}
}
}
public function hookAdminOrderEditIndexComplete(EventArgs $event)
{
$Customer = $event->getArgument('Customer');
if(!is_null($Customer))$this->customerRankService->checkRank($Customer);
}
}