<?php
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use App\Model\DataObject\Product;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Model\DataObject\Erich;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends FrontendController
{
/**
* @param Request $request
* @return Response
*/
public function defaultAction(Request $request, PaginatorInterface $paginator): Response
{
$query = $request->query->get('q', '');
// Fetch products based on search query
$products = new Erich\Listing();
$products->setCondition("sku IS NOT NULL");
if ($query) {
$products->addConditionParam("name_de LIKE ? OR sku LIKE ?",["%$query%", "%$query%"]);
}
// Paginate the results
$pagination = $paginator->paginate(
$products,
$request->query->getInt('page', 1), // page number
10 // limit per page
);
return $this->render('default/default.html.twig', [
'pagination' => $pagination,
'query' => $query,
]);
}
/**
* @Route("/product/{id}", name="product_detail", methods={"GET"})
*/
public function detailAction($id): Response
{
$product = Product::getById($id);
if (!$product) {
throw $this->createNotFoundException('Product not found');
}
return $this->render('default/detail.html.twig', [
'product' => $product,
]);
}
}