private async Task <List <CategorySimpleModel> > PrepareCategorySimpleModels(Language language, Store store, Customer customer, string rootCategoryId = "", bool loadSubCategories = true, IList <Category> allCategories = null) { var result = new List <CategorySimpleModel>(); var categories = allCategories.Where(c => c.ParentCategoryId == rootCategoryId).OrderBy(x => x.DisplayOrder).ToList(); foreach (var category in categories) { var picture = await _pictureService.GetPictureById(category.PictureId); var categoryModel = new CategorySimpleModel { Id = category.Id, Name = category.GetTranslation(x => x.Name, language.Id), SeName = category.GetSeName(language.Id), IncludeInMenu = category.IncludeInMenu, Flag = category.GetTranslation(x => x.Flag, language.Id), FlagStyle = category.FlagStyle, Icon = category.Icon, ImageUrl = await _pictureService.GetPictureUrl(picture, _mediaSettings.CategoryThumbPictureSize), UserFields = category.UserFields }; if (loadSubCategories) { var subCategories = await PrepareCategorySimpleModels(language, store, customer, category.Id, loadSubCategories, allCategories); categoryModel.SubCategories.AddRange(subCategories); } result.Add(categoryModel); } return(result); }
private async Task <List <CategorySimpleModel> > PrepareCategorySimpleModels(int rootCategoryId, bool loadSubCategories = true) { var result = new List <CategorySimpleModel>(); var catSubCatProductCountsList = _categoryService.GetNoOfProductsAndSubCategoriesByCategories(); var allCategories = await _categoryService.GetAllCategories(); var categories = allCategories.Where(c => c.CategoryId == rootCategoryId).ToList(); foreach (var category in allCategories) { var categoryModel = new CategorySimpleModel { Id = category.CategoryId, Name = category.CategoryName, SeName = category.CategoryName, IncludeInTopMenu = true }; //number of products in each category categoryModel.NumberOfProducts = catSubCatProductCountsList.Where(a => a.CategoryId == categoryModel.Id) .Select(c => c.ProductCount).FirstOrDefault(); if (loadSubCategories) { var subCategories = await PrepareSubCategorySimpleModels(category.CategoryId, loadSubCategories); categoryModel.SubCategories.AddRange(subCategories); } result.Add(categoryModel); } return(result); }
private async Task <List <CategorySimpleModel> > PrepareSubCategorySimpleModels(int currentCategoryId, bool loadSubCategories) { var result = new List <CategorySimpleModel>(); var allSubCategories = await _subCategoryService.GetAllSubCategories(); var subCategories = allSubCategories.Where(c => c.CategoryId == currentCategoryId).ToList(); foreach (var subCategory in subCategories) { var categoryModel = new CategorySimpleModel { Id = subCategory.CategoryId, Name = subCategory.SubCategoryName, SeName = subCategory.SubCategoryName, IncludeInTopMenu = true }; //number of products in each category categoryModel.NumberOfProducts = 5; result.Add(categoryModel); } return(result); }
/// <summary> /// Prepare category (simple) models /// </summary> /// <param name="rootCategoryId">Root category identifier</param> /// <param name="loadSubCategories">A value indicating whether subcategories should be loaded</param> /// <returns>List of category (simple) models</returns> public virtual List <CategorySimpleModel> PrepareCategorySimpleModels(int rootCategoryId, bool loadSubCategories = true) { var result = new List <CategorySimpleModel>(); //little hack for performance optimization //we know that this method is used to load top and left menu for categories. //it'll load all categories anyway. //so there's no need to invoke "GetAllCategoriesByParentCategoryId" multiple times (extra SQL commands) to load childs //so we load all categories at once (we know they are cached) var allCategories = _categoryService.GetAllCategories(storeId: _storeContext.CurrentStore.Id); var categories = allCategories.Where(c => c.ParentCategoryId == rootCategoryId).ToList(); foreach (var category in categories) { var categoryModel = new CategorySimpleModel { Id = category.Id, Name = _localizationService.GetLocalized(category, x => x.Name), SeName = _urlRecordService.GetSeName(category), IncludeInTopMenu = category.IncludeInTopMenu }; if (loadSubCategories) { var subCategories = PrepareCategorySimpleModels(category.Id, loadSubCategories); categoryModel.SubCategories.AddRange(subCategories); } result.Add(categoryModel); } return(result); }
protected IList <CategorySimpleModel> PrepareCategorySimpleModels(int rootCategoryId, IList <int> loadSubCategoriesForIds, int level, int levelsToLoad, bool validateIncludeInTopMenu) { var result = new List <CategorySimpleModel>(); foreach (var category in _categoryService.GetAllCategoriesByParentCategoryId(rootCategoryId)) { if (validateIncludeInTopMenu && !category.IncludeInTopMenu) { continue; } var categoryModel = new CategorySimpleModel() { Id = category.Id, Name = category.GetLocalized(x => x.Name), SeName = category.GetSeName(), PictureUrl = _pictureService.GetPictureUrl(category.PictureId), Description = category.Description }; //product number for each category if (_catalogSettings.ShowCategoryProductNumber) { categoryModel.NumberOfProducts = GetCategoryProductNumber(category.Id); } //load subcategories? bool loadSubCategories = false; if (loadSubCategoriesForIds == null) { //load all subcategories loadSubCategories = true; } else { //we load subcategories only for certain categories for (int i = 0; i <= loadSubCategoriesForIds.Count - 1; i++) { if (loadSubCategoriesForIds[i] == category.Id) { loadSubCategories = true; break; } } } if (levelsToLoad <= level) { loadSubCategories = false; } if (loadSubCategories) { var subCategories = PrepareCategorySimpleModels(category.Id, loadSubCategoriesForIds, level + 1, levelsToLoad, validateIncludeInTopMenu); categoryModel.SubCategories.AddRange(subCategories); } result.Add(categoryModel); } return(result); }
private IList <CategorySimpleModel> PrepareSubCategories(Category category) { var categories = _categoryService.Categories().Where(c => c.ParentCategoryId == category.Id); var subCategories = new List <CategorySimpleModel>(); foreach (var c in categories) { var catModel = new CategorySimpleModel() { Name = c.Name, SeName = c.GetSeName() }; subCategories.Add(catModel); } return(subCategories); }
public IViewComponentResult Invoke() { var model = new TopMenuModel(); var result = new List <CategorySimpleModel>(); var allCategories = _categoryService.GetAllCategories(); foreach (var category in allCategories) { var categoryModel = new CategorySimpleModel { Id = category.Id, Name = category.Name, SeName = FriendlyUrlHelper.GetFriendlyTitle(_urlRecordService.GetSeName(category), true), IncludeInTopMenu = category.IncludeInTopMenu, ParentCategoryId = category.ParentCategoryId }; result.Add(categoryModel); } model.Categories = result; return(View(model)); }
public async Task <IViewComponentResult> InvokeAsync() { string key = ModelCacheEventConsumer.MENU_BREADCRUMB_PATTERN_KEY; var cachedEntry = _cacheManager.Get(key, () => { var model = new MenuModel(); var categories = _categoryService.Categories(true).Where(c => c.ParentCategoryId == 0); foreach (var c in categories) { var catModel = new CategorySimpleModel() { Name = c.Name, SeName = c.GetSeName(), SubCategories = PrepareSubCategories(c) }; model.Categories.Add(catModel); } return(model); }); return(View(cachedEntry)); }
protected virtual IList <CategorySimpleModel> PrepareCategorySimpleModels(int rootCategoryId, bool loadSubCategories = true, IList <Category> allCategories = null) { var result = new List <CategorySimpleModel>(); if (allCategories == null) { allCategories = _categoryService.GetAllCategories(); } var categories = allCategories.Where(c => c.ParentCategoryId == rootCategoryId).ToList(); foreach (var category in categories) { var categoryModel = new CategorySimpleModel { Id = category.Id, Name = category.Name, // [todo] заюзать локализацию SeName = "SeName", // [todo] получить seo IncludeInTopMenu = category.IncludeInTopMenu }; // количество вещей в каждой категории категории var categoryIds = new List <int>(); categoryIds.Add(category.Id); // включая подкатегории categoryIds.AddRange(GetChildCategoryIds(category.Id)); categoryModel.NumberOfProducts = _itemService.GetNumberOfItemsInCategory(categoryIds); if (loadSubCategories) { var subCategories = PrepareCategorySimpleModels(category.Id, loadSubCategories, allCategories); categoryModel.SubCategories.AddRange(subCategories); } result.Add(categoryModel); } return(result); }
/// <summary> /// Prepare category (simple) models /// </summary> /// <param name="rootCategoryId">Root category identifier</param> /// <param name="loadSubCategories">A value indicating whether subcategories should be loaded</param> /// <param name="allCategories">All available categories; pass null to load them internally</param> /// <returns>Category models</returns> public virtual List <CategorySimpleModel> PrepareCategorySimpleModels(int rootCategoryId, bool loadSubCategories = true, IList <Category> allCategories = null) { var result = new List <CategorySimpleModel>(); //little hack for performance optimization. //we know that this method is used to load top and left menu for categories. //it'll load all categories anyway. //so there's no need to invoke "GetAllCategoriesByParentCategoryId" multiple times (extra SQL commands) to load childs //so we load all categories at once //if you don't like this implementation if you can uncomment the line below (old behavior) and comment several next lines (before foreach) //var categories = _categoryService.GetAllCategoriesByParentCategoryId(rootCategoryId); if (allCategories == null) { //load categories if null passed //we implemeneted it this way for performance optimization - recursive iterations (below) //this way all categories are loaded only once allCategories = _categoryService.GetAllCategories(storeId: _storeContext.CurrentStore.Id); } var categories = allCategories.Where(c => c.ParentCategoryId == rootCategoryId).ToList(); foreach (var category in categories) { var categoryModel = new CategorySimpleModel { Id = category.Id, Name = category.GetLocalized(x => x.Name), SeName = category.GetSeName(), IncludeInTopMenu = category.IncludeInTopMenu }; //number of products in each category if (_catalogSettings.ShowCategoryProductNumber) { string cacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_NUMBER_OF_PRODUCTS_MODEL_KEY, string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()), _storeContext.CurrentStore.Id, category.Id); categoryModel.NumberOfProducts = _cacheManager.Get(cacheKey, () => { var categoryIds = new List <int>(); categoryIds.Add(category.Id); //include subcategories if (_catalogSettings.ShowCategoryProductNumberIncludingSubcategories) { categoryIds.AddRange(GetChildCategoryIds(category.Id)); } return(_productService.GetNumberOfProductsInCategory(categoryIds, _storeContext.CurrentStore.Id)); }); } if (loadSubCategories) { var subCategories = PrepareCategorySimpleModels(category.Id, loadSubCategories, allCategories); categoryModel.SubCategories.AddRange(subCategories); } result.Add(categoryModel); } return(result); }
protected IList <CategorySimpleModel> PrepareCategorySimpleModels(IList <CategoryOverviewModel> items, bool forBoxMenu = false) { var models = new List <CategorySimpleModel>(); var categoryMediaPath = _mediaSettings.CategoryMediaPath; foreach (var item in items) { var model = new CategorySimpleModel { Name = item.Name, //Url = Url.RouteUrl("Category", new { top = item.UrlKey }), Url = string.Format("{0}category/{1}", _storeInformationSettings.StoreFrontLink, item.UrlKey), Picture = new PictureModel { ImageUrl = string.Format("{0}{1}", categoryMediaPath, item.ThumbnailFilename), AlternateText = item.Name, Title = item.Name } }; models.Add(model); } if (forBoxMenu) { models.Add(new CategorySimpleModel { Name = "Offers", //Url = Url.RouteUrl("Special Offers"), Url = string.Format("{0}special-offers", _storeInformationSettings.StoreFrontLink), Picture = new PictureModel { ImageUrl = string.Format("{0}{1}", categoryMediaPath, "box-menu-offer.png"), AlternateText = "Offers", Title = "Offers" } }); models.Add(new CategorySimpleModel { Name = "i-Zone", //Url = Url.RouteUrl("i-Zone"), Url = string.Format("{0}blog", _storeInformationSettings.StoreFrontLink), Picture = new PictureModel { ImageUrl = string.Format("{0}{1}", categoryMediaPath, "box-menu-izone.png"), AlternateText = "i-Zone", Title = "i-Zone" } }); models.Add(new CategorySimpleModel { Name = "Shop By Brands", //Url = Url.RouteUrl("Shop By Brand"), Url = string.Format("{0}brands", _storeInformationSettings.StoreFrontLink), Picture = new PictureModel { ImageUrl = string.Format("{0}{1}", categoryMediaPath, "box-menu-shopbybrand.png"), AlternateText = "Shop By Brands", Title = "Shop By Brands" } }); } return(models); }