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

Open in your IDE?
  1. <?php
  2. namespace Plugin\ContactManagement42\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\ContactManagement42\Form\Type\SearchProductType;
  15. use Plugin\ContactManagement42\Repository\ContactRepository;
  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 ContactRepository $contactRepository;
  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.         ContactRepository $contactRepository
  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->contactRepository $contactRepository;
  81.     }
  82.     
  83.     /**
  84.      * 商品一覧画面.
  85.      *
  86.      * @Route("/products/contact", name="product_contact", methods={"GET"})
  87.      * @Template("@ContactManagement42/Product/contact.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->contactRepository->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.         $Category $searchForm->get('category_id')->getData();
  136.         return [
  137.             'subtitle' => $this->getPageTitle($searchData),
  138.             'pagination' => $pagination,
  139.             'search_form' => $searchForm->createView(),
  140.             'Category' => $Category,
  141.         ];
  142.     }
  143.     
  144.     /**
  145.      * ページタイトルの設定
  146.      *
  147.      * @param  array|null $searchData
  148.      *
  149.      * @return str
  150.      */
  151.     protected function getPageTitle($searchData)
  152.     {
  153.         if (isset($searchData['name']) && !empty($searchData['name'])) {
  154.             return trans('front.product.search_result');
  155.         } elseif (isset($searchData['category_id']) && $searchData['category_id']) {
  156.             return $searchData['category_id']->getName();
  157.         } else {
  158.             return trans('front.product.all_products');
  159.         }
  160.     }
  161. }