Пример #1
0
 public System.Collections.IEnumerable Enumerate(CommerceInstance instance, CultureInfo culture)
 {
     foreach (var data in new BatchedQuery <Product>(Query(instance), 1000))
     {
         var product = data as Product;
         yield return(ProductModel.Create(product, culture, CategoryTree.Get(instance.Name)));
     }
 }
Пример #2
0
 private void PrepareEditor(CategoryEditorModel model)
 {
     ViewBag.ParentPath = "None".Localize();
     if (model.ParentId != null)
     {
         var parent = CategoryTree.Get(CurrentInstance.Name).Find(model.ParentId.Value);
         ViewBag.ParentPath = String.Join(" >> ", parent.PathFromRoot().Select(c => c.Name));
     }
 }
Пример #3
0
 private void Index(Product product, IEnumerable <CultureInfo> cultures)
 {
     foreach (var culture in cultures)
     {
         var indexer = IndexStores.Get <ProductModel>(CommerceInstance.Current.Name, culture);
         indexer.Index(ProductModel.Create(product, culture, CategoryTree.Get(CommerceInstance.Current.Name)));
         indexer.Commit();
     }
 }
Пример #4
0
        public object Execute(CommerceDataSourceContext context)
        {
            var culture = CultureInfo.InvariantCulture;

            if (!String.IsNullOrEmpty(context.Site.Culture))
            {
                var languageStore = LanguageStores.Get(context.Instance);
                if (languageStore.Exists(context.Site.Culture))
                {
                    culture = CultureInfo.GetCultureInfo(context.Site.Culture);
                }
            }

            var store = IndexStores.Get <ProductModel>(context.Instance, culture);
            var query = store.Query();

            // Apply defined filters
            if (Filters != null)
            {
                query = query.ApplyFilters(ParseFilters(context), culture);
            }

            // Apply implicit facet filters repsented in url query
            IList <Filter> facetFilters = null;

            if (IncludeFacets && Facets != null && Facets.Count > 0)
            {
                facetFilters = GetFacetFilters(context);
                if (facetFilters.Count > 0)
                {
                    query = query.ApplyFilters(facetFilters, culture);
                }
            }

            // Apply sorting
            var sortField = ParameterizedFieldValue.GetFieldValue(SortField, context.ValueProvider);

            if (!String.IsNullOrEmpty(sortField))
            {
                query.AddOrderBy(sortField);
            }

            // Execute result
            var result = new ProductSearchResult();

            var pagination = query.Paginate(EvaluatePageNumber(context) - 1, EvaluatePageSize(context));

            result.Total    = pagination.TotalItems;
            result.Products = ToProducts(pagination.Cast <ProductModel>().ToList(), context.Site.Commerce());

            // Get facets
            if (IncludeFacets && Facets != null && Facets.Count > 0)
            {
                result.Facets = query.ToFacets(Facets);

                // Generate facet urls
                var url = HttpContext.Current.Request.RawUrl;

                CategoryTree localizedCategoryTree = null;

                foreach (var facetResult in result.Facets)
                {
                    // Categories are in tree structure, and all level of categories of a product are indexed for seaier search.
                    // If Category facet is not presented in url query, we should only return the ROOT level categories.
                    // If Category facet is presented in url query, we should only return it's children categories.
                    // Otherwise there'll be too many category results.
                    if (facetResult.Field == FacetDefinition.Category.Field)
                    {
                        if (localizedCategoryTree == null)
                        {
                            localizedCategoryTree = CategoryTree.Get(context.Instance).Localize(culture, "Name");
                        }

                        var filter = facetFilters.FirstOrDefault(f => f.Name == FacetDefinition.Category.Name);

                        if (filter != null)
                        {
                            var category = localizedCategoryTree.Desendants().FirstOrDefault(c => c.Name == filter.FieldValue);
                            foreach (var value in facetResult.Values.ToList())
                            {
                                if (!category.Children.Any(c => c.Name == value.Term))
                                {
                                    facetResult.Values.Remove(value);
                                }
                            }
                        }
                        else
                        {
                            foreach (var value in facetResult.Values.ToList())
                            {
                                if (!localizedCategoryTree.Categories.Any(c => c.Name == value.Term))
                                {
                                    facetResult.Values.Remove(value);
                                }
                            }
                        }
                    }

                    foreach (var value in facetResult.Values)
                    {
                        var paramName = facetResult.Name;
                        value.Url = UrlUtility.RemoveQuery(url, paramName);
                        value.Url = UrlUtility.AddQueryParam(value.Url, paramName, value.Term);
                    }
                }
            }

            return(result);
        }