<?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\EventArgs;
use Plugin\SalesRestrictions42\Repository\ProductCustomerRankRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CsvImportProductExtEvent implements EventSubscriberInterface
{
private $entityManager;
private $productCustomerRankRepository;
public function __construct(
EntityManagerInterface $entityManager,
ProductCustomerRankRepository $productCustomerRankRepository
)
{
$this->entityManager = $entityManager;
$this->productCustomerRankRepository = $productCustomerRankRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'csvimportproductext.admin.product.csv.import.product.descriptions' => 'hookAdminProductCsvImportProductDescriptions',
'csvimportproductext.admin.product.csv.import.product.check'=> 'hookAdminProductCsvImportProductCheck',
'csvimportproductext.admin.product.csv.import.product.process' => 'hookAdminProductCsvImportProductProcess',
];
}
public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
{
$header = $event->getArgument('header');
$key = $event->getArgument('key');
if($key == trans('salesrestrictions.csv.common.id')){
$header['description'] = trans('salesrestrictions.csv.product.sales_restrictions.description');
$header['required'] = false;
}
$event->setArgument('header',$header);
}
public function hookAdminProductCsvImportProductCheck(EventArgs $event)
{
$row = $event->getArgument('row');
$lineNo = $event->getArgument('lineNo');
$errors = $event->getArgument('errors');
if(isset($row[trans('salesrestrictions.csv.common.id')])){
if($row[trans('salesrestrictions.csv.common.id')] !== ''){
$sales_restrictions = explode(',', $row[trans('salesrestrictions.csv.common.id')]);
foreach($sales_restrictions as $value){
if($value != '' && !is_numeric($value)){
$message = trans('salesrestrictions.csv.product.sales_restrictions.error', [
'%line%' => $lineNo,
'%name%' => trans('salesrestrictions.csv.common.id'),
]);
$errors[] = $message;
break;
}
}
}
}
$event->setArgument('errors',$errors);
}
public function hookAdminProductCsvImportProductProcess(EventArgs $event)
{
$row = $event->getArgument('row');
$ProductClass = $event->getArgument('ProductClass');
if(isset($row[trans('salesrestrictions.csv.common.id')])){
$Product = $ProductClass->getProduct();
$plgProducts = $this->productCustomerRankRepository->findBy(['Product' => $Product]);
foreach($plgProducts as $plgProduct){
$this->entityManager->remove($plgProduct);
}
$this->entityManager->flush();
$sales_restrictions = explode(',', $row[trans('salesrestrictions.csv.common.id')]);
if(count($sales_restrictions) > 0){
foreach($sales_restrictions as $value){
if(is_numeric($value)){
$plgProduct = new \Plugin\SalesRestrictions42\Entity\ProductCustomerRank();
$plgProduct->setProduct($Product);
$plgProduct->setCustomerRankId($value);
$this->entityManager->persist($plgProduct);
}
}
$this->entityManager->flush();
}
}
}
}