<?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 Customize\Controller;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Category;
use Eccube\Entity\Product;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Customize\Helper\BaseHelper;
use Customize\Entity\Banner;
use Customize\Entity\DisplayContent;
use Customize\Repository\DisplayContentRepository;
use Eccube\Entity\Master\ProductStatus;
class TopController extends AbstractController
{
use BaseHelper;
/**
* @var DisplayContentRepository
*/
protected $displayContentRepository;
public function __construct(DisplayContentRepository $displayContentRepository)
{
$this->displayContentRepository = $displayContentRepository;
}
/**
* @Route("/", name="homepage", methods={"GET"})
* @Template("index.twig")
*/
public function index()
{
if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
if ($myPageUrl = (env('MY_PAGE_URL') ?? null)) {
$redirectMyPage = $this->generateRedirectMyPage();
if ($this->session->get('redirect', null) === $myPageUrl && $this->session->get('has_redirect_mypage', false) === false) {
$this->session->set('has_redirect_mypage', true);
return $this->redirect($redirectMyPage);
}
}
}
// Lấy sản phẩm có category_id 1001 và 1002 lấy 4 sản phẩm
$categoryId1 = Category::$CATEGORY_TYPE_ROUTER_TERMINAL_ID; // 1001
$categoryId2 = Category::$CATEGORY_TYPE_GIGABIT_ADDITION_ID; // 1002
$category1 = $this->entityManager->getRepository(Category::class)->find($categoryId1);
$category2 = $this->entityManager->getRepository(Category::class)->find($categoryId2);
$productRepository = $this->entityManager->getRepository(Product::class);
if ($category1 && $category2) {
$searchData1 = ['category_id' => $category1];
$searchData2 = ['category_id' => $category2];
$qb1 = $productRepository->getQueryBuilderBySearchData($searchData1);
$products1 = $qb1->getQuery()->setMaxResults(4)->getResult();
$qb2 = $productRepository->getQueryBuilderBySearchData($searchData2);
$products2 = $qb2->getQuery()->setMaxResults(4)->getResult();
}
// Lấy tất cả các banner đang active status = 1 sắp xếp theo sort_no
$bannerRepository = $this->entityManager->getRepository(Banner::class);
$banners = $bannerRepository->getVisibleBanners();
// 注目の商品: IDs configured in /admin/content/display
$topRankingProducts = $this->getProductsByDisplayContentKey(
$productRepository,
DisplayContent::KEY_FEATURED_PRODUCTS
);
return [
'router_products' => $products1 ?? [],
'gigabit_products' => $products2 ?? [],
'top_ranking_products' => $topRankingProducts,
'banners' => $banners ?? [],
];
}
/**
* Load public products by display content key, preserving configured order.
*
* @param \Eccube\Repository\ProductRepository $productRepository
* @param string $configKey
*
* @return Product[]
*/
private function getProductsByDisplayContentKey($productRepository, $configKey)
{
$ids = $this->displayContentRepository->getProductIdListByKey($configKey);
if (empty($ids)) {
return [];
}
$qb = $productRepository->createQueryBuilder('p')
->where('p.id IN (:ids)')
->andWhere('p.Status = :status')
->setParameter('ids', $ids)
->setParameter('status', ProductStatus::DISPLAY_SHOW)
->innerJoin('p.ProductClasses', 'pc')
->andWhere('pc.visible = :visible')
->setParameter('visible', true)
->groupBy('p.id');
$products = $qb->getQuery()->getResult();
$byId = [];
foreach ($products as $product) {
$byId[$product->getId()] = $product;
}
$ordered = [];
foreach ($ids as $id) {
if (isset($byId[$id])) {
$ordered[] = $byId[$id];
}
}
return $ordered;
}
}