src/Eccube/Controller/TopController.php line 23

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 Eccube\Controller;
  13. use Eccube\Repository\CustomCategoryRepository;
  14. use Eccube\Repository\ProductRepository;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class TopController extends AbstractController
  19. {
  20.     /**
  21.      * @var ProductRepository
  22.      */
  23.     protected $productRepository;
  24.     public function __construct(ProductRepository $productRepository)
  25.     {
  26.         $this->productRepository $productRepository;
  27.     }
  28.     /**
  29.      * @Route("/", name="homepage", methods={"GET"})
  30.      * @Template("index.twig")
  31.      */
  32.     public function index(Request $requestCustomCategoryRepository $categoryRepository)
  33.     {
  34.         // 商品カテゴリ一覧を取得
  35.         $parentCategoryId 7//商品カテゴリの親ID
  36.         $childCategories $categoryRepository->getChildCategoriesByParentId($parentCategoryId);
  37.         $categories $categoryRepository->getList();
  38.         //ピックアップタグがついた商品を取得
  39.         $productRepository $this->getDoctrine()->getRepository('Eccube\Entity\Product');
  40.         $pickup_tag_id 5//ピックアップタグのID
  41.         $pickup_products $productRepository->findByTagWithLimit($pickup_tag_id4);
  42.         //ランキングを表示
  43.         $parentCategoryId 51// 親カテゴリのIDを指定します。
  44.         $parentCategory $this->getDoctrine()->getRepository('Eccube\Entity\Category')->find($parentCategoryId);
  45.         // 子カテゴリを昇順で取得
  46.         $subCategories $parentCategory->getChildren()->toArray();
  47.         usort($subCategories, function ($a$b) {
  48.             return $a->getSortNo() <=> $b->getSortNo();
  49.         });
  50.         // 各子カテゴリに属する商品を取得
  51.         $productsBySubcategory = [];
  52.         foreach ($subCategories as $subcategory) {
  53.             $queryBuilder $this->productRepository->createQueryBuilder('p');
  54.             $queryBuilder
  55.                 ->innerJoin('p.ProductCategories''pc')
  56.                 ->where('pc.Category = :Category')
  57.                 ->setParameter('Category'$subcategory);
  58.             $products $queryBuilder->getQuery()->getResult();
  59.             $productsBySubcategory[$subcategory->getId()] = $products;
  60.         }
  61.         return $this->render('index.twig', [
  62.             'childCategories' => $childCategories,
  63.             'categories' => $categories,
  64.             //ピックアップ商品
  65.             'pickup_products' => $pickup_products,
  66.             //ランキング一覧
  67.             'products_by_subcategory' => $productsBySubcategory,
  68.         ]);
  69.     }
  70. }