app/Plugin/OrderBySale4/Event.php line 48

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