/// <summary> /// Handles the Delete event of the gCommunication 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 gCommunication_Delete( object sender, Rock.Web.UI.Controls.RowEventArgs e ) { var rockContext = new RockContext(); var communicationService = new CommunicationService( rockContext ); var communication = communicationService.Get( e.RowKeyId ); if ( communication != null ) { string errorMessage; if ( !communicationService.CanDelete( communication, out errorMessage ) ) { mdGridWarning.Show( errorMessage, ModalAlertType.Information ); return; } communicationService.Delete( communication ); rockContext.SaveChanges(); } BindGrid(); }
/// <summary> /// Copies the specified communication identifier. /// </summary> /// <param name="communicationId">The communication identifier.</param> /// <param name="currentPersonAliasId">The current person alias identifier.</param> /// <returns></returns> public Communication Copy(int communicationId, int?currentPersonAliasId) { var dataContext = ( RockContext )Context; var service = new CommunicationService(dataContext); var communicationRecipientService = new CommunicationRecipientService(dataContext); var communication = service.Get(communicationId); if (communication != null) { var newCommunication = communication.Clone(false); newCommunication.CreatedByPersonAlias = null; newCommunication.CreatedByPersonAliasId = null; newCommunication.CreatedDateTime = RockDateTime.Now; newCommunication.ModifiedByPersonAlias = null; newCommunication.ModifiedByPersonAliasId = null; newCommunication.ModifiedDateTime = RockDateTime.Now; newCommunication.Id = 0; newCommunication.Guid = Guid.Empty; newCommunication.SenderPersonAliasId = currentPersonAliasId; newCommunication.Status = CommunicationStatus.Draft; newCommunication.ReviewerPersonAliasId = null; newCommunication.ReviewedDateTime = null; newCommunication.ReviewerNote = string.Empty; newCommunication.SendDateTime = null; // Get the recipients from the original communication, // but only for recipients that are using the person's primary alias id. // This will avoid an issue where a copied communication will include the same person multiple times // if they have been merged since the original communication was created var primaryAliasRecipients = communicationRecipientService.Queryable() .Where(a => a.CommunicationId == communication.Id) .Select(a => new { a.PersonAlias.Person, a.AdditionalMergeValuesJson, a.PersonAliasId }).ToList() .GroupBy(a => a.Person.PrimaryAliasId) .Select(s => new { PersonAliasId = s.Key, AdditionalMergeValuesJson = s.Where(a => a.PersonAliasId == s.Key).Select(x => x.AdditionalMergeValuesJson).FirstOrDefault() }) .Where(s => s.PersonAliasId.HasValue) .ToList(); foreach (var primaryAliasRecipient in primaryAliasRecipients) { newCommunication.Recipients.Add(new CommunicationRecipient() { PersonAliasId = primaryAliasRecipient.PersonAliasId.Value, Status = CommunicationRecipientStatus.Pending, StatusNote = string.Empty, AdditionalMergeValuesJson = primaryAliasRecipient.AdditionalMergeValuesJson }); } foreach (var attachment in communication.Attachments.ToList()) { var newAttachment = new CommunicationAttachment(); newAttachment.BinaryFileId = attachment.BinaryFileId; newAttachment.CommunicationType = attachment.CommunicationType; newCommunication.Attachments.Add(newAttachment); } return(newCommunication); } return(null); }
/// <summary> /// Handles the Delete event of the gCommunication 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 gCommunication_Delete( object sender, Rock.Web.UI.Controls.RowEventArgs e ) { RockTransactionScope.WrapTransaction( () => { var communicationService = new CommunicationService(); var communication = communicationService.Get( e.RowKeyId ); if ( communication != null ) { string errorMessage; if ( !communicationService.CanDelete( communication, out errorMessage ) ) { mdGridWarning.Show( errorMessage, ModalAlertType.Information ); return; } communicationService.Delete( communication, CurrentPersonId ); communicationService.Save( communication, CurrentPersonId ); } } ); BindGrid(); }
protected void btnCancel_Click( object sender, EventArgs e ) { if ( _editingApproved ) { var communicationService = new CommunicationService( new RockContext() ); var communication = communicationService.Get( CommunicationId.Value ); if ( communication != null && communication.Status == CommunicationStatus.PendingApproval ) { // Redirect back to same page without the edit param var pageRef = new Rock.Web.PageReference(); pageRef.PageId = CurrentPageReference.PageId; pageRef.RouteId = CurrentPageReference.RouteId; pageRef.Parameters = new Dictionary<string, string>(); pageRef.Parameters.Add("CommunicationId", communication.Id.ToString()); Response.Redirect( pageRef.BuildUrl() ); Context.ApplicationInstance.CompleteRequest(); } } }
/// <summary> /// Updates a communication model with the user-entered values /// </summary> /// <param name="communicationService">The service.</param> /// <returns></returns> private Rock.Model.Communication UpdateCommunication(RockContext rockContext) { var communicationService = new CommunicationService(rockContext); var recipientService = new CommunicationRecipientService(rockContext); Rock.Model.Communication communication = null; IQueryable<CommunicationRecipient> qryRecipients = null; if ( CommunicationId.HasValue ) { communication = communicationService.Get( CommunicationId.Value ); } if ( communication != null ) { // Remove any deleted recipients HashSet<int> personIdHash = new HashSet<int>( Recipients.Select( a => a.PersonId ) ); qryRecipients = communication.GetRecipientsQry( rockContext ); foreach ( var item in qryRecipients.Select( a => new { Id = a.Id, PersonId = a.PersonAlias.PersonId }) ) { if ( !personIdHash.Contains(item.PersonId) ) { var recipient = qryRecipients.Where( a => a.Id == item.Id ).FirstOrDefault(); recipientService.Delete( recipient ); communication.Recipients.Remove( recipient ); } } } if (communication == null) { communication = new Rock.Model.Communication(); communication.Status = CommunicationStatus.Transient; communication.SenderPersonAliasId = CurrentPersonAliasId; communicationService.Add( communication ); } if (qryRecipients == null) { qryRecipients = communication.GetRecipientsQry( rockContext ); } // Add any new recipients HashSet<int> communicationPersonIdHash = new HashSet<int>( qryRecipients.Select( a => a.PersonAlias.PersonId ) ); foreach(var recipient in Recipients ) { if ( !communicationPersonIdHash.Contains( recipient.PersonId ) ) { var person = new PersonService( rockContext ).Get( recipient.PersonId ); if ( person != null ) { var communicationRecipient = new CommunicationRecipient(); communicationRecipient.PersonAlias = person.PrimaryAlias; communication.Recipients.Add( communicationRecipient ); } } } communication.IsBulkCommunication = cbBulk.Checked; communication.MediumEntityTypeId = MediumEntityTypeId; communication.MediumData.Clear(); GetMediumData(); foreach ( var keyVal in MediumData ) { if ( !string.IsNullOrEmpty( keyVal.Value ) ) { communication.MediumData.Add( keyVal.Key, keyVal.Value ); } } if ( communication.MediumData.ContainsKey( "Subject" ) ) { communication.Subject = communication.MediumData["Subject"]; communication.MediumData.Remove( "Subject" ); } DateTime? futureSendDate = dtpFutureSend.SelectedDateTime; if ( futureSendDate.HasValue && futureSendDate.Value.CompareTo( RockDateTime.Now ) > 0 ) { communication.FutureSendDateTime = futureSendDate; } else { communication.FutureSendDateTime = null; } return communication; }
/// <summary> /// Updates a communication model with the user-entered values /// </summary> /// <param name="communicationService">The service.</param> /// <returns></returns> private Rock.Model.Communication UpdateCommunication(RockContext rockContext) { var communicationService = new CommunicationService(rockContext); var recipientService = new CommunicationRecipientService(rockContext); Rock.Model.Communication communication = null; if ( CommunicationId.HasValue ) { communication = communicationService.Get( CommunicationId.Value ); // Remove any deleted recipients foreach(var recipient in recipientService.GetByCommunicationId( CommunicationId.Value ) ) { if (!Recipients.Any( r => recipient.PersonAlias != null && r.PersonId == recipient.PersonAlias.PersonId)) { recipientService.Delete(recipient); communication.Recipients.Remove( recipient ); } } } if (communication == null) { communication = new Rock.Model.Communication(); communication.Status = CommunicationStatus.Transient; communication.SenderPersonAliasId = CurrentPersonAliasId; communicationService.Add( communication ); } // Add any new recipients foreach(var recipient in Recipients ) { if ( !communication.Recipients.Any( r => r.PersonAlias != null && r.PersonAlias.PersonId == recipient.PersonId ) ) { var person = new PersonService( rockContext ).Get( recipient.PersonId ); if ( person != null ) { var communicationRecipient = new CommunicationRecipient(); communicationRecipient.PersonAlias = person.PrimaryAlias; communication.Recipients.Add( communicationRecipient ); } } } communication.IsBulkCommunication = cbBulk.Checked; communication.MediumEntityTypeId = MediumEntityTypeId; communication.MediumData.Clear(); GetMediumData(); foreach ( var keyVal in MediumData ) { if ( !string.IsNullOrEmpty( keyVal.Value ) ) { communication.MediumData.Add( keyVal.Key, keyVal.Value ); } } if ( communication.MediumData.ContainsKey( "Subject" ) ) { communication.Subject = communication.MediumData["Subject"]; communication.MediumData.Remove( "Subject" ); } DateTime? futureSendDate = dtpFutureSend.SelectedDateTime; if ( futureSendDate.HasValue && futureSendDate.Value.CompareTo( RockDateTime.Now ) > 0 ) { communication.FutureSendDateTime = futureSendDate; } else { communication.FutureSendDateTime = null; } return communication; }
/// <summary> /// Sends the specified communication. /// </summary> /// <param name="communication">The communication.</param> public override void Send( Model.Communication communication ) { var rockContext = new RockContext(); var communicationService = new CommunicationService( rockContext ); communication = communicationService.Get( communication.Id ); if ( communication != null && communication.Status == Model.CommunicationStatus.Approved && communication.Recipients.Where( r => r.Status == Model.CommunicationRecipientStatus.Pending ).Any() && ( !communication.FutureSendDateTime.HasValue || communication.FutureSendDateTime.Value.CompareTo( RockDateTime.Now ) <= 0 ) ) { // Update any recipients that should not get sent the communication var recipientService = new CommunicationRecipientService( rockContext ); foreach ( var recipient in recipientService.Queryable( "Person" ) .Where( r => r.CommunicationId == communication.Id && r.Status == CommunicationRecipientStatus.Pending ) .ToList() ) { var person = recipient.Person; if ( person.IsDeceased ?? false ) { recipient.Status = CommunicationRecipientStatus.Failed; recipient.StatusNote = "Person is deceased!"; } } rockContext.SaveChanges(); } base.Send( communication ); }
protected void btnEdit_Click( object sender, EventArgs e ) { var rockContext = new RockContext(); var service = new CommunicationService( rockContext ); var communication = service.Get( CommunicationId.Value ); if ( communication != null && communication.Status == CommunicationStatus.PendingApproval && IsUserAuthorized( "Approve" ) ) { // Redirect back to same page without the edit param var pageRef = CurrentPageReference; pageRef.Parameters.Add( "edit", "true" ); Response.Redirect( pageRef.BuildUrl() ); Context.ApplicationInstance.CompleteRequest(); } }
/// <summary> /// Handles the Click event of the btnDeny 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 btnDeny_Click( object sender, EventArgs e ) { if ( Page.IsValid && CommunicationId.HasValue ) { var rockContext = new RockContext(); var service = new CommunicationService( rockContext ); var communication = service.Get( CommunicationId.Value ); if ( communication != null ) { if ( communication.Status == CommunicationStatus.PendingApproval ) { if ( IsUserAuthorized( "Approve" ) ) { communication.Status = CommunicationStatus.Denied; communication.ReviewedDateTime = RockDateTime.Now; communication.ReviewerPersonAliasId = CurrentPersonAliasId; rockContext.SaveChanges(); // TODO: Send notice to sneder that communication was denied ShowResult( "The communication has been denied", communication, NotificationBoxType.Warning ); } else { ShowResult( "Sorry, you are not authorized to approve or deny this communication!", communication, NotificationBoxType.Danger ); } } else { ShowResult( string.Format( "This communication is already {0}!", communication.Status.ConvertToString() ), communication, NotificationBoxType.Warning ); } } } }
/// <summary> /// Handles the Click event of the btnCopy 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 btnCopy_Click( object sender, EventArgs e ) { if ( Page.IsValid && CommunicationId.HasValue ) { var rockContext = new RockContext(); var service = new CommunicationService( rockContext ); var communication = service.Get( CommunicationId.Value ); if ( communication != null ) { var newCommunication = communication.Clone( false ); newCommunication.CreatedByPersonAlias = null; newCommunication.CreatedByPersonAliasId = null; newCommunication.CreatedDateTime = RockDateTime.Now; newCommunication.ModifiedByPersonAlias = null; newCommunication.ModifiedByPersonAliasId = null; newCommunication.ModifiedDateTime = RockDateTime.Now; newCommunication.Id = 0; newCommunication.Guid = Guid.Empty; newCommunication.SenderPersonAliasId = CurrentPersonAliasId; newCommunication.Status = CommunicationStatus.Draft; newCommunication.ReviewerPersonAliasId = null; newCommunication.ReviewedDateTime = null; newCommunication.ReviewerNote = string.Empty; communication.Recipients.ToList().ForEach( r => newCommunication.Recipients.Add( new CommunicationRecipient() { PersonAliasId = r.PersonAliasId, Status = CommunicationRecipientStatus.Pending, StatusNote = string.Empty, AdditionalMergeValuesJson = r.AdditionalMergeValuesJson } ) ); service.Add( newCommunication ); rockContext.SaveChanges(); // Redirect to new communication if ( CurrentPageReference.Parameters.ContainsKey( "CommunicationId" ) ) { CurrentPageReference.Parameters["CommunicationId"] = newCommunication.Id.ToString(); } else { CurrentPageReference.Parameters.Add( "CommunicationId", newCommunication.Id.ToString() ); } Response.Redirect( CurrentPageReference.BuildUrl() ); Context.ApplicationInstance.CompleteRequest(); } } }
/// <summary> /// Handles the Click event of the btnCancel 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 btnCancel_Click( object sender, EventArgs e ) { if ( Page.IsValid && CommunicationId.HasValue ) { var rockContext = new RockContext(); var service = new CommunicationService( rockContext ); var communication = service.Get( CommunicationId.Value ); if ( communication != null ) { if (communication.Status == CommunicationStatus.Approved || communication.Status == CommunicationStatus.PendingApproval) { if ( !communication.Recipients .Where( r => r.Status == CommunicationRecipientStatus.Delivered ) .Any() ) { communication.Status = CommunicationStatus.Draft; rockContext.SaveChanges(); ShowResult( "This communication has successfully been cancelled without any recipients receiving communication!", communication, NotificationBoxType.Success ); } else { communication.Recipients .Where( r => r.Status == CommunicationRecipientStatus.Pending ) .ToList() .ForEach( r => r.Status = CommunicationRecipientStatus.Cancelled ); rockContext.SaveChanges(); int delivered = communication.Recipients.Count( r => r.Status == CommunicationRecipientStatus.Delivered ); ShowResult( string.Format("This communication has been cancelled, however the communication was delivered to {0} recipients!", delivered) , communication, NotificationBoxType.Warning ); } } else { ShowResult( "This communication has already been cancelled!", communication, NotificationBoxType.Warning ); } } } }
/// <summary> /// Sends the specified communication. /// </summary> /// <param name="communication">The communication.</param> public override void Send( Model.Communication communication ) { var rockContext = new RockContext(); var communicationService = new CommunicationService( rockContext ); communication = communicationService.Get( communication.Id ); if ( communication != null && communication.Status == Model.CommunicationStatus.Approved && communication.Recipients.Where( r => r.Status == Model.CommunicationRecipientStatus.Pending ).Any() && ( !communication.FutureSendDateTime.HasValue || communication.FutureSendDateTime.Value.CompareTo( RockDateTime.Now ) <= 0 ) ) { // Update any recipients that should not get sent the communication var recipientService = new CommunicationRecipientService( rockContext ); foreach ( var recipient in recipientService.Queryable( "Person" ) .Where( r => r.CommunicationId == communication.Id && r.Status == CommunicationRecipientStatus.Pending ) .ToList() ) { var person = recipient.Person; if ( person.IsDeceased ?? false ) { recipient.Status = CommunicationRecipientStatus.Failed; recipient.StatusNote = "Person is deceased!"; } if ( person.EmailPreference == Model.EmailPreference.DoNotEmail ) { recipient.Status = CommunicationRecipientStatus.Failed; recipient.StatusNote = "Email Preference of 'Do Not Email!'"; } else if ( person.EmailPreference == Model.EmailPreference.NoMassEmails && communication.IsBulkCommunication ) { recipient.Status = CommunicationRecipientStatus.Failed; recipient.StatusNote = "Email Preference of 'No Mass Emails!'"; } } // If an unbsubcribe value has been entered, and this is a bulk email, add the text if ( communication.IsBulkCommunication ) { string unsubscribeHtml = GetAttributeValue( "UnsubscribeHTML" ); if ( !string.IsNullOrWhiteSpace( unsubscribeHtml ) ) { communication.SetChannelDataValue( "UnsubscribeHTML", unsubscribeHtml ); } } string defaultPlainText = GetAttributeValue( "DefaultPlainText" ); if ( !string.IsNullOrWhiteSpace( defaultPlainText ) ) { communication.SetChannelDataValue( "DefaultPlainText", defaultPlainText ); } rockContext.SaveChanges(); } base.Send( communication ); }
/// <summary> /// Updates a communication model with the user-entered values /// </summary> /// <param name="service">The service.</param> /// <returns></returns> private Rock.Model.Communication UpdateCommunication(CommunicationService service) { Rock.Model.Communication communication = null; if ( CommunicationId.HasValue ) { communication = service.Get( CommunicationId.Value ); } if (communication == null) { communication = new Rock.Model.Communication(); communication.Status = CommunicationStatus.Transient; communication.SenderPersonId = CurrentPersonId; service.Add( communication, CurrentPersonId ); } communication.ChannelEntityTypeId = ChannelEntityTypeId; foreach(var recipient in Recipients) { if ( !communication.Recipients.Where( r => r.PersonId == recipient.PersonId ).Any() ) { var communicationRecipient = new CommunicationRecipient(); communicationRecipient.Person = new PersonService().Get( recipient.PersonId ); communicationRecipient.Status = CommunicationRecipientStatus.Pending; communication.Recipients.Add( communicationRecipient ); } } GetChannelData(); communication.ChannelData = ChannelData; if ( communication.ChannelData.ContainsKey( "Subject" ) ) { communication.Subject = communication.ChannelData["Subject"]; communication.ChannelData.Remove( "Subject" ); } communication.FutureSendDateTime = dtpFutureSend.SelectedDateTime; return communication; }
/// <summary> /// Handles the Click event of the btnCancel 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 btnCancel_Click( object sender, EventArgs e ) { if ( Page.IsValid ) { if ( CommunicationId.HasValue ) { var service = new CommunicationService(); var communication = service.Get( CommunicationId.Value ); if ( communication != null ) { var prevStatus = communication.Status; communication.Recipients .Where( r => r.Status == CommunicationRecipientStatus.Pending ) .ToList() .ForEach( r => r.Status = CommunicationRecipientStatus.Cancelled ); // Save and re-read communication to reload recipient statuses service.Save( communication, CurrentPersonId ); communication = service.Get( communication.Id ); if ( !communication.Recipients .Where( r => r.Status == CommunicationRecipientStatus.Success ) .Any() ) { communication.Status = CommunicationStatus.Draft; } ShowResult( "The communication has been cancelled", communication ); } } } }