/// <summary> /// Handles the Click event of the btnMoveGroupMember control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void btnMoveGroupMember_Click(object sender, EventArgs e) { var rockContext = new RockContext(); var groupMemberService = new GroupMemberService(rockContext); var groupMember = groupMemberService.Get(hfGroupMemberId.Value.AsInteger()); groupMember.LoadAttributes(); int destGroupId = gpMoveGroupMember.SelectedValue.AsInteger(); var destGroup = new GroupService(rockContext).Get(destGroupId); var destGroupMember = groupMemberService.Queryable().Where(a => a.GroupId == destGroupId && a.PersonId == groupMember.PersonId && a.GroupRoleId == grpMoveGroupMember.GroupRoleId).FirstOrDefault(); if (destGroupMember != null) { nbMoveGroupMemberWarning.Visible = true; nbMoveGroupMemberWarning.Text = string.Format("{0} is already in {1}", groupMember.Person, destGroupMember.Group); return; } if (!grpMoveGroupMember.GroupRoleId.HasValue) { nbMoveGroupMemberWarning.Visible = true; nbMoveGroupMemberWarning.Text = string.Format("Please select a Group Role"); return; } string canDeleteWarning; if (!groupMemberService.CanDelete(groupMember, out canDeleteWarning)) { nbMoveGroupMemberWarning.Visible = true; nbMoveGroupMemberWarning.Text = string.Format("Unable to remove {0} from {1}: {2}", groupMember.Person, groupMember.Group, canDeleteWarning); return; } destGroupMember = new GroupMember(); destGroupMember.GroupId = destGroupId; destGroupMember.GroupRoleId = grpMoveGroupMember.GroupRoleId.Value; destGroupMember.PersonId = groupMember.PersonId; destGroupMember.LoadAttributes(); foreach (var attribute in groupMember.Attributes) { if (destGroupMember.Attributes.Any(a => a.Key == attribute.Key && a.Value.FieldTypeId == attribute.Value.FieldTypeId)) { destGroupMember.SetAttributeValue(attribute.Key, groupMember.GetAttributeValue(attribute.Key)); } } // Un-link any registrant records that point to this group member. foreach (var registrant in new RegistrationRegistrantService(rockContext).Queryable() .Where(r => r.GroupMemberId == groupMember.Id)) { registrant.GroupMemberId = null; } rockContext.WrapTransaction(() => { groupMemberService.Add(destGroupMember); rockContext.SaveChanges(); destGroupMember.SaveAttributeValues(rockContext); // move any Note records that were associated with the old groupMember to the new groupMember record if (cbMoveGroupMemberMoveNotes.Checked) { destGroupMember.Note = groupMember.Note; int groupMemberEntityTypeId = EntityTypeCache.GetId <Rock.Model.GroupMember>().Value; var noteService = new NoteService(rockContext); var groupMemberNotes = noteService.Queryable().Where(a => a.NoteType.EntityTypeId == groupMemberEntityTypeId && a.EntityId == groupMember.Id); foreach (var note in groupMemberNotes) { note.EntityId = destGroupMember.Id; } rockContext.SaveChanges(); } groupMemberService.Delete(groupMember); rockContext.SaveChanges(); destGroupMember.CalculateRequirements(rockContext, true); }); var queryString = new Dictionary <string, string>(); queryString.Add("GroupMemberId", destGroupMember.Id.ToString()); this.NavigateToPage(this.RockPage.Guid, queryString); }
/// <summary> /// Handles the Click event of the btnMoveGroupMember control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void btnMoveGroupMember_Click( object sender, EventArgs e ) { var rockContext = new RockContext(); var groupMemberService = new GroupMemberService( rockContext ); var groupMember = groupMemberService.Get( hfGroupMemberId.Value.AsInteger() ); groupMember.LoadAttributes(); int destGroupId = gpMoveGroupMember.SelectedValue.AsInteger(); var destGroup = new GroupService( rockContext ).Get( destGroupId ); var destGroupMember = groupMemberService.Queryable().Where( a => a.GroupId == destGroupId && a.PersonId == groupMember.PersonId && a.GroupRoleId == grpMoveGroupMember.GroupRoleId ).FirstOrDefault(); if ( destGroupMember != null ) { nbMoveGroupMemberWarning.Visible = true; nbMoveGroupMemberWarning.Text = string.Format( "{0} is already in {1}", groupMember.Person, destGroupMember.Group ); return; } if ( !grpMoveGroupMember.GroupRoleId.HasValue ) { nbMoveGroupMemberWarning.Visible = true; nbMoveGroupMemberWarning.Text = string.Format( "Please select a Group Role" ); return; } string canDeleteWarning; if ( !groupMemberService.CanDelete( groupMember, out canDeleteWarning ) ) { nbMoveGroupMemberWarning.Visible = true; nbMoveGroupMemberWarning.Text = string.Format( "Unable to remove {0} from {1}: {2}", groupMember.Person, groupMember.Group, canDeleteWarning ); return; } destGroupMember = new GroupMember(); destGroupMember.GroupId = destGroupId; destGroupMember.GroupRoleId = grpMoveGroupMember.GroupRoleId.Value; destGroupMember.PersonId = groupMember.PersonId; destGroupMember.LoadAttributes(); foreach ( var attribute in groupMember.Attributes ) { if ( destGroupMember.Attributes.Any( a => a.Key == attribute.Key && a.Value.FieldTypeId == attribute.Value.FieldTypeId ) ) { destGroupMember.SetAttributeValue( attribute.Key, groupMember.GetAttributeValue( attribute.Key ) ); } } rockContext.WrapTransaction( () => { groupMemberService.Add( destGroupMember ); rockContext.SaveChanges(); destGroupMember.SaveAttributeValues( rockContext ); // move any Note records that were associated with the old groupMember to the new groupMember record if ( cbMoveGroupMemberMoveNotes.Checked ) { destGroupMember.Note = groupMember.Note; int groupMemberEntityTypeId = EntityTypeCache.GetId<Rock.Model.GroupMember>().Value; var noteService = new NoteService( rockContext ); var groupMemberNotes = noteService.Queryable().Where( a => a.NoteType.EntityTypeId == groupMemberEntityTypeId && a.EntityId == groupMember.Id ); foreach ( var note in groupMemberNotes ) { note.EntityId = destGroupMember.Id; } rockContext.SaveChanges(); } groupMemberService.Delete( groupMember ); rockContext.SaveChanges(); destGroupMember.CalculateRequirements( rockContext, true ); } ); var queryString = new Dictionary<string, string>(); queryString.Add( "GroupMemberId", destGroupMember.Id.ToString() ); this.NavigateToPage( this.RockPage.Guid, queryString ); }