/// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            mdCategoryTreeConfig.Visible = false;

            bool canEditBlock = IsUserAuthorized(Authorization.EDIT);

            // hide all the actions if user doesn't have EDIT to the block
            divTreeviewActions.Visible = canEditBlock;

            var detailPageReference = new Rock.Web.PageReference(GetAttributeValue("DetailPage"));

            // NOTE: if the detail page is the current page, use the current route instead of route specified in the DetailPage (to preserve old behavior)
            if (detailPageReference == null || detailPageReference.PageId == this.RockPage.PageId)
            {
                hfPageRouteTemplate.Value = (this.RockPage.RouteData.Route as System.Web.Routing.Route).Url;
                hfDetailPageUrl.Value     = new Rock.Web.PageReference(this.RockPage.PageId).BuildUrl().RemoveLeadingForwardslash();
            }
            else
            {
                hfPageRouteTemplate.Value = string.Empty;
                var pageCache = PageCache.Read(detailPageReference.PageId);
                if (pageCache != null)
                {
                    var route = pageCache.PageRoutes.FirstOrDefault(a => a.Id == detailPageReference.RouteId);
                    if (route != null)
                    {
                        hfPageRouteTemplate.Value = route.Route;
                    }
                }

                hfDetailPageUrl.Value = detailPageReference.BuildUrl().RemoveLeadingForwardslash();
            }

            // Get EntityTypeName
            Guid?entityTypeGuid = GetAttributeValue("EntityType").AsGuidOrNull();

            nbWarning.Text    = "Please select an entity type in the block settings.";
            nbWarning.Visible = !entityTypeGuid.HasValue;
            if (entityTypeGuid.HasValue)
            {
                int    entityTypeId             = Rock.Web.Cache.EntityTypeCache.Read(entityTypeGuid.Value).Id;
                string entityTypeQualiferColumn = GetAttributeValue("EntityTypeQualifierProperty");
                string entityTypeQualifierValue = GetAttributeValue("EntityTypeQualifierValue");
                bool   showUnnamedEntityItems   = GetAttributeValue("ShowUnnamedEntityItems").AsBooleanOrNull() ?? true;

                string parms = string.Format("?getCategorizedItems=true&showUnnamedEntityItems={0}", showUnnamedEntityItems.ToTrueFalse().ToLower());
                parms += string.Format("&entityTypeId={0}", entityTypeId);

                var rootCategory = CategoryCache.Read(this.GetAttributeValue("RootCategory").AsGuid());

                // make sure the rootCategory matches the EntityTypeId (just in case they changed the EntityType after setting RootCategory
                if (rootCategory != null && rootCategory.EntityTypeId == entityTypeId)
                {
                    parms += string.Format("&rootCategoryId={0}", rootCategory.Id);
                }

                if (!string.IsNullOrEmpty(entityTypeQualiferColumn))
                {
                    parms += string.Format("&entityQualifier={0}", entityTypeQualiferColumn);

                    if (!string.IsNullOrEmpty(entityTypeQualifierValue))
                    {
                        parms += string.Format("&entityQualifierValue={0}", entityTypeQualifierValue);
                    }
                }

                var        excludeCategoriesGuids = this.GetAttributeValue("ExcludeCategories").SplitDelimitedValues().AsGuidList();
                List <int> excludedCategoriesIds  = new List <int>();
                if (excludeCategoriesGuids != null && excludeCategoriesGuids.Any())
                {
                    foreach (var excludeCategoryGuid in excludeCategoriesGuids)
                    {
                        var excludedCategory = CategoryCache.Read(excludeCategoryGuid);
                        if (excludedCategory != null)
                        {
                            excludedCategoriesIds.Add(excludedCategory.Id);
                        }
                    }

                    parms += string.Format("&excludedCategoryIds={0}", excludedCategoriesIds.AsDelimited(","));
                }

                string defaultIconCssClass = GetAttributeValue("DefaultIconCSSClass");
                if (!string.IsNullOrWhiteSpace(defaultIconCssClass))
                {
                    parms += string.Format("&defaultIconCssClass={0}", defaultIconCssClass);
                }

                RestParms = parms;

                var cachedEntityType = Rock.Web.Cache.EntityTypeCache.Read(entityTypeId);
                if (cachedEntityType != null)
                {
                    string entityTypeFriendlyName = GetAttributeValue("EntityTypeFriendlyName");
                    if (string.IsNullOrWhiteSpace(entityTypeFriendlyName))
                    {
                        entityTypeFriendlyName = cachedEntityType.FriendlyName;
                    }

                    lbAddItem.ToolTip = "Add " + entityTypeFriendlyName;
                    lAddItem.Text     = entityTypeFriendlyName;
                }

                // Attempt to retrieve an EntityId from the Page URL parameters.
                PageParameterName = GetAttributeValue("PageParameterKey");

                string selectedNodeId = null;

                int?   itemId = PageParameter(PageParameterName).AsIntegerOrNull();
                string selectedEntityType;
                if (itemId.HasValue)
                {
                    selectedNodeId     = itemId.ToString();
                    selectedEntityType = (cachedEntityType != null) ? cachedEntityType.Name : string.Empty;
                }
                else
                {
                    // If an EntityId was not specified, check for a CategoryId.
                    itemId = PageParameter("CategoryId").AsIntegerOrNull();

                    selectedNodeId     = CategoryNodePrefix + itemId;
                    selectedEntityType = "category";
                }

                lbAddCategoryRoot.Enabled  = true;
                lbAddCategoryChild.Enabled = false;
                lbAddItem.Enabled          = false;

                CategoryCache selectedCategory = null;

                if (!string.IsNullOrEmpty(selectedNodeId))
                {
                    hfSelectedItemId.Value = selectedNodeId;
                    List <string> parentIdList = new List <string>();

                    if (selectedEntityType.Equals("category"))
                    {
                        selectedCategory = CategoryCache.Read(itemId.GetValueOrDefault());
                    }
                    else
                    {
                        if (cachedEntityType != null)
                        {
                            Type entityType = cachedEntityType.GetEntityType();
                            if (entityType != null)
                            {
                                Type         serviceType     = typeof(Rock.Data.Service <>);
                                Type[]       modelType       = { entityType };
                                Type         service         = serviceType.MakeGenericType(modelType);
                                var          serviceInstance = Activator.CreateInstance(service, new object[] { new RockContext() });
                                var          getMethod       = service.GetMethod("Get", new Type[] { typeof(int) });
                                ICategorized entity          = getMethod.Invoke(serviceInstance, new object[] { itemId }) as ICategorized;

                                if (entity != null)
                                {
                                    lbAddCategoryChild.Enabled = false;
                                    if (entity.CategoryId.HasValue)
                                    {
                                        selectedCategory = CategoryCache.Read(entity.CategoryId.Value);
                                        if (selectedCategory != null)
                                        {
                                            string categoryExpandedID = CategoryNodePrefix + selectedCategory.Id.ToString();
                                            parentIdList.Insert(0, CategoryNodePrefix + categoryExpandedID);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // get the parents of the selected item so we can tell the treeview to expand those
                    var category = selectedCategory;
                    while (category != null)
                    {
                        category = category.ParentCategory;
                        if (category != null)
                        {
                            string categoryExpandedID = CategoryNodePrefix + category.Id.ToString();
                            if (!parentIdList.Contains(categoryExpandedID))
                            {
                                parentIdList.Insert(0, categoryExpandedID);
                            }
                            else
                            {
                                // infinite recursion
                                break;
                            }
                        }
                    }
                    // also get any additional expanded nodes that were sent in the Post
                    string postedExpandedIds = this.Request.Params["ExpandedIds"];
                    if (!string.IsNullOrWhiteSpace(postedExpandedIds))
                    {
                        var postedExpandedIdList = postedExpandedIds.Split(',').ToList();
                        foreach (var id in postedExpandedIdList)
                        {
                            if (!parentIdList.Contains(id))
                            {
                                parentIdList.Add(id);
                            }
                        }
                    }

                    hfInitialCategoryParentIds.Value = parentIdList.AsDelimited(",");
                }

                selectedCategory = selectedCategory ?? rootCategory;

                if (selectedCategory != null)
                {
                    lbAddItem.Enabled          = true;
                    lbAddCategoryChild.Enabled = true;
                    this.SelectedCategoryId    = selectedCategory.Id;
                }
                else
                {
                    this.SelectedCategoryId = null;
                }
            }
        }
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Get EntityTypeName
            string entityTypeName           = GetAttributeValue("EntityType");
            int    entityTypeId             = Rock.Web.Cache.EntityTypeCache.Read(entityTypeName).Id;
            string entityTypeQualiferColumn = GetAttributeValue("EntityTypeQualifierProperty");
            string entityTypeQualifierValue = GetAttributeValue("EntityTypeQualifierValue");

            var parms = new StringBuilder();

            parms.AppendFormat("/True/{0}", entityTypeId);
            if (!string.IsNullOrEmpty(entityTypeQualiferColumn))
            {
                parms.AppendFormat("/{0}", entityTypeQualiferColumn);

                if (!string.IsNullOrEmpty(entityTypeQualifierValue))
                {
                    parms.AppendFormat("/{0}", entityTypeQualifierValue);
                }
            }

            RestParms = parms.ToString();

            var cachedEntityType = Rock.Web.Cache.EntityTypeCache.Read(entityTypeId);

            if (cachedEntityType != null)
            {
                lbAddItem.ToolTip = "Add " + cachedEntityType.FriendlyName;
                lAddItem.Text     = "Add " + cachedEntityType.FriendlyName;
            }

            PageParameterName = GetAttributeValue("PageParameterKey");
            string itemId             = PageParameter(PageParameterName);
            string selectedEntityType = cachedEntityType.Name;

            if (string.IsNullOrWhiteSpace(itemId))
            {
                itemId             = PageParameter("categoryId");
                selectedEntityType = "category";
            }

            lbAddCategory.Visible = true;
            lbAddItem.Visible     = false;

            if (!string.IsNullOrWhiteSpace(itemId))
            {
                hfInitialItemId.Value           = itemId;
                hfInitialEntityIsCategory.Value = (selectedEntityType == "category").ToString();
                List <string> parentIdList = new List <string>();

                Category category = null;
                if (selectedEntityType.Equals("category"))
                {
                    category          = new CategoryService().Get(int.Parse(itemId));
                    lbAddItem.Visible = true;
                }
                else
                {
                    int id = 0;
                    if (int.TryParse(itemId, out id))
                    {
                        if (cachedEntityType != null)
                        {
                            Type entityType = cachedEntityType.GetEntityType();
                            if (entityType != null)
                            {
                                Type         serviceType     = typeof(Rock.Data.Service <>);
                                Type[]       modelType       = { entityType };
                                Type         service         = serviceType.MakeGenericType(modelType);
                                var          serviceInstance = Activator.CreateInstance(service);
                                var          getMethod       = service.GetMethod("Get", new Type[] { typeof(int) });
                                ICategorized entity          = getMethod.Invoke(serviceInstance, new object[] { id }) as ICategorized;

                                if (entity != null)
                                {
                                    lbAddCategory.Visible = false;
                                    category = entity.Category;
                                    if (category != null)
                                    {
                                        parentIdList.Insert(0, category.Id.ToString());
                                    }
                                }
                            }
                        }
                    }
                }

                while (category != null)
                {
                    category = category.ParentCategory;
                    if (category != null)
                    {
                        parentIdList.Insert(0, category.Id.ToString());
                    }
                }

                hfInitialCategoryParentIds.Value = parentIdList.AsDelimited(",");
            }
        }
        public IQueryable <CategoryItem> GetChildren(int id, bool getCategorizedItems, int entityTypeId, string entityQualifier, string entityQualifierValue)
        {
            var    user          = CurrentUser();
            Person currentPerson = user != null ? user.Person : null;

            IQueryable <Category> qry;

            qry = Get().Where(a => (a.ParentCategoryId ?? 0) == id);

            object serviceInstance = null;

            var cachedEntityType = EntityTypeCache.Read(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 == "");
                    }
                }

                // 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);
                    }
                }
            }

            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("View", currentPerson))
                {
                    var categoryItem = new CategoryItem();
                    categoryItem.Id           = category.Id.ToString();
                    categoryItem.Name         = System.Web.HttpUtility.HtmlEncode(category.Name);
                    categoryItem.IsCategory   = true;
                    categoryItem.IconCssClass = category.IconCssClass;
                    categoryItemList.Add(categoryItem);
                }
            }

            if (getCategorizedItems)
            {
                IQueryable items = GetCategorizedItems(serviceInstance, id) as IQueryable;
                if (items != null)
                {
                    foreach (var item in items)
                    {
                        ICategorized categorizedItem = item as ICategorized;
                        if (categorizedItem != null && categorizedItem.IsAuthorized("View", currentPerson))
                        {
                            var categoryItem = new CategoryItem();
                            categoryItem.Id           = categorizedItem.Id.ToString();
                            categoryItem.Name         = categorizedItem.Name;
                            categoryItem.IsCategory   = false;
                            categoryItem.IconCssClass = "fa fa-list-ol";
                            categoryItem.IconSmallUrl = string.Empty;
                            categoryItemList.Add(categoryItem);
                        }
                    }
                }
            }

            // try to figure out which items have viewable children
            foreach (var g in categoryItemList)
            {
                if (g.IsCategory)
                {
                    int parentId = int.Parse(g.Id);

                    foreach (var childCategory in Get().Where(c => c.ParentCategoryId == parentId))
                    {
                        if (childCategory.IsAuthorized("View", currentPerson))
                        {
                            g.HasChildren = true;
                            break;
                        }
                    }

                    if (!g.HasChildren)
                    {
                        if (getCategorizedItems)
                        {
                            IQueryable childItems = GetCategorizedItems(serviceInstance, parentId) as IQueryable;
                            if (childItems != null)
                            {
                                foreach (var item in childItems)
                                {
                                    ICategorized categorizedItem = item as ICategorized;
                                    if (categorizedItem != null && categorizedItem.IsAuthorized("View", currentPerson))
                                    {
                                        g.HasChildren = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(categoryItemList.AsQueryable());
        }
Example #4
0
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            bool canEditBlock = IsUserAuthorized(Authorization.EDIT);

            // hide all the actions if user doesn't have EDIT to the block
            divTreeviewActions.Visible = canEditBlock;
            hfPageRouteTemplate.Value  = (this.RockPage.RouteData.Route as System.Web.Routing.Route).Url;

            // Get EntityTypeName
            Guid?entityTypeGuid = GetAttributeValue("EntityType").AsGuidOrNull();

            nbWarning.Text    = "Please select an entity type in the block settings.";
            nbWarning.Visible = !entityTypeGuid.HasValue;
            if (entityTypeGuid.HasValue)
            {
                int    entityTypeId             = Rock.Web.Cache.EntityTypeCache.Read(entityTypeGuid.Value).Id;
                string entityTypeQualiferColumn = GetAttributeValue("EntityTypeQualifierProperty");
                string entityTypeQualifierValue = GetAttributeValue("EntityTypeQualifierValue");
                bool   showUnnamedEntityItems   = GetAttributeValue("ShowUnnamedEntityItems").AsBooleanOrNull() ?? true;

                string parms = string.Format("?getCategorizedItems=true&showUnnamedEntityItems={0}", showUnnamedEntityItems.ToTrueFalse().ToLower());
                parms += string.Format("&entityTypeId={0}", entityTypeId);

                var rootCategory = CategoryCache.Read(this.GetAttributeValue("RootCategory").AsGuid());

                // make sure the rootCategory matches the EntityTypeId (just in case they changed the EntityType after setting RootCategory
                if (rootCategory != null && rootCategory.EntityTypeId == entityTypeId)
                {
                    parms += string.Format("&rootCategoryId={0}", rootCategory.Id);
                }

                if (!string.IsNullOrEmpty(entityTypeQualiferColumn))
                {
                    parms += string.Format("&entityQualifier={0}", entityTypeQualiferColumn);

                    if (!string.IsNullOrEmpty(entityTypeQualifierValue))
                    {
                        parms += string.Format("&entityQualifierValue={0}", entityTypeQualifierValue);
                    }
                }

                RestParms = parms;

                var cachedEntityType = Rock.Web.Cache.EntityTypeCache.Read(entityTypeId);
                if (cachedEntityType != null)
                {
                    string entityTypeFriendlyName = GetAttributeValue("EntityTypeFriendlyName");
                    if (string.IsNullOrWhiteSpace(entityTypeFriendlyName))
                    {
                        entityTypeFriendlyName = cachedEntityType.FriendlyName;
                    }

                    lbAddItem.ToolTip = "Add " + entityTypeFriendlyName;
                    lAddItem.Text     = entityTypeFriendlyName;
                }

                PageParameterName = GetAttributeValue("PageParameterKey");
                int?   itemId             = PageParameter(PageParameterName).AsIntegerOrNull();
                string selectedEntityType = cachedEntityType.Name;
                if (!itemId.HasValue)
                {
                    itemId             = PageParameter("CategoryId").AsIntegerOrNull();
                    selectedEntityType = "category";
                }

                lbAddCategoryRoot.Enabled  = true;
                lbAddCategoryChild.Enabled = false;
                lbAddItem.Enabled          = false;

                CategoryCache selectedCategory = null;

                if (itemId.HasValue)
                {
                    hfSelectedItemId.Value = itemId.Value.ToString();
                    List <string> parentIdList = new List <string>();

                    if (selectedEntityType.Equals("category"))
                    {
                        selectedCategory = CategoryCache.Read(itemId.Value);
                    }
                    else
                    {
                        if (cachedEntityType != null)
                        {
                            Type entityType = cachedEntityType.GetEntityType();
                            if (entityType != null)
                            {
                                Type         serviceType     = typeof(Rock.Data.Service <>);
                                Type[]       modelType       = { entityType };
                                Type         service         = serviceType.MakeGenericType(modelType);
                                var          serviceInstance = Activator.CreateInstance(service, new object[] { new RockContext() });
                                var          getMethod       = service.GetMethod("Get", new Type[] { typeof(int) });
                                ICategorized entity          = getMethod.Invoke(serviceInstance, new object[] { itemId }) as ICategorized;

                                if (entity != null)
                                {
                                    lbAddCategoryChild.Enabled = false;
                                    if (entity.CategoryId.HasValue)
                                    {
                                        selectedCategory = CategoryCache.Read(entity.CategoryId.Value);
                                        if (selectedCategory != null)
                                        {
                                            parentIdList.Insert(0, selectedCategory.Id.ToString());
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // get the parents of the selected item so we can tell the treeview to expand those
                    var category = selectedCategory;
                    while (category != null)
                    {
                        category = category.ParentCategory;
                        if (category != null)
                        {
                            parentIdList.Insert(0, category.Id.ToString());
                        }
                    }
                    // also get any additional expanded nodes that were sent in the Post
                    string postedExpandedIds = this.Request.Params["ExpandedIds"];
                    if (!string.IsNullOrWhiteSpace(postedExpandedIds))
                    {
                        var postedExpandedIdList = postedExpandedIds.Split(',').ToList();
                        foreach (var id in postedExpandedIdList)
                        {
                            if (!parentIdList.Contains(id))
                            {
                                parentIdList.Add(id);
                            }
                        }
                    }

                    hfInitialCategoryParentIds.Value = parentIdList.AsDelimited(",");
                }

                selectedCategory = selectedCategory ?? rootCategory;

                if (selectedCategory != null)
                {
                    lbAddItem.Enabled          = true;
                    lbAddCategoryChild.Enabled = true;
                    this.SelectedCategoryId    = selectedCategory.Id;
                }
                else
                {
                    this.SelectedCategoryId = null;
                }
            }
        }
Example #5
0
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            RockPage.AddScriptLink("~/Scripts/jquery.tinyscrollbar.js");


            // Get EntityTypeName
            Guid entityTypeGuid = Guid.Empty;

            if (Guid.TryParse(GetAttributeValue("EntityType"), out entityTypeGuid))
            {
                int    entityTypeId             = Rock.Web.Cache.EntityTypeCache.Read(entityTypeGuid).Id;
                string entityTypeQualiferColumn = GetAttributeValue("EntityTypeQualifierProperty");
                string entityTypeQualifierValue = GetAttributeValue("EntityTypeQualifierValue");

                var parms = new StringBuilder();
                parms.AppendFormat("/True/{0}", entityTypeId);
                if (!string.IsNullOrEmpty(entityTypeQualiferColumn))
                {
                    parms.AppendFormat("/{0}", entityTypeQualiferColumn);

                    if (!string.IsNullOrEmpty(entityTypeQualifierValue))
                    {
                        parms.AppendFormat("/{0}", entityTypeQualifierValue);
                    }
                }

                RestParms = parms.ToString();

                var cachedEntityType = Rock.Web.Cache.EntityTypeCache.Read(entityTypeId);
                if (cachedEntityType != null)
                {
                    lbAddItem.ToolTip = "Add " + cachedEntityType.FriendlyName;
                    lAddItem.Text     = cachedEntityType.FriendlyName;
                }

                PageParameterName = GetAttributeValue("PageParameterKey");
                string itemId             = PageParameter(PageParameterName);
                string selectedEntityType = cachedEntityType.Name;
                if (string.IsNullOrWhiteSpace(itemId))
                {
                    itemId             = PageParameter("categoryId");
                    selectedEntityType = "category";
                }

                lbAddCategoryRoot.Enabled  = true;
                lbAddCategoryChild.Enabled = false;
                lbAddItem.Enabled          = false;

                if (!string.IsNullOrWhiteSpace(itemId))
                {
                    hfInitialItemId.Value           = itemId;
                    hfInitialEntityIsCategory.Value = (selectedEntityType == "category").ToString();
                    hfSelectedCategoryId.Value      = itemId;
                    List <string> parentIdList = new List <string>();

                    Category category = null;
                    if (selectedEntityType.Equals("category"))
                    {
                        category                   = new CategoryService().Get(int.Parse(itemId));
                        lbAddItem.Enabled          = true;
                        lbAddCategoryChild.Enabled = true;
                    }
                    else
                    {
                        int id = 0;
                        if (int.TryParse(itemId, out id))
                        {
                            if (cachedEntityType != null)
                            {
                                Type entityType = cachedEntityType.GetEntityType();
                                if (entityType != null)
                                {
                                    Type         serviceType     = typeof(Rock.Data.Service <>);
                                    Type[]       modelType       = { entityType };
                                    Type         service         = serviceType.MakeGenericType(modelType);
                                    var          serviceInstance = Activator.CreateInstance(service);
                                    var          getMethod       = service.GetMethod("Get", new Type[] { typeof(int) });
                                    ICategorized entity          = getMethod.Invoke(serviceInstance, new object[] { id }) as ICategorized;

                                    if (entity != null)
                                    {
                                        lbAddCategoryRoot.Enabled  = false;
                                        lbAddCategoryChild.Enabled = false;
                                        category = entity.Category;
                                        if (category != null)
                                        {
                                            parentIdList.Insert(0, category.Id.ToString());
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // get the parents of the selected item so we can tell the treeview to expand those
                    while (category != null)
                    {
                        category = category.ParentCategory;
                        if (category != null)
                        {
                            parentIdList.Insert(0, category.Id.ToString());
                        }
                    }
                    // also get any additional expanded nodes that were sent in the Post
                    string postedExpandedIds = this.Request.Params["expandedIds"];
                    if (!string.IsNullOrWhiteSpace(postedExpandedIds))
                    {
                        var postedExpandedIdList = postedExpandedIds.Split(',').ToList();
                        foreach (var id in postedExpandedIdList)
                        {
                            if (!parentIdList.Contains(id))
                            {
                                parentIdList.Add(id);
                            }
                        }
                    }

                    hfInitialCategoryParentIds.Value = parentIdList.AsDelimited(",");
                }
            }
        }