<?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;
class TopController extends AbstractController
{
use BaseHelper;
/**
* @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();
// Lấy 5 sản phẩm top ranking
$qbRanking = $productRepository->createQueryBuilder('p')
->where('p.Status = :status')
->setParameter('status', 1)
->andWhere('p.ranking IS NOT NULL')
->innerJoin('p.ProductClasses', 'pc')
->andWhere('pc.visible = :visible')
->setParameter('visible', true)
->groupBy('p.id')
->orderBy('p.ranking', 'ASC')
->addOrderBy('p.id', 'DESC')
->setMaxResults(5);
$topRankingProducts = $qbRanking->getQuery()->getResult();
return [
'router_products' => $products1 ?? [],
'gigabit_products' => $products2 ?? [],
'top_ranking_products' => $topRankingProducts,
'banners' => $banners ?? [],
];
}
}