/**
  * Pagination is HARD. We let the core do the heavy lifting from
  * a simple representation of the pagination.
  *
  * Generated URLs will include the page number, obviously,
  * but also the sort order and the "q" (facets) parameters.
  *
  * @param ProductSearchQuery  $query
  * @param ProductSearchResult $result
  *
  * @return an array that makes rendering the pagination very easy
  */
 protected function getTemplateVarPagination(ProductSearchQuery $query, ProductSearchResult $result)
 {
     $pagination = new Pagination();
     $pagination->setPage($query->getPage())->setPagesCount(ceil($result->getTotalProductsCount() / $query->getResultsPerPage()));
     $totalItems = $result->getTotalProductsCount();
     $itemsShownFrom = $query->getResultsPerPage() * ($query->getPage() - 1) + 1;
     $itemsShownTo = $query->getResultsPerPage() * $query->getPage();
     return array('total_items' => $totalItems, 'items_shown_from' => $itemsShownFrom, 'items_shown_to' => $itemsShownTo <= $totalItems ? $itemsShownTo : $totalItems, 'pages' => array_map(function ($link) {
         $link['url'] = $this->updateQueryString(array('page' => $link['page']));
         return $link;
     }, $pagination->buildLinks()));
 }
 private function getProductsOrCount(ProductSearchContext $context, ProductSearchQuery $query, $type = 'products')
 {
     if ($query->getSortOrder()->isRandom()) {
         return $this->category->getProducts($context->getIdLang(), 1, $query->getResultsPerPage(), null, null, $type !== 'products', true, true, $query->getResultsPerPage());
     } else {
         return $this->category->getProducts($context->getIdLang(), $query->getPage(), $query->getResultsPerPage(), $query->getSortOrder()->toLegacyOrderBy(), $query->getSortOrder()->toLegacyOrderWay(), $type !== 'products');
     }
 }
 public function runQuery(ProductSearchContext $context, ProductSearchQuery $query)
 {
     $products = [];
     $count = 0;
     if ($string = $query->getSearchString()) {
         $result = Search::find($context->getIdLang(), Tools::replaceAccentedChars(urldecode($string)), $query->getPage(), $query->getResultsPerPage(), $query->getSortOrder()->toLegacyOrderBy(), $query->getSortOrder()->toLegacyOrderWay(), false, false, null);
         $products = $result['result'];
         $count = $result['total'];
     } elseif ($tag = $query->getSearchTag()) {
         $products = Search::searchTag($context->getIdLang(), urldecode($tag), false, $query->getPage(), $query->getResultsPerPage(), $query->getSortOrder()->toLegacyOrderBy(true), $query->getSortOrder()->toLegacyOrderWay(), false, null);
         $count = Search::searchTag($context->getIdLang(), urldecode($tag), true, $query->getPage(), $query->getResultsPerPage(), $query->getSortOrder()->toLegacyOrderBy(true), $query->getSortOrder()->toLegacyOrderWay(), false, null);
     }
     $result = new ProductSearchResult();
     $result->setProducts($products)->setTotalProductsCount($count);
     $result->setAvailableSortOrders($this->sortOrderFactory->getDefaultSortOrders());
     return $result;
 }
 public function runQuery(ProductSearchContext $context, ProductSearchQuery $query)
 {
     if (!($products = ProductSale::getBestSales($context->getIdLang(), $query->getPage(), $query->getResultsPerPage(), $query->getSortOrder()->toLegacyOrderBy(), $query->getSortOrder()->toLegacyOrderWay()))) {
         $products = array();
     }
     $count = (int) ProductSale::getNbSales();
     $result = new ProductSearchResult();
     $result->setProducts($products)->setTotalProductsCount($count);
     $result->setAvailableSortOrders(array((new SortOrder('product', 'name', 'asc'))->setLabel($this->translator->trans('Name, A to Z', array(), 'Shop.Theme.Catalog')), (new SortOrder('product', 'name', 'desc'))->setLabel($this->translator->trans('Name, Z to A', array(), 'Shop.Theme.Catalog')), (new SortOrder('product', 'price', 'asc'))->setLabel($this->translator->trans('Price, low to high', array(), 'Shop.Theme.Catalog')), (new SortOrder('product', 'price', 'desc'))->setLabel($this->translator->trans('Price, high to low', array(), 'Shop.Theme.Catalog'))));
     return $result;
 }
 private function getProductsOrCount(ProductSearchContext $context, ProductSearchQuery $query, $type = 'products')
 {
     return Product::getNewProducts($context->getIdLang(), $query->getPage(), $query->getResultsPerPage(), $type !== 'products', $query->getSortOrder()->toLegacyOrderBy(), $query->getSortOrder()->toLegacyOrderWay());
 }
 private function getProductsOrCount(ProductSearchContext $context, ProductSearchQuery $query, $type = 'products')
 {
     return $this->supplier->getProducts($this->supplier->id, $context->getIdLang(), $query->getPage(), $query->getResultsPerPage(), $query->getSortOrder()->toLegacyOrderBy(), $query->getSortOrder()->toLegacyOrderWay(), $type !== 'products');
 }