app/Plugin/OrderBySale4/Event.php line 55

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of OrderBySale4
  4.  *
  5.  * Copyright(c) U-Mebius Inc. All Rights Reserved.
  6.  *
  7.  * https://umebius.com/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\OrderBySale4;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Doctrine\ORM\QueryBuilder;
  15. use Eccube\Entity\Master\OrderStatus;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Plugin\OrderBySale4\Entity\Config;
  19. use Plugin\OrderBySale4\Repository\ConfigRepository;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class Event implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var EntityManagerInterface
  25.      */
  26.     private $entityManager;
  27.     /**
  28.      * @var ConfigRepository
  29.      */
  30.     private $configRepository;
  31.     public function __construct(
  32.         EntityManagerInterface $entityManager,
  33.         ConfigRepository $configRepository
  34.     ) {
  35.         $this->entityManager $entityManager;
  36.         $this->configRepository $configRepository;
  37.     }
  38.     /**
  39.      * @return array
  40.      */
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH => 'onFrontProductIndexSearch',
  45.         ];
  46.     }
  47.     public function onFrontProductIndexSearch(EventArgs $eventArgs)
  48.     {
  49.         /* @var $qb QueryBuilder */
  50.         $qb $eventArgs->getArgument('qb');
  51.         $searchData $eventArgs->getArgument('searchData');
  52.         $ProductListOrderBy $searchData['orderby'];
  53.         if ($ProductListOrderBy) {
  54.             $Config $this->configRepository->findOneBy([
  55.                 'product_list_order_by_id' => $ProductListOrderBy->getId(),
  56.             ]);
  57.             $excludes = [OrderStatus::CANCELOrderStatus::PENDINGOrderStatus::PROCESSINGOrderStatus::RETURNED];
  58.             if ($Config) {
  59.                 if ($Config->getType() == Config::ORDER_BY_AMOUNT) {
  60.                     $qb->addSelect('(SELECT CASE WHEN SUM(oi.price * oi.quantity) IS NULL THEN -1 ELSE SUM(oi.price * oi.quantity) END
  61.                     FROM \Eccube\Entity\OrderItem AS oi 
  62.                     LEFT JOIN oi.Order AS o 
  63.                     WHERE 
  64.                      oi.Product=p.id 
  65.                      AND o.OrderStatus not in (:excludes)
  66.                      ) AS HIDDEN  buy_quantity')
  67.                         ->orderBy('buy_quantity''DESC')
  68.                         ->groupBy('p.id')
  69.                         ->setParameter('excludes'$excludes)
  70.                     ;
  71.                 } elseif ($Config->getType() == Config::ORDER_BY_QUANTITY) {
  72.                     $qb->addSelect('(SELECT CASE WHEN SUM(oi.quantity) IS NULL THEN -1 ELSE SUM(oi.quantity) END
  73.                     FROM \Eccube\Entity\OrderItem AS oi 
  74.                     LEFT JOIN oi.Order AS o 
  75.                     WHERE 
  76.                      oi.Product=p.id 
  77.                      AND o.OrderStatus not in (:excludes)
  78.                      ) AS HIDDEN buy_quantity')
  79.                         ->orderBy('buy_quantity''DESC')
  80.                         ->groupBy('p.id')
  81.                         ->setParameter('excludes'$excludes)
  82.                     ;
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }