<?php
/**
* Copyright(c) 2019 SYSTEM_KD
* Date: 2019/06/30
*/
namespace Plugin\PointExDx\EventSubscriber;
use Eccube\Entity\Order;
use Eccube\Event\TemplateEvent;
use Plugin\PointExDx\Config\ConfigSetting;
use Plugin\PointExDx\Service\PlgConfigService\ConfigService;
use Plugin\PointExDx\Service\PointExDxHelper;
use Plugin\PointExDx\Service\TwigRenderService\TwigRenderService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AdminOrderEditEventSubscriber implements EventSubscriberInterface
{
/** @var TwigRenderService */
protected $twigRenderService;
/** @var ConfigService */
protected $configService;
/** @var PointExDxHelper */
protected $pointExHelper;
public function __construct(
TwigRenderService $twigRenderService,
ConfigService $configService,
PointExDxHelper $pointExHelper
)
{
$this->twigRenderService = $twigRenderService;
$this->configService = $configService;
$this->pointExHelper = $pointExHelper;
}
/**
* 受注編集
*
* @param TemplateEvent $event
*/
public function onTemplateAdminOrderEdit(TemplateEvent $event)
{
// 設定情報取得
$rateUse = $this->configService->getKeyInteger(ConfigSetting::KEY_RATE_USE);
$event->setParameter('rate_use', $rateUse);
// 基準ポイント付与率
/** @var Order $order */
$order = $event->getParameter('Order');
$pointRate = 0;
foreach ($order->getOrderItems() as $orderItem) {
if($orderItem->isProduct()) {
$itemPointRate = $orderItem->getPointRate();
if(!empty($itemPointRate) && $itemPointRate > 0) {
$pointRate = $itemPointRate;
break;
}
}
}
$event->setParameter('base_point_rate', $pointRate);
$this->twigRenderService->initRenderService($event);
$insertPosition = 5;
// タイトル ボーナスポイント
$this->twigRenderService
->insertBuilder()
->find('#table-form-field')
->find('thead > tr > th')
->eq($insertPosition)
->setTargetId("point_ex_bonus_title")
->setInsertModeAfter();
// タイトル ポイント付与
$this->twigRenderService
->insertBuilder()
->find('#table-form-field')
->find('thead > tr > th')
->eq($insertPosition + 1)
->setTargetId("point_ex_nopointflg_title")
->setInsertModeAfter();
// ポイント付与対象
$child = $this->twigRenderService
->eachChildBuilder()
->findThis()
->find('td')
->eq($insertPosition)
->targetFindAndIndexKey('#point_ex_nopointflg_', 'pointExIndex2')
->setInsertModeAfter();
$this->twigRenderService
->eachBuilder()
->find('#table-form-field')
->find('tbody > tr')
->setEachIndexKey('pointExIndex2')
->each($child->build());
// ボーナスポイント
$child = $this->twigRenderService
->eachChildBuilder()
->findThis()
->find('td')
->eq($insertPosition)
->targetFindAndIndexKey('#point_ex_bonus_', 'pointExIndex')
->setInsertModeAfter();
$this->twigRenderService
->eachBuilder()
->find('#table-form-field')
->find('tbody > tr')
->setEachIndexKey('pointExIndex')
->each($child->build());
// 購入時点のポイント付与率
$this->twigRenderService
->insertBuilder()
->find('.row.justify-content-end.mb-3')
->eq(4)
->setTargetId('point_ex_point_rate')
->setInsertModeAfter();
$this->twigRenderService->addSupportSnippet(
'@PointExDx/admin/Order/point_ex_order_edit.twig'
);
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
'@admin/Order/edit.twig' => 'onTemplateAdminOrderEdit',
];
}
}