app/Plugin/RefineNewProduct42/Controller/RefineNewProductController.php line 64

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Refine
  4.  *
  5.  * Copyright(c) 2023 Refine Co.,Ltd. All Rights Reserved.
  6.  *
  7.  * https://www.re-fine.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 Plugin\RefineNewProduct42\Controller;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Repository\ProductRepository;
  15. use Plugin\RefineNewProduct42\Entity\RefineNewProductConfig;
  16. use Plugin\RefineNewProduct42\Repository\RefineNewProductConfigRepository;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. /**
  21.  * Class RefineNewProductController.
  22.  */
  23. class RefineNewProductController extends AbstractController
  24. {
  25.     /**
  26.      * @var RefineNewProductConfigRepository
  27.      */
  28.     private $refineNewProductConfigRepository;
  29.     /**
  30.      * @var ProductRepository
  31.      */
  32.     private $productRepository;
  33.     /**
  34.      * RefineNewProductController constructor.
  35.      *
  36.      * @param RefineNewProductConfigRepository $refineNewProductConfigRepository
  37.      * @param ProductRepository $productRepository
  38.      * @throws \Exception
  39.      */
  40.     public function __construct(
  41.         RefineNewProductConfigRepository $refineNewProductConfigRepository,
  42.         ProductRepository $productRepository
  43.     ) {
  44.         $this->refineNewProductConfigRepository $refineNewProductConfigRepository;
  45.         $this->productRepository $productRepository;
  46.     }
  47.     /**
  48.      * 新着商品一覧
  49.      *
  50.      * @param Request $request
  51.      *
  52.      * @return array
  53.      *
  54.      * @Route("/block/refine_new_product", name="block_refine_new_product")
  55.      * @Template("Block/refine_new_product.twig")
  56.      */
  57.     public function index(Request $request)
  58.     {
  59.         /** @var RefineNewProductConfig $Config */
  60.         $Config $this->refineNewProductConfigRepository->get();
  61.         if (is_null($Config)) {
  62.             return [
  63.                 'Products' => null,
  64.             ];
  65.         }
  66.         // 何日までのものを表示するか
  67.         $elapsedDays $Config->getElapsedDays();
  68.         // 現時点で create_date が現在年月日から指定日数前以降のものを取得
  69.         $startDate = new \DateTime('now', new \DateTimeZone('UTC'));
  70.         $startDate->setTime(00);
  71.         $startDate->modify('-' . ($elapsedDays 1) . ' day');
  72.         // 最大で何件まで表示するか
  73.         $maxDispNum $Config->getMaxDispNum();
  74.         $qb $this->productRepository
  75.             ->createQueryBuilder('p')
  76.             ->andWhere('p.Status = 1')
  77.             ->andWhere('p.create_date > :create_date_start')
  78.             ->setParameter('create_date_start'$startDate)
  79.             ->orderBy('p.create_date''DESC')
  80.             ->setMaxResults($maxDispNum);
  81.         $Products $qb->getQuery()->getResult();
  82.         return [
  83.             'Products' => $Products,
  84.             'count' => count($Products),
  85.         ];
  86.     }
  87. }