src/Controller/ProductController.php line 31

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Controller;
  15. use App\Website\Tool\Text;
  16. use Pimcore\Model\DataObject;
  17. use Pimcore\Translation\Translator;
  18. use Pimcore\Model\DataObject\Products;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Pimcore\Controller\Configuration\ResponseHeader;
  24. use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  27. class ProductController extends BaseController
  28. {
  29.   
  30.     public function productAction(Request $request,PaginatorInterface $paginator)
  31.     {
  32.         $products = new Products\Listing();
  33.         $products =  $products->load();
  34.         $pagination $paginator->paginate(
  35.             $products,
  36.             $request->query->getInt('page'1),
  37.             // Number of items per page
  38.         );
  39.         $currentPage $pagination->getCurrentPageNumber();
  40.         $totalPages ceil($pagination->getTotalItemCount() / $pagination->getItemNumberPerPage());
  41.         $showLoadMoreButton $currentPage $totalPages;
  42.         if ($request->isXmlHttpRequest()) {
  43.             $html $this->render('products/__productPartial.html.twig', [
  44.                 'products' => $pagination,
  45.                 
  46.             ])->getContent();
  47.             return new JsonResponse([
  48.                 'html' => $html,
  49.                 'showLoadMoreButton' => $showLoadMoreButton,
  50.             ]);
  51.         }
  52.         return $this->render('products/productListing.html.twig', [
  53.             'products' => $pagination,
  54.             'showLoadMoreButton' => $showLoadMoreButton,
  55.         ]);
  56.         
  57.     }
  58.     /**
  59.      *@Route("/product/{url}", name="product-detail")
  60.      */
  61.     public function productDetailAction(Request $request)
  62.     {   
  63.         
  64.         $product DataObject\Products::getByUrl($request->get('url') , true);
  65.         if ($product instanceof \Pimcore\Model\DataObject\Products) {
  66.             // Get three randon Procuts
  67.             $otherProducts = new Products\Listing();
  68.             $otherProducts->setLimit(3);
  69.             $otherProducts->setCondition("o_id != ?", [$product->getId()]);
  70.             $otherProducts->setOrderKey("RAND()"false);
  71.             $otherProducts->load();
  72.             return $this->render('products/productDetail.html.twig',
  73.             [
  74.                 'product' => $product,
  75.                 'otherProducts' =>$otherProducts
  76.             ]);
  77.         }
  78.         throw new NotFoundHttpException('product not found.');
  79.     }
  80.     /**
  81.      * @param Request $request
  82.      *
  83.      * @return Response
  84.      */
  85.     public function productTeaserAction(Request $request)
  86.     {
  87.         $paramsBag = [];
  88.         if ($request->get('type') == 'object') {
  89.             $product Products::getById($request->get('id'));
  90.             $paramsBag['product'] = $product;
  91.             
  92.             return $this->render('products/product-teaser-block.html.twig'$paramsBag);
  93.         }
  94.         throw new NotFoundHttpException('product not found.');
  95.     }
  96. }