app/Customize/EventListener/KarteEventListener.php line 197

Open in your IDE?
  1. <?php
  2. namespace Customize\EventListener;
  3. use Customize\Event\LmEvents;
  4. use Customize\Service\AutoLoadService;
  5. use Customize\Service\Karte\KarteService;
  6. use Eccube\Entity\Customer;
  7. use Eccube\Entity\Order;
  8. use Eccube\Event\EccubeEvents;
  9. use Eccube\Event\EventArgs;
  10. use Eccube\Event\TemplateEvent;
  11. use Customize\Service\CartService;
  12. use Lm\Engine\EC\Entity\GoodsWithRelated;
  13. use Lm\Entity\OrderHeader;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  19. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  22. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  23. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  24. use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
  25. use Twig\Environment;
  26. class KarteEventListener implements EventSubscriberInterfaceLogoutHandlerInterface
  27. {
  28.     const SESSION_FRONT_SIGNUP 'karte.front.signup';
  29.     const SESSION_FRONT_LOGIN 'karte.front.login';
  30.     const SESSION_FRONT_LOGOUT 'karte.front.logout';
  31.     const SESSION_FRONT_FAVORITE_ADD 'karte.front.favorite.add';
  32.     const SESSION_FRONT_FAVORITE_DELETE 'karte.front.favorite.delete';
  33.     /**
  34.      * @var RequestStack
  35.      */
  36.     private $requestStack;
  37.     /**
  38.      * @var SessionInterface
  39.      */
  40.     private $session;
  41.     /**
  42.      * @var TokenStorageInterface
  43.      */
  44.     private $tokenStorage;
  45.     private $karteService;
  46.     private $cartService;
  47.     /**
  48.      * @var Environment
  49.      */
  50.     private $twig;
  51.     /**
  52.      * @var AutoLoadService
  53.      */
  54.     private $autoLoadService;
  55.     public function __construct(RequestStack          $requestStack,
  56.                                 SessionInterface      $session,
  57.                                 KarteService          $karteService,
  58.                                 CartService           $cartService,
  59.                                 TokenStorageInterface $tokenStorage,
  60.                                 Environment           $twig,
  61.                                 AutoLoadService       $autoLoadService)
  62.     {
  63.         //
  64.         $this->requestStack $requestStack;
  65.         $this->session $session;
  66.         $this->tokenStorage $tokenStorage;
  67.         $this->karteService $karteService;
  68.         $this->cartService $cartService;
  69.         $this->twig $twig;
  70.         $this->autoLoadService $autoLoadService;
  71.     }
  72.     public function onKernelResponse(ResponseEvent $event)
  73.     {
  74.         //
  75.         $response $event->getResponse();
  76.         $request $event->getRequest();
  77.         //
  78.         if (!$event->isMasterRequest()) {
  79.             //
  80.             return;
  81.         }
  82.         //
  83.         if ($request->isXmlHttpRequest()) {
  84.             return;
  85.         }
  86.         //
  87.         if ($this->autoLoadService->GetNarikawariSession(false)) {
  88.             return;
  89.         }
  90.         //
  91.         $this->injectIndex($response);
  92.     }
  93.     protected function injectIndex(Response $response)
  94.     {
  95.         //
  96.         $content $response->getContent();
  97.         //
  98.         $content preg_replace_callback("/<head[^>]*?>/", function ($matches) {
  99.             //
  100.             return "{$matches[0]}\n\n{$this->twig->render('Karte/index.twig')}";
  101.         }, $content);
  102.         //
  103.         $response->setContent($content);
  104.     }
  105.     /**
  106.      * @inheritDoc
  107.      */
  108.     public static function getSubscribedEvents()
  109.     {
  110.         return [
  111.             //
  112.             KernelEvents::RESPONSE => 'onKernelResponse',
  113.             //
  114.             'Entry/activate.twig' => 'onEntryActivate',
  115.             'Shopping/index.twig' => 'onEntryActivate',
  116.             'Mypage/index.twig' => 'onEntryActivate',
  117.             'Top/index.twig' => 'onEntryActivate',
  118.             'security.interactive_login' => 'onInteractiveLogin',
  119.             //'Page/sub-category.twig' => 'onProductList',
  120.             'Item/detail.twig' => 'onProductDetail',
  121.             'Mypage/favorite.twig' => 'onFavoriteUpdate',
  122.             'Cart/index.twig' => 'onTrackInformationOfCart'// for event Cart
  123.             'Shopping/complete.twig' => 'onPurchaseCompleted'// for event Purchase
  124.             'Estimate/index.twig' => 'onQuotation'// for event Quotation simulation
  125.             'Mypage/update.twig' => 'onMyPageUpdateInformation'// for event User attribute information
  126.             'Mypage/withdraw_complete.twig' => 'onMyPageUpdateInformation'// for event User attribute information
  127.             'Mitsumori/index.twig' => 'onQuotation'// for event create Quotation
  128.             'Contact/complete.twig' => 'onEntryActivate'// for event email inquiry
  129.             EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE => 'onEccubeFrontEntryIndexComplete',
  130.             LmEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE => 'onLmFrontFavoriteAddComplete',
  131.             LmEvents::FRONT_PRODUCT_FAVORITE_DELETE_COMPLETE => 'onLmFrontFavoriteDeleteComplete',
  132.             LmEvents::SESSION_FRONT_PRODUCT_USER_CART_STEP2_REGISTER_COMPLETED => 'onCartStep2RegisterCompleted',
  133.             LmEvents::SESSION_FRONT_PRODUCT_CART_ADD_PRODUCT => 'onAddProduct',
  134.             LmEvents::SESSION_FRONT_PRODUCT_CART_DELETE_PRODUCT => 'onDeleteProduct',
  135.             LmEvents::SESSION_FRONT_SEND_QUOTATION_BY_EMAIL => 'onSendQuotationByEmail',
  136.             LmEvents::SESSION_FRONT_SEND_QUOTATION_BY_FAX => 'onSendQuotationByFax',
  137.         ];
  138.     }
  139.     public function onEntryActivate(TemplateEvent $event)
  140.     {
  141.         $parameter $event->hasParameter('Karte') ? $event->getParameter('Karte') : [];
  142.         /**
  143.          * @var Customer $customer
  144.          */
  145.         if ($token $this->tokenStorage->getToken()) {
  146.             if (($customer $token->getUser())
  147.                 && ($customer instanceof Customer)
  148.                 && ($lmCustomer $customer->getLmCustomer())) {
  149.                 $parameter += [
  150.                     'customer' => $lmCustomer,
  151.                 ];
  152.             }
  153.         }
  154.         if ($this->session->remove(self::SESSION_FRONT_SIGNUP)) {
  155.             $event->addSnippet('Karte/signup.twig');
  156.             if ($lmCustomer) {
  157.                 $event->addSnippet('Karte/identify.twig');
  158.             }
  159.         }
  160.         if ($this->session->remove(self::SESSION_FRONT_LOGIN)) {
  161.             $parameter += [
  162.                 'login' => [
  163.                     'status' => true,
  164.                 ],
  165.             ];
  166.             $event->addSnippet('Karte/login.twig');
  167.             if ($lmCustomer) {
  168.                 $event->addSnippet('Karte/identify.twig');
  169.             }
  170.         }
  171.         if ($this->session->remove(self::SESSION_FRONT_LOGOUT)) {
  172.             $parameter += [
  173.                 'login' => [
  174.                     'status' => false,
  175.                 ],
  176.             ];
  177.             $event->addSnippet('Karte/login.twig');
  178.         }
  179.         if ($this->session->remove(LmEvents::SESSION_FRONT_PRODUCT_USER_CART_STEP2_REGISTER_COMPLETED)) {
  180.             $cart $this->cartService->getCart();
  181.             if (!empty($cart)) {
  182.                 $customer $cart->getCustomer();
  183.                 $parameter += $this->karteService->getAttributeForPersonalInfomation($customer);
  184.                 $event->addSnippet('Karte/attribute.twig');
  185.             }
  186.         }
  187.         if ($email $this->session->remove(LmEvents::SESSION_FRONT_MYPAGE_EMAIL_INQUIRY)) {
  188.             $parameter += [
  189.                 'email_inquiry' => [
  190.                     'email' => $email
  191.                 ]
  192.             ];
  193.             $event->addSnippet('Karte/email_inquiry.twig');
  194.         }
  195.         if (!empty($parameter)) {
  196.             $event->setParameter('Karte'$parameter);
  197.         }
  198.     }
  199.     public function onEccubeFrontEntryIndexComplete(EventArgs $event)
  200.     {
  201.         $this->session->set(self::SESSION_FRONT_SIGNUPtrue);
  202.     }
  203.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  204.     {
  205.         $this->session->set(self::SESSION_FRONT_LOGINtrue);
  206.         $this->session->set(LmEvents::SESSION_FRONT_PRODUCT_CART_STEP2_LOGIN_COMPLETEDtrue);
  207.     }
  208.     /**
  209.      * @inheritDoc
  210.      */
  211.     public function logout(Request $requestResponse $responseTokenInterface $token)
  212.     {
  213.         //
  214.         $this->session->set(self::SESSION_FRONT_LOGOUTtrue);
  215.     }
  216.     public function onProductList(TemplateEvent $event)
  217.     {
  218.         //
  219.         $karte $event->hasParameter('Karte') ? $event->getParameter('Karte') : [];
  220.         //
  221.         if ($request $this->requestStack->getCurrentRequest()) {
  222.             //
  223.             $karte['list_url'] = $request->getPathInfo();
  224.         }
  225.         //
  226.         if (!empty($karte)) {
  227.             //
  228.             $event->setParameter('Karte'$karte);
  229.         }
  230.         //
  231.         $event->addSnippet('Karte/view_list.twig');
  232.     }
  233.     public function onProductDetail(TemplateEvent $event)
  234.     {
  235.         //
  236.         $karte $event->hasParameter('Karte') ? $event->getParameter('Karte') : [];
  237.         //
  238.         if ($request $this->requestStack->getCurrentRequest()) {
  239.             //
  240.             $karte['item_url'] = $request->getPathInfo();
  241.         }
  242.         //
  243.         if (!empty($karte)) {
  244.             //
  245.             $event->setParameter('Karte'$karte);
  246.         }
  247.         //
  248.         $event->addSnippet('Karte/view_item.twig');
  249.     }
  250.     public function onFavoriteUpdate(TemplateEvent $event)
  251.     {
  252.         //
  253.         $karte $event->hasParameter('Karte') ? $event->getParameter('Karte') : [];
  254.         //
  255.         if (!isset($karte['favorite'])) {
  256.             //
  257.             $karte['favorite'] = [];
  258.         }
  259.         //
  260.         if ($goodsList $event->getParameter('data')) {
  261.             //
  262.             $karte['favorite']['items'] = array_map(function ($goods) {
  263.                 //
  264.                 if ($_goods = new GoodsWithRelated($goods['goods_id'])) {
  265.                     //
  266.                     return [
  267.                         'item_id' => $_goods->getGoodsId(),
  268.                         'name' => $_goods->getGoodsName(),
  269.                         'item_url' => "/item/{$_goods->getItemId()}.html"// TODO:
  270.                         "l_category_name" => $_goods->getMainCategory()->getMainCategoryName(),
  271.                         "s_category_name" => $_goods->getMainCategory()->getCategoryName(),
  272.                         "l_category_cd" => $_goods->getMainCategory()->getMainCategoryWebname(),
  273.                         "s_category_cd" => $_goods->getMainCategory()->getCategoryWebname(),
  274.                     ];
  275.                 }
  276.             }, $goodsList);
  277.             //
  278.             $karte['favorite']['status'] = !empty($karte['favorite']['items']);
  279.             $karte['favorite']['quantity'] = count($karte['favorite']['items']);
  280.         }
  281.         //
  282.         if ($added $this->session->remove(self::SESSION_FRONT_FAVORITE_ADD)) {
  283.             //
  284.             $karte['favorite']['added_item_id'] = $added['goods_id'];
  285.         }
  286.         //
  287.         if ($deleted $this->session->remove(self::SESSION_FRONT_FAVORITE_DELETE)) {
  288.             //
  289.             $karte['favorite']['deleted_item_id'] = $deleted['goods_id'];
  290.         }
  291.         //
  292.         if (!empty($karte)) {
  293.             //
  294.             $event->setParameter('Karte'$karte);
  295.         }
  296.         //
  297.         $event->addSnippet('Karte/favorite.twig');
  298.     }
  299.     public function onLmFrontFavoriteAddComplete(EventArgs $event)
  300.     {
  301.         $this->session->set(self::SESSION_FRONT_FAVORITE_ADD$event->getArguments());
  302.     }
  303.     public function onLmFrontFavoriteDeleteComplete(EventArgs $event)
  304.     {
  305.         $this->session->set(self::SESSION_FRONT_FAVORITE_DELETE$event->getArguments());
  306.     }
  307.     public function onAddProduct(EventArgs $event)
  308.     {
  309.         $this->session->set(LmEvents::SESSION_FRONT_PRODUCT_CART_ADD_PRODUCT$event->getArguments());
  310.     }
  311.     public function onDeleteProduct(EventArgs $event)
  312.     {
  313.         $this->session->set(LmEvents::SESSION_FRONT_PRODUCT_CART_DELETE_PRODUCT$event->getArguments());
  314.     }
  315.     public function onSendQuotationByEmail(EventArgs $event)
  316.     {
  317.         $this->session->set(LmEvents::SESSION_FRONT_SEND_QUOTATION_BY_EMAIL$event->getArguments());
  318.     }
  319.     public function onSendQuotationByFax(EventArgs $event)
  320.     {
  321.         $this->session->set(LmEvents::SESSION_FRONT_SEND_QUOTATION_BY_FAX$event->getArguments());
  322.     }
  323.     public function onTrackInformationOfCart(TemplateEvent $event)
  324.     {
  325.         $karte $event->hasParameter('Karte') ? $event->getParameter('Karte') : [];
  326.         $cart $this->cartService->getCart();
  327.         if ($this->session->remove(LmEvents::SESSION_FRONT_QUOTATION_SIMULATION)) {
  328.             if (!empty($cart)) {
  329.                 $karte['quotation_simulation'] = $this->karteService->getAttributesForCart($cart);
  330.                 $event->addSnippet('Karte/quotation_simulation.twig');
  331.             }
  332.         }
  333.         if ($this->session->remove(LmEvents::SESSION_FRONT_PRODUCT_CART)) {
  334.             if (!empty($cart)) {
  335.                 $data $this->karteService->getAttributesForCart($cart);
  336.                 if (!empty($data)) {
  337.                     $karte['cart'] = $data;
  338.                     $event->addSnippet('Karte/cart.twig');
  339.                 }
  340.             }
  341.         }
  342.         if (!empty($karte)) {
  343.             $event->setParameter('Karte'$karte);
  344.         }
  345.     }
  346.     public function onQuotation(TemplateEvent $event) {
  347.         $karte $event->hasParameter('Karte') ? $event->getParameter('Karte') : [];
  348.         if ($this->session->remove(LmEvents::SESSION_FRONT_QUOTATION_CREATE)) {
  349.             $cart $this->cartService->getCart();
  350.             if (!empty($cart)) {
  351.                 $karte['quotation_create'] = $this->karteService->getAttributesForCart($cart);
  352.                 $event->addSnippet('Karte/quotation_create.twig');
  353.             }
  354.         }
  355.         if ($dataQuotationSendByEmail $this->session->remove(LmEvents::SESSION_FRONT_SEND_QUOTATION_BY_EMAIL)) {
  356.             if (!empty($dataQuotationSendByEmail)) {
  357.                 $karte['quotation_by_email'] = $dataQuotationSendByEmail;
  358.                 $event->addSnippet('Karte/quotation_by_email.twig');
  359.             }
  360.         }
  361.         if ($dataQuotationSendByFax $this->session->remove(LmEvents::SESSION_FRONT_SEND_QUOTATION_BY_FAX)) {
  362.             if (!empty($dataQuotationSendByFax)) {
  363.                 $karte['quotation_by_fax'] = $dataQuotationSendByFax;
  364.                 $event->addSnippet('Karte/quotation_by_fax.twig');
  365.             }
  366.         }
  367.         if (!empty($karte)) {
  368.             $event->setParameter('Karte'$karte);
  369.         }
  370.     }
  371.     public function onPurchaseCompleted(TemplateEvent $event)
  372.     {
  373.         /**
  374.          * @var Order $order
  375.          * @var OrderHeader $orderHeader
  376.          */
  377.         $karte $event->hasParameter('Karte') ? $event->getParameter('Karte') : [];
  378.         if (!isset($karte['cart'])) {
  379.             $karte['cart'] = [];
  380.         }
  381.         // get variable from view purchase completed
  382.         $dataLayer $event->getParameter('dataLayer');
  383.         //
  384.         if (!($orderHeader $event->getParameter('orderHeader'))) {
  385.             return;
  386.         } else if ($orderHeader->getOhType() === OrderHeader::TYPE_SAMPLE || $orderHeader->getOhType() === OrderHeader::TYPE_CATALOG) {
  387.             return;
  388.         }
  389.         //
  390.         if (!($Order $event->getParameter('Order'))) {
  391.             return;
  392.         }
  393.         //
  394.         if (empty($dataLayer[1])) {
  395.             return;
  396.         }
  397.         $order $event->getParameter('Order');
  398.         $customer $order->getCustomer();
  399.         $karte['buy'] = $this->karteService->getAttributesForPurchase($event$dataLayer);
  400.         $karte['buy_stats'] = $this->karteService->getAttributesForUserPurchaseInfomation($customer);
  401.         $event->addSnippet('Karte/buy_stats.twig');
  402.         $event->addSnippet('Karte/buy.twig');
  403.         if (!empty($karte)) {
  404.             $event->setParameter('Karte'$karte);
  405.         }
  406.     }
  407.     public function onCartStep2RegisterCompleted(EventArgs $event)
  408.     {
  409.         $this->session->set(LmEvents::SESSION_FRONT_PRODUCT_USER_CART_STEP2_REGISTER_COMPLETED$event->getArguments());
  410.     }
  411.     public function onMyPageUpdateInformation(TemplateEvent $event)
  412.     {
  413.         $parameter $event->hasParameter('Karte') ? $event->getParameter('Karte') : [];
  414.         if (in_array(true, [$this->session->remove(LmEvents::SESSION_FRONT_PRODUCT_CART_STEP2_LOGIN_COMPLETED), $this->session->remove(LmEvents::SESSION_FRONT_MYPAGE_USER_UPDATE_INFOMATION)])) {
  415.             /**
  416.              * @var Customer $customer
  417.              */
  418.             $customer $this->tokenStorage->getToken()->getUser();
  419.             $parameter $this->karteService->getAttributeForPersonalInfomation($customernull);
  420.         }
  421.         if ($dataUserUnsubscribe $this->session->remove(LmEvents::SESSION_FRONT_MYPAGE_USER_UNSUBSCRIBE)) {
  422.             $customer $dataUserUnsubscribe['customer'];
  423.             $unsubscribeDate $dataUserUnsubscribe['unsubscribeDate'];
  424.             $parameter $this->karteService->getAttributeForPersonalInfomation($customer$unsubscribeDate);
  425.         }
  426.         if (!empty($parameter)) {
  427.             $event->addSnippet('Karte/attribute.twig');
  428.             $event->setParameter('Karte'$parameter);
  429.         }
  430.     }
  431. }