Ejemplo n.º 1
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);

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

            // 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());
                        if (selectedCategory != null && !canEditBlock && selectedCategory.IsAuthorized(Authorization.EDIT, CurrentPerson))
                        {
                            // Show the action buttons if user has edit rights to category
                            divTreeviewActions.Visible = true;
                        }
                    }
                    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;
                }
            }
        }