<?php
namespace Plugin\CustomProductSearch\Controller;
use Eccube\Controller\AbstractController;
use Eccube\Repository\ProductRepository;
use Eccube\Repository\CategoryRepository;
use Plugin\CustomProductSearch\Form\Type\SearchProductType;
use Plugin\CustomProductSearch\Service\ProductSearchService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Component\Pager\PaginatorInterface;
use Eccube\Form\Type\AddCartType;
use Eccube\Repository\Master\ProductListOrderByRepository;
class CustomProductSearchController extends AbstractController
{
/**
* @var ProductRepository
*/
private $productRepository;
/**
* @var CategoryRepository
*/
private $categoryRepository;
/**
* @var PaginatorInterface
*/
private $paginator;
/**
* @var ProductListOrderByRepository
*/
private $productListOrderByRepository;
/**
* @var ProductSearchService
*/
private $productSearchService;
public function __construct(
ProductRepository $productRepository,
CategoryRepository $categoryRepository,
ProductListOrderByRepository $productListOrderByRepository,
PaginatorInterface $paginator,
ProductSearchService $productSearchService
) {
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->productListOrderByRepository = $productListOrderByRepository;
$this->paginator = $paginator;
$this->productSearchService = $productSearchService;
}
/**
* @Route("/products/search", name="custom_product_search")
*/
public function search(Request $request)
{
// Create search form
$searchForm = $this->createForm(SearchProductType::class);
$searchForm->handleRequest($request);
// Initialize query builder
$ProductListOrder = $this->productListOrderByRepository->find($this->eccubeConfig['eccube_product_order_newer']);
$qb = $this->productRepository->getQueryBuilderBySearchData(['orderby' => $ProductListOrder]);
// Process search logic if form is submitted
$category = [];
$category17 = [];
$category19 = [];
if ($searchForm->isSubmitted() && $searchForm->isValid()) {
$searchData = $searchForm->getData();
$qb = $this->productSearchService->searchProducts($searchData, $qb);
if (!empty($searchData['category']) || !empty($searchData['category_17']) || !empty($searchData['category_19'])) {
if (!empty($searchData['category'])) {
$category = $searchData['category'];
}
if (!empty($searchData['category_17'])) {
$category17 = $searchData['category_17'];
}
if (!empty($searchData['category_19'])) {
$category19 = $searchData['category_19'];
}
}
}
// Paginate results
$page = $request->query->getInt('page', 1);
$pageSize = 20; // Number of products per page
//var_dump($qb->getQuery()->getSQL());exit;
$pagination = $this->paginator->paginate(
$qb->getQuery(),
$page,
$pageSize
);
// Get categories to display
$categories = $this->categoryRepository->findAll();
$ids = [];
foreach ($pagination as $Product) {
$ids[] = $Product->getId();
}
$ProductsAndClassCategories = $this->productRepository->findProductsWithSortedClassCategories($ids, 'p.id');
// AddCart form
$forms = [];
foreach ($pagination as $Product) {
/* @var $builder \Symfony\Component\Form\FormBuilderInterface */
$builder = $this->formFactory->createNamedBuilder(
'',
AddCartType::class,
null,
[
'product' => $ProductsAndClassCategories[$Product->getId()],
'allow_extra_fields' => true,
]
);
$addCartForm = $builder->getForm();
$forms[$Product->getId()] = $addCartForm->createView();
}
return $this->render('@CustomProductSearch/default/Product/search_result.twig', [
'searchForm' => $searchForm->createView(),
'pagination' => $pagination,
'form' => $searchForm->createView(),
'forms' => $forms,
'categories' => $categories,
'category' => $category,
'category17' => $category17,
'category19' => $category19,
'page_title' => '製品検索結果',
]);
}
/**
* @Route("/block/custom_search_product", name="block_custom_search_product", methods={"GET"})
* @Route("/block/custom_search_product_sp", name="block_custom_search_product_sp", methods={"GET"})
* @Template("Block/custom_search_product.twig")
*/
// public function index(Request $request)
// {
// // Create search form
// $searchForm = $this->createForm(SearchProductType::class);
// $searchForm->handleRequest($request);
// // Initialize query builder
// $ProductListOrder = $this->productListOrderByRepository->find($this->eccubeConfig['eccube_product_order_newer']);
// $qb = $this->productRepository->getQueryBuilderBySearchData(['orderby' => $ProductListOrder]);
// // Process search logic if form is submitted
// $category = [];
// $category17 = [];
// $category19 = [];
// if ($searchForm->isSubmitted() && $searchForm->isValid()) {
// $searchData = $searchForm->getData();
// $qb = $this->productSearchService->searchProducts($searchData, $qb);
// if (!empty($searchData['category']) || !empty($searchData['category_17']) || !empty($searchData['category_19'])) {
// if (!empty($searchData['category'])) {
// $category = $searchData['category'];
// }
// if (!empty($searchData['category_17'])) {
// $category17 = $searchData['category_17'];
// }
// if (!empty($searchData['category_19'])) {
// $category19 = $searchData['category_19'];
// }
// }
// }
// // Paginate results
// $page = $request->query->getInt('page', 1);
// $pageSize = 20; // Number of products per page
// //var_dump($qb->getQuery()->getSQL());exit;
// $pagination = $this->paginator->paginate(
// $qb->getQuery(),
// $page,
// $pageSize
// );
// // Get categories to display
// $categories = $this->categoryRepository->findAll();
// $ids = [];
// foreach ($pagination as $Product) {
// $ids[] = $Product->getId();
// }
// $ProductsAndClassCategories = $this->productRepository->findProductsWithSortedClassCategories($ids, 'p.id');
// // AddCart form
// $forms = [];
// foreach ($pagination as $Product) {
// /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
// $builder = $this->formFactory->createNamedBuilder(
// '',
// AddCartType::class,
// null,
// [
// 'product' => $ProductsAndClassCategories[$Product->getId()],
// 'allow_extra_fields' => true,
// ]
// );
// $addCartForm = $builder->getForm();
// $forms[$Product->getId()] = $addCartForm->createView();
// }
// // return $this->render('@CustomProductSearch/default/Product/search_result.twig', [
// // 'searchForm' => $searchForm->createView(),
// // 'pagination' => $pagination,
// // 'form' => $searchForm->createView(),
// // 'forms' => $forms,
// // 'categories' => $categories,
// // 'category' => $category,
// // 'category17' => $category17,
// // 'category19' => $category19,
// // 'page_title' => 'Product Search Results',
// // ]);
// return [
// 'form' => $searchForm->createView(),
// ];
// }
}