src/Eccube/Controller/TopController.php line 156

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\Entity\BaseInfo;
  14. use Eccube\Entity\Category;
  15. use Eccube\Entity\Master\ProductStatus;
  16. use Eccube\Entity\Product;
  17. use Eccube\Event\EccubeEvents;
  18. use Eccube\Event\EventArgs;
  19. use Eccube\Form\Type\AddCartType;
  20. use Eccube\Form\Type\SearchProductType;
  21. use Eccube\Repository\BaseInfoRepository;
  22. use Eccube\Repository\CategoryRepository;
  23. use Eccube\Repository\CustomerFavoriteProductRepository;
  24. use Eccube\Repository\Master\ProductListMaxRepository;
  25. use Eccube\Repository\ProductRepository;
  26. use Eccube\Service\CartService;
  27. use Eccube\Service\PurchaseFlow\PurchaseContext;
  28. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  29. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  30. use Knp\Component\Pager\PaginatorInterface;
  31. use Plugin\ProductReview42\Entity\ProductReview;
  32. use Plugin\ProductReview42\Repository\ProductReviewRepository;
  33. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  34. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  35. use Symfony\Component\HttpFoundation\JsonResponse;
  36. use Symfony\Component\HttpFoundation\Request;
  37. use Symfony\Component\HttpFoundation\Response;
  38. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  39. use Symfony\Component\Routing\Annotation\Route;
  40. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  41. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  42. class TopController extends AbstractController
  43. {
  44.     /**
  45.      * @var PurchaseFlow
  46.      */
  47.     protected $purchaseFlow;
  48.     /**
  49.      * @var CustomerFavoriteProductRepository
  50.      */
  51.     protected $customerFavoriteProductRepository;
  52.     /**
  53.      * @var CartService
  54.      */
  55.     protected $cartService;
  56.     /**
  57.      * @var ProductRepository
  58.      */
  59.     protected $productRepository;
  60.     /**
  61.      * @var BaseInfo
  62.      */
  63.     protected $BaseInfo;
  64.     /**
  65.      * @var AuthenticationUtils
  66.      */
  67.     protected $helper;
  68.     /**
  69.      * @var ProductListMaxRepository
  70.      */
  71.     protected $productListMaxRepository;
  72.     private $title '';
  73.     private CategoryRepository $categoryRepository;
  74.     /**
  75.      * ProductController constructor.
  76.      *
  77.      * @param PurchaseFlow $cartPurchaseFlow
  78.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  79.      * @param CartService $cartService
  80.      * @param ProductRepository $productRepository
  81.      * @param BaseInfoRepository $baseInfoRepository
  82.      * @param AuthenticationUtils $helper
  83.      * @param ProductListMaxRepository $productListMaxRepository
  84.      * @param ProductReviewRepository $productReviewRepository
  85.      * @param CategoryRepository $categoryRepository
  86.      */
  87.     public function __construct(
  88.         PurchaseFlow                      $cartPurchaseFlow,
  89.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  90.         CartService                       $cartService,
  91.         ProductRepository                 $productRepository,
  92.         BaseInfoRepository                $baseInfoRepository,
  93.         AuthenticationUtils               $helper,
  94.         ProductListMaxRepository          $productListMaxRepository,
  95.         ProductReviewRepository           $productReviewRepositoryCategoryRepository $categoryRepository
  96.     )
  97.     {
  98.         $this->purchaseFlow $cartPurchaseFlow;
  99.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  100.         $this->cartService $cartService;
  101.         $this->productRepository $productRepository;
  102.         $this->BaseInfo $baseInfoRepository->get();
  103.         $this->helper $helper;
  104.         $this->productListMaxRepository $productListMaxRepository;
  105.         $this->productReviewRepository $productReviewRepository;
  106.         $this->categoryRepository $categoryRepository;
  107.     }
  108.     /**
  109.      * @Route("/", name="homepage", methods={"GET"})
  110.      * @Template("index.twig")
  111.      */
  112.     public function index(Request $requestPaginatorInterface $paginator)
  113.     {
  114.         // Doctrine SQLFilter
  115.         if ($this->BaseInfo->isOptionNostockHidden()) {
  116.             $this->entityManager->getFilters()->enable('option_nostock_hidden');
  117.         }
  118.         // handleRequestは空のqueryの場合は無視するため
  119.         if ($request->getMethod() === 'GET') {
  120.             $request->query->set('pageno'$request->query->get('pageno'''));
  121.         }
  122.         // searchForm
  123.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  124.         $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  125.         if ($request->getMethod() === 'GET') {
  126.             $builder->setMethod('GET');
  127.         }
  128.         $event = new EventArgs(
  129.             [
  130.                 'builder' => $builder,
  131.             ],
  132.             $request
  133.         );
  134.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE);
  135.         /* @var $searchForm \Symfony\Component\Form\FormInterface */
  136.         $searchForm $builder->getForm();
  137.         $searchForm->handleRequest($request);
  138.         // paginator
  139.         $searchData $searchForm->getData();
  140.         $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  141.         $event = new EventArgs(
  142.             [
  143.                 'searchData' => $searchData,
  144.                 'qb' => $qb,
  145.             ],
  146.             $request
  147.         );
  148.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_SEARCH);
  149.         $searchData $event->getArgument('searchData');
  150.         $query $qb->getQuery()
  151.             ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  152.         /** @var SlidingPagination $pagination */
  153.         $pagination $paginator->paginate($query110);
  154.         $ids = [];
  155.         foreach ($pagination as $key => $Product) {
  156.             $ids[] = $Product->getId();
  157.             $pagination->getItems()[$key]->review $Product->getProductReviewStar();
  158.             $pagination->getItems()[$key]->count $Product->getProductReviewCount();
  159.         }
  160.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  161.         // addCart form
  162.         $forms = [];
  163.         foreach ($pagination as $Product) {
  164.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  165.             $builder $this->formFactory->createNamedBuilder(
  166.                 '',
  167.                 AddCartType::class,
  168.                 null,
  169.                 [
  170.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  171.                     'allow_extra_fields' => true,
  172.                 ]
  173.             );
  174.             $addCartForm $builder->getForm();
  175.             $forms[$Product->getId()] = $addCartForm->createView();
  176.         }
  177.         $Category $searchForm->get('category_id')->getData();
  178.         return [
  179.             'subtitle' => 'subtitle',
  180.             'pagination' => $pagination,
  181.             'search_form' => $searchForm->createView(),
  182.             'forms' => $forms,
  183.             'Category' => $Category,
  184.         ];
  185.     }
  186.     /**
  187.      * カテゴリ検索画面を表示する.
  188.      *
  189.      * @param Request $request
  190.      *
  191.      * @return Response
  192.      * @Route("/search_category", name="search_category")
  193.      */
  194.     public function searchCategory(Request $request)
  195.     {
  196.         if ($request->isXmlHttpRequest()) {
  197.             $categoryId $request->get('category_id');
  198.             if (empty($categoryId)) {
  199.                 $categoryId 0;
  200.             }
  201.             $Category $this->categoryRepository->find($categoryId);
  202.             $Categories $this->categoryRepository->getListArray($Category);
  203.             return new JsonResponse($Categories);
  204.         }
  205.         return new Response();
  206.     }
  207. }