app/Plugin/ReceiptPdf2/ReceiptPdfEvent.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Receipt Pdf plugin
  4.  *
  5.  * Copyright (C) 2018 NinePoint Co. LTD. All Rights Reserved.
  6.  *
  7.  * Copyright (C) 2016 LOCKON CO.,LTD. All Rights Reserved.
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\ReceiptPdf2;
  13. use Eccube\Event\TemplateEvent;
  14. use Plugin\ReceiptPdf2\Common\ReceiptPdfCommonData;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Twig\Environment;
  17. use Twig\Loader\ArrayLoader;
  18. class ReceiptPdfEvent implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * リッスンしたいサブスクライバのイベント名の配列を返します。
  22.      * 配列のキーはイベント名、値は以下のどれかをしてします。
  23.      * - 呼び出すメソッド名
  24.      * - 呼び出すメソッド名と優先度の配列
  25.      * - 呼び出すメソッド名と優先度の配列の配列
  26.      * 優先度を省略した場合は0
  27.      *
  28.      * 例:
  29.      * - array('eventName' => 'methodName')
  30.      * - array('eventName' => array('methodName', $priority))
  31.      * - array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  32.      *
  33.      * {@inheritdoc}
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             '@admin/Order/index.twig' => 'onAdminReceiptIndexRender',
  39.             'ReceiptPdf2/Resource/template/admin/receipt_pdf.twig' => 'onAdminReceiptIndexRender',
  40.         ];
  41.     }
  42.     /**
  43.      * Event for new hook point.
  44.      *
  45.      * @param TemplateEvent $event
  46.      */
  47.     public function onAdminReceiptIndexRender(TemplateEvent $event)
  48.     {
  49.         $loader = new ArrayLoader();
  50.         $twig = new Environment($loader);
  51.         $twigSource $event->getSource('@admin/Order/index.twig');
  52.         $part = array();
  53.         $twigSource $this->renderPosition($twigSource$part);
  54.         $event->setSource($twigSource);
  55.     }
  56.     /**
  57.      * Render position.
  58.      *
  59.      * @param string $html
  60.      * @param string $part
  61.      *
  62.      * @return string
  63.      */
  64.     public function renderPosition($html$part)
  65.     {
  66.         $common = new ReceiptPdfCommonData();
  67.         $search1 $common::SEARCH_PLUGIN_URL;
  68.         $search2 $common::SEARCH_BOOTTON_LABEL;
  69.         if(preg_match($search1$html) === 1){
  70.             // new html
  71.             $html preg_replace($search1$common::PLUGIN_URL$html);
  72.         }
  73.         if(preg_match($search2$html) === 1){
  74.             $html preg_replace($search2$common::BUSINESS_FORM_BOOTTON$html);
  75.         }
  76.         return $html;
  77.     }
  78. }