<?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 Eccube\Controller;
use Eccube\Repository\CustomCategoryRepository;
use Eccube\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
class TopController extends AbstractController
{
/**
* @var ProductRepository
*/
protected $productRepository;
public function __construct(ProductRepository $productRepository)
{
$this->productRepository = $productRepository;
}
/**
* @Route("/", name="homepage", methods={"GET"})
* @Template("index.twig")
*/
public function index(Request $request, CustomCategoryRepository $categoryRepository)
{
// 商品カテゴリ一覧を取得
$parentCategoryId = 7; //商品カテゴリの親ID
$childCategories = $categoryRepository->getChildCategoriesByParentId($parentCategoryId);
$categories = $categoryRepository->getList();
//ピックアップタグがついた商品を取得
$productRepository = $this->getDoctrine()->getRepository('Eccube\Entity\Product');
$pickup_tag_id = 5; //ピックアップタグのID
$pickup_products = $productRepository->findByTagWithLimit($pickup_tag_id, 4);
//ランキングを表示
$parentCategoryId = 51; // 親カテゴリのIDを指定します。
$parentCategory = $this->getDoctrine()->getRepository('Eccube\Entity\Category')->find($parentCategoryId);
// 子カテゴリを昇順で取得
$subCategories = $parentCategory->getChildren()->toArray();
usort($subCategories, function ($a, $b) {
return $a->getSortNo() <=> $b->getSortNo();
});
// 各子カテゴリに属する商品を取得
$productsBySubcategory = [];
foreach ($subCategories as $subcategory) {
$queryBuilder = $this->productRepository->createQueryBuilder('p');
$queryBuilder
->innerJoin('p.ProductCategories', 'pc')
->where('pc.Category = :Category')
->setParameter('Category', $subcategory);
$products = $queryBuilder->getQuery()->getResult();
$productsBySubcategory[$subcategory->getId()] = $products;
}
return $this->render('index.twig', [
'childCategories' => $childCategories,
'categories' => $categories,
//ピックアップ商品
'pickup_products' => $pickup_products,
//ランキング一覧
'products_by_subcategory' => $productsBySubcategory,
]);
}
}