<?php
/*
* This file is part of Receipt Pdf plugin
*
* Copyright (C) 2018 NinePoint Co. LTD. All Rights Reserved.
*
* Copyright (C) 2016 LOCKON CO.,LTD. All Rights Reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\ReceiptPdf2;
use Eccube\Event\TemplateEvent;
use Plugin\ReceiptPdf2\Common\ReceiptPdfCommonData;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
class ReceiptPdfEvent implements EventSubscriberInterface
{
/**
* リッスンしたいサブスクライバのイベント名の配列を返します。
* 配列のキーはイベント名、値は以下のどれかをしてします。
* - 呼び出すメソッド名
* - 呼び出すメソッド名と優先度の配列
* - 呼び出すメソッド名と優先度の配列の配列
* 優先度を省略した場合は0
*
* 例:
* - array('eventName' => 'methodName')
* - array('eventName' => array('methodName', $priority))
* - array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
'@admin/Order/index.twig' => 'onAdminReceiptIndexRender',
'ReceiptPdf2/Resource/template/admin/receipt_pdf.twig' => 'onAdminReceiptIndexRender',
];
}
/**
* Event for new hook point.
*
* @param TemplateEvent $event
*/
public function onAdminReceiptIndexRender(TemplateEvent $event)
{
$loader = new ArrayLoader();
$twig = new Environment($loader);
$twigSource = $event->getSource('@admin/Order/index.twig');
$part = array();
$twigSource = $this->renderPosition($twigSource, $part);
$event->setSource($twigSource);
}
/**
* Render position.
*
* @param string $html
* @param string $part
*
* @return string
*/
public function renderPosition($html, $part)
{
$common = new ReceiptPdfCommonData();
$search1 = $common::SEARCH_PLUGIN_URL;
$search2 = $common::SEARCH_BOOTTON_LABEL;
if(preg_match($search1, $html) === 1){
// new html
$html = preg_replace($search1, $common::PLUGIN_URL, $html);
}
if(preg_match($search2, $html) === 1){
$html = preg_replace($search2, $common::BUSINESS_FORM_BOOTTON, $html);
}
return $html;
}
}