<?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\Common\EccubeConfig;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Plugin\ProductOption42\Service\ProductOptionService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AdminOrderEvent implements EventSubscriberInterface
{
private $eccubeConfig;
private $productOptionService;
public function __construct(
EccubeConfig $eccubeConfig,
ProductOptionService $productOptionService
)
{
$this->eccubeConfig = $eccubeConfig;
$this->productOptionService = $productOptionService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Order/edit.twig' => 'onTemplateAdminOrderEdit',
'@admin/Order/order_item_prototype.twig' => 'onTemplateAdminOrderItemPrototype',
'@admin/Order/shipping.twig' => 'onTemplateAdminOrderEdit',
'@admin/Order/search_product.twig' => 'onTemplateAdminOrderSearchProduct',
EccubeEvents::ADMIN_ORDER_CSV_EXPORT_ORDER => 'hookAdminOrderCsvExport',
];
}
public function onTemplateAdminOrderEdit(TemplateEvent $event)
{
$source = $event->getSource();
if(preg_match("/\{\{\sOrderItem\.product\_name\s\}\}/",$source, $result)){
$search = $result[0];
$replace = $search . "{{ include('@ProductOption42/admin/Order/orderitem_option.twig') }}";
$source = str_replace($search, $replace, $source);
}
if(preg_match("/\{\{\s*form\_widget\(orderItemForm\.product\_name,\s*\{\s*'type'\:\s*'hidden'\s*\}\)\s*\}\}/",$source, $result)){
$search = $result[0];
$replace = $search . "{{ form_widget(orderItemForm.option_serial, { 'type': 'hidden' }) }}";
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
}
public function onTemplateAdminOrderItemPrototype(TemplateEvent $event)
{
$source = $event->getSource();
if(preg_match("/\{\{\s*form\_widget\(orderItemForm\.tax\_type\)\s*\}\}/",$source, $result)){
$search = $result[0];
$replace = $search . "{{ form_widget(orderItemForm.option_serial) }}";
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
}
public function onTemplateAdminOrderSearchProduct(TemplateEvent $event)
{
$source = $event->getSource();
if(preg_match("/\<\/script\>/",$source, $result)){
$search = $result[0];
$replace = $search . "{{ include('@ProductOption42/admin/Order/search_product_js.twig') }}";
$source = str_replace($search, $replace, $source);
}
if(preg_match("/fnAddOrderItem/",$source, $result)){
$search = $result[0];
$replace = "fnAddOrderItemOption";
$source = str_replace($search, $replace, $source);
}
if(preg_match("/\{\{\s\'admin\.product\.product\_class\_\_short\'\|trans\s\}\}/",$source, $result)){
$search = $result[0];
$replace = $search . "/{{ 'productoption.admin.common.option'|trans }}";
$source = str_replace($search, $replace, $source);
}
if(preg_match("/\<\/td\>[\n|\r\n|\r]\s*\<td class=\"align\-middle pr\-3 text\-end\"\>/",$source, $result)){
$search = $result[0];
$snippet = file_get_contents($this->eccubeConfig->get('plugin_realdir'). '/ProductOption42/Resource/template/admin/Order/search_product_option.twig');
$replace = $snippet . $search;
$source = str_replace($search, $replace, $source);
}elseif(preg_match("/\<\/td\>[\n|\r\n|\r]\s*\{\%\sif\sProduct\.stock_find\s\%\}/",$source, $result)){
$search = $result[0];
$snippet = file_get_contents($this->eccubeConfig->get('plugin_realdir'). '/ProductOption42/Resource/template/admin/Order/search_product_option.twig');
$replace = $snippet . $search;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$parameters = $event->getParameters();
$Products = $parameters['Products'];
$optionParameters = $this->productOptionService->getOptionParameters($Products);
$parameters = array_merge($parameters, $optionParameters);
$event->setParameters($parameters);
}
public function hookAdminOrderCsvExport(EventArgs $event)
{
$ExportCsvRow = $event->getArgument('ExportCsvRow');
if ($ExportCsvRow->isDataNull()) {
$OrderItem = $event->getArgument('OrderItem');
$Csv = $event->getArgument('Csv');
$csvEntityName = str_replace('\\\\', '\\', $Csv->getEntityName());
if($csvEntityName == 'Plugin\ProductOption42\Entity\OrderItemOption'){
$data = null;
$arrData = [];
$OrderItemOptions = $OrderItem->getOrderItemOptions();
if(!is_null($Csv->getReferenceFieldName())){
$optionId = $Csv->getReferenceFieldName();
foreach($OrderItemOptions as $OrderItemOption){
if($optionId == $OrderItemOption->getOptionId()){
foreach($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategory){
$arrData[] = $OrderItemOptionCategory->getValue();
}
}
}
if(count($arrData) > 0)$data = implode(',',$arrData);
}else{
foreach($OrderItemOptions as $OrderItemOption){
$text = $OrderItemOption->getLabel(). ':';
foreach($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategory){
$text .= $OrderItemOptionCategory->getValue() .',';
}
$arrData[] = rtrim($text,',');
}
if(count($arrData) > 0)$data = implode(',',$arrData);
}
$ExportCsvRow->setData($data);
}
}
}
}