app/Plugin/Recommend42/Controller/ProductController.php line 101

Open in your IDE?
  1. <?php
  2. namespace Plugin\Recommend42\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Event\EccubeEvents;
  5. use Eccube\Event\EventArgs;
  6. use Eccube\Form\Type\AddCartType;
  7. use Eccube\Repository\BaseInfoRepository;
  8. use Eccube\Repository\CustomerFavoriteProductRepository;
  9. use Eccube\Repository\Master\ProductListMaxRepository;
  10. use Eccube\Repository\ProductRepository;
  11. use Eccube\Service\CartService;
  12. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  13. use Knp\Component\Pager\PaginatorInterface;
  14. use Plugin\Recommend42\Form\Type\SearchProductType;
  15. use Plugin\Recommend42\Repository\RecommendProductRepository;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  20. class ProductController extends AbstractController
  21. {
  22.         /**
  23.      * @var PurchaseFlow
  24.      */
  25.     protected $purchaseFlow;
  26.     /**
  27.      * @var CustomerFavoriteProductRepository
  28.      */
  29.     protected $customerFavoriteProductRepository;
  30.     /**
  31.      * @var CartService
  32.      */
  33.     protected $cartService;
  34.     /**
  35.      * @var ProductRepository
  36.      */
  37.     protected $productRepository;
  38.     /**
  39.      * @var BaseInfo
  40.      */
  41.     protected $BaseInfo;
  42.     /**
  43.      * @var AuthenticationUtils
  44.      */
  45.     protected $helper;
  46.     /**
  47.      * @var ProductListMaxRepository
  48.      */
  49.     protected $productListMaxRepository;
  50.     protected RecommendProductRepository $recommendProductRepository;
  51.     private $title '';
  52.     /**
  53.      * ProductController constructor.
  54.      *
  55.      * @param PurchaseFlow $cartPurchaseFlow
  56.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  57.      * @param CartService $cartService
  58.      * @param ProductRepository $productRepository
  59.      * @param BaseInfoRepository $baseInfoRepository
  60.      * @param AuthenticationUtils $helper
  61.      * @param ProductListMaxRepository $productListMaxRepository
  62.      */
  63.     public function __construct(
  64.         PurchaseFlow $cartPurchaseFlow,
  65.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  66.         CartService $cartService,
  67.         ProductRepository $productRepository,
  68.         BaseInfoRepository $baseInfoRepository,
  69.         AuthenticationUtils $helper,
  70.         ProductListMaxRepository $productListMaxRepository,
  71.         RecommendProductRepository $recommendProductRepository
  72.     ) {
  73.         $this->purchaseFlow $cartPurchaseFlow;
  74.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  75.         $this->cartService $cartService;
  76.         $this->productRepository $productRepository;
  77.         $this->BaseInfo $baseInfoRepository->get();
  78.         $this->helper $helper;
  79.         $this->productListMaxRepository $productListMaxRepository;
  80.         $this->recommendProductRepository $recommendProductRepository;
  81.     }
  82.     
  83.     /**
  84.      * 商品一覧画面.
  85.      *
  86.      * @Route("/products/recommend", name="product_recommend", methods={"GET"})
  87.      * @Template("@Recommend42/Product/list.twig")
  88.      */
  89.     public function index(Request $requestPaginatorInterface $paginator)
  90.     {
  91.         // Doctrine SQLFilter
  92.         if ($this->BaseInfo->isOptionNostockHidden()) {
  93.             $this->entityManager->getFilters()->enable('option_nostock_hidden');
  94.         }
  95.         // handleRequestは空のqueryの場合は無視するため
  96.         if ($request->getMethod() === 'GET') {
  97.             $request->query->set('pageno'$request->query->get('pageno'''));
  98.         }
  99.         // searchForm
  100.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  101.         $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  102.         if ($request->getMethod() === 'GET') {
  103.             $builder->setMethod('GET');
  104.         }
  105.         $event = new EventArgs(
  106.             [
  107.                 'builder' => $builder,
  108.             ],
  109.             $request
  110.         );
  111.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE);
  112.         /* @var $searchForm \Symfony\Component\Form\FormInterface */
  113.         $searchForm $builder->getForm();
  114.         $searchForm->handleRequest($request);
  115.         // paginator
  116.         $searchData $searchForm->getData();
  117.         $qb $this->recommendProductRepository->getQueryBuilderBySearchData($searchData);
  118.         $event = new EventArgs(
  119.             [
  120.                 'searchData' => $searchData,
  121.                 'qb' => $qb,
  122.             ],
  123.             $request
  124.         );
  125.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_SEARCH);
  126.         $searchData $event->getArgument('searchData');
  127.         $query $qb->getQuery()
  128.             ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  129.         /** @var SlidingPagination $pagination */
  130.         $pagination $paginator->paginate(
  131.             $query,
  132.             !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  133.             !empty($searchData['disp_number']) ? $searchData['disp_number']->getId() : $this->productListMaxRepository->findOneBy([], ['sort_no' => 'ASC'])->getId()
  134.         );
  135.         $ids = [];
  136.         foreach ($pagination as $Product) {
  137.             $ids[] = $Product->getId();
  138.         }
  139.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  140.         // addCart form
  141.         $forms = [];
  142.         foreach ($pagination as $Product) {
  143.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  144.             $builder $this->formFactory->createNamedBuilder(
  145.                 '',
  146.                 AddCartType::class,
  147.                 null,
  148.                 [
  149.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  150.                     'allow_extra_fields' => true,
  151.                 ]
  152.             );
  153.             $addCartForm $builder->getForm();
  154.             $forms[$Product->getId()] = $addCartForm->createView();
  155.         }
  156.         $Category $searchForm->get('category_id')->getData();
  157.         return [
  158.             'subtitle' => $this->getPageTitle($searchData),
  159.             'pagination' => $pagination,
  160.             'search_form' => $searchForm->createView(),
  161.             'forms' => $forms,
  162.             'Category' => $Category,
  163.         ];
  164.     }
  165.     
  166.     /**
  167.      * ページタイトルの設定
  168.      *
  169.      * @param  array|null $searchData
  170.      *
  171.      * @return str
  172.      */
  173.     protected function getPageTitle($searchData)
  174.     {
  175.         if (isset($searchData['name']) && !empty($searchData['name'])) {
  176.             return trans('front.product.search_result');
  177.         } elseif (isset($searchData['category_id']) && $searchData['category_id']) {
  178.             return $searchData['category_id']->getName();
  179.         } else {
  180.             return trans('front.product.all_products');
  181.         }
  182.     }
  183. }