app/Plugin/EtunaItemRanking/Controller/EtunaItemRankingController.php line 38

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) Takashi Otaki All Rights Reserved.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\EtunaItemRanking\Controller;
  11. use Plugin\EtunaItemRanking\Repository\EtunaItemRankingConfigRepository;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Eccube\Repository\OrderItemRepository;
  16. use Eccube\Repository\ProductRepository;
  17. use Eccube\Repository\CategoryRepository;
  18. use Eccube\Entity\Master\ProductStatus;
  19. use Eccube\Entity\Master\OrderStatus;
  20. /**
  21.  * Class EtunaItemRankingController front.
  22.  */
  23. class EtunaItemRankingController extends \Eccube\Controller\AbstractController
  24. {
  25.     /**
  26.      * @Route("/block/etuna_item_ranking", name="block_etuna_item_ranking")
  27.      * @Template("Block/etuna_item_ranking.twig")
  28.      *
  29.      * @param Request $request
  30.      * @param EtunaItemRankingConfigRepository $configRepository
  31.      * @param ProductRepository $productRepository
  32.      */
  33.     public function index(Request $requestEtunaItemRankingConfigRepository $configRepositoryOrderItemRepository $orderItemRepositoryProductRepository $productRepositoryCategoryRepository $categoryRepository)
  34.     {
  35.         $catFlg false;
  36.         if (isset($_GET["category_id"]) && is_numeric($_GET["category_id"])) {
  37.             $catId $_GET["category_id"];
  38.             $catFlg true;
  39.         }
  40.         $Config $configRepository->get();
  41.         if ($Config->getItemRankingSort() == 0) {
  42.             $selectSql "SUM(oi.price * oi.quantity) as total, p.id";
  43.         } else {
  44.             $selectSql "SUM(oi.quantity) as total, p.id";
  45.         }
  46.         if ($Config->getItemRankingPeriod() == 0) {
  47.             $period "- 1 day";
  48.         } else if ($Config->getItemRankingPeriod() == 1) {
  49.             $period "- 7 day";
  50.         } else if ($Config->getItemRankingPeriod() == 2) {
  51.             $period "- 31 day";
  52.         }
  53.         $Date = new \DateTime();
  54.         $end $Date->format("Y-m-d 23:59:59");
  55.         $start $Date->modify($period)->format("Y-m-d 00:00:00");
  56.         $qb $orderItemRepository->createQueryBuilder('oi')
  57.             ->select($selectSql)
  58.             ->innerJoin('Eccube\Entity\Product''p''WITH''p.id = oi.Product')
  59.             ->innerJoin('Eccube\Entity\Order''o''WITH''oi.Order = o')
  60.             ->where('p.Status = :Disp')
  61.             ->andWhere('o.OrderStatus = :OrderStatus')
  62.             ->andWhere('o.create_date >= :start')
  63.             ->andWhere('o.create_date <= :end')
  64.             ->orderBy('total''DESC')
  65.             ->setParameter('Disp'ProductStatus::DISPLAY_SHOW)
  66.             ->setParameter(':OrderStatus'OrderStatus::DELIVERED)
  67.             ->setParameter(':start'$start)
  68.             ->setParameter(':end'$end)
  69.             ->groupBy('p.id')
  70.             ->setMaxResults($Config->getItemRankingCount())
  71.             ;
  72.         $categoryName "";
  73.         if ($catFlg) {
  74.             $qb $qb
  75.                 ->innerJoin('Eccube\Entity\ProductCategory''pc''WITH''pc.product_id = p.id')
  76.                 ->andWhere('pc.category_id = :category_id')
  77.                 ->setParameter(':category_id'$catId)
  78.                 ;
  79.             $category $categoryRepository->find($catId);
  80.             $categoryName $category->getName();
  81.         }
  82.         $orderItems $qb->getQuery()->getResult();
  83.         $ItemRanking = [];
  84.         foreach ($orderItems as $item) {
  85.             $ItemRanking[] = $productRepository->find($item['id']);
  86.         }
  87.         return [
  88.             'Config' => $Config,
  89.             'ItemRanking' => $ItemRanking,
  90.             'categoryName' => $categoryName,
  91.         ];
  92.     }
  93. }