public virtual void LoadSpecsFilters(Category category, ISpecificationAttributeService specificationAttributeService, IWebHelper webHelper, IWorkContext workContext) { if (category == null) { throw new ArgumentNullException("category"); } var alreadyFilteredOptions = GetAlreadyFilteredSpecs(specificationAttributeService, webHelper, workContext); var notFilteredOptions = GetNotFilteredSpecs(category.Id, specificationAttributeService, webHelper, workContext); if (alreadyFilteredOptions.Count > 0 || notFilteredOptions.Count > 0) { this.Enabled = true; this.AlreadyFilteredItems = alreadyFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; return(item); }).ToList(); this.NotFilteredItems = notFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; //filter URL var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (!alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId)) { alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId); } string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return(item); }).ToList(); //remove filter URL string removeFilterUrl = webHelper.RemoveQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM); removeFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); this.RemoveFilterUrl = removeFilterUrl; } else { this.Enabled = false; } }
public virtual void PrepareSpecsFilters(IList<int> alreadyFilteredSpecOptionIds, IList<int> filterableSpecificationAttributeOptionIds, ISpecificationAttributeService specificationAttributeService, IWebHelper webHelper, IWorkContext workContext) { var allFilters = new List<SpecificationAttributeOptionFilter>(); var specificationAttributeOptions = specificationAttributeService .GetSpecificationAttributeOptionsByIds(filterableSpecificationAttributeOptionIds != null ? filterableSpecificationAttributeOptionIds.ToArray() : null); foreach (var sao in specificationAttributeOptions) { var sa = sao.SpecificationAttribute; if (sa != null) { allFilters.Add(new SpecificationAttributeOptionFilter { SpecificationAttributeId = sa.Id, SpecificationAttributeName = sa.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeDisplayOrder = sa.DisplayOrder, SpecificationAttributeOptionId = sao.Id, SpecificationAttributeOptionName = sao.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeOptionDisplayOrder = sao.DisplayOrder }); } } //sort loaded options allFilters = allFilters.OrderBy(saof => saof.SpecificationAttributeDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeName) .ThenBy(saof => saof.SpecificationAttributeOptionDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeOptionName).ToList(); //get already filtered specification options var alreadyFilteredOptions = allFilters .Where(x => alreadyFilteredSpecOptionIds.Contains(x.SpecificationAttributeOptionId)) .Select(x => x) .ToList(); //get not filtered specification options var notFilteredOptions = new List<SpecificationAttributeOptionFilter>(); foreach (var saof in allFilters) { //do not add already filtered specification options if (alreadyFilteredOptions.FirstOrDefault(x => x.SpecificationAttributeId == saof.SpecificationAttributeId) != null) continue; //else add it notFilteredOptions.Add(saof); } //prepare the model properties if (alreadyFilteredOptions.Count > 0 || notFilteredOptions.Count > 0) { this.Enabled = true; this.AlreadyFilteredItems = alreadyFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; return item; }).ToList(); this.NotFilteredItems = notFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; //filter URL var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (!alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId)) alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId); string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return item; }).ToList(); //remove filter URL string removeFilterUrl = webHelper.RemoveQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM); removeFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); this.RemoveFilterUrl = removeFilterUrl; } else { this.Enabled = false; } }
public virtual void PrepareSpecsFilters(IList <string> alreadyFilteredSpecOptionIds, IList <string> filterableSpecificationAttributeOptionIds, ISpecificationAttributeService specificationAttributeService, IWebHelper webHelper, IWorkContext workContext, ICacheManager cacheManager) { string cacheKey = string.Format(ModelCacheEventConsumer.SPECS_FILTER_MODEL_KEY, filterableSpecificationAttributeOptionIds != null ? string.Join(",", filterableSpecificationAttributeOptionIds) : "", workContext.WorkingLanguage.Id); var allFilters = cacheManager.Get(cacheKey, () => { var _allFilters = new List <SpecificationAttributeOptionFilter>(); foreach (var sao in filterableSpecificationAttributeOptionIds) { int _specificationAttributeId = Convert.ToInt32(sao.Split(':').FirstOrDefault().ToString()); int _specificationAttributeOptionId = Convert.ToInt32(sao.Split(':').LastOrDefault().ToString()); var sa = EngineContext.Current.Resolve <ISpecificationAttributeService>().GetSpecificationAttributeById(_specificationAttributeId); if (sa != null) { _allFilters.Add(new SpecificationAttributeOptionFilter { SpecificationAttributeId = sa.Id, SpecificationAttributeName = sa.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeDisplayOrder = sa.DisplayOrder, SpecificationAttributeOptionId = sao, //_specificationAttributeOptionId.ToString(), //sa.SpecificationAttributeOptions.FirstOrDefault(x=>x.Id == ), SpecificationAttributeOptionName = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == _specificationAttributeOptionId).GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeOptionDisplayOrder = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == _specificationAttributeOptionId).DisplayOrder }); } } return(_allFilters.ToList()); }); //sort loaded options allFilters = allFilters.OrderBy(saof => saof.SpecificationAttributeDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeName) .ThenBy(saof => saof.SpecificationAttributeOptionDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeOptionName).ToList(); //get already filtered specification options var alreadyFilteredOptions = allFilters .Where(x => alreadyFilteredSpecOptionIds.Contains(x.SpecificationAttributeOptionId)) .Select(x => x) .ToList(); //get not filtered specification options var notFilteredOptions = new List <SpecificationAttributeOptionFilter>(); foreach (var saof in allFilters) { //do not add already filtered specification options if (alreadyFilteredOptions.FirstOrDefault(x => x.SpecificationAttributeId == saof.SpecificationAttributeId) != null) { continue; } //else add it notFilteredOptions.Add(saof); } //prepare the model properties if (alreadyFilteredOptions.Count > 0 || notFilteredOptions.Count > 0) { this.Enabled = true; this.AlreadyFilteredItems = alreadyFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; return(item); }).ToList(); this.NotFilteredItems = notFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; //filter URL var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (!alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId.ToString())) { alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId.ToString()); } string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return(item); }).ToList(); //remove filter URL string removeFilterUrl = webHelper.RemoveQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM); removeFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); this.RemoveFilterUrl = removeFilterUrl; } else { this.Enabled = false; } }
public virtual void PrepareSpecsFilters(IList <int> alreadyFilteredSpecOptionIds, IList <int> filterableSpecificationAttributeOptionIds, ISpecificationAttributeService specificationAttributeService, IWebHelper webHelper, IWorkContext workContext) { var allFilters = new List <SpecificationAttributeOptionFilter>(); var specificationAttributeOptions = specificationAttributeService .GetSpecificationAttributeOptionsByIds(filterableSpecificationAttributeOptionIds != null ? filterableSpecificationAttributeOptionIds.ToArray() : null); foreach (var sao in specificationAttributeOptions) { var sa = sao.SpecificationAttribute; if (sa != null) { allFilters.Add(new SpecificationAttributeOptionFilter { SpecificationAttributeId = sa.Id, SpecificationAttributeName = sa.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeDisplayOrder = sa.DisplayOrder, SpecificationAttributeOptionId = sao.Id, SpecificationAttributeOptionName = sao.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeOptionDisplayOrder = sao.DisplayOrder }); } } //sort loaded options allFilters = allFilters.OrderBy(saof => saof.SpecificationAttributeDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeName) .ThenBy(saof => saof.SpecificationAttributeOptionDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeOptionName).ToList(); //get filtered specification options var allFilteredOptions = new List <SpecificationAttributeOptionFilter>(); foreach (var saof in allFilters) { allFilteredOptions.Add(saof); } //get already filtered specification options var alreadyFilteredOptions = allFilters .Where(x => alreadyFilteredSpecOptionIds.Contains(x.SpecificationAttributeOptionId)) .Select(x => x) .ToList(); //get not filtered specification options var notFilteredOptions = new List <SpecificationAttributeOptionFilter>(); foreach (var saof in allFilters) { //do not add already filtered specification options if (alreadyFilteredOptions.FirstOrDefault(x => x.SpecificationAttributeId == saof.SpecificationAttributeId) != null) { continue; } //else add it notFilteredOptions.Add(saof); } //prepare the model properties this.AllFilteredItems = allFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; item.SpecificationAttributeOptionId = x.SpecificationAttributeOptionId; //filter URL var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (!alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId)) { alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId); } else { item.isSelected = true; alreadyFilteredOptionIds.Remove(x.SpecificationAttributeOptionId); } string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return(item); }).ToList(); if (alreadyFilteredOptions.Count > 0 || notFilteredOptions.Count > 0) { this.Enabled = true; this.AlreadyFilteredItems = alreadyFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; return(item); }).ToList(); this.NotFilteredItems = notFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; //filter URL var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (!alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId)) { alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId); } string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return(item); }).ToList(); //remove filter URL string removeFilterUrl = webHelper.RemoveQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM); removeFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); this.RemoveFilterUrl = removeFilterUrl; } else { this.Enabled = false; } }
public virtual void LoadSpecsFilters(Category category, ISpecificationAttributeService specificationAttributeService, IWebHelper webHelper, IWorkContext workContext) { if (category == null) throw new ArgumentNullException("category"); var alreadyFilteredOptions = GetAlreadyFilteredSpecs(specificationAttributeService, webHelper, workContext); var notFilteredOptions = GetNotFilteredSpecs(category.Id, specificationAttributeService, webHelper, workContext); if (alreadyFilteredOptions.Count > 0 || notFilteredOptions.Count > 0) { this.Enabled = true; this.AlreadyFilteredItems = alreadyFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; return item; }).ToList(); this.NotFilteredItems = notFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; //filter URL var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (!alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId)) alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId); string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return item; }).ToList(); //remove filter URL string removeFilterUrl = webHelper.RemoveQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM); removeFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); this.RemoveFilterUrl = removeFilterUrl; } else { this.Enabled = false; } }
public virtual void PrepareSpecsFilters(IList<string> alreadyFilteredSpecOptionIds, IList<string> filterableSpecificationAttributeOptionIds, ISpecificationAttributeService specificationAttributeService, IWebHelper webHelper, IWorkContext workContext, ICacheManager cacheManager) { string cacheKey = string.Format(ModelCacheEventConsumer.SPECS_FILTER_MODEL_KEY, filterableSpecificationAttributeOptionIds != null ? string.Join(",", filterableSpecificationAttributeOptionIds) : "", workContext.WorkingLanguage.Id); var allFilters = cacheManager.Get(cacheKey, () => { var _allFilters = new List<SpecificationAttributeOptionFilter>(); foreach (var sao in filterableSpecificationAttributeOptionIds) { int _specificationAttributeId = Convert.ToInt32(sao.Split(':').FirstOrDefault().ToString()); int _specificationAttributeOptionId = Convert.ToInt32(sao.Split(':').LastOrDefault().ToString()); var sa = EngineContext.Current.Resolve<ISpecificationAttributeService>().GetSpecificationAttributeById(_specificationAttributeId); if (sa != null) { _allFilters.Add(new SpecificationAttributeOptionFilter { SpecificationAttributeId = sa.Id, SpecificationAttributeName = sa.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeDisplayOrder = sa.DisplayOrder, SpecificationAttributeOptionId = sao, //_specificationAttributeOptionId.ToString(), //sa.SpecificationAttributeOptions.FirstOrDefault(x=>x.Id == ), SpecificationAttributeOptionName = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == _specificationAttributeOptionId).GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeOptionDisplayOrder = sa.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == _specificationAttributeOptionId).DisplayOrder }); } } return _allFilters.ToList(); }); //sort loaded options allFilters = allFilters.OrderBy(saof => saof.SpecificationAttributeDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeName) .ThenBy(saof => saof.SpecificationAttributeOptionDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeOptionName).ToList(); //get already filtered specification options var alreadyFilteredOptions = allFilters .Where(x => alreadyFilteredSpecOptionIds.Contains(x.SpecificationAttributeOptionId)) .Select(x => x) .ToList(); //get not filtered specification options var notFilteredOptions = new List<SpecificationAttributeOptionFilter>(); foreach (var saof in allFilters) { //do not add already filtered specification options if (alreadyFilteredOptions.FirstOrDefault(x => x.SpecificationAttributeId == saof.SpecificationAttributeId) != null) continue; //else add it notFilteredOptions.Add(saof); } //prepare the model properties if (alreadyFilteredOptions.Count > 0 || notFilteredOptions.Count > 0) { this.Enabled = true; this.AlreadyFilteredItems = alreadyFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; return item; }).ToList(); this.NotFilteredItems = notFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; //filter URL var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (!alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId.ToString())) alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId.ToString()); string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return item; }).ToList(); //remove filter URL string removeFilterUrl = webHelper.RemoveQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM); removeFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); this.RemoveFilterUrl = removeFilterUrl; } else { this.Enabled = false; } }
public virtual void PrepareSpecsFilters(IList <CategorySpecificationAtrribute> filterableSpecificationAttributes, ISpecificationAttributeService specificationAttributeService, IWebHelper webHelper, IWorkContext workContext, string baseUrl) { var allFilters = new List <SpecificationAttributeOptionFilter>(); foreach (var attr in filterableSpecificationAttributes) { var specificationAttributeOptions = specificationAttributeService.GetSpecificationAttributeOptionsBySpecificationAttribute(attr.SpecificationAttributeId); foreach (var sao in specificationAttributeOptions) { var sa = sao.SpecificationAttribute; if (sa != null) { allFilters.Add(new SpecificationAttributeOptionFilter { SpecificationAttributeId = sa.Id, SpecificationAttributeName = sa.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeDisplayOrder = sa.DisplayOrder, SpecificationAttributeOptionId = sao.Id, SpecificationAttributeOptionName = sao.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeOptionDisplayOrder = sao.DisplayOrder }); } } } //sort loaded options allFilters = allFilters.OrderBy(saof => saof.SpecificationAttributeDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeName) .ThenBy(saof => saof.SpecificationAttributeOptionDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeOptionName).ToList(); //get not filtered specification options var notFilteredOptions = new List <SpecificationAttributeOptionFilter>(); foreach (var saof in allFilters) { //else add it notFilteredOptions.Add(saof); } this.NotFilteredItems = notFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; //filter URL var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (!alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId)) { alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId); } string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(baseUrl, QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return(item); }).ToList(); }
public virtual void PrepareSpecsFilters(IList <int> alreadyFilteredSpecOptionIds, IList <CategorySpecificationAtrribute> filterableSpecificationAttributes, ISpecificationAttributeService specificationAttributeService, IWebHelper webHelper, IWorkContext workContext) { var allFilters = new List <SpecificationAttributeOptionFilter>(); foreach (var attr in filterableSpecificationAttributes) { var specificationAttributeOptions = specificationAttributeService.GetSpecificationAttributeOptionsBySpecificationAttribute(attr.SpecificationAttributeId); foreach (var sao in specificationAttributeOptions) { var sa = sao.SpecificationAttribute; if (sa != null) { allFilters.Add(new SpecificationAttributeOptionFilter { SpecificationAttributeId = sa.Id, SpecificationAttributeName = sa.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeDisplayOrder = sa.DisplayOrder, SpecificationAttributeOptionId = sao.Id, SpecificationAttributeOptionName = sao.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeOptionDisplayOrder = sao.DisplayOrder }); } } } //sort loaded options allFilters = allFilters.OrderBy(saof => saof.SpecificationAttributeDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeName) .ThenBy(saof => saof.SpecificationAttributeOptionDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeOptionName).ToList(); //get already filtered specification options var alreadyFilteredOptions = allFilters .Where(x => alreadyFilteredSpecOptionIds.Contains(x.SpecificationAttributeOptionId)) .Select(x => x) .ToList(); //get not filtered specification options var notFilteredOptions = new List <SpecificationAttributeOptionFilter>(); foreach (var saof in allFilters) { //do not add already filtered specification options //所有可筛选条件都显示出来 //if (alreadyFilteredOptions.FirstOrDefault(x => x.SpecificationAttributeId == saof.SpecificationAttributeId) != null) // continue; //else add it notFilteredOptions.Add(saof); } //prepare the model properties if (alreadyFilteredOptions.Count > 0 || notFilteredOptions.Count > 0) { this.Enabled = true; Dictionary <int, int> alreadyFilterAttribute = new Dictionary <int, int>(); this.AlreadyFilteredItems = alreadyFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; if (!alreadyFilterAttribute.ContainsKey(x.SpecificationAttributeId)) { alreadyFilterAttribute.Add(x.SpecificationAttributeId, x.SpecificationAttributeOptionId); } var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId)) { alreadyFilteredOptionIds.Remove(x.SpecificationAttributeOptionId); } string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return(item); }).ToList(); this.NotFilteredItems = notFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = x.SpecificationAttributeOptionName; //filter URL List <int> alreadyFilteredOptionIds = new List <int>(); var _alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); foreach (int oid in _alreadyFilteredOptionIds) { int aid = 0; if (alreadyFilterAttribute.TryGetValue(x.SpecificationAttributeId, out aid) && aid == oid) { alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId); } else { alreadyFilteredOptionIds.Add(oid); } } if (!alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId)) { alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId); } if (alreadyFilterAttribute.ContainsValue(x.SpecificationAttributeOptionId)) { item.Filtered = false; } else { item.Filtered = true; } string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return(item); }).ToList(); //remove filter URL string removeFilterUrl = webHelper.RemoveQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM); removeFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); removeFilterUrl = webHelper.RemoveQueryString(removeFilterUrl, PriceRangeFilterModel.QUERYSTRINGPARAM); removeFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); this.RemoveFilterUrl = removeFilterUrl; } else { this.Enabled = false; } }
public virtual void PrepareSpecsFilters( Category category, int productsTotalAmount, IEnumerable <SpecificationAttributeService.SpecificationAttributeOptionWithCount> allOptionsCountTbl, IList <int> alreadyFilteredSpecOptionIds, IList <int> filterableSpecificationAttributeOptionIds, ISpecificationAttributeService specificationAttributeService, IWebHelper webHelper, IWorkContext workContext) { var allFilters = new List <SpecificationAttributeOptionFilter>(); if (filterableSpecificationAttributeOptionIds != null) { // Для оптимизации нужно вытащить сразу все спецификации и их значения var allSpecsAttribs = specificationAttributeService.GetSpecificationAttributeOptionsBySpecificationAttributeList(filterableSpecificationAttributeOptionIds); foreach (var sao in allSpecsAttribs) // цикл по айдишникам значений { if (sao != null) { var sa = sao.SpecificationAttribute; // вытаскиваем из БД спецификацию if (sa != null) { // не показываем для некоторых категорий некоторые фильтры bool isShowInFilter; bool isShowInShortDescription; YandexMarketHelpers.IsSpecAllow(sa.Name, new List <Category>() { category }, out isShowInFilter, out isShowInShortDescription); if (!isShowInFilter) { continue; } int existTimes = GetSpecificationAttributeOptionExistTimesInFilteredProducts(allOptionsCountTbl, sao.Id); allFilters.Add( new SpecificationAttributeOptionFilter { SpecificationAttributeId = sa.Id, SpecificationAttributeName = sa.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeDisplayOrder = sa.DisplayOrder, SpecificationAttributeOptionId = sao.Id, SpecificationAttributeOptionName = sao.GetLocalized(x => x.Name, workContext.WorkingLanguage.Id), SpecificationAttributeOptionDisplayOrder = sao.DisplayOrder, SpecificationAttributeOptionExistTimesInFilteredProducts = existTimes }); } } } // end for } //sort loaded options allFilters = allFilters.OrderBy(saof => saof.SpecificationAttributeDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeName) .ThenBy(saof => saof.SpecificationAttributeOptionDisplayOrder) .ThenBy(saof => saof.SpecificationAttributeOptionName, new SpecOptionComp()).ToList(); //get already filtered specification options var alreadyFilteredOptions = allFilters .Where(x => alreadyFilteredSpecOptionIds.Contains(x.SpecificationAttributeOptionId)) .Select(x => x) .ToList(); //get not filtered specification options var notFilteredOptions = new List <SpecificationAttributeOptionFilter>(); foreach (var saof in allFilters) { //do not add already filtered specification options if (alreadyFilteredOptions.FirstOrDefault(x => x.SpecificationAttributeId == saof.SpecificationAttributeId) != null) { continue; } //else add it if (productsTotalAmount != saof.SpecificationAttributeOptionExistTimesInFilteredProducts) { notFilteredOptions.Add(saof); } } //prepare the model properties if (alreadyFilteredOptions.Count > 0 || notFilteredOptions.Count > 0) { this.Enabled = true; this.AlreadyFilteredItems = alreadyFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = RemoveHtmlTags(x.SpecificationAttributeOptionName); //filter URL var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId)) { alreadyFilteredOptionIds.Remove(x.SpecificationAttributeOptionId); // add to old list me } string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return(item); }).ToList(); this.NotFilteredItems = notFilteredOptions.ToList().Select(x => { var item = new SpecificationFilterItem(); item.SpecificationAttributeName = x.SpecificationAttributeName; item.SpecificationAttributeOptionName = RemoveHtmlTags(x.SpecificationAttributeOptionName); item.SpecificationAttributeOptionExistTimesInFilteredProducts = x.SpecificationAttributeOptionExistTimesInFilteredProducts; //filter URL var alreadyFilteredOptionIds = GetAlreadyFilteredSpecOptionIds(webHelper); if (!alreadyFilteredOptionIds.Contains(x.SpecificationAttributeOptionId)) { alreadyFilteredOptionIds.Add(x.SpecificationAttributeOptionId); // add to old list me } string newQueryParam = GenerateFilteredSpecQueryParam(alreadyFilteredOptionIds); string filterUrl = webHelper.ModifyQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM + "=" + newQueryParam, null); filterUrl = ExcludeQueryStringParams(filterUrl, webHelper); item.FilterUrl = filterUrl; return(item); }).ToList(); //remove filter URL string removeFilterUrl = webHelper.RemoveQueryString(webHelper.GetThisPageUrl(true), QUERYSTRINGPARAM); removeFilterUrl = ExcludeQueryStringParams(removeFilterUrl, webHelper); this.RemoveFilterUrl = removeFilterUrl; } else { this.Enabled = false; } }