vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/FilterService/ListHelper.php line 36

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 Commercial License (PCL)
  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 PCL
  13.  */
  14. namespace Pimcore\Bundle\EcommerceFrameworkBundle\FilterService;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractCategory;
  17. use Pimcore\Model\DataObject\Fieldcollection\Data\OrderByFields;
  18. /**
  19.  * Helper service class for setting up a product list utilizing the filter service
  20.  * based on a filter definition and set filter parameters
  21.  */
  22. class ListHelper
  23. {
  24.     /**
  25.      * @param \Pimcore\Model\DataObject\FilterDefinition $filterDefinition
  26.      * @param ProductListInterface $productList
  27.      * @param array $params
  28.      * @param FilterService $filterService
  29.      * @param bool $loadFullPage
  30.      * @param bool $excludeLimitOfFirstpage
  31.      */
  32.     public function setupProductList(
  33.         \Pimcore\Model\DataObject\FilterDefinition $filterDefinition,
  34.         ProductListInterface $productList,
  35.         &$params,
  36.         FilterService $filterService,
  37.         $loadFullPage,
  38.         $excludeLimitOfFirstpage false
  39.     ) {
  40.         $orderByOptions = [];
  41.         $orderKeysAsc explode(','$filterDefinition->getOrderByAsc());
  42.         foreach ($orderKeysAsc as $orderByEntry) {
  43.             if (!empty($orderByEntry)) {
  44.                 $orderByOptions[$orderByEntry]['asc'] = true;
  45.             }
  46.         }
  47.         $orderKeysDesc explode(','$filterDefinition->getOrderByDesc());
  48.         foreach ($orderKeysDesc as $orderByEntry) {
  49.             if (!empty($orderByEntry)) {
  50.                 $orderByOptions[$orderByEntry]['desc'] = true;
  51.             }
  52.         }
  53.         $offset 0;
  54.         $pageLimit = isset($params['perPage']) ? (int)$params['perPage'] : null;
  55.         if (!$pageLimit) {
  56.             $pageLimit $filterDefinition->getPageLimit();
  57.         }
  58.         if (!$pageLimit) {
  59.             $pageLimit 50;
  60.         }
  61.         $limitOnFirstLoad $filterDefinition->getLimitOnFirstLoad();
  62.         if (!$limitOnFirstLoad) {
  63.             $limitOnFirstLoad 6;
  64.         }
  65.         if (isset($params['page'])) {
  66.             $params['currentPage'] = (int)$params['page'];
  67.             $offset $pageLimit * ($params['page'] - 1);
  68.         }
  69.         if ($filterDefinition->getAjaxReload()) {
  70.             if ($loadFullPage && !$excludeLimitOfFirstpage) {
  71.                 $productList->setLimit($pageLimit);
  72.             } elseif ($loadFullPage && $excludeLimitOfFirstpage) {
  73.                 $offset += $limitOnFirstLoad;
  74.                 $productList->setLimit($pageLimit $limitOnFirstLoad);
  75.             } else {
  76.                 $productList->setLimit($limitOnFirstLoad);
  77.             }
  78.         } else {
  79.             $productList->setLimit($pageLimit);
  80.         }
  81.         $productList->setOffset($offset);
  82.         $params['pageLimit'] = $pageLimit;
  83.         $orderByField null;
  84.         $orderByDirection null;
  85.         if (isset($params['orderBy'])) {
  86.             $orderBy explode('#'$params['orderBy']);
  87.             $orderByField $orderBy[0];
  88.             if (count($orderBy) > 1) {
  89.                 $orderByDirection $orderBy[1];
  90.             }
  91.         }
  92.         if (array_key_exists($orderByField$orderByOptions)) {
  93.             $params['currentOrderBy'] = htmlentities($params['orderBy']);
  94.             $productList->setOrderKey($orderByField);
  95.             if ($orderByDirection) {
  96.                 $productList->setOrder($orderByDirection);
  97.             }
  98.         } else {
  99.             $orderByCollection $filterDefinition->getDefaultOrderBy();
  100.             $orderByList = [];
  101.             if ($orderByCollection) {
  102.                 /** @var OrderByFields $orderBy */
  103.                 foreach ($orderByCollection as $orderBy) {
  104.                     if ($orderBy->getField()) {
  105.                         $orderByList[] = [$orderBy->getField(), $orderBy->getDirection()];
  106.                     }
  107.                 }
  108.                 $params['currentOrderBy'] = implode('#'reset($orderByList));
  109.             }
  110.             if ($orderByList) {
  111.                 $productList->setOrderKey($orderByList);
  112.                 $productList->setOrder('ASC');
  113.             }
  114.         }
  115.         if ($filterService) {
  116.             $params['currentFilter'] = $filterService->initFilterService($filterDefinition$productList$params);
  117.         }
  118.         $params['orderByOptions'] = $orderByOptions;
  119.     }
  120.     /**
  121.      * @param int $page
  122.      *
  123.      * @return string
  124.      */
  125.     public function createPagingQuerystring($page)
  126.     {
  127.         $params $_REQUEST;
  128.         $params['page'] = $page;
  129.         unset($params['fullpage']);
  130.         $string '?';
  131.         foreach ($params as $k => $p) {
  132.             if (is_array($p)) {
  133.                 foreach ($p as $subKey => $subValue) {
  134.                     $string .= $k '[' $subKey ']' '=' urlencode($subValue) . '&';
  135.                 }
  136.             } else {
  137.                 $string .= $k '=' urlencode($p) . '&';
  138.             }
  139.         }
  140.         return $string;
  141.     }
  142.     /**
  143.      * @param array $conditions
  144.      *
  145.      * @return AbstractCategory|null
  146.      */
  147.     public function getFirstFilteredCategory($conditions)
  148.     {
  149.         if (!empty($conditions)) {
  150.             foreach ($conditions as $c) {
  151.                 if ($c instanceof \Pimcore\Model\DataObject\Fieldcollection\Data\FilterCategory) {
  152.                     $result $c->getPreSelect();
  153.                     if ($result instanceof AbstractCategory) {
  154.                         return $result;
  155.                     }
  156.                 }
  157.             }
  158.         }
  159.         return null;
  160.     }
  161. }