app/Customize/Controller/CustomizeTopController.php line 118

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