app/Plugin/UKOMI42/Event.php line 87

Open in your IDE?
  1. <?php
  2. namespace Plugin\UKOMI42;
  3. use Eccube\Entity\Master\OrderStatus;
  4. use Eccube\Entity\Order;
  5. use Eccube\Entity\Product;
  6. use Eccube\Event\EccubeEvents;
  7. use Eccube\Event\EventArgs;
  8. use Eccube\Event\TemplateEvent;
  9. use Eccube\Repository\Master\OrderStatusRepository;
  10. use Eccube\Repository\OrderRepository;
  11. use Eccube\Repository\ProductRepository;
  12. use Eccube\Request\Context;
  13. use Plugin\UKOMI42\Repository\ConfigRepository;
  14. use Plugin\UKOMI42\Service\UkomiService;
  15. use Symfony\Component\DomCrawler\Crawler;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Workflow\Event\Event as WorkflowEvent;
  20. class Event implements EventSubscriberInterface
  21. {
  22.     private $context;
  23.     /**
  24.      * @var ConfigRepository
  25.      */
  26.     private $configRepository;
  27.     /**
  28.      * @var ProductRepository
  29.      */
  30.     private $productRepository;
  31.     /**
  32.      * @var OrderRepository
  33.      */
  34.     private $orderRepository;
  35.     /**
  36.      * @var UkomiService
  37.      */
  38.     private $ukomiService;
  39.     public function __construct(
  40.         Context $context,
  41.         ConfigRepository $configRepository,
  42.         OrderRepository $orderRepository,
  43.         ProductRepository $productRepository,
  44.         UkomiService $ukomiService
  45.     )
  46.     {
  47.         $this->context $context;
  48.         $this->configRepository $configRepository;
  49.         $this->productRepository $productRepository;
  50.         $this->orderRepository $orderRepository;
  51.         $this->ukomiService $ukomiService;
  52.     }
  53.     /**
  54.      * @return array
  55.      */
  56.     public static function getSubscribedEvents()
  57.     {
  58.         return [
  59.             KernelEvents::RESPONSE => ['onRenderFrame'1],
  60.             'Shopping/complete.twig' => ['onRenderShoppingComplete'1],
  61.             EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE => ['onAdminOrderEditIndexComplete'1],
  62.             'workflow.order.completed' => ['onOrderCompleted']
  63.         ];
  64.     }
  65.     protected function isInitialized()
  66.     {
  67.         return $this->configRepository->getConfig('api_init'0) != false true;
  68.     }
  69.     protected function isActive()
  70.     {
  71.         if ($this->configRepository->getConfig('api_init'0) != || $this->configRepository->getConfig('no_auto'0) == 1) {
  72.             return false;
  73.         }
  74.         return true;
  75.     }
  76.     public function onRenderFrame(ResponseEvent $event)
  77.     {
  78.         $path $event->getRequest()->getPathInfo();
  79.         if (!$this->context->isFront()) {
  80.             return;
  81.         }
  82.         if (!$this->isActive()) {
  83.             return;
  84.         }
  85.         $response $event->getResponse();
  86.         $tags $this->ukomiService->tags();
  87.         $html $response->getContent();
  88.         if (preg_match('#^/products/list#'$path)) {
  89.             $html $this->doProductList($tags$html);
  90.         }
  91.         if (preg_match('#^/products/detail/(\d+)+$#'$path$m)) {
  92.             $html $this->doProductDetail($m[1], $tags$html);
  93.         }
  94.         $response->setContent(preg_replace('#</\s*head>#i'"{$tags['head']}\n</head>"$html));
  95.     }
  96.     protected function doProductList($tags$html)
  97.     {
  98.         $crawler = new Crawler($html);
  99.         $html $crawler->html();
  100.         $productRepo $this->productRepository;
  101.         $updated false;
  102.         $crawler->filter('.ec-shelfGrid__item a')->each(function (Crawler $crawler) use (&$html, &$updated$tags$productRepo) {
  103.             /** @var \DOMDocument $document */
  104.             $id_text trim($crawler->getNode(0)->getAttribute('href'));
  105.             if (preg_match('#detail/(\d+)$#'$id_text$m)) {
  106.                 $id $m[1];
  107.             } else {
  108.                 $id $id_text;
  109.             }
  110.             /** @var Product $product */
  111.             $product $productRepo->find($id);
  112.             //Replace value for PRODUCT_GROUP
  113.             $tags['star_group']=str_replace('{PRODUCT_GROUP}'$product->getId(), $tags['star_group']);
  114.             $old $crawler->html();
  115.             $new $old str_replace('{PRODUCT_ID}'$product $product->getCodeMin() : $id$tags['star_group']);
  116.             $html str_replace($old$new$html);
  117.             $updated true;
  118.         });
  119.         if ($updated) {
  120.             $html html_entity_decode($html);
  121.         }
  122.         return '<!doctype html><html lang="ja">' $html "</html>";
  123.     }
  124.     protected function doProductDetail($product_id$tags$html)
  125.     {
  126.         $product $this->productRepository->find($product_id);
  127.         if (empty($product)) return $html;
  128.         $updated false;
  129.         //Replace value for PRODUCT_GROUP
  130.         $tags['star_group']=str_replace('{PRODUCT_GROUP}'$product->getId(), $tags['star_group']);
  131.         $tags['review_group']=str_replace('{PRODUCT_GROUP}'$product->getId(), $tags['review_group']);
  132.         $snippet str_replace('{PRODUCT_ID}'$product->getCodeMin(), $tags['star_group']);
  133.         $crawler = new Crawler($html);
  134.         $html $crawler->html();
  135.         if (preg_match('/<ul\s+class=\"ec-productRole__tags">/'$html)) {
  136.             $html preg_replace('/<ul\s+class=\"ec-productRole__tags">/'$snippet "\n\$0"$html);
  137.             $updated true;
  138.         }
  139.         $crawler = new Crawler($html);
  140.         $html $crawler->html();
  141.         $detail $crawler->filter('.ec-productRole')->first();
  142.         if ($detail->count() > 0) {
  143.             $old $detail->html();
  144.             $new $old str_replace('{PRODUCT_ID}'$product->getCodeMin(), $tags['review_group']);
  145.             $html str_replace($old$new$html);
  146.             $updated true;
  147.         }
  148.         if ($updated) {
  149.             return '<!doctype html><html lang="ja">' html_entity_decode($html) . '</html>';
  150.         } else {
  151.             return $html;
  152.         }
  153.     }
  154.     public function onRenderShoppingComplete(TemplateEvent $event)
  155.     {
  156.         if (!$this->context->isFront()) {
  157.             return;
  158.         }
  159.         if (!$this->isActive()) {
  160.             return;
  161.         }
  162.         $tags $this->ukomiService->tags();
  163.         /** @var Order $order */
  164.         $order $event->getParameter('Order');
  165.         $event->addSnippet(
  166.             str_replace(
  167.             ['######ORDER_ID######''######ORDER_AMOUNT######''######ORDER_CURRENCY######'],
  168.             [$order->getId(), $order->getTotal(), 'JPY'], $tags['tracking']), false);
  169.     }
  170.     public function onAdminOrderEditIndexComplete(EventArgs $event)
  171.     {
  172.         if (!$this->isActive()) {
  173.             return;
  174.         }
  175.         /** @var Order $origin_order */
  176.         $origin_order $event->getArgument('OriginOrder');
  177.         /** @var Order $target_order */
  178.         $target_order $event->getArgument('TargetOrder');
  179.         //BOC if new order then clone target order to original order
  180.         if(empty($origin_order->getId())){
  181.             $origin_order = clone $target_order;
  182.         }
  183.         //EOC if new order then clone target order to original order
  184.         if ($origin_order->getOrderStatus()->getId() == $target_order->getOrderStatus()->getId()) {
  185.             return;
  186.         }
  187.         $detect_order_status $this->configRepository->getConfig('order_status');
  188.         if ($target_order->getOrderStatus()->getId() == OrderStatus::CANCEL) {
  189.             $this->ukomiService->cancelOrders([$target_order]);
  190.         } else if (!empty($detect_order_status) && $target_order->getOrderStatus()->getId() == $detect_order_status) {
  191.             $this->ukomiService->createOrders([$target_order],$detect_order_status);
  192.         }
  193.     }
  194.     public function onOrderCompleted(WorkflowEvent $event) {
  195.         $context $event->getSubject();
  196.         $Order $context->getOrder();
  197.         $order_status $Order->getOrderStatus()->getId();
  198.         $detect_order_status $this->configRepository->getConfig('order_status');
  199.         if ($order_status == $detect_order_status) {
  200.             $this->ukomiService->createOrders([$Order],$detect_order_status);
  201.         }
  202.     }
  203. }