// // GET: /Search/ public override ActionResult Index() { // make sure this is in <appSettings> // <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> var filter = new TypeFilter(typeof(PageModelBase)) { Inverse = true }; var parts = CurrentItem.GetChildren(filter); var productResultsPart = parts.SelectMany(CmsFinder.FindAllDescendentsOf <ProductResultsPart>).FirstOrDefault() ?? parts.FirstOrDefault(p => p is ProductResultsPart) as ProductResultsPart; var pageHasProdResultsPart = productResultsPart != null; var listPriceRanges = productResultsPart != null ? productResultsPart.ListPriceRanges : string.Empty; var listPriceRangesNames = Regex.Split(listPriceRanges, @"\s*,\s*").ToList(); var query = Request["query"]; var page = HttpUtility.UrlEncode(Request["pg"]); /* can't use query param named "page" because N2 grabs it and makes a big nuisance of itself */ var pageSize = HttpUtility.UrlEncode(Request["pageSize"]); if (string.IsNullOrEmpty(pageSize)) { pageSize = DefaultPageSize; } var sortBy = HttpUtility.UrlEncode(Request["sortBy"]); var sortDir = HttpUtility.UrlEncode(Request["sortDir"]); // search in commerce var searchOptions = SearchOptionsUtils.GetPagingOptions(page, pageSize, sortBy, sortDir); //var facets = JsonToFacets(Request["facetsJson"]); var catalogPageViewModel = new SearchPageViewModel { Title = query, Products = CatalogApi.GetProductsByKeywordAsync(string.Format("{0}", query), searchOptions).Result }; #if false bool enableFacets = catalogPageViewModel.Products.ExtendedSearchResult != null && catalogPageViewModel.Products.ExtendedSearchResult.FacetFields != null && catalogPageViewModel.Products.ExtendedSearchResult.FacetFields.Length > 0; if (enableFacets) { catalogPageViewModel.Products.PageSize = DefaultPageSize_3across.AsInt(); catalogPageViewModel.Products.TotalPages = (catalogPageViewModel.Products.TotalCount / DefaultPageSize_3across.AsInt()) + 1; catalogPageViewModel.Products.Products = catalogPageViewModel.Products.Products.Take(catalogPageViewModel.Products.Products.Length - 1).ToArray(); } #endif if (catalogPageViewModel.Products.Product == null) { catalogPageViewModel.Products.Product = new ProductWithRanking[0]; } // if facets specified, need to render page with full facets checkbox selections, but with restricted results // this will almost always be a cheap call, because we probably just did it moments before and cached it #if false if (facets != null) { catalogPageViewModel.Facets = facets; var noFacetsResults = _catAdapter.SearchProduct(query, searchOptions); if (catalogPageViewModel.Products.ExtendedSearchResult == null) { catalogPageViewModel.Products.ExtendedSearchResult = noFacetsResults.ExtendedSearchResult; } else if (noFacetsResults.ExtendedSearchResult != null) { catalogPageViewModel.Products.ExtendedSearchResult.FacetFields = noFacetsResults.ExtendedSearchResult.FacetFields; } else { catalogPageViewModel.Products.ExtendedSearchResult = null; } } #endif catalogPageViewModel.SetPageTitle(string.Format("{0} “{1}”", Res.Catalog_CatalogSearchResultsTitle, Server.HtmlEncode(query ?? ""))); WebSession.Current.Set(WebSession.SearchResultSlot, catalogPageViewModel); // TODO - redirect to page when there's only one exact hit // - highlight results in target page, e.g. via jQuery plugin SetPageTitleOverrideResKey("SearchResults"); catalogPageViewModel.PageHasProdResultsPart = pageHasProdResultsPart; catalogPageViewModel.EnableFacets = false; catalogPageViewModel.ListPriceRangeNames = listPriceRangesNames; SetPageInfo(query, null, "Sales.Catalog.SearchResults"); return(View(catalogPageViewModel)); }
public override ActionResult Index() { var filter = new TypeFilter(typeof(PageModelBase)) { Inverse = true }; var parts = CurrentItem.GetChildren(filter); var productResultsPart = parts.SelectMany(CmsFinder.FindAllDescendentsOf <ProductResultsPart>).FirstOrDefault() ?? parts.FirstOrDefault(p => p is ProductResultsPart) as ProductResultsPart; var pageHasProdResultsPart = productResultsPart != null; var listPriceRanges = productResultsPart != null ? productResultsPart.ListPriceRanges : string.Empty; var listPriceRangesNames = Regex.Split(listPriceRanges, @"\s*,\s*").ToList(); var cid = Arguments.Length > 0 ? Arguments.FirstOrDefault(a => !string.IsNullOrEmpty(a)) : null; if (!string.IsNullOrEmpty(cid)) { ActionResult redirectResult; long lcid; if (long.TryParse(cid, out lcid)) { if (GetRedirect(long.Parse(cid), out redirectResult)) { return(redirectResult); } } } AssertProductsLoaded(); var page = Request["pg"]; /* can't use query param named "page" because N2 grabs it and makes a big nuisance of itself */ var pageSize = Request["pageSize"]; if (string.IsNullOrEmpty(pageSize)) { pageSize = DefaultPageSize4Across; } var sortBy = Request["sortBy"]; var sortDir = Request["sortDir"]; // use CMS configured category as a fallback on null, so that admins have better WYSIWYG if (string.IsNullOrEmpty(cid)) { cid = CurrentItem.CategoryID; } long categoryId; try { categoryId = long.Parse(cid); } catch { return(!IsManaging?NotFound() : View()); } var searchOptions = SearchOptionsUtils.GetPagingOptions(page, pageSize, sortBy, sortDir); // If page is based on product results, then the count should be based on the category. var catalogPageViewModel = _catViewModelBuilder.SearchProductByCategoryAsync(categoryId, searchOptions).Result; #if false // Facet logic var enableFacets = productResultsPart != null && productResultsPart.EnableFacets && catalogPageViewModel.Products.ExtendedSearchResult != null && catalogPageViewModel.Products.ExtendedSearchResult.FacetFields != null && catalogPageViewModel.Products.ExtendedSearchResult.FacetFields.Length > 0; if (enableFacets) { catalogPageViewModel.Products.PageSize = DefaultPageSize_3across.AsInt(); catalogPageViewModel.Products.TotalPages = (catalogPageViewModel.Products.TotalCount / DefaultPageSize_3across.AsInt()) + 1; catalogPageViewModel.Products.Products = catalogPageViewModel.Products.Products.Take(catalogPageViewModel.Products.Products.Length - 1).ToArray(); } #endif catalogPageViewModel.PageHasProdResultsPart = pageHasProdResultsPart; catalogPageViewModel.EnableFacets = false; catalogPageViewModel.ListPriceRangeNames = listPriceRangesNames; // if facets specified, need to render page with full facets checkbox selections, but with restricted results // this will almost always be a cheap call, because we probably just did it moments before and cached it #if false // Facet logic if (facets != null) { catalogPageViewModel.Facets = facets; var noFacetsResults = _catViewModelBuilder.SearchProductByCategoryAsync(categoryId, searchOptions); if (catalogPageViewModel.Products.ExtendedSearchResult == null) { catalogPageViewModel.Products.ExtendedSearchResult = noFacetsResults.Products.ExtendedSearchResult; } else if (noFacetsResults.Products.ExtendedSearchResult != null) { catalogPageViewModel.Products.ExtendedSearchResult.FacetFields = noFacetsResults.Products.ExtendedSearchResult.FacetFields; } else { catalogPageViewModel.Products.ExtendedSearchResult = null; } } #endif if (!pageHasProdResultsPart && catalogPageViewModel.Products.Product.Length == 0) { var products = new List <Product>(); if (Products.Any()) { products.AddRange(Products.Values.ToArray()); } catalogPageViewModel.Products.Product.AddRange( products.Select(p => new ProductWithRanking().InjectFrom(p) as ProductWithRanking)); } if (catalogPageViewModel.Products.Product == null) { catalogPageViewModel.Products.Product = new ProductWithRanking[0]; } WebSession.Current.Set(WebSession.SearchResultSlot, catalogPageViewModel); WebSession.Current.Set(WebSession.CategoryIdSlot, categoryId); SetPageTitle(GetPageTitle(catalogPageViewModel)); SetPageMetaData(catalogPageViewModel); catalogPageViewModel.Metadata.OgType = !string.IsNullOrEmpty(CurrentItem.OgType) ? CurrentItem.OgType : "website"; SetPageInfo(null, catalogPageViewModel.Title, "Sales.Catalog.Product"); return(View(catalogPageViewModel)); }