app/Plugin/CustomProductSearch/Controller/CustomProductSearchController.php line 62

Open in your IDE?
  1. <?php
  2. namespace Plugin\CustomProductSearch\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Repository\ProductRepository;
  5. use Eccube\Repository\CategoryRepository;
  6. use Plugin\CustomProductSearch\Form\Type\SearchProductType;
  7. use Plugin\CustomProductSearch\Service\ProductSearchService;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use Eccube\Form\Type\AddCartType;
  13. use Eccube\Repository\Master\ProductListOrderByRepository;
  14. class CustomProductSearchController extends AbstractController
  15. {
  16.     /**
  17.      * @var ProductRepository
  18.      */
  19.     private $productRepository;
  20.     /**
  21.      * @var CategoryRepository
  22.      */
  23.     private $categoryRepository;
  24.     /**
  25.      * @var PaginatorInterface
  26.      */
  27.     private $paginator;
  28.     /**
  29.      * @var ProductListOrderByRepository
  30.      */
  31.     private $productListOrderByRepository;
  32.     /**
  33.      * @var ProductSearchService
  34.      */
  35.     private $productSearchService;
  36.     public function __construct(
  37.         ProductRepository $productRepository,
  38.         CategoryRepository $categoryRepository,
  39.         ProductListOrderByRepository $productListOrderByRepository,
  40.         PaginatorInterface $paginator,
  41.         ProductSearchService $productSearchService
  42.     ) {
  43.         $this->productRepository $productRepository;
  44.         $this->categoryRepository $categoryRepository;
  45.         $this->productListOrderByRepository $productListOrderByRepository;
  46.         $this->paginator $paginator;
  47.         $this->productSearchService $productSearchService;
  48.     }
  49.     /**
  50.      * @Route("/products/search", name="custom_product_search")
  51.      */
  52.     public function search(Request $request)
  53.     {
  54.         // Create search form
  55.         $searchForm $this->createForm(SearchProductType::class);
  56.         $searchForm->handleRequest($request);
  57.         // Initialize query builder
  58.         $ProductListOrder $this->productListOrderByRepository->find($this->eccubeConfig['eccube_product_order_newer']);
  59.         $qb $this->productRepository->getQueryBuilderBySearchData(['orderby' => $ProductListOrder]);
  60.         // Process search logic if form is submitted
  61.         $category = [];
  62.         $category17 = [];
  63.         $category19 = [];
  64.         if ($searchForm->isSubmitted() && $searchForm->isValid()) {
  65.             $searchData $searchForm->getData();
  66.             $qb $this->productSearchService->searchProducts($searchData$qb);
  67.             if (!empty($searchData['category']) || !empty($searchData['category_17']) || !empty($searchData['category_19'])) {
  68.                 if (!empty($searchData['category'])) {
  69.                     $category $searchData['category'];
  70.                 }
  71.                 if (!empty($searchData['category_17'])) {
  72.                     $category17 $searchData['category_17'];
  73.                 }
  74.                 if (!empty($searchData['category_19'])) {
  75.                     $category19 $searchData['category_19'];
  76.                 }
  77.             }
  78.         }
  79.         // Paginate results
  80.         $page $request->query->getInt('page'1);
  81.         $pageSize 20// Number of products per page
  82.         //var_dump($qb->getQuery()->getSQL());exit;
  83.         $pagination $this->paginator->paginate(
  84.             $qb->getQuery(),
  85.             $page,
  86.             $pageSize
  87.         );
  88.         // Get categories to display
  89.         $categories $this->categoryRepository->findAll();
  90.         $ids = [];
  91.         foreach ($pagination as $Product) {
  92.             $ids[] = $Product->getId();
  93.         }
  94.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  95.         // AddCart form
  96.         $forms = [];
  97.         foreach ($pagination as $Product) {
  98.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  99.             $builder $this->formFactory->createNamedBuilder(
  100.                 '',
  101.                 AddCartType::class,
  102.                 null,
  103.                 [
  104.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  105.                     'allow_extra_fields' => true,
  106.                 ]
  107.             );
  108.             $addCartForm $builder->getForm();
  109.             $forms[$Product->getId()] = $addCartForm->createView();
  110.         }
  111.         return $this->render('@CustomProductSearch/default/Product/search_result.twig', [
  112.             'searchForm' => $searchForm->createView(),
  113.             'pagination' => $pagination,
  114.             'form' => $searchForm->createView(),
  115.             'forms' => $forms,
  116.             'categories' => $categories,
  117.             'category' => $category,
  118.             'category17' => $category17,
  119.             'category19' => $category19,
  120.             'page_title' => '製品検索結果',
  121.         ]);
  122.     }
  123.     /**
  124.      * @Route("/block/custom_search_product", name="block_custom_search_product", methods={"GET"})
  125.      * @Route("/block/custom_search_product_sp", name="block_custom_search_product_sp", methods={"GET"})
  126.      * @Template("Block/custom_search_product.twig")
  127.      */
  128.     // public function index(Request $request)
  129.     // {
  130.     //     // Create search form
  131.     //     $searchForm = $this->createForm(SearchProductType::class);
  132.     //     $searchForm->handleRequest($request);
  133.     //     // Initialize query builder
  134.     //     $ProductListOrder = $this->productListOrderByRepository->find($this->eccubeConfig['eccube_product_order_newer']);
  135.     //     $qb = $this->productRepository->getQueryBuilderBySearchData(['orderby' => $ProductListOrder]);
  136.     //     // Process search logic if form is submitted
  137.     //     $category = [];
  138.     //     $category17 = [];
  139.     //     $category19 = [];
  140.     //     if ($searchForm->isSubmitted() && $searchForm->isValid()) {
  141.     //         $searchData = $searchForm->getData();
  142.     //         $qb = $this->productSearchService->searchProducts($searchData, $qb);
  143.     //         if (!empty($searchData['category']) || !empty($searchData['category_17']) || !empty($searchData['category_19'])) {
  144.     //             if (!empty($searchData['category'])) {
  145.     //                 $category = $searchData['category'];
  146.     //             }
  147.     //             if (!empty($searchData['category_17'])) {
  148.     //                 $category17 = $searchData['category_17'];
  149.     //             }
  150.     //             if (!empty($searchData['category_19'])) {
  151.     //                 $category19 = $searchData['category_19'];
  152.     //             }
  153.     //         }
  154.     //     }
  155.     //     // Paginate results
  156.     //     $page = $request->query->getInt('page', 1);
  157.     //     $pageSize = 20; // Number of products per page
  158.     //     //var_dump($qb->getQuery()->getSQL());exit;
  159.     //     $pagination = $this->paginator->paginate(
  160.     //         $qb->getQuery(),
  161.     //         $page,
  162.     //         $pageSize
  163.     //     );
  164.     //     // Get categories to display
  165.     //     $categories = $this->categoryRepository->findAll();
  166.     //     $ids = [];
  167.     //     foreach ($pagination as $Product) {
  168.     //         $ids[] = $Product->getId();
  169.     //     }
  170.     //     $ProductsAndClassCategories = $this->productRepository->findProductsWithSortedClassCategories($ids, 'p.id');
  171.     //     // AddCart form
  172.     //     $forms = [];
  173.     //     foreach ($pagination as $Product) {
  174.     //         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  175.     //         $builder = $this->formFactory->createNamedBuilder(
  176.     //             '',
  177.     //             AddCartType::class,
  178.     //             null,
  179.     //             [
  180.     //                 'product' => $ProductsAndClassCategories[$Product->getId()],
  181.     //                 'allow_extra_fields' => true,
  182.     //             ]
  183.     //         );
  184.     //         $addCartForm = $builder->getForm();
  185.     //         $forms[$Product->getId()] = $addCartForm->createView();
  186.     //     }
  187.     //     // return $this->render('@CustomProductSearch/default/Product/search_result.twig', [
  188.     //     //     'searchForm' => $searchForm->createView(),
  189.     //     //     'pagination' => $pagination,
  190.     //     //     'form' => $searchForm->createView(),
  191.     //     //     'forms' => $forms,
  192.     //     //     'categories' => $categories,
  193.     //     //     'category' => $category,
  194.     //     //     'category17' => $category17,
  195.     //     //     'category19' => $category19,
  196.     //     //     'page_title' => 'Product Search Results',
  197.     //     // ]);
  198.     //     return [
  199.     //         'form' => $searchForm->createView(),
  200.     //     ];
  201.     // }
  202. }