app/Customize/Controller/TopController.php line 45

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. use Customize\Entity\DisplayContent;
  21. use Customize\Repository\DisplayContentRepository;
  22. use Eccube\Entity\Master\ProductStatus;
  23. class TopController extends AbstractController
  24. {
  25.     use BaseHelper;
  26.     /**
  27.      * @var DisplayContentRepository
  28.      */
  29.     protected $displayContentRepository;
  30.     public function __construct(DisplayContentRepository $displayContentRepository)
  31.     {
  32.         $this->displayContentRepository $displayContentRepository;
  33.     }
  34.     /**
  35.      * @Route("/", name="homepage", methods={"GET"})
  36.      * @Template("index.twig")
  37.      */
  38.     public function index()
  39.     {
  40.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  41.             if ($myPageUrl = (env('MY_PAGE_URL') ?? null)) {
  42.                 $redirectMyPage $this->generateRedirectMyPage();
  43.                 if ($this->session->get('redirect'null) === $myPageUrl && $this->session->get('has_redirect_mypage'false) === false) {
  44.                     $this->session->set('has_redirect_mypage'true);
  45.                     return $this->redirect($redirectMyPage);
  46.                 }
  47.             }
  48.         }
  49.         // Lấy sản phẩm có category_id 1001 và 1002 lấy 4 sản phẩm
  50.         $categoryId1 Category::$CATEGORY_TYPE_ROUTER_TERMINAL_ID// 1001
  51.         $categoryId2 Category::$CATEGORY_TYPE_GIGABIT_ADDITION_ID// 1002
  52.         $category1 $this->entityManager->getRepository(Category::class)->find($categoryId1);
  53.         $category2 $this->entityManager->getRepository(Category::class)->find($categoryId2);
  54.         $productRepository $this->entityManager->getRepository(Product::class);
  55.         if ($category1 && $category2) {
  56.             $searchData1 = ['category_id' => $category1];
  57.             $searchData2 = ['category_id' => $category2];
  58.             $qb1 $productRepository->getQueryBuilderBySearchData($searchData1);
  59.             $products1 $qb1->getQuery()->setMaxResults(4)->getResult();
  60.             $qb2 $productRepository->getQueryBuilderBySearchData($searchData2);
  61.             $products2 $qb2->getQuery()->setMaxResults(4)->getResult();
  62.         }
  63.         // Lấy tất cả các banner đang active status = 1 sắp xếp theo sort_no
  64.         $bannerRepository $this->entityManager->getRepository(Banner::class);
  65.         $banners $bannerRepository->getVisibleBanners();
  66.         // 注目の商品: IDs configured in /admin/content/display
  67.         $topRankingProducts $this->getProductsByDisplayContentKey(
  68.             $productRepository,
  69.             DisplayContent::KEY_FEATURED_PRODUCTS
  70.         );
  71.         return [
  72.             'router_products' => $products1 ?? [],
  73.             'gigabit_products' => $products2 ?? [],
  74.             'top_ranking_products' => $topRankingProducts,
  75.             'banners' => $banners ?? [],
  76.         ];
  77.     }
  78.     /**
  79.      * Load public products by display content key, preserving configured order.
  80.      *
  81.      * @param \Eccube\Repository\ProductRepository $productRepository
  82.      * @param string $configKey
  83.      *
  84.      * @return Product[]
  85.      */
  86.     private function getProductsByDisplayContentKey($productRepository$configKey)
  87.     {
  88.         $ids $this->displayContentRepository->getProductIdListByKey($configKey);
  89.         if (empty($ids)) {
  90.             return [];
  91.         }
  92.         $qb $productRepository->createQueryBuilder('p')
  93.             ->where('p.id IN (:ids)')
  94.             ->andWhere('p.Status = :status')
  95.             ->setParameter('ids'$ids)
  96.             ->setParameter('status'ProductStatus::DISPLAY_SHOW)
  97.             ->innerJoin('p.ProductClasses''pc')
  98.             ->andWhere('pc.visible = :visible')
  99.             ->setParameter('visible'true)
  100.             ->groupBy('p.id');
  101.         $products $qb->getQuery()->getResult();
  102.         $byId = [];
  103.         foreach ($products as $product) {
  104.             $byId[$product->getId()] = $product;
  105.         }
  106.         $ordered = [];
  107.         foreach ($ids as $id) {
  108.             if (isset($byId[$id])) {
  109.                 $ordered[] = $byId[$id];
  110.             }
  111.         }
  112.         return $ordered;
  113.     }
  114. }