/** * @dataProvider serialization_examples */ public function test_serialization($data) { $opt = new SortOrder($data['entity'], $data['field'], $data['direction']); $encoded = $opt->toString(); $this->assertInternalType('string', $encoded); $unserialized = SortOrder::newFromString($encoded); $arr = $unserialized->toArray(); $this->assertEquals($data['entity'], $arr['entity']); $this->assertEquals($data['field'], $arr['field']); $this->assertEquals($data['direction'], $arr['direction']); }
/** * This returns all template variables needed for rendering * the product list, the facets, the pagination and the sort orders. * * @return array variables ready for templating */ protected function getProductSearchVariables() { /* * To render the page we need to find something (a ProductSearchProviderInterface) * that knows how to query products. */ // the search provider will need a context (language, shop...) to do its job $context = $this->getProductSearchContext(); // the controller generates the query... $query = $this->getProductSearchQuery(); // ...modules decide if they can handle it (first one that can is used) $provider = $this->getProductSearchProviderFromModules($query); // if no module wants to do the query, then the core feature is used if (null === $provider) { $provider = $this->getDefaultProductSearchProvider(); } $resultsPerPage = (int) Tools::getValue('resultsPerPage'); if ($resultsPerPage <= 0 || $resultsPerPage > 36) { $resultsPerPage = Configuration::get('PS_PRODUCTS_PER_PAGE'); } // we need to set a few parameters from back-end preferences $query->setResultsPerPage($resultsPerPage)->setPage(max((int) Tools::getValue('page'), 1)); // set the sort order if provided in the URL if ($encodedSortOrder = Tools::getValue('order')) { $query->setSortOrder(SortOrder::newFromString($encodedSortOrder)); } // get the parameters containing the encoded facets from the URL $encodedFacets = Tools::getValue('q'); /* * The controller is agnostic of facets. * It's up to the search module to use /define them. * * Facets are encoded in the "q" URL parameter, which is passed * to the search provider through the query's "$encodedFacets" property. */ $query->setEncodedFacets($encodedFacets); // We're ready to run the actual query! $result = $provider->runQuery($context, $query); // sort order is useful for template, // add it if undefined - it should be the same one // as for the query anyway if (!$result->getCurrentSortOrder()) { $result->setCurrentSortOrder($query->getSortOrder()); } // prepare the products $products = $this->prepareMultipleProductsForTemplate($result->getProducts()); // render the facets if ($provider instanceof FacetsRendererInterface) { // with the provider if it wants to $rendered_facets = $provider->renderFacets($context, $result); $rendered_active_filters = $provider->renderActiveFilters($context, $result); } else { // with the core $rendered_facets = $this->renderFacets($result); $rendered_active_filters = $this->renderActiveFilters($result); } $pagination = $this->getTemplateVarPagination($query, $result); // prepare the sort orders // note that, again, the product controller is sort-orders // agnostic // a module can easily add specific sort orders that it needs // to support (e.g. sort by "energy efficiency") $sort_orders = $this->getTemplateVarSortOrders($result->getAvailableSortOrders(), $query->getSortOrder()->toString()); $searchVariables = array('label' => $this->getListingLabel(), 'products' => $products, 'sort_orders' => $sort_orders, 'pagination' => $pagination, 'rendered_facets' => $rendered_facets, 'rendered_active_filters' => $rendered_active_filters, 'js_enabled' => $this->ajax, 'current_url' => $this->updateQueryString(array('q' => $result->getEncodedFacets()))); Hook::exec('actionProductSearchComplete', $searchVariables); return $searchVariables; }
protected function getProducts() { $category = new Category((int) Configuration::get('HOME_FEATURED_CAT')); $searchProvider = new CategoryProductSearchProvider($this->context->getTranslator(), $category); $context = new ProductSearchContext($this->context); $query = new ProductSearchQuery(); $nProducts = Configuration::get('HOME_FEATURED_NBR'); if ($nProducts < 0) { $nProducts = 12; } $query->setResultsPerPage($nProducts)->setPage(1); if (Configuration::get('HOME_FEATURED_RANDOMIZE')) { $query->setSortOrder(SortOrder::random()); } else { $query->setSortOrder(new SortOrder('product', 'position', 'asc')); } $result = $searchProvider->runQuery($context, $query); $assembler = new ProductAssembler($this->context); $presenterFactory = new ProductPresenterFactory($this->context); $presentationSettings = $presenterFactory->getPresentationSettings(); $presenter = new ProductListingPresenter(new ImageRetriever($this->context->link), $this->context->link, new PriceFormatter(), new ProductColorsRetriever(), $this->context->getTranslator()); $products_for_template = []; foreach ($result->getProducts() as $rawProduct) { $products_for_template[] = $presenter->present($presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language); } return $products_for_template; }