app/Plugin/CategoryRecommend4/Controller/CategoryRecommendSearchModelController.php line 63

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 Plugin\CategoryRecommend4\Controller;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Eccube\Controller\AbstractController;
  15. use Eccube\Repository\CategoryRepository;
  16. use Eccube\Repository\ProductRepository;
  17. use Knp\Component\Pager\Paginator;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. /**
  22.  * Class CategoryRecommendSearchModelController.
  23.  */
  24. class CategoryRecommendSearchModelController extends AbstractController
  25. {
  26.     /**
  27.      * @var CategoryRepository
  28.      */
  29.     private $categoryRepository;
  30.     /**
  31.      * @var ProductRepository
  32.      */
  33.     private $productRepository;
  34.     /**
  35.      * CategoryRecommendSearchModelController constructor.
  36.      *
  37.      * @param CategoryRepository $categoryRepository
  38.      * @param ProductRepository $productRepository
  39.      */
  40.     public function __construct(CategoryRepository $categoryRepositoryProductRepository $productRepository)
  41.     {
  42.         $this->categoryRepository $categoryRepository;
  43.         $this->productRepository $productRepository;
  44.     }
  45.     /**
  46.      * 商品検索画面を表示する.
  47.      *
  48.      * @param Request     $request
  49.      * @param int         $page_no
  50.      *
  51.      * @return array
  52.      * @Route("/%eccube_admin_route%/product/category/recommend/search/product", name="plugin_category_recommend_search_product")
  53.      * @Route("/%eccube_admin_route%/product/category/recommend/search/product/page/{page_no}", requirements={"page_no" = "\d+"}, name="plugin_category_recommend_search_product_page")
  54.      * @Template("@CategoryRecommend4/admin/search_product.twig")
  55.      */
  56.     public function searchProduct(Request $request$page_no nullPaginator $paginator)
  57.     {
  58.         if (!$request->isXmlHttpRequest()) {
  59.             return [];
  60.         }
  61.         log_debug('Search product start.');
  62.         $pageCount $this->eccubeConfig['eccube_default_page_count'];
  63.         $session $this->session;
  64.         if ('POST' === $request->getMethod()) {
  65.             $page_no 1;
  66.             $searchData = [
  67.                 'name' => trim($request->get('id')),
  68.             ];
  69.             if ($categoryId $request->get('category_id')) {
  70.                 $searchData['category_id'] = $categoryId;
  71.             }
  72.             $session->set('eccube.plugin.recommend.product.search'$searchData);
  73.             $session->set('eccube.plugin.recommend.product.search.page_no'$page_no);
  74.         } else {
  75.             $searchData = (array) $session->get('eccube.plugin.recommend.product.search');
  76.             if (is_null($page_no)) {
  77.                 $page_no intval($session->get('eccube.plugin.recommend.product.search.page_no'));
  78.             } else {
  79.                 $session->set('eccube.plugin.recommend.product.search.page_no'$page_no);
  80.             }
  81.         }
  82.         //set parameter
  83.         $searchData['id'] = $searchData['name'];
  84.         if (!empty($searchData['category_id'])) {
  85.             $searchData['category_id'] = $this->categoryRepository->find($searchData['category_id']);
  86.         }
  87.         $qb $this->productRepository->getQueryBuilderBySearchDataForAdmin($searchData);
  88.         /** @var \Knp\Component\Pager\Pagination\SlidingPagination $pagination */
  89.         $pagination $paginator->paginate(
  90.             $qb,
  91.             $page_no,
  92.             $pageCount,
  93.             ['wrap-queries' => true]
  94.         );
  95.         /** @var ArrayCollection */
  96.         $arrProduct $pagination->getItems();
  97.         log_debug('Search product finish.');
  98.         if (count($arrProduct) == 0) {
  99.             log_debug('Search product not found.');
  100.         }
  101.         return [
  102.             'pagination' => $pagination,
  103.         ];
  104.     }
  105. }