<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Eccube\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
class TopController extends AbstractController
{
/**
* @Route("/", name="homepage")
* @Template("index.twig")
*/
public function index()
{
// OPTIMIZED: Reduce products for faster loading (homepage only needs 20-30 products)
$qb = $this->entityManager->getRepository('Eccube\Entity\Product')
->createQueryBuilder('p')
->leftJoin('Plugin\\ProductReview4\\Entity\\ProductReview', 'pr', 'WITH', 'p.id = pr.Product AND pr.Status = 1')
->andWhere('p.Status = 1')
->groupBy('p.id')
->addSelect('COUNT(pr.id) as review_count')
->addSelect('AVG(pr.recommend_level) as recommend_avg')
->orderBy('COUNT(pr.id)', 'DESC')
->addOrderBy('AVG(pr.recommend_level)', 'DESC')
->setMaxResults(30); // Reduced from 60 to 30 for faster loading
// OPTIMIZED: Use result cache for 5 minutes to avoid repeated queries
$results = $qb->getQuery()
->useResultCache(true, 300) // Cache for 5 minutes
->getResult();
$Products = [];
$ReviewAveList = [];
$ReviewCntList = [];
foreach ($results as $row) {
$product = $row[0]; // The product entity
$Products[] = $product;
$ReviewAveList[$product->getId()] = round($row['recommend_avg'] ?? 0);
$ReviewCntList[$product->getId()] = intval($row['review_count'] ?? 0);
}
// OPTIMIZED: Get only 3 most recent reviews (reduced query complexity)
$ProductReviews = [];
if (!empty($Products)) {
$productIds = array_slice(array_map(function($product) {
return $product->getId();
}, $Products), 0, 10); // Only check top 10 products for reviews
$ProductReviews = $this->entityManager
->getRepository('Plugin\ProductReview4\Entity\ProductReview')
->createQueryBuilder('pr')
->select('pr')
->andWhere('pr.Status = 1')
->andWhere('pr.Product IN (:productIds)')
->setParameter('productIds', $productIds)
->orderBy('pr.id', 'DESC')
->setMaxResults(3)
->getQuery()
->useResultCache(true, 300) // Cache reviews for 5 minutes
->getResult();
}
$Customer = $this->getUser();
return [
'Products' => $Products,
'ProductReviews' => $ProductReviews,
'ReviewAveList' => $ReviewAveList,
'ReviewCntList' => $ReviewCntList,
'Customer' => $Customer,
];
}
}