<?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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Common\Collections\Criteria;
use Eccube\Repository\ProductRepository;
use Eccube\Form\Type\AddCartType;
class TopController extends AbstractController
{
public function __construct(
ProductRepository $productRepository
) {
$this->productRepository = $productRepository;
}
/**
* @Route("/", name="homepage")
* @Template("index.twig")
*/
public function index()
{
// 件数
$limit = 4;
// 新着商品を取得
$query = $this->productRepository->getQueryBuilderBySearchData([]);
$query->orderBy('p.create_date', Criteria::DESC)->setMaxResults($limit);
$products = $query->getQuery()->getResult();
$ids = [];
foreach ($products as $Product) {
$ids[] = $Product->getId();
}
$ProductsAndClassCategories = $this->productRepository->findProductsWithSortedClassCategories($ids, 'p.id');
// addCart form
$forms = [];
foreach ($products as $Product) {
$builder = $this->formFactory->createNamedBuilder(
'',
AddCartType::class,
null,
[
'product' => $ProductsAndClassCategories[$Product->getId()],
'allow_extra_fields' => true,
]
);
$addCartForm = $builder->getForm();
$forms[$Product->getId()] = $addCartForm->createView();
}
return [ 'products' => $products, 'forms' => $forms, ];
}
}