/// <summary> /// Binds the groups. /// </summary> private void BindGroups() { if (Person == null || Person.Id == 0 || Person.IsNameless()) { return; } // If this is a Family GroupType and they belong to multiple families, // first make sure that the GroupMember.GroupOrder is set for this Person's Families. // This will ensure that other spots that rely on the GroupOrder provide consistent results. if (this._IsFamilyGroupType) { using (var rockContext = new RockContext()) { var memberService = new GroupMemberService(rockContext); var groupMemberGroups = memberService.Queryable(true) .Where(m => m.PersonId == Person.Id && m.Group.GroupTypeId == _groupType.Id) .OrderBy(m => m.GroupOrder ?? int.MaxValue).ThenBy(m => m.Id) .ToList(); if (groupMemberGroups.Count > 1 && memberService.SetGroupMemberGroupOrder(groupMemberGroups)) { rockContext.SaveChanges(); } } } // Gind the Groups repeater which will show the Groups with a list of GroupMembers using (_bindGroupsRockContext = new RockContext()) { var memberService = new GroupMemberService(_bindGroupsRockContext); var groups = memberService.Queryable(true) .Where(m => m.PersonId == Person.Id && m.Group.GroupTypeId == _groupType.Id) .OrderBy(m => m.GroupOrder ?? int.MaxValue).ThenBy(m => m.Id) .Select(m => m.Group) .AsNoTracking() .ToList(); if (!groups.Any() && GetAttributeValue(AttributeKey.AutoCreateGroup).AsBoolean(true)) { // ensure that the person is in a group var groupService = new GroupService(_bindGroupsRockContext); var group = new Group(); group.Name = Person.LastName; group.GroupTypeId = _groupType.Id; groupService.Add(group); _bindGroupsRockContext.SaveChanges(); var groupMember = new GroupMember(); groupMember.PersonId = Person.Id; groupMember.GroupRoleId = _groupType.DefaultGroupRoleId.Value; groupMember.GroupId = group.Id; group.Members.Add(groupMember); _bindGroupsRockContext.SaveChanges(); groups.Add(groupService.Get(group.Id)); } rptrGroups.DataSource = groups; _showReorderIcon = groups.Count > 1; rptrGroups.DataBind(); } }