/// <summary> /// Handles the Click event of the DeleteGroupMember control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs" /> instance containing the event data.</param> protected void DeleteGroupMember_Click( object sender, Rock.Web.UI.Controls.RowEventArgs e ) { RockContext rockContext = new RockContext(); GroupMemberService groupMemberService = new GroupMemberService( rockContext ); GroupMember groupMember = groupMemberService.Get( e.RowKeyId ); if ( groupMember != null ) { string errorMessage; if ( !groupMemberService.CanDelete( groupMember, out errorMessage ) ) { mdGridWarning.Show( errorMessage, ModalAlertType.Information ); return; } int groupId = groupMember.GroupId; groupMemberService.Delete( groupMember ); rockContext.SaveChanges(); Group group = new GroupService( rockContext ).Get( groupId ); if ( group.IsSecurityRole || group.GroupType.Guid.Equals( Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid() ) ) { // person removed from SecurityRole, Flush Rock.Security.Role.Flush( group.Id ); Rock.Security.Authorization.Flush(); } } BindGroupMembersGrid(); }
/// <summary> /// Handles the SaveClick event of the mdPlaceElsewhere 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 mdPlaceElsewhere_SaveClick( object sender, EventArgs e ) { using ( var rockContext = new RockContext() ) { var groupService = new GroupService( rockContext ); var groupMemberService = new GroupMemberService( rockContext ); var groupMember = groupMemberService.Get( hfPlaceElsewhereGroupMemberId.Value.AsInteger() ); if ( groupMember != null ) { string errorMessage; if ( !groupMemberService.CanDelete( groupMember, out errorMessage ) ) { nbPlaceElsewhereWarning.Text = errorMessage; return; } var trigger = _group.GetGroupMemberWorkflowTriggers().FirstOrDefault( a => a.Id == hfPlaceElsewhereTriggerId.Value.AsInteger() ); if ( trigger != null ) { // create a transaction for the selected trigger var transaction = new Rock.Transactions.GroupMemberPlacedElsewhereTransaction( groupMember, tbPlaceElsewhereNote.Text, trigger ); // delete the group member from the current group groupMemberService.Delete( groupMember ); rockContext.SaveChanges(); // queue up the transaction Rock.Transactions.RockQueue.TransactionQueue.Enqueue( transaction ); } } mdPlaceElsewhere.Hide(); mdPlaceElsewhere.Visible = false; BindGroupMembersGrid(); } }
/// <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 ); }