app/Plugin/AddOnForCategory42/Controller/ProductControllerExtension.php line 146

Open in your IDE?
  1. <?php
  2. namespace Plugin\AddOnForCategory42\Controller;
  3. use Eccube\Controller\ProductController;
  4. use Eccube\Entity\BaseInfo;
  5. use Eccube\Entity\Master\ProductStatus;
  6. use Eccube\Entity\Product;
  7. use Eccube\Event\EccubeEvents;
  8. use Eccube\Event\EventArgs;
  9. use Eccube\Form\Type\AddCartType;
  10. use Eccube\Form\Type\SearchProductType;
  11. use Eccube\Repository\BaseInfoRepository;
  12. use Eccube\Repository\CustomerFavoriteProductRepository;
  13. use Eccube\Repository\Master\ProductListMaxRepository;
  14. use Eccube\Repository\ProductRepository;
  15. use Eccube\Service\CartService;
  16. use Eccube\Service\PurchaseFlow\PurchaseContext;
  17. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  18. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  26. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  27. use Eccube\Repository\CategoryRepository;
  28. use Plugin\AddOnForCategory42\Repository\AddOnForCategoryRepository;
  29. class ProductControllerExtension extends ProductController
  30. {
  31.     /**
  32.      * @var PurchaseFlow
  33.      */
  34.     protected $purchaseFlow;
  35.     /**
  36.      * @var CustomerFavoriteProductRepository
  37.      */
  38.     protected $customerFavoriteProductRepository;
  39.     /**
  40.      * @var CartService
  41.      */
  42.     protected $cartService;
  43.     /**
  44.      * @var ProductRepository
  45.      */
  46.     protected $productRepository;
  47.     /**
  48.      * @var BaseInfo
  49.      */
  50.     protected $BaseInfo;
  51.     /**
  52.      * @var AuthenticationUtils
  53.      */
  54.     protected $helper;
  55.     /**
  56.      * @var ProductListMaxRepository
  57.      */
  58.     protected $productListMaxRepository;
  59.     private $title '';
  60.     private $categoryRepository;
  61.     private $addOnForCategoryRepository;
  62.     /**
  63.      * ProductController constructor.
  64.      *
  65.      * @param PurchaseFlow $cartPurchaseFlow
  66.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  67.      * @param CartService $cartService
  68.      * @param ProductRepository $productRepository
  69.      * @param BaseInfoRepository $baseInfoRepository
  70.      * @param AuthenticationUtils $helper
  71.      * @param ProductListMaxRepository $productListMaxRepository
  72.      * @param CategoryRepository $categoryRepository
  73.      * @param AddOnForCategoryRepository $addOnForCategoryRepository
  74.      */
  75.     public function __construct(
  76.         PurchaseFlow $cartPurchaseFlow,
  77.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  78.         CartService $cartService,
  79.         ProductRepository $productRepository,
  80.         BaseInfoRepository $baseInfoRepository,
  81.         AuthenticationUtils $helper,
  82.         ProductListMaxRepository $productListMaxRepository,
  83.         CategoryRepository $categoryRepository,
  84.         AddOnForCategoryRepository $addOnForCategoryRepository
  85.     ) {
  86.         $this->purchaseFlow $cartPurchaseFlow;
  87.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  88.         $this->cartService $cartService;
  89.         $this->productRepository $productRepository;
  90.         $this->BaseInfo $baseInfoRepository->get();
  91.         $this->helper $helper;
  92.         $this->productListMaxRepository $productListMaxRepository;
  93.         $this->categoryRepository $categoryRepository;
  94.         $this->addOnForCategoryRepository $addOnForCategoryRepository;
  95.     }
  96.     /**
  97.      * 商品一覧画面.
  98.      *
  99.      * @Route("/products/list", name="product_list", methods={"GET"})
  100.      * @Template("Product/list.twig")
  101.      */
  102.     public function index(Request $requestPaginatorInterface $paginator)
  103.     {
  104.         // Doctrine SQLFilter
  105.         if ($this->BaseInfo->isOptionNostockHidden()) {
  106.             $this->entityManager->getFilters()->enable('option_nostock_hidden');
  107.         }
  108.         // handleRequestは空のqueryの場合は無視するため
  109.         if ($request->getMethod() === 'GET') {
  110.             $request->query->set('pageno'$request->query->get('pageno'''));
  111.         }
  112.         // searchForm
  113.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  114.         $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  115.         if ($request->getMethod() === 'GET') {
  116.             $builder->setMethod('GET');
  117.         }
  118.         $event = new EventArgs(
  119.             [
  120.                 'builder' => $builder,
  121.             ],
  122.             $request
  123.         );
  124.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE);
  125.         /* @var $searchForm \Symfony\Component\Form\FormInterface */
  126.         $searchForm $builder->getForm();
  127.         $searchForm->handleRequest($request);
  128.         // paginator
  129.         $searchData $searchForm->getData();
  130.         $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  131.         $event = new EventArgs(
  132.             [
  133.                 'searchData' => $searchData,
  134.                 'qb' => $qb,
  135.             ],
  136.             $request
  137.         );
  138.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_INDEX_SEARCH);
  139.         $searchData $event->getArgument('searchData');
  140.         // 表示するカテゴリーチェック
  141.         if(array_key_exists('category_id'$searchData)){
  142.             if($searchData['category_id']) {
  143.                 $Category $this->categoryRepository->find($searchData['category_id']);
  144.                 $AddOnForCategory $this->addOnForCategoryRepository->findOneBy(['Category' => $Category]);
  145.                 if($AddOnForCategory && $AddOnForCategory->getPrivateFlg() == true) {
  146.                     return $this->redirectToRoute('campaign_product_list', ['category_id' => $Category->getId()]);
  147.                     exit;
  148.                 }
  149.             }
  150.         }
  151.         $query $qb->getQuery()
  152.             ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  153.         /** @var SlidingPagination $pagination */
  154.         $pagination $paginator->paginate(
  155.             $query,
  156.             !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  157.             !empty($searchData['disp_number']) ? $searchData['disp_number']->getId() : $this->productListMaxRepository->findOneBy([], ['sort_no' => 'ASC'])->getId()
  158.         );
  159.         $ids = [];
  160.         foreach ($pagination as $Product) {
  161.             $ids[] = $Product->getId();
  162.         }
  163.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  164.         // addCart form
  165.         $forms = [];
  166.         foreach ($pagination as $Product) {
  167.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  168.             $builder $this->formFactory->createNamedBuilder(
  169.                 '',
  170.                 AddCartType::class,
  171.                 null,
  172.                 [
  173.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  174.                     'allow_extra_fields' => true,
  175.                 ]
  176.             );
  177.             $addCartForm $builder->getForm();
  178.             $forms[$Product->getId()] = $addCartForm->createView();
  179.         }
  180.         $Category $searchForm->get('category_id')->getData();
  181.         return [
  182.             'subtitle' => $this->getPageTitle($searchData),
  183.             'pagination' => $pagination,
  184.             'search_form' => $searchForm->createView(),
  185.             'forms' => $forms,
  186.             'Category' => $Category,
  187.         ];
  188.     }
  189.     /**
  190.      * 商品詳細画面.
  191.      *
  192.      * @Route("/products/detail/{id}", name="product_detail", methods={"GET"}, requirements={"id" = "\d+"})
  193.      * @Template("Product/detail.twig")
  194.      * @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
  195.      *
  196.      * @param Request $request
  197.      * @param Product $Product
  198.      *
  199.      * @return array
  200.      */
  201.     public function detail(Request $requestProduct $Product)
  202.     {
  203.         if (!$this->checkVisibility($Product)) {
  204.             throw new NotFoundHttpException();
  205.         }
  206.         if ($Product->getProductCategories()){
  207.             foreach($Product->getProductCategories() as $ProductCategory) {
  208.                 if($ProductCategory->getCategory()->getAddOnForCategory()){
  209.                     if($ProductCategory->getCategory()->getAddOnForCategory()->getPrivateFlg() == true){
  210.                         return $this->redirectToRoute('campaign_product_detail', ['id' => $Product->getId()]);
  211.                         exit;
  212.                     }
  213.                 }
  214.             }
  215.         }
  216.         $builder $this->formFactory->createNamedBuilder(
  217.             '',
  218.             AddCartType::class,
  219.             null,
  220.             [
  221.                 'product' => $Product,
  222.                 'id_add_product_id' => false,
  223.             ]
  224.         );
  225.         $event = new EventArgs(
  226.             [
  227.                 'builder' => $builder,
  228.                 'Product' => $Product,
  229.             ],
  230.             $request
  231.         );
  232.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE);
  233.         $is_favorite false;
  234.         if ($this->isGranted('ROLE_USER')) {
  235.             $Customer $this->getUser();
  236.             $is_favorite $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  237.         }
  238.         return [
  239.             'title' => $this->title,
  240.             'subtitle' => $Product->getName(),
  241.             'form' => $builder->getForm()->createView(),
  242.             'Product' => $Product,
  243.             'is_favorite' => $is_favorite,
  244.         ];
  245.     }
  246. }