<?php
/*
* This file is part of Refine
*
* Copyright(c) 2023 Refine Co.,Ltd. All Rights Reserved.
*
* https://www.re-fine.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\RefineNewProduct42\Controller;
use Eccube\Controller\AbstractController;
use Eccube\Repository\ProductRepository;
use Plugin\RefineNewProduct42\Entity\RefineNewProductConfig;
use Plugin\RefineNewProduct42\Repository\RefineNewProductConfigRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class RefineNewProductController.
*/
class RefineNewProductController extends AbstractController
{
/**
* @var RefineNewProductConfigRepository
*/
private $refineNewProductConfigRepository;
/**
* @var ProductRepository
*/
private $productRepository;
/**
* RefineNewProductController constructor.
*
* @param RefineNewProductConfigRepository $refineNewProductConfigRepository
* @param ProductRepository $productRepository
* @throws \Exception
*/
public function __construct(
RefineNewProductConfigRepository $refineNewProductConfigRepository,
ProductRepository $productRepository
) {
$this->refineNewProductConfigRepository = $refineNewProductConfigRepository;
$this->productRepository = $productRepository;
}
/**
* 新着商品一覧
*
* @param Request $request
*
* @return array
*
* @Route("/block/refine_new_product", name="block_refine_new_product")
* @Template("Block/refine_new_product.twig")
*/
public function index(Request $request)
{
/** @var RefineNewProductConfig $Config */
$Config = $this->refineNewProductConfigRepository->get();
if (is_null($Config)) {
return [
'Products' => null,
];
}
// 何日までのものを表示するか
$elapsedDays = $Config->getElapsedDays();
// 現時点で create_date が現在年月日から指定日数前以降のものを取得
$startDate = new \DateTime('now', new \DateTimeZone('UTC'));
$startDate->setTime(0, 0);
$startDate->modify('-' . ($elapsedDays + 1) . ' day');
// 最大で何件まで表示するか
$maxDispNum = $Config->getMaxDispNum();
$qb = $this->productRepository
->createQueryBuilder('p')
->andWhere('p.Status = 1')
->andWhere('p.create_date > :create_date_start')
->setParameter('create_date_start', $startDate)
->orderBy('p.create_date', 'DESC')
->setMaxResults($maxDispNum);
$Products = $qb->getQuery()->getResult();
return [
'Products' => $Products,
'count' => count($Products),
];
}
}