/// <summary> /// Gets the expression. /// </summary> /// <param name="entityType">Type of the entity.</param> /// <param name="serviceInstance">The service instance.</param> /// <param name="parameterExpression">The parameter expression.</param> /// <param name="selection">The selection.</param> /// <returns></returns> public override Expression GetExpression( Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection ) { string[] selectionValues = selection.Split( '|' ); if ( selectionValues.Length >= 2 ) { GroupMemberService groupMemberService = new GroupMemberService( (RockContext)serviceInstance.Context ); List<Guid> groupGuids = selectionValues[0].Split( ',' ).AsGuidList(); var groupService = new GroupService( (RockContext)serviceInstance.Context ); var groupIds = groupService.GetByGuids( groupGuids ).Select( a => a.Id ).Distinct().ToList(); bool includeChildGroups = false; bool includeChildGroupsIncludeSelected = false; bool includeChildGroupsPlusDescendants = false; bool includeInactiveGroups = false; if ( selectionValues.Length >= 3 ) { includeChildGroups = selectionValues[2].AsBooleanOrNull() ?? false; } if ( selectionValues.Length >= 6 ) { includeChildGroupsIncludeSelected = selectionValues[4].AsBooleanOrNull() ?? false; includeChildGroupsPlusDescendants = selectionValues[5].AsBooleanOrNull() ?? false; } else if ( includeChildGroups ) { // in case the selection was saved before these options where added includeChildGroupsIncludeSelected = true; includeChildGroupsPlusDescendants = true; } if ( selectionValues.Length >= 7 ) { includeInactiveGroups = selectionValues[6].AsBooleanOrNull() ?? true; ; } else { // if options where saved before this option was added, set to false, even though it would have included inactive before includeInactiveGroups = false; } GroupMemberStatus? groupMemberStatus = null; if ( selectionValues.Length >= 4 ) { groupMemberStatus = selectionValues[3].ConvertToEnumOrNull<GroupMemberStatus>(); } var groupMemberServiceQry = groupMemberService.Queryable(); if ( includeChildGroups ) { List<int> childGroupIds = new List<int>(); foreach ( var groupId in groupIds ) { if ( includeChildGroupsPlusDescendants ) { // get all children and descendants of the selected group(s) var descendants = groupService.GetAllDescendents( groupId ); if ( !includeInactiveGroups ) { descendants = descendants.Where( a => a.IsActive == true ); } childGroupIds.AddRange( descendants.Select( a => a.Id ).Distinct().ToList() ); } else { // get only immediate children of the selected group(s) var childGroups = groupService.Queryable().Where( a => a.ParentGroupId == groupId ); if ( !includeInactiveGroups ) { childGroups = childGroups.Where( a => a.IsActive == true ); } childGroupIds.AddRange( childGroups.Select( a => a.Id ) ); } } if ( includeChildGroupsIncludeSelected ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => groupIds.Contains( xx.GroupId ) || childGroupIds.Contains( xx.GroupId ) ); } else { groupMemberServiceQry = groupMemberServiceQry.Where( xx => childGroupIds.Contains( xx.GroupId ) ); } } else { groupMemberServiceQry = groupMemberServiceQry.Where( xx => groupIds.Contains( xx.GroupId ) ); } if ( groupMemberStatus.HasValue ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => xx.GroupMemberStatus == groupMemberStatus.Value ); } var groupRoleGuids = selectionValues[1].Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ).Select( n => n.AsGuid() ).ToList(); if ( groupRoleGuids.Count() > 0 ) { var groupRoleIds = new GroupTypeRoleService( (RockContext)serviceInstance.Context ).Queryable().Where( a => groupRoleGuids.Contains( a.Guid ) ).Select( a => a.Id ).ToList(); groupMemberServiceQry = groupMemberServiceQry.Where( xx => groupRoleIds.Contains( xx.GroupRoleId ) ); } if ( selectionValues.Length >= 8 ) { string slidingDelimitedValues = selectionValues[7].Replace( ',', '|' ); DateRange dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues( slidingDelimitedValues ); if ( dateRange.Start.HasValue ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => xx.DateTimeAdded >= dateRange.Start.Value ); } if ( dateRange.End.HasValue ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => xx.DateTimeAdded < dateRange.End.Value ); } } var qry = new PersonService( (RockContext)serviceInstance.Context ).Queryable() .Where( p => groupMemberServiceQry.Any( xx => xx.PersonId == p.Id ) ); Expression extractedFilterExpression = FilterExpressionExtractor.Extract<Rock.Model.Person>( qry, parameterExpression, "p" ); return extractedFilterExpression; } return null; }
/// <summary> /// Gets the expression. /// </summary> /// <param name="entityType">Type of the entity.</param> /// <param name="serviceInstance">The service instance.</param> /// <param name="parameterExpression">The parameter expression.</param> /// <param name="selection">The selection.</param> /// <returns></returns> public override Expression GetExpression( Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection ) { string[] selectionValues = selection.Split( '|' ); if ( selectionValues.Length >= 2 ) { GroupMemberService groupMemberService = new GroupMemberService( (RockContext)serviceInstance.Context ); int groupTypeId = 0; Guid groupTypeGuid = selectionValues[0].AsGuid(); var groupType = GroupTypeCache.Read( groupTypeGuid ); if ( groupType != null ) { groupTypeId = groupType.Id; } var groupMemberServiceQry = groupMemberService.Queryable().Where( xx => xx.Group.GroupTypeId == groupTypeId && xx.Group.IsActive == true ); var groupRoleGuids = selectionValues[1].Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ).Select( n => n.AsGuid() ).ToList(); if ( groupRoleGuids.Count() > 0 ) { var groupRoleIds = new GroupTypeRoleService( (RockContext)serviceInstance.Context ).Queryable().Where( a => groupRoleGuids.Contains( a.Guid ) ).Select( a => a.Id ).ToList(); groupMemberServiceQry = groupMemberServiceQry.Where( xx => groupRoleIds.Contains( xx.GroupRoleId ) ); } GroupMemberStatus? groupMemberStatus = null; if ( selectionValues.Length >= 3 ) { groupMemberStatus = selectionValues[2].ConvertToEnumOrNull<GroupMemberStatus>(); } if ( groupMemberStatus.HasValue ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => xx.GroupMemberStatus == groupMemberStatus.Value ); } var qry = new PersonService( (RockContext)serviceInstance.Context ).Queryable() .Where( p => groupMemberServiceQry.Any( xx => xx.PersonId == p.Id ) ); Expression extractedFilterExpression = FilterExpressionExtractor.Extract<Rock.Model.Person>( qry, parameterExpression, "p" ); return extractedFilterExpression; } return null; }
/// <summary> /// Gets the expression. /// </summary> /// <param name="context">The context.</param> /// <param name="entityIdProperty">The entity identifier property.</param> /// <param name="selection">The selection.</param> /// <returns></returns> public override Expression GetExpression( RockContext context, MemberExpression entityIdProperty, string selection ) { var qryGroupService = new GroupService( context ).Queryable(); Expression<Func<Rock.Model.GroupMember, bool>> memberWhereGroupType = a => 1 == 1; Expression<Func<Rock.Model.GroupMember, bool>> memberWhereGroupRoles = a => 1 == 1; Expression<Func<Rock.Model.GroupMember, bool>> memberWhereStatus = a => 1 == 1; string[] selectionValues = selection.Split( '|' ); if ( selectionValues.Length >= 3 ) { GroupMemberService groupMemberService = new GroupMemberService( context ); int? groupTypeId = null; Guid groupTypeGuid = selectionValues[1].AsGuid(); var groupType = GroupTypeCache.Read( groupTypeGuid ); if ( groupType != null ) { groupTypeId = groupType.Id; } if ( groupTypeId.HasValue ) { memberWhereGroupType = xx => xx.Group.GroupTypeId == groupTypeId; } var groupRoleGuids = selectionValues[2].Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ).Select( n => n.AsGuid() ).ToList(); List<int> groupRoleIds = null; if ( groupRoleGuids.Count() > 0 ) { groupRoleIds = new GroupTypeRoleService( context ).Queryable().Where( a => groupRoleGuids.Contains( a.Guid ) ).Select( a => a.Id ).ToList(); memberWhereGroupRoles = xx => groupRoleIds.Contains( xx.GroupRoleId ); } GroupMemberStatus? groupMemberStatus = selectionValues[3].ConvertToEnumOrNull<GroupMemberStatus>(); if ( groupMemberStatus.HasValue ) { memberWhereStatus = xx => xx.GroupMemberStatus == groupMemberStatus.Value; } } var memberListQuery = qryGroupService.Select( p => p.Members.AsQueryable() .Where( memberWhereGroupType ) .Where( memberWhereGroupRoles ) .Where( memberWhereStatus ) .Select( m => new MemberInfo { NickName = m.Person.NickName, LastName = m.Person.LastName, SuffixValueId = m.Person.SuffixValueId, PersonId = m.PersonId, GroupMemberId = m.Id } ).OrderBy( a => a.LastName ).ThenBy( a => a.NickName ) ); var selectChildrenExpression = SelectExpressionExtractor.Extract( memberListQuery, entityIdProperty, "p" ); return selectChildrenExpression; }