<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) Takashi Otaki All Rights Reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\EtunaItemRanking42\Controller;
use Plugin\EtunaItemRanking42\Repository\EtunaItemRankingConfigRepository;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Eccube\Repository\OrderItemRepository;
use Eccube\Repository\ProductRepository;
use Eccube\Repository\CategoryRepository;
use Eccube\Entity\Master\ProductStatus;
use Eccube\Entity\Master\OrderStatus;
/**
* Class EtunaItemRankingController front.
*/
class EtunaItemRankingController extends \Eccube\Controller\AbstractController
{
/**
* @Route("/block/etuna_item_ranking", name="block_etuna_item_ranking")
* @Template("Block/etuna_item_ranking.twig")
*
* @param Request $request
* @param EtunaItemRankingConfigRepository $configRepository
* @param ProductRepository $productRepository
*/
public function index(Request $request, EtunaItemRankingConfigRepository $configRepository, OrderItemRepository $orderItemRepository, ProductRepository $productRepository, CategoryRepository $categoryRepository)
{
$catFlg = false;
if (isset($_GET["category_id"]) && is_numeric($_GET["category_id"])) {
$catId = $_GET["category_id"];
$catFlg = true;
}
$Config = $configRepository->get();
if ($Config->getItemRankingSort() == 0) {
$selectSql = "SUM(oi.price * oi.quantity) as total, p.id";
} else {
$selectSql = "SUM(oi.quantity) as total, p.id";
}
if ($Config->getItemRankingPeriod() == 0) {
$period = "- 1 day";
} else if ($Config->getItemRankingPeriod() == 1) {
$period = "- 7 day";
} else if ($Config->getItemRankingPeriod() == 2) {
$period = "- 31 day";
}
$Date = new \DateTime();
$end = $Date->format("Y-m-d 23:59:59");
$start = $Date->modify($period)->format("Y-m-d 00:00:00");
$qb = $orderItemRepository->createQueryBuilder('oi')
->select($selectSql)
->innerJoin('Eccube\Entity\Product', 'p', 'WITH', 'p.id = oi.Product')
->innerJoin('Eccube\Entity\Order', 'o', 'WITH', 'oi.Order = o')
->where('p.Status = :Disp')
->andWhere('o.OrderStatus = :OrderStatus')
->andWhere('o.create_date >= :start')
->andWhere('o.create_date <= :end')
->orderBy('total', 'DESC')
->setParameter('Disp', ProductStatus::DISPLAY_SHOW)
->setParameter(':OrderStatus', OrderStatus::DELIVERED)
->setParameter(':start', $start)
->setParameter(':end', $end)
->groupBy('p.id')
->setMaxResults($Config->getItemRankingCount())
;
$categoryName = "";
if ($catFlg) {
$qb = $qb
->innerJoin('Eccube\Entity\ProductCategory', 'pc', 'WITH', 'pc.product_id = p.id')
->andWhere('pc.category_id = :category_id')
->setParameter(':category_id', $catId)
;
$category = $categoryRepository->find($catId);
$categoryName = $category->getName();
}
$orderItems = $qb->getQuery()->getResult();
$ItemRanking = [];
foreach ($orderItems as $item) {
$ItemRanking[] = $productRepository->find($item['id']);
}
return [
'Config' => $Config,
'ItemRanking' => $ItemRanking,
'categoryName' => $categoryName,
];
}
}