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

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_COMPLETE => 'hookAdminOrderEditIndexComplete',
  49.         ];
  50.     }
  51.     public function onTemplateAdminOrder(TemplateEvent $event)
  52.     {
  53.         $twig '@CustomerRank42/admin/Order/order_index.twig';
  54.         $event->addSnippet($twig);
  55.         $js '@CustomerRank42/admin/Order/order_index.js';
  56.         $event->addAsset($js);
  57.     }
  58.     public function onTemplateAdminOrderEdit(TemplateEvent $event)
  59.     {
  60.         $source $event->getSource();
  61.         if(preg_match("/\\$\('\#admin\_search\_product\_id'\)\.val\(\),/",$source$result)){
  62.             $search $result[0];
  63.             $replace $search "\n'customer_id':$('#order_CustomerId').text(),";
  64.             $source str_replace($search$replace$source);
  65.         }
  66.         $event->setSource($source);
  67.         $twig '@CustomerRank42/admin/Order/customer_rank.twig';
  68.         $event->addSnippet($twig);
  69.         $js '@CustomerRank42/admin/Order/customer_rank.js';
  70.         $event->addAsset($js);
  71.     }
  72.     public function hookAdminOrderEditSearchProductComplete(EventArgs $event)
  73.     {
  74.         $request $event->getRequest();
  75.         $session $request->getSession();
  76.         $pagination $event->getArgument('pagination');
  77.         if ('POST' === $request->getMethod()) {
  78.             $customer_id $request->get('customer_id');
  79.             $session->set('eccube.cusstomerrank.order.product.search'$customer_id);
  80.         }else{
  81.             $customer_id $session->get('eccube.cusstomerrank.order.product.search');
  82.         }
  83.         if(!is_null($customer_id) && is_numeric($customer_id) && $customer_id 0){
  84.             $Customer $this->customerRepository->find($customer_id);
  85.             if(!is_null($Customer)){
  86.                 $CustomerRank $Customer->getCustomerRank();
  87.                 if(!is_null($CustomerRank)){
  88.                     foreach($pagination as $Product){
  89.                         foreach($Product->getProductClasses() as $ProductClass){
  90.                             $ProductClass->setPrice02($this->customerPriceRepository->getCustomerPriceByProductClass($CustomerRank$ProductClass));
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }
  97.     public function hookAdminOrderEditIndexComplete(EventArgs $event)
  98.     {
  99.         $Customer $event->getArgument('Customer');
  100.         if(!is_null($Customer))$this->customerRankService->checkRank($Customer);
  101.     }
  102. }