<?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 Doctrine\ORM\EntityManagerInterface;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\ProductClassRepository;
use Plugin\CustomerRank42\Entity\CustomerPrice;
use Plugin\CustomerRank42\Repository\CustomerPriceRepository;
use Plugin\CustomerRank42\Repository\CustomerRankRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AdminProductEvent implements EventSubscriberInterface
{
private $entityManager;
private $productClassRepository;
private $customerRankRepository;
private $customerPriceRepository;
/**
* CustomerRankController constructor.
* @param CustomerRankRepository $customerRankRepository
*/
public function __construct(
EntityManagerInterface $entityManager,
ProductClassRepository $productClassRepository,
CustomerRankRepository $customerRankRepository,
CustomerPriceRepository $customerPriceRepository
)
{
$this->entityManager = $entityManager;
$this->productClassRepository = $productClassRepository;
$this->customerRankRepository = $customerRankRepository;
$this->customerPriceRepository = $customerPriceRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'hookAdminProductEditComplete',
EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
'@admin/Product/product_class.twig' => 'onTemplateAdminProductClassEdit',
];
}
public function onTemplateAdminProductEdit(TemplateEvent $event)
{
$parameters = $event->getParameters();
$CustomerRanks = $this->customerRankRepository->getList();
$parameters['CustomerRanks'] = $CustomerRanks;
$event->setParameters($parameters);
$twig = '@CustomerRank42/admin/Product/customer_price.twig';
$event->addSnippet($twig);
$js = '@CustomerRank42/admin/Product/customer_price.js';
$event->addAsset($js);
}
public function hookAdminProductEditComplete(EventArgs $event)
{
$Product = $event->getArgument('Product');
$form = $event->getArgument('form');
$has_class = $Product->hasProductClass();
if(!$has_class){
$CustomerRanks = $this->customerRankRepository->getList();
$ProductClass = $form['class']->getData();
foreach($CustomerRanks as $CustomerRank){
if($form['class']->has('customer_price_'. $CustomerRank->getId())){
$CustomerPrice = $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass, 'CustomerRank' => $CustomerRank]);
if(!$CustomerPrice){
$CustomerPrice = new CustomerPrice();
$CustomerPrice->setProductClass($ProductClass);
$CustomerPrice->setCustomerRank($CustomerRank);
}
$CustomerPrice->setPrice($form['class']->get('customer_price_'. $CustomerRank->getId())->getData());
$this->entityManager->persist($CustomerPrice);
}
}
$this->entityManager->flush();
}
}
public function hookAdminProductCopyComplete(EventArgs $event)
{
$Product = $event->getArgument('Product');
$CopyProduct = $event->getArgument('CopyProduct');
$orgProductClasses = $Product->getProductClasses();
$CustomerRanks = $this->customerRankRepository->getList();
foreach($CustomerRanks as $CustomerRank){
foreach ($orgProductClasses as $ProductClass) {
$CopyProductClass = $this->productClassRepository->findOneBy(['Product'=> $CopyProduct, 'ClassCategory1' => $ProductClass->getClassCategory1(), 'ClassCategory2' => $ProductClass->getClassCategory2()]);
$orgCustomerPrice = $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass, 'CustomerRank' => $CustomerRank]);
if($CopyProductClass){
$CustomerPrice = new CustomerPrice();
$CustomerPrice->setProductClass($CopyProductClass);
$CustomerPrice->setCustomerRank($CustomerRank);
if($orgCustomerPrice){
$CustomerPrice->setPrice($orgCustomerPrice->getPrice());
}
$this->entityManager->persist($CustomerPrice);
}
}
}
$this->entityManager->flush();
}
public function hookAdminProductCsvExport(EventArgs $event)
{
$ExportCsvRow = $event->getArgument('ExportCsvRow');
if ($ExportCsvRow->isDataNull()) {
$csvService = $event->getArgument('csvService');
$ProductClass = $event->getArgument('ProductClass');
$Csv = $event->getArgument('Csv');
$csvEntityName = str_replace('\\\\', '\\', $Csv->getEntityName());
if($csvEntityName == 'Plugin\CustomerRank42\Entity\CustomerPrice'){
$customer_rank_id = ltrim($Csv->getFieldName(), 'customerrank_price_');
if(is_numeric($customer_rank_id)){
$CustomerRank = $this->customerRankRepository->find($customer_rank_id);
if(!is_null($CustomerRank)){
$CustomerPrice = $this->customerPriceRepository->findOneBy(['ProductClass' => $ProductClass, 'CustomerRank' => $CustomerRank]);
if(!is_null($CustomerPrice)){
$ExportCsvRow->setData($CustomerPrice->getPrice());
}
}
}
}
}
}
public function onTemplateAdminProductClassEdit(TemplateEvent $event)
{
$parameters = $event->getParameters();
$CustomerRanks = $this->customerRankRepository->getList();
$parameters['CustomerRanks'] = $CustomerRanks;
$event->setParameters($parameters);
$twig = '@CustomerRank42/admin/Product/customer_price_class.twig';
$event->addSnippet($twig);
$js = '@CustomerRank42/admin/Product/customer_price_class.js';
$event->addAsset($js);
}
}