/// <summary> /// Handles the CommunicateClick event of the Actions 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 Actions_CommunicateClick( object sender, EventArgs e ) { var rockPage = Page as RockPage; if ( rockPage != null ) { OnGridRebind( e ); var recipients = GetPersonData(); if ( recipients.Any() ) { // Create communication var rockContext = new RockContext(); var service = new Rock.Model.CommunicationService( rockContext ); var communication = new Rock.Model.Communication(); communication.IsBulkCommunication = true; communication.Status = Model.CommunicationStatus.Transient; CommunicateMergeFields.ForEach( f => communication.AdditionalMergeFields.Add( f.Replace( '.', '_' ) ) ); if ( rockPage.CurrentPerson != null ) { communication.SenderPersonAliasId = rockPage.CurrentPersonAliasId; } service.Add( communication ); var personIds = recipients.Select( r => r.Key ).ToList(); var personAliasService = new Rock.Model.PersonAliasService( new Rock.Data.RockContext() ); // Get the primary aliases foreach ( var personAlias in personAliasService.Queryable() .Where( p => p.PersonId == p.AliasPersonId && personIds.Contains( p.PersonId ) ) ) { var recipient = new Rock.Model.CommunicationRecipient(); recipient.PersonAliasId = personAlias.Id; recipient.AdditionalMergeValues = recipients[personAlias.PersonId]; communication.Recipients.Add( recipient ); } rockContext.SaveChanges(); // Get the URL to communication page string url = CommunicationPageRoute; if ( string.IsNullOrWhiteSpace(url) ) { var pageRef = rockPage.Site.CommunicationPageReference; if ( pageRef.PageId > 0 ) { pageRef.Parameters.AddOrReplace( "CommunicationId", communication.Id.ToString() ); url = pageRef.BuildUrl(); } else { url = "~/Communication/{0}"; } } if ( url.Contains("{0}")) { url = string.Format( url, communication.Id ); } Page.Response.Redirect( url, false ); Context.ApplicationInstance.CompleteRequest(); } } }
/// <summary> /// Handles the CommunicateClick event of the Actions 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 Actions_CommunicateClick( object sender, EventArgs e ) { if ( !string.IsNullOrWhiteSpace( PersonIdField ) ) { var peopleSelected = SelectedKeys.ToList(); // Set Sender var rockPage = Page as RockPage; if ( rockPage != null ) { var communication = new Rock.Model.Communication(); communication.IsBulkCommunication = true; communication.Status = Model.CommunicationStatus.Transient; if ( rockPage.CurrentPerson != null ) { communication.SenderPersonId = rockPage.CurrentPerson.Id; } OnGridRebind( e ); if ( this.DataSource is DataTable || this.DataSource is DataView ) { communication.AdditionalMergeFields = CommunicateMergeFields; DataTable data = null; if ( this.DataSource is DataTable ) { data = (DataTable)this.DataSource; } else if ( this.DataSource is DataView ) { data = ( (DataView)this.DataSource ).Table; } foreach ( DataRow row in data.Rows ) { int? personId = null; var mergeValues = new Dictionary<string, string>(); for ( int i = 0; i < data.Columns.Count; i++ ) { if ( data.Columns[i].ColumnName == this.PersonIdField ) { personId = row[i] as int?; } if ( CommunicateMergeFields.Contains( data.Columns[i].ColumnName ) ) { mergeValues.Add( data.Columns[i].ColumnName, row[i].ToString() ); } } // If valid personid and either no people were selected or this person was selected add them as a recipient if ( personId.HasValue && ( !peopleSelected.Any() || peopleSelected.Contains( personId.Value ) ) ) { var recipient = new Rock.Model.CommunicationRecipient(); recipient.PersonId = personId.Value; recipient.AdditionalMergeValues = mergeValues; communication.Recipients.Add( recipient ); } } } else { CommunicateMergeFields.ForEach( f => communication.AdditionalMergeFields.Add( f.Replace( '.', '_' ) ) ); // get access to the List<> and its properties IList data = (IList)this.DataSource; Type oType = data.GetType().GetProperty( "Item" ).PropertyType; PropertyInfo idProp = oType.GetProperty( this.PersonIdField ); foreach ( var item in data ) { if ( idProp == null ) { idProp = item.GetType().GetProperty( this.PersonIdField ); } if ( idProp != null ) { int personId = (int)idProp.GetValue( item, null ); if ( !peopleSelected.Any() || peopleSelected.Contains( personId ) ) { var recipient = new Rock.Model.CommunicationRecipient(); recipient.PersonId = personId; foreach ( string mergeField in CommunicateMergeFields ) { object obj = item.GetPropertyValue( mergeField ); if ( obj != null ) { recipient.AdditionalMergeValues.Add( mergeField.Replace( '.', '_' ), obj.ToString() ); } } communication.Recipients.Add( recipient ); } } } if (idProp == null) { // Couldn't determine data source, at least add recipients for any selected people foreach ( int personId in peopleSelected ) { var recipient = new Rock.Model.CommunicationRecipient(); recipient.PersonId = personId; communication.Recipients.Add( recipient ); } } } if ( communication.Recipients.Any() ) { var rockContext = new RockContext(); var service = new Rock.Model.CommunicationService( rockContext ); service.Add( communication ); rockContext.SaveChanges(); Page.Response.Redirect( string.Format( CommunicationPageRoute, communication.Id ), false ); Context.ApplicationInstance.CompleteRequest(); } } } }