Beispiel #1
0
        public static bool IsActive(this FilterProductContext context, FilterCriteria criteria)
        {
            if (criteria != null && context.Criteria != null)
            {
                var foundCriteria = context.Criteria.FirstOrDefault(c => !c.IsInactive &&
                                                                    c.Entity.IsCaseInsensitiveEqual(criteria.Entity) && c.Name.IsCaseInsensitiveEqual(criteria.Name) && c.Value.IsCaseInsensitiveEqual(criteria.Value));

                return(foundCriteria != null);
            }
            return(false);
        }
        public virtual void ProductFilterable(FilterProductContext context)
        {
            if (context.Criteria.FirstOrDefault(c => c.Entity.IsCaseInsensitiveEqual(FilterService.ShortcutPrice)) == null)
            {
                context.Criteria.AddRange(ProductFilterablePrices(context));
            }

            if (context.Criteria.FirstOrDefault(c => c.Name.IsCaseInsensitiveEqual("Name") && c.Entity.IsCaseInsensitiveEqual("Manufacturer")) == null)
            {
                context.Criteria.AddRange(ProductFilterableManufacturer(context));
            }

            context.Criteria.AddRange(ProductFilterableSpecAttributes(context));
        }
        private List <FilterCriteria> ProductFilterableManufacturer(FilterProductContext context, bool getAll = false)
        {
            var query = ProductFilter(context);

            var manus =
                from p in query
                from pm in p.ProductManufacturers
                where !pm.Manufacturer.Deleted
                select pm.Manufacturer;

            var grouped =
                from m in manus
                group m by m.Id into grp
                orderby grp.Key
                select new FilterCriteria
            {
                MatchCount   = grp.Count(),
                Value        = grp.FirstOrDefault().Name,
                DisplayOrder = grp.FirstOrDefault().DisplayOrder
            };

            if (_catalogSettings.SortFilterResultsByMatches)
            {
                grouped = grouped.OrderByDescending(m => m.MatchCount);
            }
            else
            {
                grouped = grouped.OrderBy(m => m.DisplayOrder);
            }

            if (!getAll)
            {
                grouped = grouped.Take(_catalogSettings.MaxFilterItemsToDisplay);
            }

            var lst = grouped.ToList();

            lst.ForEach(c =>
            {
                c.Name       = "Name";
                c.Entity     = "Manufacturer";
                c.IsInactive = true;
            });

            return(lst);
        }
        private List <FilterCriteria> ProductFilterablePrices(FilterProductContext context)
        {
            var            result = new List <FilterCriteria>();
            FilterCriteria criteria;
            Category       category;

            var tmp = new FilterProductContext
            {
                ParentCategoryID = context.ParentCategoryID,
                CategoryIds      = context.CategoryIds,
                Criteria         = new List <FilterCriteria>()
            };

            if (context.ParentCategoryID != 0 && (category = _categoryService.GetCategoryById(context.ParentCategoryID)) != null && category.PriceRanges.HasValue())
            {
                string[] ranges = category.PriceRanges.SplitSafe(";");

                foreach (string range in ranges)
                {
                    if ((criteria = range.ParsePriceString()) != null)
                    {
                        tmp.Criteria.Clear();
                        tmp.Criteria.AddRange(context.Criteria);
                        tmp.Criteria.Add(criteria);

                        try
                        {
                            criteria.MatchCount = ProductFilter(tmp).Count();
                        }
                        catch (Exception exc)
                        {
                            exc.Dump();
                        }

                        if (criteria.MatchCount > 0)
                        {
                            result.Add(criteria);
                        }
                    }
                }
            }

            result.ForEach(c => c.IsInactive = true);
            return(result);
        }
