<?php
/*
* Plugin Name : DeliveryCool4
*
* 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\DeliveryCool42\Event;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\ProductClassRepository;
use Plugin\DeliveryCool42\Service\DeliveryCoolService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AdminProductEvent implements EventSubscriberInterface
{
private $entityManager;
private $productClassRepository;
public function __construct(
EntityManagerInterface $entityManager,
ProductClassRepository $productClassRepository
)
{
$this->entityManager = $entityManager;
$this->productClassRepository = $productClassRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Product/product_class.twig' => 'onTemplateAdminProductClassEdit',
EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
];
}
public function onTemplateAdminProductClassEdit(TemplateEvent $event)
{
$twig = '@DeliveryCool42/admin/Product/product_class_js.twig';
$event->addSnippet($twig);
}
public function hookAdminProductCopyComplete(EventArgs $event)
{
$Product = $event->getArgument('Product');
$CopyProduct = $event->getArgument('CopyProduct');
$orgProductClasses = $Product->getProductClasses();
foreach ($orgProductClasses as $ProductClass) {
$CopyProductClass = $this->productClassRepository->findOneBy(['Product'=> $CopyProduct, 'ClassCategory1' => $ProductClass->getClassCategory1(), 'ClassCategory2' => $ProductClass->getClassCategory2()]);
if($CopyProductClass){
$CopyProductClass->setDeliveryCoolType($ProductClass->getDeliveryCoolType());
$this->entityManager->persist($CopyProductClass);
}
}
$this->entityManager->flush();
}
public function hookAdminProductCsvExport(EventArgs $event)
{
$arrTYPE = [];
$arrTYPE[DeliveryCoolService::REGULAR_PRODUCT] = trans('deliverycool.const.product.temptype.normal_not_all');
$arrTYPE[DeliveryCoolService::REGULAR_INC_COOL_PRODUCT] = trans('deliverycool.const.product.temptype.normal_inc_cool');
$arrTYPE[DeliveryCoolService::REGULAR_INC_ALL_PRODUCT] = trans('deliverycool.const.product.temptype.normal_inc_all');
$arrTYPE[DeliveryCoolService::COOL_PRODUCT] = trans('deliverycool.const.product.temptype.cool_not_all');
$arrTYPE[DeliveryCoolService::COOL_INC_COLD_PRODUCT] = trans('deliverycool.const.product.temptype.cool_inc_cold');
$arrTYPE[DeliveryCoolService::COLD_PRODUCT] = trans('deliverycool.const.product.temptype.cold');
$ExportCsvRow = $event->getArgument('ExportCsvRow');
if ($ExportCsvRow->isDataNull()) {
$ProductClass = $event->getArgument('ProductClass');
$Csv = $event->getArgument('Csv');
$csvEntityName = str_replace('\\\\', '\\', $Csv->getEntityName());
if($csvEntityName == 'Eccube\Entity\ProductClass' && $Csv->getFieldName() == 'delivery_cool_disp_name'){
$type = $ProductClass->getDeliveryCoolType();
if(is_null($type))return;
$data = $arrTYPE[$type];
$ExportCsvRow->setData($data);
}
}
}
}