app/Plugin/CustomerRank42/Event/AdminOrderEvent.php line 103

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : CustomerRank
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Plugin\CustomerRank42\Event;
  12. use Eccube\Event\EccubeEvents;
  13. use Eccube\Event\EventArgs;
  14. use Eccube\Event\TemplateEvent;
  15. use Eccube\Repository\CustomerRepository;
  16. use Plugin\CustomerRank42\Repository\CustomerPriceRepository;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class AdminOrderEvent implements EventSubscriberInterface
  19. {
  20.     private $customerRepository;
  21.     private $customerPriceRepository;
  22.     public function __construct(
  23.             CustomerRepository $customerRepository,
  24.             CustomerPriceRepository $customerPriceRepository
  25.             )
  26.     {
  27.         $this->customerRepository $customerRepository;
  28.         $this->customerPriceRepository $customerPriceRepository;
  29.     }
  30.     /**
  31.      * @return array
  32.      */
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             '@admin/Order/index.twig' => 'onTemplateAdminOrder',
  37.             '@admin/Order/edit.twig' => 'onTemplateAdminOrderEdit',
  38.             EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_COMPLETE => 'hookAdminOrderEditSearchProductComplete',
  39.             EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS => 'hookAdminOrderEditIndexProgress'
  40.         ];
  41.     }
  42.     public function onTemplateAdminOrder(TemplateEvent $event)
  43.     {
  44.         $twig '@CustomerRank42/admin/Order/order_index.twig';
  45.         $event->addSnippet($twig);
  46.         $js '@CustomerRank42/admin/Order/order_index.js';
  47.         $event->addAsset($js);
  48.     }
  49.     public function onTemplateAdminOrderEdit(TemplateEvent $event)
  50.     {
  51.         $source $event->getSource();
  52.         if(preg_match("/\\$\('\#admin\_search\_product\_id'\)\.val\(\),/",$source$result)){
  53.             $search $result[0];
  54.             $replace $search "\n'customer_id':$('#order_CustomerId').text(),";
  55.             $source str_replace($search$replace$source);
  56.         }
  57.         $event->setSource($source);
  58.         $twig '@CustomerRank42/admin/Order/customer_rank.twig';
  59.         $event->addSnippet($twig);
  60.         $js '@CustomerRank42/admin/Order/customer_rank.js';
  61.         $event->addSnippet($js);
  62.     }
  63.     public function hookAdminOrderEditSearchProductComplete(EventArgs $event)
  64.     {
  65.         $request $event->getRequest();
  66.         $session $request->getSession();
  67.         $pagination $event->getArgument('pagination');
  68.         if ('POST' === $request->getMethod()) {
  69.             $customer_id $request->get('customer_id');
  70.             $session->set('eccube.cusstomerrank.order.product.search'$customer_id);
  71.         }else{
  72.             $customer_id $session->get('eccube.cusstomerrank.order.product.search');
  73.         }
  74.         if(!is_null($customer_id) && is_numeric($customer_id) && $customer_id 0){
  75.             $Customer $this->customerRepository->find($customer_id);
  76.             if(!is_null($Customer)){
  77.                 $CustomerRank $Customer->getCustomerRank();
  78.                 if(!is_null($CustomerRank)){
  79.                     foreach($pagination as $Product){
  80.                         foreach($Product->getProductClasses() as $ProductClass){
  81.                             $ProductClass->setPrice02($this->customerPriceRepository->getCustomerPriceByProductClass($CustomerRank$ProductClass));
  82.                         }
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     public function hookAdminOrderEditIndexProgress(EventArgs $event)
  89.     {
  90.         $TargetOrder $event->getArgument('TargetOrder');
  91.         $Customer $TargetOrder->getCustomer();
  92.         if(is_null($TargetOrder->getId()) && !is_null($Customer)){
  93.             $CustomerRank $Customer->getCustomerRank();
  94.             if($CustomerRank){
  95.                 $TargetOrder->setCustomerRankId($CustomerRank->getId());
  96.                 $TargetOrder->setCustomerRankName($CustomerRank->getName());
  97.             }
  98.         }
  99.     }
  100. }