<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace App\Controller;
use App\Website\Tool\Text;
use Pimcore\Model\DataObject;
use Pimcore\Translation\Translator;
use Pimcore\Model\DataObject\Products;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Pimcore\Controller\Configuration\ResponseHeader;
use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ProductController extends BaseController
{
public function productAction(Request $request,PaginatorInterface $paginator)
{
$products = new Products\Listing();
$products = $products->load();
$pagination = $paginator->paginate(
$products,
$request->query->getInt('page', 1),
4 // Number of items per page
);
$currentPage = $pagination->getCurrentPageNumber();
$totalPages = ceil($pagination->getTotalItemCount() / $pagination->getItemNumberPerPage());
$showLoadMoreButton = $currentPage < $totalPages;
if ($request->isXmlHttpRequest()) {
$html = $this->render('products/__productPartial.html.twig', [
'products' => $pagination,
])->getContent();
return new JsonResponse([
'html' => $html,
'showLoadMoreButton' => $showLoadMoreButton,
]);
}
return $this->render('products/productListing.html.twig', [
'products' => $pagination,
'showLoadMoreButton' => $showLoadMoreButton,
]);
}
/**
*@Route("/product/{url}", name="product-detail")
*/
public function productDetailAction(Request $request)
{
$product = DataObject\Products::getByUrl($request->get('url') , true);
if ($product instanceof \Pimcore\Model\DataObject\Products) {
// Get three randon Procuts
$otherProducts = new Products\Listing();
$otherProducts->setLimit(3);
$otherProducts->setCondition("o_id != ?", [$product->getId()]);
$otherProducts->setOrderKey("RAND()", false);
$otherProducts->load();
return $this->render('products/productDetail.html.twig',
[
'product' => $product,
'otherProducts' =>$otherProducts
]);
}
throw new NotFoundHttpException('product not found.');
}
/**
* @param Request $request
*
* @return Response
*/
public function productTeaserAction(Request $request)
{
$paramsBag = [];
if ($request->get('type') == 'object') {
$product = Products::getById($request->get('id'));
$paramsBag['product'] = $product;
return $this->render('products/product-teaser-block.html.twig', $paramsBag);
}
throw new NotFoundHttpException('product not found.');
}
}