<?php
/*
* Plugin Name : ProductOption
*
* 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\ProductOption42\Event;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Event\EventArgs;
use Plugin\ProductOption42\Entity\ProductOption;
use Plugin\ProductOption42\Repository\OptionRepository;
use Plugin\ProductOption42\Repository\ProductOptionRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CsvImportProductExtEvent implements EventSubscriberInterface
{
private $entityManager;
private $optionRepository;
private $productOptionRepository;
public function __construct(
EntityManagerInterface $entityManager,
OptionRepository $optionRepository,
ProductOptionRepository $productOptionRepository
)
{
$this->entityManager = $entityManager;
$this->optionRepository = $optionRepository;
$this->productOptionRepository = $productOptionRepository;
}
/**
* @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('productoption.csv.product.label.id')){
$header['description'] = trans('productoption.admin.product.product_csv.product_assign_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('productoption.csv.product.label.id')])){
if($row[trans('productoption.csv.product.label.id')] !== '' && preg_match("/[^0-9,]/", $row[trans('productoption.csv.product.label.id')])){
$message = trans('productoption.admin.product.product_csv.not_correct', [
'%line%' => $lineNo,
'%name%' => trans('productoption.csv.product.label.id'),
]);
$errors[] = $message;
}
}
$event->setArgument('errors',$errors);
}
public function hookAdminProductCsvImportProductProcess(EventArgs $event)
{
$row = $event->getArgument('row');
$ProductClass = $event->getArgument('ProductClass');
$Product = $ProductClass->getProduct();
if(isset($row[trans('productoption.csv.product.label.id')])){
// 一旦クリア
$currentProductOptions = $Product->getProductOptions();
foreach ($currentProductOptions as $currentProductOption) {
$this->entityManager->remove($currentProductOption);
$Product->removeProductOption($currentProductOption);
}
$this->entityManager->persist($Product);
$this->entityManager->flush();
}
if(isset($row[trans('productoption.csv.product.label.id')]) && strlen($row[trans('productoption.csv.product.label.id')]) > 0){
$productOptions = explode(',', $row[trans('productoption.csv.product.label.id')]);
foreach($productOptions as $option_id){
if(is_numeric($option_id)){
$Option = $this->optionRepository->find($option_id);
if($Option){
$ProductOption = new ProductOption();
$ProductOption->setProduct($Product);
$ProductOption->setOption($Option);
$this->productOptionRepository->save($ProductOption);
$Product->addProductOption($ProductOption);
}
}
}
}
}
}