<?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 Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Plugin\ProductOption42\Entity\Option;
use Plugin\ProductOption42\Entity\ProductOption;
use Plugin\ProductOption42\Repository\ProductOptionRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AdminProductEvent implements EventSubscriberInterface
{
private $productOptionRepository;
public function __construct(
ProductOptionRepository $productOptionRepository
)
{
$this->productOptionRepository = $productOptionRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Product/index.twig' => 'onTemplateAdminProduct',
'@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
];
}
public function onTemplateAdminProduct(TemplateEvent $event)
{
$twig = '@ProductOption42/admin/Product/product_list.twig';
$event->addSnippet($twig);
$js = '@ProductOption42/admin/Product/product_list.js';
$event->addAsset($js);
}
public function onTemplateAdminProductEdit(TemplateEvent $event)
{
$source = $event->getSource();
if(preg_match("/\<\/div\>[\n|\r\n|\r]\s*\{\%\sif\sid\sis\snot\snull\s\%\}[\n|\r\n|\r]/",$source, $result)){
$search = $result[0];
$replace_parts = trim($search, '</div>');
$replace = "</div>{{ include('@ProductOption42/admin/Product/product_edit.twig') }}" . $replace_parts;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$parameters = $event->getParameters();
$arrType[Option::SELECT_TYPE] = trans("productoption.option.type.select");
$arrType[Option::RADIO_TYPE] = trans("productoption.option.type.radio");
$arrType[Option::CHECKBOX_TYPE] = trans("productoption.option.type.checkbox");
$arrType[Option::TEXT_TYPE] = trans("productoption.option.type.text");
$arrType[Option::TEXTAREA_TYPE] = trans("productoption.option.type.textarea");
$arrType[Option::DATE_TYPE] = trans("productoption.option.type.date");
$arrType[Option::NUMBER_TYPE] = trans("productoption.option.type.number");
$parameters['arrType'] = $arrType;
$event->setParameters($parameters);
}
public function hookAdminProductCopyComplete(EventArgs $event)
{
$Product = $event->getArgument('Product');
$CopyProduct = $event->getArgument('CopyProduct');
foreach ($Product->getProductOptions() as $oldProductOption) {
$newProductOption = new ProductOption();
$newProductOption->setProduct($CopyProduct);
$newProductOption->setOption($oldProductOption->getOption());
$newProductOption->setSortNo($oldProductOption->getSortNo());
$this->productOptionRepository->saveProductOption($newProductOption);
}
}
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\ProductOption42\Entity\ProductOption'){
$array = [];
foreach($Product->getProductOptions() as $ProductOption){
if($Csv->getFieldName() == 'product_option_id'){
$array[] = $ProductOption->getOption()->getId();
}elseif($Csv->getFieldName() == 'product_option_name'){
$array[] = $ProductOption->getOption()->getBackendName();
}
}
$ExportCsvRow->setData(implode(',', $array));
}
}
}
}