app/Customize/Controller/TopController.php line 51

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