app/Plugin/ReceiptPdf2/ReceiptPdfEvent.php line 53

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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Twig\Environment;
  16. use Twig\Loader\ArrayLoader;
  17. use Plugin\ReceiptPdf2\Common\ReceiptPdfCommonData;
  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.             '@admin/Order/edit.twig' => 'onAdminReceiptEditRender',
  41.             'ReceiptPdf2/Resource/template/admin/receipt_pdf.twig' => 'onAdminReceiptEditRender',
  42.         ];
  43.     }
  44.     /**
  45.      * Event for new hook point.
  46.      *
  47.      * @param TemplateEvent $event
  48.      */
  49.     public function onAdminReceiptIndexRender(TemplateEvent $event)
  50.     {
  51.         $loader = new ArrayLoader();
  52.         $twig = new Environment($loader);
  53.         $twigSource $event->getSource('@admin/Order/index.twig');
  54.         $part = array();
  55.         $twigSource $this->renderPosition($twigSource$part);
  56.         $event->setSource($twigSource);
  57.     }
  58.     public function onAdminReceiptEditRender(TemplateEvent $event)
  59.     {
  60.         $loader = new ArrayLoader();
  61.         $twig = new Environment($loader);
  62.         $twigSource $event->getSource('@admin/Order/edit.twig');
  63.         $part = array();
  64.         $twigSource $this->renderPosition($twigSource$part);
  65.         $event->setSource($twigSource);
  66.     }
  67.     /**
  68.      * Render position.
  69.      *
  70.      * @param string $html
  71.      * @param string $part
  72.      *
  73.      * @return string
  74.      */
  75.     public function renderPosition($html$part)
  76.     {
  77.         $common = new ReceiptPdfCommonData();
  78.         $search1 $common::SEARCH_PLUGIN_URL;
  79.         $search2 $common::SEARCH_BOOTTON_LABEL;
  80.         $search3 $common::SEARCH_BOOTTON_LABEL2;
  81.         if(preg_match($search1$html) === 1){
  82.             // new html
  83.             $html preg_replace($search1$common::PLUGIN_URL$html);
  84.         }
  85.         if(preg_match($search2$html) === 1){
  86.             $html preg_replace($search2$common::BUSINESS_FORM_BOOTTON$html);
  87.         }
  88.         if(preg_match($search3$html) === 1){
  89.             $html preg_replace($search3$common::BUSINESS_FORM_BOOTTON$html);
  90.         }
  91.         return $html;
  92.     }
  93. }