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

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 Plugin\CustomerRank42\Repository\CustomerRankRepository;
  18. use Plugin\CustomerRank42\Service\CustomerRankService;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class AdminOrderEvent implements EventSubscriberInterface
  21. {
  22.     private $customerRepository;
  23.     private $customerPriceRepository;
  24.     private $customerRankService;
  25.     /**
  26.      * CustomerRankController constructor.
  27.      * @param CustomerRankRepository $customerRankRepository
  28.      */
  29.     public function __construct(
  30.             CustomerRepository $customerRepository,
  31.             CustomerPriceRepository $customerPriceRepository,
  32.             CustomerRankService $customerRankService
  33.             )
  34.     {
  35.         $this->customerRepository $customerRepository;
  36.         $this->customerPriceRepository $customerPriceRepository;
  37.         $this->customerRankService $customerRankService;
  38.     }
  39.     /**
  40.      * @return array
  41.      */
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             '@admin/Order/index.twig' => 'onTemplateAdminOrder',
  46.             '@admin/Order/edit.twig' => 'onTemplateAdminOrderEdit',
  47.             EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_COMPLETE => 'hookAdminOrderEditSearchProductComplete',
  48.             EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS => 'hookAdminOrderEditIndexProgress',
  49.             EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE => 'hookAdminOrderEditIndexComplete',
  50.         ];
  51.     }
  52.     public function onTemplateAdminOrder(TemplateEvent $event)
  53.     {
  54.         $twig '@CustomerRank42/admin/Order/order_index.twig';
  55.         $event->addSnippet($twig);
  56.         $js '@CustomerRank42/admin/Order/order_index.js';
  57.         $event->addAsset($js);
  58.     }
  59.     public function onTemplateAdminOrderEdit(TemplateEvent $event)
  60.     {
  61.         $source $event->getSource();
  62.         if(preg_match("/\\$\('\#admin\_search\_product\_id'\)\.val\(\),/",$source$result)){
  63.             $search $result[0];
  64.             $replace $search "\n'customer_id':$('#order_CustomerId').text(),";
  65.             $source str_replace($search$replace$source);
  66.         }
  67.         $event->setSource($source);
  68.         $twig '@CustomerRank42/admin/Order/customer_rank.twig';
  69.         $event->addSnippet($twig);
  70.         $js '@CustomerRank42/admin/Order/customer_rank.js';
  71.         $event->addAsset($js);
  72.     }
  73.     public function hookAdminOrderEditSearchProductComplete(EventArgs $event)
  74.     {
  75.         $request $event->getRequest();
  76.         $session $request->getSession();
  77.         $pagination $event->getArgument('pagination');
  78.         if ('POST' === $request->getMethod()) {
  79.             $customer_id $request->get('customer_id');
  80.             $session->set('eccube.cusstomerrank.order.product.search'$customer_id);
  81.         }else{
  82.             $customer_id $session->get('eccube.cusstomerrank.order.product.search');
  83.         }
  84.         if(!is_null($customer_id) && is_numeric($customer_id) && $customer_id 0){
  85.             $Customer $this->customerRepository->find($customer_id);
  86.             if(!is_null($Customer)){
  87.                 $CustomerRank $Customer->getCustomerRank();
  88.                 if(!is_null($CustomerRank)){
  89.                     foreach($pagination as $Product){
  90.                         foreach($Product->getProductClasses() as $ProductClass){
  91.                             $ProductClass->setPrice02($this->customerPriceRepository->getCustomerPriceByProductClass($CustomerRank$ProductClass));
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.     }
  98.     public function hookAdminOrderEditIndexProgress(EventArgs $event)
  99.     {
  100.         $TargetOrder $event->getArgument('TargetOrder');
  101.         $Customer $TargetOrder->getCustomer();
  102.         if(is_null($TargetOrder->getId()) && !is_null($Customer)){
  103.             $CustomerRank $Customer->getCustomerRank();
  104.             if($CustomerRank){
  105.                 $TargetOrder->setCustomerRankId($CustomerRank->getId());
  106.                 $TargetOrder->setCustomerRankName($CustomerRank->getName());
  107.             }
  108.         }
  109.     }
  110.     public function hookAdminOrderEditIndexComplete(EventArgs $event)
  111.     {
  112.         $Customer $event->getArgument('Customer');
  113.         if(!is_null($Customer))$this->customerRankService->checkRank($Customer);
  114.     }
  115. }