<?php
/*
* Plugin Name : SalesRestrictions4
*
* 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\SalesRestrictions42\Event;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Plugin\SalesRestrictions42\Repository\ProductCustomerRankRepository;
use Plugin\SalesRestrictions42\Service\SalesRestrictionsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AdminProductEvent implements EventSubscriberInterface
{
private $entityManager;
private $productCustomerRankRepository;
private $salesRestrictionsService;
public function __construct(
EntityManagerInterface $entityManager,
ProductCustomerRankRepository $productCustomerRankRepository,
SalesRestrictionsService $salesRestrictionsService
)
{
$this->entityManager = $entityManager;
$this->productCustomerRankRepository = $productCustomerRankRepository;
$this->salesRestrictionsService = $salesRestrictionsService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
];
}
public function onTemplateAdminProductEdit(TemplateEvent $event)
{
$source = $event->getSource();
if(preg_match("/\{\%\sfor\sf\sin\sform(\sif|\|filter\(f\s\=\>)\sf\.vars\.eccube\_form\_options\.auto\_render/",$source, $result)){
$search = $result[0];
$replace = "{{ include('@SalesRestrictions42/admin/Product/ext_edit.twig') }}" . $search;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
}
public function hookAdminProductCopyComplete(EventArgs $event)
{
$Product = $event->getArgument('Product');
$CopyProduct = $event->getArgument('CopyProduct');
$plgProducts = $this->productCustomerRankRepository->findBy(['Product' => $Product]);
if($plgProducts){
foreach($plgProducts as $plgProduct){
$copyPlgProduct = new \Plugin\SalesRestrictions42\Entity\ProductCustomerRank();
$copyPlgProduct->setProduct($CopyProduct);
$copyPlgProduct->setCustomerRankId($plgProduct->getCustomerRankId());
$this->entityManager->persist($copyPlgProduct);
}
$this->entityManager->flush();
}
}
public function hookAdminProductCsvExport(EventArgs $event)
{
$ExportCsvRow = $event->getArgument('ExportCsvRow');
if ($ExportCsvRow->isDataNull()) {
$ProductClass = $event->getArgument('ProductClass');
$Product = $ProductClass->getProduct();
$Csv = $event->getArgument('Csv');
$csvEntityName = str_replace('\\\\', '\\', $Csv->getEntityName());
if($csvEntityName == 'Plugin\SalesRestrictions42\Entity\ProductCustomerRank'){
$plgProducts = $this->productCustomerRankRepository->findBy(['Product' => $Product]);
$ret = [];
foreach($plgProducts as $plgProduct){
if($Csv->getFieldName() == 'id'){
$ret[] = $plgProduct->getCustomerRankId();
}elseif($Csv->getFieldName() == 'name'){
$rank_id = $plgProduct->getCustomerRankId();
if($rank_id == -1){
$ret[] = trans('salesrestrictions.form.product.choice.guest');
}else {
if($this->salesRestrictionsService->checkInstallPlugin('CustomerRank42')){
if($rank_id == 0){
$ret[] = trans('salesrestrictions.form.product.choice.empty');
}else{
$customerRankRepository = $this->entityManager->getRepository('Plugin\CustomerRank42\Entity\CustomerRank');
$CustomerRanks = $customerRankRepository->getList();
foreach($CustomerRanks as $CustomerRank){
if($CustomerRank->getId() == $rank_id)
$ret[] = $CustomerRank->getName();
}
}
}
}
}
}
$value = implode(',', $ret);
$ExportCsvRow->setData($value);
}
}
}
}