app/Customize/Controller/TenderListController.php line 32

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 Customize\Controller;
  13. use Customize\Event\EccubeEvents;
  14. use Customize\Form\Type\SearchProductType;
  15. use Customize\Repository\BitHistoryRepository;
  16. use Customize\Repository\ProductRepository;
  17. use Customize\Repository\PreviewTenderRepository;
  18. use Customize\Repository\OrderRepository;
  19. use Eccube\Controller\AbstractController;
  20. use Eccube\Entity\Product;
  21. use Eccube\Event\EventArgs;
  22. use Eccube\Repository\CustomerFavoriteProductRepository;
  23. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  24. use Knp\Component\Pager\PaginatorInterface;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. class TenderListController extends AbstractController
  29. {
  30.     /**
  31.      * @var BitHistoryRepository
  32.      */
  33.     protected $bitHistoryRepository;
  34.     /**
  35.      * @var CustomerFavoriteProductRepository
  36.      */
  37.     protected $customerFavoriteProductRepository;
  38.     /**
  39.      * @var ProductRepository
  40.      */
  41.     protected $productRepository;
  42.     /**
  43.      * @var PreviewTenderRepository
  44.      */
  45.     protected $previewTenderRepository;
  46.     /**
  47.      * @var OrderRepository
  48.      */
  49.     protected $orderRepository;
  50.     /**
  51.      * SuccessfulBidController constructor.
  52.      */
  53.     public function __construct(
  54.         ProductRepository $productRepository,
  55.         PreviewTenderRepository $previewTenderRepository,
  56.         BitHistoryRepository $bitHistoryRepository,
  57.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  58.         OrderRepository $orderRepository,
  59.     ) {
  60.         $this->productRepository = $productRepository;
  61.         $this->previewTenderRepository = $previewTenderRepository;
  62.         $this->bitHistoryRepository = $bitHistoryRepository;
  63.         $this->customerFavoriteProductRepository = $customerFavoriteProductRepository;
  64.         $this->orderRepository = $orderRepository;
  65.     }
  66.     /**
  67.      * 入札中一覧画面.
  68.      *
  69.      * @Route("/tenders/list", name="tender_list", methods={"GET"})
  70.      *
  71.      * @Template("Product/tender_list.twig")
  72.      */
  73.     public function index(Request $request, PaginatorInterface $paginator)
  74.     {
  75.         if ($this->isGranted('ROLE_USER')) {
  76.             // handleRequestは空のqueryの場合は無視するため
  77.             if ($request->getMethod() === 'GET') {
  78.                 $request->query->set('pageno', $request->query->get('pageno', ''));
  79.                 $request->query->set('orderby', $request->query->get('orderby', 3));
  80.             }
  81.             // searchForm
  82.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  83.             $builder = $this->formFactory->createNamedBuilder('', SearchProductType::class);
  84.             $Customer = $this->getUser();
  85.             if ($request->getMethod() === 'GET') {
  86.                 $builder->setMethod('GET');
  87.             }
  88.             $event = new EventArgs(
  89.                 [
  90.                     'builder' => $builder,
  91.                 ],
  92.                 $request
  93.             );
  94.             $this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE);
  95.             /* @var $searchForm \Symfony\Component\Form\FormInterface */
  96.             $searchForm = $builder->getForm();
  97.             $searchForm->handleRequest($request);
  98.             // paginator
  99.             $searchData = $searchForm->getData();
  100.             $searchData['Customer'] = $Customer;
  101.             $qb = $this->productRepository->getQueryBuilderByTenderSearchData($searchData);
  102.             $event = new EventArgs(
  103.                 [
  104.                     'searchData' => $searchData,
  105.                     'qb' => $qb,
  106.                 ],
  107.                 $request
  108.             );
  109.             $this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH);
  110.             $searchData = $event->getArgument('searchData');
  111.             $query = $qb->getQuery()
  112.                 ->useResultCache(true, $this->eccubeConfig['eccube_result_cache_lifetime_short']);
  113.             /** @var SlidingPagination $pagination */
  114.             $pagination = $paginator->paginate(
  115.                 $query,
  116.                 ! empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  117.                 ! empty($searchData['disp_number']) ? $searchData['disp_number'] : 20
  118.             );
  119.             $isPreviews = [];
  120.             $isCloseds = [];
  121.             foreach ($pagination as $Product) {
  122.                 $Auction = $Product->getAuction();
  123.                 $isPreviews[$Product->getId()] = $Auction->isPreview();
  124.                 $isCloseds[$Product->getId()] = $Auction->isClosed();
  125.             }
  126.             $isPreviewIds = [];
  127.             $isClosedIds = [];
  128.             foreach ($isPreviews as $productId => $isPreview) {
  129.                 if ($isPreview) {
  130.                     $isPreviewIds[] = $productId;
  131.                 } else {
  132.                     $isClosedIds[] = $productId;
  133.                 }
  134.             }
  135.             $biddingCompaniesCounts = [];
  136.             if (!empty($isPreviewIds)) {
  137.                 $trueBiddingCompaniesResults = $this->productRepository->countBiddingCompaniesForProducts($isPreviewIds);
  138.                 foreach ($trueBiddingCompaniesResults as $result) {
  139.                     $biddingCompaniesCounts[$result['productId']] = $result['count'];
  140.                 }
  141.             }
  142.             if (!empty($isClosedIds)) {
  143.                 $falseBiddingCompaniesResults = $this->bitHistoryRepository->countBiddingCompaniesForProducts($isClosedIds);
  144.                 foreach ($falseBiddingCompaniesResults as $result) {
  145.                     $biddingCompaniesCounts[$result['productId']] = $result['count'];
  146.                 }
  147.             }
  148.             $totalPrice = $this->productRepository->getTotalTenderPrice($Customer, $searchData["auction"]);
  149.             $totalCount = count($pagination);
  150.             $productIds = [];
  151.             foreach ($pagination as $Product) {
  152.                 $productIds[] = $Product->getId();
  153.             }
  154.             $displayMetrics = $this->productRepository->getListDisplayMetrics($productIds, $Customer);
  155.             $isUnpaid = !empty($this->orderRepository->getNotPaidOrder($Customer)) ? true : false;
  156.             return [
  157.                 'subtitle' => '入札中一覧',
  158.                 'pagination' => $pagination,
  159.                 'search_form' => $searchForm->createView(),
  160.                 'Customer' => $Customer,
  161.                 'isPreviews' => $isPreviews,
  162.                 'isCloseds' => $isCloseds,
  163.                 'biddingCompaniesCounts' => $biddingCompaniesCounts,
  164.                 'totalPrice' => $totalPrice,
  165.                 'totalCount' => $totalCount,
  166.                 'isUnpaid' => $isUnpaid,
  167.                 'favoriteCounts' => $displayMetrics['favoriteCounts'],
  168.                 'accessCounts' => $displayMetrics['accessCounts'],
  169.                 'totalBidCounts' => $displayMetrics['totalBidCounts'],
  170.                 'productMemos' => $displayMetrics['memos'],
  171.                 'isFavoriteProducts' => $displayMetrics['isFavoriteProducts'],
  172.             ];
  173.         } else {
  174.             // 非会員の場合、ログイン画面を表示
  175.             return $this->redirectToRoute('mypage_login');
  176.         }
  177.     }
  178.     protected function getPageTitle($searchData)
  179.     {
  180.         if (isset($searchData['name']) && ! empty($searchData['name'])) {
  181.             return trans('front.auction.search_result');
  182.         } elseif (isset($searchData['category_id']) && $searchData['category_id']) {
  183.             return $searchData['category_id']->getName();
  184.         } else {
  185.             return trans('front.auction.all_auctions');
  186.         }
  187.     }
  188. }