src/Controller/ProductController.php line 115

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\Model\Product\AbstractProduct;
  16. use App\Model\Product\AccessoryPart;
  17. use App\Model\Product\Car;
  18. use App\Model\Product\Category;
  19. use App\Services\SegmentTrackingHelperService;
  20. use App\Website\LinkGenerator\ProductLinkGenerator;
  21. use App\Website\Navigation\BreadcrumbHelperService;
  22. use Knp\Component\Pager\Pagination\SlidingPagination;
  23. use Knp\Component\Pager\PaginatorInterface;
  24. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  25. use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper;
  26. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\DefaultMysql;
  27. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ElasticSearch\AbstractElasticSearch;
  28. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  29. use Pimcore\Config;
  30. use Pimcore\Model\DataObject\AbstractObject;
  31. use Pimcore\Model\DataObject\Concrete;
  32. use Pimcore\Model\DataObject\Data\UrlSlug;
  33. use Pimcore\Model\DataObject\FilterDefinition;
  34. use Pimcore\Translation\Translator;
  35. use Pimcore\Twig\Extension\Templating\HeadTitle;
  36. use Pimcore\Twig\Extension\Templating\Placeholder;
  37. use Symfony\Component\HttpFoundation\JsonResponse;
  38. use Symfony\Component\HttpFoundation\Request;
  39. use Symfony\Component\HttpFoundation\Response;
  40. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  41. use Symfony\Component\Routing\Annotation\Route;
  42. class ProductController extends BaseController
  43. {
  44.     /**
  45.      * @param Request $request
  46.      * @param AbstractObject $object
  47.      * @param UrlSlug $urlSlug
  48.      * @return Response
  49.      */
  50.     public function productDetailSlugAction(Request $requestAbstractObject $objectUrlSlug $urlSlug) {
  51.         return $this->forward('App\Controller\ProductController::detailAction', ['product' => $object]);
  52.     }
  53.     /**
  54.      * @Route("/shop/{path}{productname}~p{product}", name="shop-detail", defaults={"path"=""}, requirements={"path"=".*?", "productname"="[\w-]+", "product"="\d+"})
  55.      *
  56.      * @param Request $request
  57.      * @param HeadTitle $headTitleHelper
  58.      * @param BreadcrumbHelperService $breadcrumbHelperService
  59.      * @param Factory $ecommerceFactory
  60.      * @param SegmentTrackingHelperService $segmentTrackingHelperService
  61.      * @param Concrete $product built-in parameter conversion, please see https://github.com/pimcore/pimcore/pull/5554
  62.      * @param ProductLinkGenerator $productLinkGenerator
  63.      *
  64.      * @return Response
  65.      *
  66.      * @throws \Exception
  67.      */
  68.     public function detailAction(
  69.         Request $request,
  70.         HeadTitle $headTitleHelper,
  71.         BreadcrumbHelperService $breadcrumbHelperService,
  72.         Factory $ecommerceFactory,
  73.         SegmentTrackingHelperService $segmentTrackingHelperService,
  74.         Concrete $product,
  75.         ProductLinkGenerator $productLinkGenerator
  76.     )
  77.     {
  78.         if (!(
  79.                 $product && ($product->isPublished() && (($product instanceof Car && $product->getObjectType() == Car::OBJECT_TYPE_ACTUAL_CAR) || $product instanceof AccessoryPart) || $this->verifyPreviewRequest($request$product))
  80.             )
  81.         ) {
  82.             throw new NotFoundHttpException('Product not found.');
  83.         }
  84.         //redirect to main url
  85.         $generatorUrl $productLinkGenerator->generate($product);
  86.         if($generatorUrl != $request->getPathInfo()) {
  87.             $queryString $request->getQueryString();
  88.             return $this->redirect($generatorUrl . ($queryString '?' $queryString ''));
  89.         }
  90.         $breadcrumbHelperService->enrichProductDetailPage($product);
  91.         $headTitleHelper($product->getOSName());
  92.         $paramBag $this->getAllParameters($request);
  93.         $paramBag['product'] = $product;
  94.         //track segments for personalization
  95.         $segmentTrackingHelperService->trackSegmentsForProduct($product);
  96.         $trackingManager $ecommerceFactory->getTrackingManager();
  97.         $trackingManager->trackProductView($product);
  98.         if ($product instanceof Car) {
  99.             foreach ($product->getAccessories() as $accessory) {
  100.                 $trackingManager->trackProductImpression($accessory'crosssells');
  101.             }
  102.             return $this->render('product/detail.html.twig'$paramBag);
  103.         } elseif ($product instanceof AccessoryPart) {
  104.             // get all compatible products
  105.             $productList $ecommerceFactory->getIndexService()->getProductListForCurrentTenant();
  106.             $productList->setVariantMode(ProductListInterface::VARIANT_MODE_VARIANTS_ONLY);
  107.             if($productList instanceof DefaultMysql) {
  108.                 $productList->addCondition('o_id IN (' implode(','$product->getCompatibleToProductIds()) . ')''o_id');
  109.             } else if($productList instanceof AbstractElasticSearch) {
  110.                 $productList->addCondition(['terms' => ['system.o_id' => $product->getCompatibleToProductIds()]], 'o_id');
  111.             }
  112.             foreach($productList as $compatibleProduct) {
  113.                 $trackingManager->trackProductImpression($compatibleProduct'crosssells');
  114.             }
  115.             $paramBag['compatibleTo'] = $productList;
  116.             return $this->render('product/detail_accessory.html.twig'$paramBag);
  117.         }
  118.     }
  119.     /**
  120.      * @Route("/shop/{path}{categoryname}~c{category}", name="shop-category", defaults={"path"=""}, requirements={"path"=".*?", "categoryname"="[\w-]+", "category"="\d+"})
  121.      *
  122.      * @param Request $request
  123.      * @param HeadTitle $headTitleHelper
  124.      * @param BreadcrumbHelperService $breadcrumbHelperService
  125.      * @param Factory $ecommerceFactory
  126.      * @param SegmentTrackingHelperService $segmentTrackingHelperService
  127.      * @param ListHelper $listHelper
  128.      *
  129.      * @return Response
  130.      */
  131.     public function listingAction(Request $requestHeadTitle $headTitleHelperBreadcrumbHelperService $breadcrumbHelperServiceFactory $ecommerceFactorySegmentTrackingHelperService $segmentTrackingHelperServiceListHelper $listHelperPaginatorInterface $paginator)
  132.     {
  133.         $params array_merge($request->query->all(), $request->attributes->all());
  134.         //needed to make sure category filter filters for active category
  135.         $params['parentCategoryIds'] = $params['category'] ?? null;
  136.         $category Category::getById($params['category'] ?? null);
  137.         $params['category'] = $category;
  138.         if ($category) {
  139.             $headTitleHelper($category->getName());
  140.             $breadcrumbHelperService->enrichCategoryPage($category);
  141.         }
  142.         $indexService $ecommerceFactory->getIndexService();
  143.         $productListing $indexService->getProductListForCurrentTenant();
  144.         $productListing->setVariantMode(ProductListInterface::VARIANT_MODE_VARIANTS_ONLY);
  145.         $params['productListing'] = $productListing;
  146.         // load current filter
  147.         if ($category) {
  148.             $filterDefinition $category->getFilterdefinition();
  149.             //track segments for personalization
  150.             $segmentTrackingHelperService->trackSegmentsForCategory($category);
  151.             $trackingManager $ecommerceFactory->getTrackingManager();
  152.             $trackingManager->trackCategoryPageView($category->getName(), null);
  153.         }
  154.         if ($request->get('filterdefinition') instanceof FilterDefinition) {
  155.             $filterDefinition $request->get('filterdefinition');
  156.         }
  157.         if (empty($filterDefinition)) {
  158.             $filterDefinition Config::getWebsiteConfig()->get('fallbackFilterdefinition');
  159.         }
  160.         $filterService $ecommerceFactory->getFilterService();
  161.         $listHelper->setupProductList($filterDefinition$productListing$params$filterServicetrue);
  162.         $params['filterService'] = $filterService;
  163.         $params['filterDefinition'] = $filterDefinition;
  164.         /** @var SlidingPagination $paginator */
  165.         // init pagination
  166.         $paginator $paginator->paginate(
  167.             $productListing,
  168.             $request->get('page'1),
  169.             $filterDefinition->getPageLimit()
  170.         );
  171.         $params['results'] = $paginator;
  172.         $params['paginationVariables'] = $paginator->getPaginationData();
  173.         if ($request->attributes->get('noLayout')) {
  174.             return $this->render('product/listing_content.html.twig'$params);
  175.         }
  176.         // track product impressions
  177.         $trackingManager $ecommerceFactory->getTrackingManager();
  178.         foreach ($paginator as $product) {
  179.             $trackingManager->trackProductImpression($product'grid');
  180.         }
  181.         return $this->render('product/listing.html.twig'$params);
  182.     }
  183.     /**
  184.      * @param Request $request
  185.      * @param Factory $ecommerceFactory
  186.      *
  187.      * @return Response
  188.      */
  189.     public function productTeaserAction(Request $requestFactory $ecommerceFactory)
  190.     {
  191.         $paramsBag = [];
  192.         if ($request->get('type') == 'object') {
  193.             AbstractObject::setGetInheritedValues(true);
  194.             $product AbstractProduct::getById($request->get('id'));
  195.             $paramsBag['product'] = $product;
  196.             //track product impression
  197.             $trackingManager $ecommerceFactory->getTrackingManager();
  198.             $trackingManager->trackProductImpression($product'teaser');
  199.             return $this->render('product/product_teaser.html.twig'$paramsBag);
  200.         }
  201.         throw new NotFoundHttpException('Product not found.');
  202.     }
  203.     /**
  204.      * @Route("/search", name="search")
  205.      *
  206.      * @param Request $request
  207.      * @param ListHelper $listHelper
  208.      * @param Factory $ecommerceFactory
  209.      * @param ProductLinkGenerator $productLinkGenerator
  210.      * @param Translator $translator
  211.      * @param BreadcrumbHelperService $breadcrumbHelperService
  212.      * @param HeadTitle $headTitleHelper
  213.      * @param Placeholder $placeholder
  214.      *
  215.      * @return Response|JsonResponse
  216.      */
  217.     public function searchAction(Request $requestPaginatorInterface $paginator)
  218.     {
  219.         $searchTerm $request->get('term');
  220.         $response $this->forward('App\Controller\MovieDataController::movieDataAction', [
  221.             'request' => $request,
  222.             'searchTerm' => $searchTerm 
  223.         ]);
  224.         return $response;
  225.         
  226.     }
  227. }