<?php
/*
* This file is part of the Gift42 Plugin
*
* Copyright (C) 2023 Diezon.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\Gift42;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Plugin\Gift42\Common\GiftKind;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Event implements EventSubscriberInterface
{
/**
* Event constructor.
*/
public function __construct() {
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Product/product.twig' => 'onAdminProductProductTwig',
'@admin/Order/edit.twig' => 'onAdminOrderEditTwig',
'Product/detail.twig' => 'onProductDetailTwig',
'Cart/index.twig' => 'onCartIndexTwig',
'Shopping/index.twig' => 'onShoppingIndexTwig',
'Shopping/confirm.twig' => 'onShoppingConfirmTwig',
'Mypage/index.twig' => 'onMypageIndexTwig',
'Mypage/history.twig' => 'onMypageHistoryTwig',
EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => self::getMethodName(EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT),
EccubeEvents::ADMIN_ORDER_CSV_EXPORT_ORDER => self::getMethodName(EccubeEvents::ADMIN_ORDER_CSV_EXPORT_ORDER),
];
}
/**
* @param TemplateEvent $event
*/
public function onAdminProductProductTwig(TemplateEvent $event)
{
$event->addSnippet('@Gift42/admin/Product/product.twig');
}
/**
* @param TemplateEvent $event
*/
public function onAdminOrderEditTwig(TemplateEvent $event)
{
$event->addSnippet('@Gift42/admin/Order/edit.twig');
}
/**
* @param TemplateEvent $event
*/
public function onProductDetailTwig(TemplateEvent $event)
{
$event->addSnippet('@Gift42/front/Product/detail.twig');
}
/**
* @param TemplateEvent $event
*/
public function onCartIndexTwig(TemplateEvent $event)
{
$event->addSnippet('@Gift42/front/Cart/index.twig');
}
/**
* @param TemplateEvent $event
*/
public function onShoppingIndexTwig(TemplateEvent $event)
{
$event->addSnippet('@Gift42/front/Shopping/index.twig');
}
/**
* @param TemplateEvent $event
*/
public function onShoppingConfirmTwig(TemplateEvent $event)
{
$event->addSnippet('@Gift42/front/Shopping/confirm.twig');
}
/**
* @param TemplateEvent $event
*/
public function onMypageIndexTwig(TemplateEvent $event)
{
$event->addSnippet('@Gift42/front/Mypage/index.twig');
}
/**
* @param TemplateEvent $event
*/
public function onMypageHistoryTwig(TemplateEvent $event)
{
$event->addSnippet('@Gift42/front/Mypage/history.twig');
}
/**
* @param string $const
* @return string $methodName
*/
public static function getMethodName(string $const): string
{
$words = explode('.', $const);
$methodName = null;
foreach ($words as $word) {
$methodName .= ucfirst($word);
}
$methodName = 'on' . $methodName;
return $methodName;
}
/**
* @param EventArgs $event
*/
public function onAdminProductCsvExport(EventArgs $event)
{
$Csv = $event->getArgument('Csv');
$ProductClass = $event->getArgument('ProductClass');
$ExportCsvRow = $event->getArgument('ExportCsvRow');
if ($Csv->getFieldName() == 'gift_kind' && $Csv->getDispName() == trans('admin.gift_kind_name')) {
$Product = $ProductClass->getProduct();
$giftKindName = trans(array_search($Product->getGiftKind(), GiftKind::GIFT_KIND_LIST));
$ExportCsvRow->setData($giftKindName);
}
}
/**
* @param EventArgs $event
*/
public function onAdminOrderCsvExportOrder(EventArgs $event)
{
$Csv = $event->getArgument('Csv');
$OrderItem = $event->getArgument('OrderItem');
$ExportCsvRow = $event->getArgument('ExportCsvRow');
if ($Csv->getFieldName() == 'gift_kind' && $Csv->getDispName() == trans('admin.gift_kind_name') && !$ExportCsvRow->isDataNull()) {
$giftKindName = trans(array_search($OrderItem->getGiftKind(), GiftKind::GIFT_KIND_LIST));
$ExportCsvRow->setData($giftKindName);
}
}
}