public IQueryable <CategoryItem> GetChildren( int id, int rootCategoryId = 0, bool getCategorizedItems = false, int entityTypeId = 0, string entityQualifier = null, string entityQualifierValue = null, bool showUnnamedEntityItems = true, bool showCategoriesThatHaveNoChildren = true, string includedCategoryIds = null, string excludedCategoryIds = null, string defaultIconCssClass = null, bool includeInactiveItems = true, string itemFilterPropertyName = null, string itemFilterPropertyValue = null, bool lazyLoad = true) { Person currentPerson = GetPerson(); var includedCategoryIdList = includedCategoryIds.SplitDelimitedValues().AsIntegerList().Except(new List <int> { 0 }).ToList(); var excludedCategoryIdList = excludedCategoryIds.SplitDelimitedValues().AsIntegerList().Except(new List <int> { 0 }).ToList(); defaultIconCssClass = defaultIconCssClass ?? "fa fa-list-ol"; bool hasActiveFlag = false; bool excludeInactiveItems = !includeInactiveItems; IQueryable <Category> qry = Get(); if (id == 0) { if (rootCategoryId != 0) { qry = qry.Where(a => a.ParentCategoryId == rootCategoryId); } else { qry = qry.Where(a => a.ParentCategoryId == null); } } else { qry = qry.Where(a => a.ParentCategoryId == id); } if (includedCategoryIdList.Any()) { // if includedCategoryIdList is specified, only get categories that are in the includedCategoryIdList // NOTE: no need to factor in excludedCategoryIdList since included would take precendance and the excluded ones would already not be included qry = qry.Where(a => includedCategoryIdList.Contains(a.Id)); } else if (excludedCategoryIdList.Any()) { qry = qry.Where(a => !excludedCategoryIdList.Contains(a.Id)); } IService serviceInstance = null; var cachedEntityType = EntityTypeCache.Get(entityTypeId); if (cachedEntityType != null) { qry = qry.Where(a => a.EntityTypeId == entityTypeId); if (!string.IsNullOrWhiteSpace(entityQualifier)) { qry = qry.Where(a => string.Compare(a.EntityTypeQualifierColumn, entityQualifier, true) == 0); if (!string.IsNullOrWhiteSpace(entityQualifierValue)) { qry = qry.Where(a => string.Compare(a.EntityTypeQualifierValue, entityQualifierValue, true) == 0); } else { qry = qry.Where(a => a.EntityTypeQualifierValue == null || a.EntityTypeQualifierValue == string.Empty); } } // Get the GetByCategory method if (cachedEntityType.AssemblyName != null) { Type entityType = cachedEntityType.GetEntityType(); if (entityType != null) { Type[] modelType = { entityType }; Type genericServiceType = typeof(Rock.Data.Service <>); Type modelServiceType = genericServiceType.MakeGenericType(modelType); serviceInstance = Activator.CreateInstance(modelServiceType, new object[] { new RockContext() }) as IService; hasActiveFlag = typeof(IHasActiveFlag).IsAssignableFrom(entityType); } } } excludeInactiveItems = excludeInactiveItems && hasActiveFlag; List <Category> categoryList = qry.OrderBy(c => c.Order).ThenBy(c => c.Name).ToList(); List <CategoryItem> categoryItemList = new List <CategoryItem>(); foreach (var category in categoryList) { if (category.IsAuthorized(Authorization.VIEW, currentPerson)) { var categoryItem = new CategoryItem(); categoryItem.Id = category.Id.ToString(); categoryItem.Name = category.Name; categoryItem.IsCategory = true; categoryItem.IconCssClass = category.IconCssClass; categoryItemList.Add(categoryItem); } } if (getCategorizedItems) { // if id is zero and we have a rootCategory, show the children of that rootCategory (but don't show the rootCategory) int parentItemId = id == 0 ? rootCategoryId : id; var itemsQry = GetCategorizedItems(serviceInstance, parentItemId, showUnnamedEntityItems, excludeInactiveItems, itemFilterPropertyName, itemFilterPropertyValue); if (itemsQry != null) { // do a ToList to load from database prior to ordering by name, just in case Name is a virtual property var itemsList = itemsQry.ToList(); foreach (var categorizedItem in itemsList.OrderBy(i => i.Name)) { if (categorizedItem != null && categorizedItem.IsAuthorized(Authorization.VIEW, currentPerson)) { var categoryItem = new CategoryItem(); categoryItem.Id = categorizedItem.Id.ToString(); categoryItem.Name = categorizedItem.Name; categoryItem.IsCategory = false; categoryItem.IconCssClass = categorizedItem.GetPropertyValue("IconCssClass") as string ?? defaultIconCssClass; categoryItem.IconSmallUrl = string.Empty; if (hasActiveFlag) { IHasActiveFlag activatedItem = categorizedItem as IHasActiveFlag; if (activatedItem != null && !activatedItem.IsActive) { categoryItem.IsActive = false; } } categoryItemList.Add(categoryItem); } } } if (lazyLoad) { // try to figure out which items have viewable children in the existing list and set them appropriately foreach (var categoryItemListItem in categoryItemList) { if (categoryItemListItem.IsCategory) { int parentId = int.Parse(categoryItemListItem.Id); foreach (var childCategory in Get().Where(c => c.ParentCategoryId == parentId)) { if (childCategory.IsAuthorized(Authorization.VIEW, currentPerson)) { categoryItemListItem.HasChildren = true; break; } } if (!categoryItemListItem.HasChildren) { if (getCategorizedItems) { var childItems = GetCategorizedItems(serviceInstance, parentId, showUnnamedEntityItems, excludeInactiveItems, itemFilterPropertyName, itemFilterPropertyValue); if (childItems != null) { foreach (var categorizedItem in childItems) { if (categorizedItem != null && categorizedItem.IsAuthorized(Authorization.VIEW, currentPerson)) { categoryItemListItem.HasChildren = true; break; } } } } } } } } else { foreach (var item in categoryItemList) { int parentId = int.Parse(item.Id); if (item.Children == null) { item.Children = new List <Web.UI.Controls.TreeViewItem>(); } GetAllDecendents(item, currentPerson, getCategorizedItems, defaultIconCssClass, hasActiveFlag, serviceInstance, showUnnamedEntityItems, excludeInactiveItems, itemFilterPropertyName, itemFilterPropertyValue); } } } if (!showCategoriesThatHaveNoChildren) { categoryItemList = categoryItemList.Where(a => !a.IsCategory || (a.IsCategory && a.HasChildren)).ToList(); } return(categoryItemList.AsQueryable()); }
/// <summary> /// Gets all both category and non-category item decendents for the provided categoryItem. /// This method updates the provided categoryItem. /// </summary> /// <param name="categoryItem">The category item.</param> /// <param name="currentPerson">The current person.</param> /// <param name="getCategorizedItems">if set to <c>true</c> [get categorized items].</param> /// <param name="defaultIconCssClass">The default icon CSS class.</param> /// <param name="hasActiveFlag">if set to <c>true</c> [has active flag].</param> /// <param name="serviceInstance">The service instance.</param> /// <param name="showUnnamedEntityItems">if set to <c>true</c> [show unnamed entity items].</param> /// <param name="excludeInactiveItems">if set to <c>true</c> [exclude inactive items].</param> /// <param name="itemFilterPropertyName">Name of the item filter property.</param> /// <param name="itemFilterPropertyValue">The item filter property value.</param> /// <returns></returns> private CategoryItem GetAllDecendents(CategoryItem categoryItem, Person currentPerson, bool getCategorizedItems, string defaultIconCssClass, bool hasActiveFlag, IService serviceInstance, bool showUnnamedEntityItems, bool excludeInactiveItems, string itemFilterPropertyName = null, string itemFilterPropertyValue = null) { if (categoryItem.IsCategory) { int parentId = int.Parse(categoryItem.Id); var childCategories = Get().Where(c => c.ParentCategoryId == parentId); foreach (var childCategory in childCategories) { if (childCategory.IsAuthorized(Authorization.VIEW, currentPerson)) { // This category has child categories that the person can view so add them to categoryItemList categoryItem.HasChildren = true; var childCategoryItem = new CategoryItem { Id = childCategory.Id.ToString(), Name = childCategory.Name, IsCategory = true, IconCssClass = childCategory.GetPropertyValue("IconCssClass") as string ?? defaultIconCssClass, IconSmallUrl = string.Empty }; if (hasActiveFlag) { IHasActiveFlag activatedItem = childCategory as IHasActiveFlag; if (activatedItem != null && !activatedItem.IsActive) { childCategoryItem.IsActive = false; } } var childCategorizedItemBranch = GetAllDecendents(childCategoryItem, currentPerson, getCategorizedItems, defaultIconCssClass, hasActiveFlag, serviceInstance, showUnnamedEntityItems, excludeInactiveItems, itemFilterPropertyName, itemFilterPropertyValue); if (categoryItem.Children == null) { categoryItem.Children = new List <Web.UI.Controls.TreeViewItem>(); } categoryItem.Children.Add(childCategorizedItemBranch); } } // now that we have taken care of the child categories get the items for this category. if (getCategorizedItems) { var childItems = GetCategorizedItems(serviceInstance, parentId, showUnnamedEntityItems, excludeInactiveItems, itemFilterPropertyName, itemFilterPropertyValue); if (childItems != null) { foreach (var categorizedItem in childItems) { if (categorizedItem != null && categorizedItem.IsAuthorized(Authorization.VIEW, currentPerson)) { categoryItem.HasChildren = true; var childCategoryItem = new CategoryItem { Id = categorizedItem.Id.ToString(), Name = categorizedItem.Name, IsCategory = false, IconCssClass = categorizedItem.GetPropertyValue("IconCssClass") as string ?? defaultIconCssClass, IconSmallUrl = string.Empty }; if (hasActiveFlag) { IHasActiveFlag activatedItem = categorizedItem as IHasActiveFlag; if (activatedItem != null && !activatedItem.IsActive) { childCategoryItem.IsActive = false; } } if (categoryItem.Children == null) { categoryItem.Children = new List <Web.UI.Controls.TreeViewItem>(); } categoryItem.Children.Add(childCategoryItem); } } } } } return(categoryItem); }