/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { // Find all the Group Types var groupTypeIds = GetAvailableGroupTypes(); if (GetAttributeValue("DisplayFilter").AsBooleanOrNull() ?? false) { int?groupTypeFilter = gfSettings.GetUserPreference("Group Type").AsIntegerOrNull(); if (groupTypeFilter.HasValue) { groupTypeIds = groupTypeIds.Where(g => g == groupTypeFilter.Value).ToList(); } } // filter to a specific group type if provided in the query string if (!string.IsNullOrWhiteSpace(RockPage.PageParameter("GroupTypeId"))) { int?groupTypeId = RockPage.PageParameter("GroupTypeId").AsIntegerOrNull(); if (groupTypeId.HasValue) { groupTypeIds.Clear(); groupTypeIds.Add(groupTypeId.Value); } } var rockContext = new RockContext(); var groupService = new GroupService(rockContext); SortProperty sortProperty = gGroups.SortProperty; if (sortProperty == null) { sortProperty = new SortProperty(new GridViewSortEventArgs("Name", SortDirection.Ascending)); } bool onlySecurityGroups = GetAttributeValue("LimittoSecurityRoleGroups").AsBoolean(); var qryGroups = groupService.Queryable() .Where(g => groupTypeIds.Contains(g.GroupTypeId) && (!onlySecurityGroups || g.IsSecurityRole)); string limitToActiveStatus = GetAttributeValue("LimittoActiveStatus"); bool showActive = true; bool showInactive = true; if (limitToActiveStatus == "all" && gfSettings.Visible) { // Filter by active/inactive unless the block settings restrict it if (ddlActiveFilter.SelectedIndex > -1) { switch (ddlActiveFilter.SelectedValue) { case "active": showInactive = false; break; case "inactive": showActive = false; break; } } } else if (limitToActiveStatus != "all") { // filter by the block setting for Active Status if (limitToActiveStatus == "active") { showInactive = false; } } var groupTypePurposeValue = gfSettings.GetUserPreference("Group Type Purpose").AsIntegerOrNull(); var groupList = new List <GroupListRowInfo>(); // Person context will exist if used on a person detail page int personEntityTypeId = EntityTypeCache.Read("Rock.Model.Person").Id; if (ContextTypesRequired.Any(e => e.Id == personEntityTypeId)) { var personContext = ContextEntity <Person>(); if (personContext != null) { // limit to Groups that the person is a member of var qry = new GroupMemberService(rockContext).Queryable(true) .Where(m => m.PersonId == personContext.Id) .Join(qryGroups, gm => gm.GroupId, g => g.Id, (gm, g) => new { Group = g, GroupMember = gm }); // Filter by Active Status of Group and Group Membership. if (showActive && !showInactive) { // Show only active Groups and active Memberships. qry = qry.Where(gmg => gmg.Group.IsActive && gmg.GroupMember.GroupMemberStatus == GroupMemberStatus.Active); } else if (!showActive) { // Show only inactive Groups or inactive Memberships. qry = qry.Where(gmg => !gmg.Group.IsActive || gmg.GroupMember.GroupMemberStatus == GroupMemberStatus.Inactive); } if (groupTypePurposeValue.HasValue && gfSettings.Visible) { qry = qry.Where(t => t.Group.GroupType.GroupTypePurposeValueId == groupTypePurposeValue); } groupList = qry .AsEnumerable() .Where(gm => gm.Group.IsAuthorized(Rock.Security.Authorization.VIEW, CurrentPerson)) .Select(m => new GroupListRowInfo { Id = m.Group.Id, Path = string.Empty, Name = m.Group.Name, GroupTypeName = m.Group.GroupType.Name, GroupOrder = m.Group.Order, GroupTypeOrder = m.Group.GroupType.Order, Description = m.Group.Description, IsSystem = m.Group.IsSystem, GroupRole = m.GroupMember.GroupRole.Name, DateAdded = m.GroupMember.DateTimeAdded ?? m.GroupMember.CreatedDateTime, IsActive = m.Group.IsActive && (m.GroupMember.GroupMemberStatus == GroupMemberStatus.Active), IsActiveOrder = (m.Group.IsActive && (m.GroupMember.GroupMemberStatus == GroupMemberStatus.Active) ? 1 : 2), IsSynced = m.Group.SyncDataViewId.HasValue, MemberCount = 0 }) .AsQueryable() .Sort(sortProperty) .ToList(); } } else { var roleGroupType = GroupTypeCache.Read(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()); int roleGroupTypeId = roleGroupType != null ? roleGroupType.Id : 0; bool useRolePrefix = onlySecurityGroups || groupTypeIds.Contains(roleGroupTypeId); if (!showInactive) { qryGroups = qryGroups.Where(x => x.IsActive); } else if (!showActive) { qryGroups = qryGroups.Where(x => !x.IsActive); } if (groupTypePurposeValue.HasValue && gfSettings.Visible) { qryGroups = qryGroups.Where(t => t.GroupType.GroupTypePurposeValueId == groupTypePurposeValue); } groupList = qryGroups .AsEnumerable() .Where(g => g.IsAuthorized(Rock.Security.Authorization.VIEW, CurrentPerson)) .Select(g => new GroupListRowInfo { Id = g.Id, Path = string.Empty, Name = ((useRolePrefix && g.GroupType.Id != roleGroupTypeId) ? "GROUP - " : "") + g.Name, GroupTypeName = g.GroupType.Name, GroupOrder = g.Order, GroupTypeOrder = g.GroupType.Order, Description = g.Description, IsSystem = g.IsSystem, IsActive = g.IsActive, IsActiveOrder = g.IsActive ? 1 : 2, GroupRole = string.Empty, DateAdded = DateTime.MinValue, IsSynced = g.SyncDataViewId.HasValue, MemberCount = g.Members.Count() }) .AsQueryable() .Sort(sortProperty) .ToList(); } if (_showGroupPath) { foreach (var groupRow in groupList) { groupRow.Path = groupService.GroupAncestorPathName(groupRow.Id); } } gGroups.DataSource = groupList; gGroups.EntityTypeId = EntityTypeCache.Read <Group>().Id; gGroups.DataBind(); // hide the group type column if there's only one type; must come after DataBind() if (_groupTypesCount == 1) { var groupTypeColumn = this.gGroups.ColumnsOfType <RockBoundField>().FirstOrDefault(a => a.DataField == "GroupTypeName"); groupTypeColumn.Visible = false; } }