app/Plugin/ApgRichEditor/Event.php line 62

Open in your IDE?
  1. <?php
  2. namespace Plugin\ApgRichEditor;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Eccube\Entity\News;
  5. use Eccube\Entity\Product;
  6. use Eccube\Event\EccubeEvents;
  7. use Eccube\Event\EventArgs;
  8. use Eccube\Event\TemplateEvent;
  9. use Plugin\ApgRichEditor\Domain\RichEditorType;
  10. use Plugin\ApgRichEditor\Entity\Config;
  11. use Plugin\ApgRichEditor\Entity\RichEditorContent;
  12. use Plugin\ApgRichEditor\Repository\ConfigRepository;
  13. use Plugin\ApgRichEditor\Repository\RichEditorContentRepository;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Form\FormInterface;
  16. class Event implements EventSubscriberInterface
  17. {
  18.     const TEMPLATE_NAMESPACE '@ApgRichEditor';
  19.     /**
  20.      * @var ConfigRepository
  21.      */
  22.     protected $Config;
  23.     protected $em;
  24.     protected $twig;
  25.     public function __construct(
  26.         ConfigRepository $configRepository,
  27.         EntityManagerInterface $em,
  28.         \Twig_Environment $twig
  29.     )
  30.     {
  31.         $this->Config $configRepository;
  32.         $this->em $em;
  33.         $this->twig $twig;
  34.     }
  35.     /**
  36.      * @return array
  37.      */
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             '@admin/Product/product.twig' => 'onAdminProductRender',
  42.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'onAdminProductEditComplete',
  43.             '@admin/Content/news_edit.twig' => 'onAdminNewsRender',
  44.             EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_COMPLETE => 'onAdminNewsEditComplete',
  45.         ];
  46.     }
  47.     /**
  48.      * @param TemplateEvent $event
  49.      * @throws \Twig_Error_Loader
  50.      */
  51.     public function onAdminProductRender(TemplateEvent $event)
  52.     {
  53.         $source $event->getSource();
  54.         // data
  55.         $parameters $event->getParameters();
  56.         // setting
  57.         $ApgRichEditorConfig $this->Config->getOrNew();
  58.         $parameters['ApgRichEditorConfig'] = $ApgRichEditorConfig;
  59.         // help
  60.         if ($ApgRichEditorConfig->isEnableProductMarkdown()) {
  61.             $pattern '|<div id="ex-conversion-action"|s';
  62.             $addRow $this->twig->getLoader()->getSourceContext(self::TEMPLATE_NAMESPACE '/admin/markdown_help.twig')->getCode();
  63.             if (preg_match($pattern$source$matchesPREG_OFFSET_CAPTURE)) {
  64.                 $replacement $addRow $matches[0][0];
  65.                 $source preg_replace($pattern$replacement$source);
  66.             }
  67.         }
  68.         $event->setSource($source);
  69.         $event->setParameters($parameters);
  70.         $event->addAsset(self::TEMPLATE_NAMESPACE '/admin/rich_editor_product_css.twig');
  71.         $event->addSnippet(self::TEMPLATE_NAMESPACE '/admin/rich_editor_product_js.twig');
  72.     }
  73.     /**
  74.      * @param Event $event
  75.      */
  76.     public function onAdminProductEditComplete(EventArgs $event)
  77.     {
  78.         /** @var Product $Product */
  79.         $Product $event->getArgument('Product');
  80.         /** @var FormInterface $form */
  81.         $form $event->getArgument('form');
  82.         /** @var Config $RichEditorConfig */
  83.         $RichEditorConfig $this->Config->getOrNew();
  84.         // Product - description detail
  85.         if ($RichEditorConfig->getProductDescriptionDetail() === RichEditorType::MARKDOWN) {
  86.             $convertedContent $Product->getPlgMarkdownDescriptionDetail();
  87.             $markdownContent $Product->getDescriptionDetail();
  88.             $Product->setDescriptionDetail($convertedContent);
  89.             $Product->setPlgMarkdownDescriptionDetail($markdownContent);
  90.         }
  91.         if ($RichEditorConfig->getProductFreeArea() === RichEditorType::MARKDOWN) {
  92.             $convertedContent $Product->getPlgMarkdownFreeArea();
  93.             $markdownContent $Product->getFreeArea();
  94.             $Product->setFreeArea($convertedContent);
  95.             $Product->setPlgMarkdownFreeArea($markdownContent);
  96.         }
  97.         $this->em->persist($Product);
  98.         $this->em->flush();
  99.     }
  100.     /**
  101.      * @param TemplateEvent $event
  102.      * @throws \Twig_Error_Loader
  103.      */
  104.     public function onAdminNewsRender(TemplateEvent $event)
  105.     {
  106.         $source $event->getSource();
  107.         // data
  108.         $parameters $event->getParameters();
  109.         // setting
  110.         $ApgRichEditorConfig $this->Config->getOrNew();
  111.         $parameters['ApgRichEditorConfig'] = $ApgRichEditorConfig;
  112.         // help
  113.         if ($ApgRichEditorConfig->isEnableNewsMarkdown()) {
  114.             $pattern '|<div id="ex-conversion-action"|s';
  115.             $addRow $this->twig->getLoader()->getSourceContext(self::TEMPLATE_NAMESPACE '/admin/markdown_help.twig')->getCode();
  116.             if (preg_match($pattern$source$matchesPREG_OFFSET_CAPTURE)) {
  117.                 $replacement $addRow $matches[0][0];
  118.                 $source preg_replace($pattern$replacement$source);
  119.             }
  120.         }
  121.         $event->setSource($source);
  122.         $event->setParameters($parameters);
  123.         $event->addAsset(self::TEMPLATE_NAMESPACE '/admin/rich_editor_news_css.twig');
  124.         $event->addSnippet(self::TEMPLATE_NAMESPACE '/admin/rich_editor_news_js.twig');
  125.     }
  126.     /**
  127.      * @param Event $event
  128.      */
  129.     public function onAdminNewsEditComplete(EventArgs $event)
  130.     {
  131.         /** @var News $News */
  132.         $News $event->getArgument('News');
  133.         /** @var Config $RichEditorConfig */
  134.         $RichEditorConfig $this->Config->getOrNew();
  135.         // Product - description detail
  136.         if ($RichEditorConfig->getNewsDescription() === RichEditorType::MARKDOWN) {
  137.             $convertedContent $News->getPlgMarkdownDescription();
  138.             $markdownContent $News->getDescription();
  139.             $News->setDescription($convertedContent);
  140.             $News->setPlgMarkdownDescription($markdownContent);
  141.         }
  142.         $this->em->persist($News);
  143.         $this->em->flush();
  144.     }
  145. }