app/Customize/Controller/TopController.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Customize\Controller;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Entity\Category;
  15. use Eccube\Entity\Product;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Customize\Helper\BaseHelper;
  19. use Customize\Entity\Banner;
  20. class TopController extends AbstractController
  21. {
  22.     use BaseHelper;
  23.     /**
  24.      * @Route("/", name="homepage", methods={"GET"})
  25.      * @Template("index.twig")
  26.      */
  27.     public function index()
  28.     {
  29.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  30.             if ($myPageUrl = (env('MY_PAGE_URL') ?? null)) {
  31.                 $redirectMyPage $this->generateRedirectMyPage();
  32.                 if ($this->session->get('redirect'null) === $myPageUrl && $this->session->get('has_redirect_mypage'false) === false) {
  33.                     $this->session->set('has_redirect_mypage'true);
  34.                     return $this->redirect($redirectMyPage);
  35.                 }
  36.             }
  37.         }
  38.         // Lấy sản phẩm có category_id 1001 và 1002 lấy 4 sản phẩm
  39.         $categoryId1 Category::$CATEGORY_TYPE_ROUTER_TERMINAL_ID// 1001
  40.         $categoryId2 Category::$CATEGORY_TYPE_GIGABIT_ADDITION_ID// 1002
  41.         $category1 $this->entityManager->getRepository(Category::class)->find($categoryId1);
  42.         $category2 $this->entityManager->getRepository(Category::class)->find($categoryId2);
  43.         $productRepository $this->entityManager->getRepository(Product::class);
  44.         if ($category1 && $category2) {
  45.             $searchData1 = ['category_id' => $category1];
  46.             $searchData2 = ['category_id' => $category2];
  47.             $qb1 $productRepository->getQueryBuilderBySearchData($searchData1);
  48.             $products1 $qb1->getQuery()->setMaxResults(4)->getResult();
  49.             $qb2 $productRepository->getQueryBuilderBySearchData($searchData2);
  50.             $products2 $qb2->getQuery()->setMaxResults(4)->getResult();
  51.         }
  52.         // Lấy tất cả các banner đang active status = 1 sắp xếp theo sort_no
  53.         $bannerRepository $this->entityManager->getRepository(Banner::class);
  54.         $banners $bannerRepository->getVisibleBanners();
  55.         // Lấy 5 sản phẩm top ranking
  56.         $qbRanking $productRepository->createQueryBuilder('p')
  57.             ->where('p.Status = :status')
  58.             ->setParameter('status'1)
  59.             ->andWhere('p.ranking IS NOT NULL')
  60.             ->innerJoin('p.ProductClasses''pc')
  61.             ->andWhere('pc.visible = :visible')
  62.             ->setParameter('visible'true)
  63.             ->groupBy('p.id')
  64.             ->orderBy('p.ranking''ASC')
  65.             ->addOrderBy('p.id''DESC')
  66.             ->setMaxResults(5);
  67.         $topRankingProducts $qbRanking->getQuery()->getResult();
  68.         return [
  69.             'router_products' => $products1 ?? [],
  70.             'gigabit_products' => $products2 ?? [],
  71.             'top_ranking_products' => $topRankingProducts,
  72.             'banners' => $banners ?? [],
  73.         ];
  74.     }
  75. }