Beispiel #5
0
        public static string GetUrl(this FilterProductContext context, FilterCriteria criteriaAdd = null, FilterCriteria criteriaRemove = null)
        {
            var url = "{0}?pagesize={1}&viewmode={2}".FormatInvariant(context.Path, context.PageSize, context.ViewMode);

            if (context.OrderBy.HasValue)
            {
                url = "{0}&orderby={1}".FormatInvariant(url, context.OrderBy.Value);
            }

            try
            {
                if (criteriaAdd != null || criteriaRemove != null)
                {
                    var criterias = new List <FilterCriteria>();

                    if (context.Criteria != null && context.Criteria.Count > 0)
                    {
                        criterias.AddRange(context.Criteria.Where(c => !c.IsInactive));
                    }

                    if (criteriaAdd != null)
                    {
                        criterias.Add(criteriaAdd);
                    }
                    else
                    {
                        criterias.RemoveAll(c => c.Entity.IsCaseInsensitiveEqual(criteriaRemove.Entity) && c.Name.IsCaseInsensitiveEqual(criteriaRemove.Name) && c.Value.IsCaseInsensitiveEqual(criteriaRemove.Value));
                    }

                    if (criterias.Count > 0)
                    {
                        url = "{0}&filter={1}".FormatInvariant(url, HttpUtility.UrlEncode(JsonConvert.SerializeObject(criterias)));
                    }
                }
            }
            catch (Exception exc)
            {
                exc.Dump();
            }

            return(url);
        }
        public virtual FilterProductContext CreateFilterProductContext(string filter, int categoryID, string path, int?pagesize, int?orderby, string viewmode)
        {
            var context = new FilterProductContext
            {
                Filter           = filter,
                ParentCategoryID = categoryID,
                CategoryIds      = new List <int> {
                    categoryID
                },
                Path     = path,
                PageSize = pagesize ?? 12,
                ViewMode = viewmode,
                OrderBy  = orderby,
                Criteria = Deserialize(filter)
            };

            if (_catalogSettings.ShowProductsFromSubcategories)
            {
                context.CategoryIds.AddRange(
                    _categoryService.GetAllCategoriesByParentCategoryId(categoryID).Select(x => x.Id)
                    );
            }

            int languageId = _services.WorkContext.WorkingLanguage.Id;

            foreach (var criteria in context.Criteria.Where(x => x.Entity.IsCaseInsensitiveEqual(ShortcutSpecAttribute)))
            {
                if (criteria.PId.HasValue)
                {
                    criteria.NameLocalized = _localizedEntityService.GetLocalizedValue(languageId, criteria.PId.Value, "SpecificationAttribute", "Name");
                }

                if (criteria.ID.HasValue)
                {
                    criteria.ValueLocalized = _localizedEntityService.GetLocalizedValue(languageId, criteria.ID.Value, "SpecificationAttributeOption", "Name");
                }
            }

            return(context);
        }
        public virtual void ProductFilterableMultiSelect(FilterProductContext context, string filterMultiSelect)
        {
            var criteriaMultiSelect        = Deserialize(filterMultiSelect).FirstOrDefault();
            List <FilterCriteria> inactive = null;

            if (criteriaMultiSelect != null)
            {
                context.Criteria.RemoveAll(c => c.Name.IsCaseInsensitiveEqual(criteriaMultiSelect.Name) && c.Entity.IsCaseInsensitiveEqual(criteriaMultiSelect.Entity));

                if (criteriaMultiSelect.Name.IsCaseInsensitiveEqual("Name") && criteriaMultiSelect.Entity.IsCaseInsensitiveEqual("Manufacturer"))
                {
                    inactive = ProductFilterableManufacturer(context, true);
                }
                else if (criteriaMultiSelect.Entity.IsCaseInsensitiveEqual(FilterService.ShortcutPrice))
                {
                    inactive = ProductFilterablePrices(context);
                }
                else if (criteriaMultiSelect.Entity.IsCaseInsensitiveEqual(FilterService.ShortcutSpecAttribute))
                {
                    inactive = ProductFilterableSpecAttributes(context, criteriaMultiSelect.Name);
                }
            }

            // filters WITHOUT the multiple selectable filters
            var excludedFilter = Serialize(context.Criteria);

            // filters WITH the multiple selectable filters (required for highlighting selected values)
            context.Criteria = Deserialize(context.Filter);

            context.Filter = excludedFilter;

            if (inactive != null)
            {
                inactive.ForEach(c => c.IsInactive = true);
                context.Criteria.AddRange(inactive);
            }
        }
        public virtual IQueryable <Product> ProductFilter(FilterProductContext context)
        {
            var sql   = new FilterSql();
            var query = AllProducts(context.CategoryIds);

            // prices
            if (ToWhereClause(sql, context.Criteria, c => !c.IsInactive && c.Entity.IsCaseInsensitiveEqual(ShortcutPrice)))
            {
                query = query.Where(sql.WhereClause.ToString(), sql.Values.ToArray());
            }

            // manufacturer
            if (ToWhereClause(sql, context.Criteria, c => !c.IsInactive && c.Entity.IsCaseInsensitiveEqual("Manufacturer")))
            {
                var pmq =
                    from p in query
                    from pm in p.ProductManufacturers
                    where !pm.Manufacturer.Deleted
                    select pm;

                query = pmq
                        .Where(sql.WhereClause.ToString(), sql.Values.ToArray())
                        .Select(pm => pm.Product);
            }

            // specification attribute
            if (ToWhereClause(sql, context.Criteria, c => !c.IsInactive && (c.Entity.IsCaseInsensitiveEqual("SpecificationAttributeOption") || c.Entity.IsCaseInsensitiveEqual(ShortcutSpecAttribute))))
            {
                //var saq = (
                //	from p in query
                //	from sa in p.ProductSpecificationAttributes
                //	select sa).Where(a => a.AllowFiltering).Where(sql.WhereClause.ToString(), sql.Values.ToArray());

                //query = saq.Select(sa => sa.Product);

                int countSameNameAttributes = sql.Criteria
                                              .Where(c => c.Entity.IsCaseInsensitiveEqual(ShortcutSpecAttribute))
                                              .GroupBy(c => c.Name)
                                              .Count();

                var specRepository = EngineContext.Current.Resolve <IRepository <ProductSpecificationAttribute> >();

                var saq = specRepository.TableUntracked
                          .Where(a => a.AllowFiltering)
                          .Where(sql.WhereClause.ToString(), sql.Values.ToArray())
                          .GroupBy(a => a.ProductId)
                          .Where(grp => (grp.Count() >= countSameNameAttributes));

                query =
                    from p in query
                    join sa in saq on p.Id equals sa.Key
                    select p;
            }

            // sort
            var order = (ProductSortingEnum)(context.OrderBy ?? 0);

            switch (order)
            {
            case ProductSortingEnum.NameDesc:
                query = query.OrderByDescending(p => p.Name);
                break;

            case ProductSortingEnum.PriceAsc:
                query = query.OrderBy(p => p.Price);
                break;

            case ProductSortingEnum.PriceDesc:
                query = query.OrderByDescending(p => p.Price);
                break;

            case ProductSortingEnum.CreatedOn:
                query = query.OrderByDescending(p => p.CreatedOnUtc);
                break;

            case ProductSortingEnum.CreatedOnAsc:
                query = query.OrderBy(p => p.CreatedOnUtc);
                break;

            default:
                query = query.OrderBy(p => p.Name);
                break;
            }

            // distinct cause same products can be mapped to sub-categories... too slow
            //query =
            //	from p in query
            //	group p by p.Id into grp
            //	orderby grp.Key
            //	select grp.FirstOrDefault();

            //query.ToString().Dump();
            return(query);
        }
        private List <FilterCriteria> ProductFilterableSpecAttributes(FilterProductContext context, string attributeName = null)
        {
            List <FilterCriteria> criterias = null;
            var languageId = _services.WorkContext.WorkingLanguage.Id;
            var query      = ProductFilter(context);

            var attributes =
                from p in query
                from sa in p.ProductSpecificationAttributes
                where sa.AllowFiltering
                select sa.SpecificationAttributeOption;

            if (attributeName.HasValue())
            {
                attributes = attributes.Where(a => a.SpecificationAttribute.Name == attributeName);
            }

            var grouped =
                from a in attributes
                group a by new { a.SpecificationAttributeId, a.Id } into g
                select new FilterCriteria
            {
                Name               = g.FirstOrDefault().SpecificationAttribute.Name,
                Value              = g.FirstOrDefault().Name,
                ID                 = g.Key.Id,
                PId                = g.FirstOrDefault().SpecificationAttribute.Id,
                MatchCount         = g.Count(),
                DisplayOrder       = g.FirstOrDefault().SpecificationAttribute.DisplayOrder,
                DisplayOrderValues = g.FirstOrDefault().DisplayOrder
            };

            if (_catalogSettings.SortFilterResultsByMatches)
            {
                criterias = grouped
                            .OrderBy(a => a.DisplayOrder)
                            .ThenByDescending(a => a.MatchCount)
                            .ThenBy(a => a.DisplayOrderValues)
                            .ToList();
            }
            else
            {
                criterias = grouped
                            .OrderBy(a => a.DisplayOrder)
                            .ThenBy(a => a.DisplayOrderValues)
                            .ToList();
            }

            criterias.ForEach(c =>
            {
                c.Entity     = ShortcutSpecAttribute;
                c.IsInactive = true;

                if (c.PId.HasValue)
                {
                    c.NameLocalized = _localizedEntityService.GetLocalizedValue(languageId, c.PId.Value, "SpecificationAttribute", "Name");
                }

                if (c.ID.HasValue)
                {
                    c.ValueLocalized = _localizedEntityService.GetLocalizedValue(languageId, c.ID.Value, "SpecificationAttributeOption", "Name");
                }
            });

            return(criterias);
        }