Beispiel #1
0
        /// <summary>
        /// Creates a new communication.
        /// </summary>
        /// <param name="fromPersonAliasId">From person alias identifier.</param>
        /// <param name="fromPersonName">Name of from person.</param>
        /// <param name="toPersonAliasId">To person alias identifier.</param>
        /// <param name="message">The message to send.</param>
        /// <param name="transportPhone">The transport phone.</param>
        /// <param name="responseCode">The reponseCode to use for tracking the conversation.</param>
        /// <param name="rockContext">A context to use for database calls.</param>
        private void CreateCommunication(int fromPersonAliasId, string fromPersonName, int toPersonAliasId, string message, string transportPhone, string responseCode, Rock.Data.RockContext rockContext)
        {
            // add communication for reply
            var communication = new Rock.Model.Communication();

            communication.IsBulkCommunication = false;
            communication.Status = CommunicationStatus.Approved;
            communication.SenderPersonAliasId = fromPersonAliasId;
            communication.Subject             = string.Format("From: {0}", fromPersonName);

            communication.SetMediumDataValue("Message", message);
            communication.SetMediumDataValue("FromValue", transportPhone);

            communication.MediumEntityTypeId = EntityTypeCache.Read("Rock.Communication.Medium.Sms").Id;

            var recipient = new Rock.Model.CommunicationRecipient();

            recipient.Status        = CommunicationRecipientStatus.Pending;
            recipient.PersonAliasId = toPersonAliasId;
            recipient.ResponseCode  = responseCode;
            communication.Recipients.Add(recipient);

            var communicationService = new Rock.Model.CommunicationService(rockContext);

            communicationService.Add(communication);
            rockContext.SaveChanges();

            // queue the sending
            var transaction = new Rock.Transactions.SendCommunicationTransaction();

            transaction.CommunicationId = communication.Id;
            transaction.PersonAlias     = null;
            Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
        }
        public static Rock.Model.CommunicationRecipient GetNextPending(int communicationId, Rock.Data.RockContext rockContext)
        {
            CommunicationRecipient recipient = null;

            var delayTime = RockDateTime.Now.AddMinutes(-10);

            lock ( _obj )
            {
                recipient = new CommunicationRecipientService(rockContext).Queryable("Communication,PersonAlias.Person")
                            .Where(r =>
                                   r.CommunicationId == communicationId &&
                                   (r.PersonAlias.Person.IsDeceased == false) &&
                                   (r.Status == CommunicationRecipientStatus.Pending ||
                                    (r.Status == CommunicationRecipientStatus.Sending && r.ModifiedDateTime < delayTime)))
                            .FirstOrDefault();

                if (recipient != null)
                {
                    recipient.Status = CommunicationRecipientStatus.Sending;
                    rockContext.SaveChanges();
                }
            }

            return(recipient);
        }
        /// <summary>
        /// Creates the email communication.
        /// </summary>
        /// <param name="recipientEmails">The recipient emails.</param>
        /// <param name="fromName">From name.</param>
        /// <param name="fromAddress">From address.</param>
        /// <param name="replyTo">The reply to.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="message">The message.</param>
        /// <param name="bulkCommunication">if set to <c>true</c> [bulk communication].</param>\
        /// <param name="sendDateTime">The send date time.</param>
        /// <param name="recipientStatus">The recipient status.</param>
        /// <param name="senderPersonAliasId">The sender person alias identifier.</param>
        /// <returns></returns>
        public Communication CreateEmailCommunication
        (
            List <string> recipientEmails,
            string fromName,
            string fromAddress,
            string replyTo,
            string subject,
            string message,
            bool bulkCommunication,
            DateTime?sendDateTime,
            CommunicationRecipientStatus recipientStatus = CommunicationRecipientStatus.Delivered,
            int?senderPersonAliasId = null)
        {
            var recipients = new PersonService((RockContext)Context)
                             .Queryable()
                             .Where(p => recipientEmails.Contains(p.Email))
                             .ToList();

            if (!recipients.Any())
            {
                return(null);
            }

            var communication = new Communication
            {
                CommunicationType   = CommunicationType.Email,
                Status              = CommunicationStatus.Approved,
                SenderPersonAliasId = senderPersonAliasId
            };

            communication.FromName            = fromName.TrimForMaxLength(communication, "FromName");
            communication.FromEmail           = fromAddress.TrimForMaxLength(communication, "FromEmail");
            communication.ReplyToEmail        = replyTo.TrimForMaxLength(communication, "ReplyToEmail");
            communication.Subject             = subject.TrimForMaxLength(communication, "Subject");
            communication.Message             = message;
            communication.IsBulkCommunication = bulkCommunication;
            communication.FutureSendDateTime  = null;
            communication.SendDateTime        = sendDateTime;
            Add(communication);

            // add each person as a recipient to the communication
            foreach (var person in recipients)
            {
                var personAliasId = person.PrimaryAliasId;
                if (!personAliasId.HasValue)
                {
                    continue;
                }

                var communicationRecipient = new CommunicationRecipient
                {
                    PersonAliasId = personAliasId.Value,
                    Status        = recipientStatus,
                    SendDateTime  = sendDateTime
                };
                communication.Recipients.Add(communicationRecipient);
            }

            return(communication);
        }
        /// <summary>
        /// Gets the next pending.
        /// </summary>
        /// <param name="communicationId">The communication identifier.</param>
        /// <param name="mediumEntityId">The medium entity identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        public static Rock.Model.CommunicationRecipient GetNextPending(int communicationId, int mediumEntityId, Rock.Data.RockContext rockContext)
        {
            CommunicationRecipient recipient = null;

            var delayTime = RockDateTime.Now.AddMinutes(-240);

            lock ( _obj )
            {
                recipient = new CommunicationRecipientService(rockContext).Queryable().Include(r => r.Communication).Include(r => r.PersonAlias.Person)
                            .Where(r =>
                                   r.CommunicationId == communicationId &&
                                   (r.Status == CommunicationRecipientStatus.Pending ||
                                    (r.Status == CommunicationRecipientStatus.Sending && r.ModifiedDateTime < delayTime)
                                   ) &&
                                   r.MediumEntityTypeId.HasValue &&
                                   r.MediumEntityTypeId.Value == mediumEntityId)
                            .FirstOrDefault();

                if (recipient != null)
                {
                    recipient.ModifiedDateTime = RockDateTime.Now;
                    recipient.Status           = CommunicationRecipientStatus.Sending;
                    rockContext.SaveChanges();
                }
            }

            return(recipient);
        }
Beispiel #5
0
 /// <summary>
 /// Copies the properties from another CommunicationRecipient object to this CommunicationRecipient object
 /// </summary>
 /// <param name="target">The target.</param>
 /// <param name="source">The source.</param>
 public static void CopyPropertiesFrom(this CommunicationRecipient target, CommunicationRecipient source)
 {
     target.Id = source.Id;
     target.AdditionalMergeValuesJson = source.AdditionalMergeValuesJson;
     target.CommunicationId           = source.CommunicationId;
     target.ForeignGuid             = source.ForeignGuid;
     target.ForeignKey              = source.ForeignKey;
     target.MediumEntityTypeId      = source.MediumEntityTypeId;
     target.OpenedClient            = source.OpenedClient;
     target.OpenedDateTime          = source.OpenedDateTime;
     target.PersonAliasId           = source.PersonAliasId;
     target.ResponseCode            = source.ResponseCode;
     target.SendDateTime            = source.SendDateTime;
     target.SentMessage             = source.SentMessage;
     target.Status                  = source.Status;
     target.StatusNote              = source.StatusNote;
     target.TransportEntityTypeName = source.TransportEntityTypeName;
     target.UniqueMessageId         = source.UniqueMessageId;
     target.CreatedDateTime         = source.CreatedDateTime;
     target.ModifiedDateTime        = source.ModifiedDateTime;
     target.CreatedByPersonAliasId  = source.CreatedByPersonAliasId;
     target.ModifiedByPersonAliasId = source.ModifiedByPersonAliasId;
     target.Guid      = source.Guid;
     target.ForeignId = source.ForeignId;
 }
Beispiel #6
0
        /// <summary>
        /// Creates the email communication.
        /// </summary>
        /// <param name="recipientEmails">The recipient emails.</param>
        /// <param name="fromName">From name.</param>
        /// <param name="fromAddress">From address.</param>
        /// <param name="replyTo">The reply to.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="htmlMessage">The HTML message.</param>
        /// <param name="textMessage">The text message.</param>
        /// <param name="bulkCommunication">if set to <c>true</c> [bulk communication].</param>
        /// <param name="recipientStatus">The recipient status.</param>
        /// <param name="senderPersonAliasId">The sender person alias identifier.</param>
        /// <returns></returns>
        public Communication CreateEmailCommunication(
            List <string> recipientEmails,
            string fromName,
            string fromAddress,
            string replyTo,
            string subject,
            string htmlMessage,
            string textMessage,
            bool bulkCommunication,
            CommunicationRecipientStatus recipientStatus = CommunicationRecipientStatus.Delivered,
            int?senderPersonAliasId = null)
        {
            var recipients = new PersonService((RockContext)this.Context)
                             .Queryable()
                             .Where(p => recipientEmails.Contains(p.Email))
                             .ToList();

            if (recipients.Any())
            {
                Rock.Model.Communication communication = new Rock.Model.Communication();
                communication.Status = CommunicationStatus.Approved;
                communication.SenderPersonAliasId = senderPersonAliasId;
                communication.Subject             = subject;
                Add(communication);

                communication.IsBulkCommunication = bulkCommunication;
                communication.MediumEntityTypeId  = EntityTypeCache.Read("Rock.Communication.Medium.Email").Id;
                communication.FutureSendDateTime  = null;

                // add each person as a recipient to the communication
                foreach (var person in recipients)
                {
                    int?personAliasId = person.PrimaryAliasId;
                    if (personAliasId.HasValue)
                    {
                        var communicationRecipient = new CommunicationRecipient();
                        communicationRecipient.PersonAliasId = personAliasId.Value;
                        communicationRecipient.Status        = recipientStatus;
                        communication.Recipients.Add(communicationRecipient);
                    }
                }

                // add the MediumData to the communication
                communication.MediumData.Clear();
                communication.MediumData.Add("FromName", fromName);
                communication.MediumData.Add("FromAddress", fromAddress);
                communication.MediumData.Add("ReplyTo", replyTo);
                communication.MediumData.Add("Subject", subject);
                communication.MediumData.Add("HtmlMessage", htmlMessage);
                communication.MediumData.Add("TextMessage", textMessage);

                return(communication);
            }

            return(null);
        }
Beispiel #7
0
 /// <summary>
 /// Copies the properties from another CommunicationRecipient object to this CommunicationRecipient object
 /// </summary>
 /// <param name="target">The target.</param>
 /// <param name="source">The source.</param>
 public static void CopyPropertiesFrom(this CommunicationRecipient target, CommunicationRecipient source)
 {
     target.PersonId                  = source.PersonId;
     target.CommunicationId           = source.CommunicationId;
     target.Status                    = source.Status;
     target.StatusNote                = source.StatusNote;
     target.AdditionalMergeValuesJson = source.AdditionalMergeValuesJson;
     target.Id   = source.Id;
     target.Guid = source.Guid;
 }
 /// <summary>
 /// Clones this CommunicationRecipient object to a new CommunicationRecipient object
 /// </summary>
 /// <param name="source">The source.</param>
 /// <param name="deepCopy">if set to <c>true</c> a deep copy is made. If false, only the basic entity properties are copied.</param>
 /// <returns></returns>
 public static CommunicationRecipient Clone(this CommunicationRecipient source, bool deepCopy)
 {
     if (deepCopy)
     {
         return(source.Clone() as CommunicationRecipient);
     }
     else
     {
         var target = new CommunicationRecipient();
         target.CopyPropertiesFrom(source);
         return(target);
     }
 }
        private Dictionary <string, string> CreateCommunication()
        {
            var selectedMembers = Request.Form["selectedmembers"];
            var selectedIds     = new List <string>();

            if (selectedMembers != null && !string.IsNullOrWhiteSpace(selectedMembers))
            {
                selectedIds = selectedMembers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            else
            {
                mdAlert.Show("Please select members to communicate to.", ModalAlertType.Warning);
                return(new Dictionary <string, string>());
            }
            var rockContext   = new RockContext();
            var service       = new Rock.Model.CommunicationService(rockContext);
            var communication = new Rock.Model.Communication();

            communication.IsBulkCommunication = false;
            communication.Status = Rock.Model.CommunicationStatus.Transient;

            communication.SenderPersonAliasId = this.CurrentPersonAliasId;

            service.Add(communication);

            var personAliasIds = new PersonAliasService(rockContext).Queryable().AsNoTracking()
                                 .Where(a => selectedIds.Contains(a.PersonId.ToString()))
                                 .GroupBy(a => a.PersonId)
                                 .Select(a => a.Min(m => m.Id))
                                 .ToList();

            // Get the primary aliases
            foreach (int personAlias in personAliasIds)
            {
                var recipient = new Rock.Model.CommunicationRecipient();
                recipient.PersonAliasId = personAlias;
                communication.Recipients.Add(recipient);
            }

            rockContext.SaveChanges();

            var queryParameters = new Dictionary <string, string>();

            queryParameters.Add("CommunicationId", communication.Id.ToString());

            return(queryParameters);
        }
Beispiel #10
0
        /// <summary>
        /// Sends the communication.
        /// </summary>
        private void SendCommunication()
        {
            // create communication
            if (this.CurrentPerson != null && _groupId != -1 && !string.IsNullOrWhiteSpace(GetAttributeValue("CommunicationPage")))
            {
                var rockContext   = new RockContext();
                var service       = new Rock.Model.CommunicationService(rockContext);
                var communication = new Rock.Model.Communication();
                communication.IsBulkCommunication = false;
                communication.Status = Rock.Model.CommunicationStatus.Transient;

                communication.SenderPersonAliasId = this.CurrentPersonAliasId;

                service.Add(communication);

                var personAliasIds = new GroupMemberService(rockContext).Queryable()
                                     .Where(m => m.GroupId == _groupId && m.GroupMemberStatus != GroupMemberStatus.Inactive)
                                     .ToList()
                                     .Select(m => m.Person.PrimaryAliasId)
                                     .ToList();

                // Get the primary aliases
                foreach (int personAlias in personAliasIds)
                {
                    var recipient = new Rock.Model.CommunicationRecipient();
                    recipient.PersonAliasId = personAlias;
                    communication.Recipients.Add(recipient);
                }

                rockContext.SaveChanges();

                Dictionary <string, string> queryParameters = new Dictionary <string, string>();
                queryParameters.Add("CommunicationId", communication.Id.ToString());

                NavigateToLinkedPage("CommunicationPage", queryParameters);
            }
        }
        /// <summary>
        /// Refresh the recipients list.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        public void RefreshCommunicationRecipientList(RockContext rockContext)
        {
            if (!ListGroupId.HasValue)
            {
                return;
            }

            var emailMediumEntityType = EntityTypeCache.Get(SystemGuid.EntityType.COMMUNICATION_MEDIUM_EMAIL.AsGuid());
            var smsMediumEntityType   = EntityTypeCache.Get(SystemGuid.EntityType.COMMUNICATION_MEDIUM_SMS.AsGuid());
            var preferredCommunicationTypeAttribute = AttributeCache.Get(SystemGuid.Attribute.GROUPMEMBER_COMMUNICATION_LIST_PREFERRED_COMMUNICATION_MEDIUM.AsGuid());
            var segmentDataViewGuids = this.Segments.SplitDelimitedValues().AsGuidList();
            var segmentDataViewIds   = new DataViewService(rockContext).GetByGuids(segmentDataViewGuids).Select(a => a.Id).ToList();

            var qryCommunicationListMembers = GetCommunicationListMembers(rockContext, ListGroupId, this.SegmentCriteria, segmentDataViewIds);

            // NOTE: If this is scheduled communication, don't include Members that were added after the scheduled FutureSendDateTime
            if (this.FutureSendDateTime.HasValue)
            {
                var memberAddedCutoffDate = this.FutureSendDateTime;
                qryCommunicationListMembers = qryCommunicationListMembers.Where(a => (a.DateTimeAdded.HasValue && a.DateTimeAdded.Value < memberAddedCutoffDate) || (a.CreatedDateTime.HasValue && a.CreatedDateTime.Value < memberAddedCutoffDate));
            }

            var communicationRecipientService = new CommunicationRecipientService(rockContext);

            var recipientsQry = GetRecipientsQry(rockContext);

            // Get all the List member which is not part of communication recipients yet
            var newMemberInList = qryCommunicationListMembers
                                  .Where(a => !recipientsQry.Any(r => r.PersonAlias.PersonId == a.PersonId))
                                  .AsNoTracking()
                                  .ToList();

            foreach (var newMember in newMemberInList)
            {
                var communicationRecipient = new CommunicationRecipient
                {
                    PersonAliasId   = newMember.Person.PrimaryAliasId.Value,
                    Status          = CommunicationRecipientStatus.Pending,
                    CommunicationId = Id
                };

                switch (CommunicationType)
                {
                case CommunicationType.Email:
                    communicationRecipient.MediumEntityTypeId = emailMediumEntityType.Id;
                    break;

                case CommunicationType.SMS:
                    communicationRecipient.MediumEntityTypeId = smsMediumEntityType.Id;
                    break;

                case CommunicationType.RecipientPreference:
                    newMember.LoadAttributes();

                    if (preferredCommunicationTypeAttribute != null)
                    {
                        var recipientPreference = ( CommunicationType? )newMember
                                                  .GetAttributeValue(preferredCommunicationTypeAttribute.Key).AsIntegerOrNull();

                        switch (recipientPreference)
                        {
                        case CommunicationType.SMS:
                            communicationRecipient.MediumEntityTypeId = smsMediumEntityType.Id;
                            break;

                        case CommunicationType.Email:
                            communicationRecipient.MediumEntityTypeId = emailMediumEntityType.Id;
                            break;

                        default:
                            communicationRecipient.MediumEntityTypeId = emailMediumEntityType.Id;
                            break;
                        }
                    }

                    break;

                default:
                    throw new Exception("Unexpected CommunicationType: " + CommunicationType.ConvertToString());
                }

                communicationRecipientService.Add(communicationRecipient);
            }

            // Get all pending communication recipents that are no longer part of the group list member, then delete them from the Recipients
            var missingMemberInList = recipientsQry.Where(a => a.Status == CommunicationRecipientStatus.Pending)
                                      .Where(a => !qryCommunicationListMembers.Any(r => r.PersonId == a.PersonAlias.PersonId))
                                      .ToList();

            foreach (var missingMember in missingMemberInList)
            {
                communicationRecipientService.Delete(missingMember);
            }

            rockContext.SaveChanges();
        }
Beispiel #12
0
        /// <summary>
        /// Creates the email communication.
        /// </summary>
        /// <param name="createEmailCommunicationArgs">The create email communication arguments.</param>
        /// <returns></returns>
        public Communication CreateEmailCommunication(CreateEmailCommunicationArgs createEmailCommunicationArgs)
        {
            var recipients            = createEmailCommunicationArgs.Recipients;
            var senderPersonAliasId   = createEmailCommunicationArgs.SenderPersonAliasId;
            var fromName              = createEmailCommunicationArgs.FromName;
            var fromAddress           = createEmailCommunicationArgs.FromAddress;
            var replyTo               = createEmailCommunicationArgs.ReplyTo;
            var subject               = createEmailCommunicationArgs.Subject;
            var message               = createEmailCommunicationArgs.Message;
            var bulkCommunication     = createEmailCommunicationArgs.BulkCommunication;
            var sendDateTime          = createEmailCommunicationArgs.SendDateTime;
            var recipientStatus       = createEmailCommunicationArgs.RecipientStatus;
            var systemCommunicationId = createEmailCommunicationArgs.SystemCommunicationId;

            var recipientsWithPersonIds       = recipients.Where(a => a.PersonId.HasValue).Select(a => a.PersonId).ToList();
            var recipientEmailsUnknownPersons = recipients.Where(a => a.PersonId == null).Select(a => a.EmailAddress);

            var recipientPersonList = new PersonService(( RockContext )Context)
                                      .Queryable()
                                      .Where(p => recipientsWithPersonIds.Contains(p.Id))
                                      .ToList();

            if (!recipientPersonList.Any() && recipientEmailsUnknownPersons.Any(a => a != null))
            {
                // For backwards compatibility, if no PersonIds where specified, but there are recipients that are only specified by EmailAddress, take a guess at the personIds by looking for matching email addresses
                recipientPersonList = new PersonService(( RockContext )Context)
                                      .Queryable()
                                      .Where(p => recipientEmailsUnknownPersons.Contains(p.Email))
                                      .ToList();
            }

            if (!recipientPersonList.Any())
            {
                return(null);
            }

            var communication = new Communication
            {
                CommunicationType     = CommunicationType.Email,
                Status                = CommunicationStatus.Approved,
                ReviewedDateTime      = RockDateTime.Now,
                ReviewerPersonAliasId = senderPersonAliasId,
                SenderPersonAliasId   = senderPersonAliasId
            };

            communication.FromName              = fromName.TrimForMaxLength(communication, "FromName");
            communication.FromEmail             = fromAddress.TrimForMaxLength(communication, "FromEmail");
            communication.ReplyToEmail          = replyTo.TrimForMaxLength(communication, "ReplyToEmail");
            communication.Subject               = subject.TrimForMaxLength(communication, "Subject");
            communication.Message               = message;
            communication.IsBulkCommunication   = bulkCommunication;
            communication.FutureSendDateTime    = null;
            communication.SendDateTime          = sendDateTime;
            communication.SystemCommunicationId = systemCommunicationId;
            Add(communication);

            // add each person as a recipient to the communication
            foreach (var person in recipientPersonList)
            {
                var personAliasId = person.PrimaryAliasId;
                if (!personAliasId.HasValue)
                {
                    continue;
                }

                var communicationRecipient = new CommunicationRecipient
                {
                    PersonAliasId = personAliasId.Value,
                    Status        = recipientStatus,
                    SendDateTime  = sendDateTime
                };
                communication.Recipients.Add(communicationRecipient);
            }

            return(communication);
        }
        /// <summary>
        /// Creates the email communication.
        /// </summary>
        /// <param name="createEmailCommunicationArgs">The create email communication arguments.</param>
        /// <returns></returns>
        public Communication CreateEmailCommunication(CreateEmailCommunicationArgs createEmailCommunicationArgs)
        {
            var recipients            = createEmailCommunicationArgs.Recipients;
            var senderPersonAliasId   = createEmailCommunicationArgs.SenderPersonAliasId;
            var fromName              = createEmailCommunicationArgs.FromName;
            var fromAddress           = createEmailCommunicationArgs.FromAddress;
            var replyTo               = createEmailCommunicationArgs.ReplyTo;
            var subject               = createEmailCommunicationArgs.Subject;
            var message               = createEmailCommunicationArgs.Message;
            var bulkCommunication     = createEmailCommunicationArgs.BulkCommunication;
            var sendDateTime          = createEmailCommunicationArgs.SendDateTime;
            var recipientStatus       = createEmailCommunicationArgs.RecipientStatus;
            var systemCommunicationId = createEmailCommunicationArgs.SystemCommunicationId;

            var recipientsWithPersonIds       = recipients.Where(a => a.PersonId.HasValue).Select(a => a.PersonId).ToList();
            var recipientEmailsUnknownPersons = recipients.Where(a => a.PersonId == null).Select(a => a.EmailAddress);

            /*
             * 4-MAY-2022 DMV
             *
             * In tracking down alleged duplicate communications we discovered
             * that duplicates could be sent to the same person if they are in the
             * recipient list more that once with mulitple Person Alias IDs.
             * This could have occured through a person merge or other data changes
             * in Rock. This code removes those duplicates from the list before
             * sending the communication.
             *
             */

            var recipientPersonList = new PersonAliasService(( RockContext )Context)
                                      .GetPrimaryAliasQuery()
                                      .Where(pa => recipientsWithPersonIds.Contains(pa.PersonId))
                                      .Select(a => a.Person)
                                      .ToList();

            if (!recipientPersonList.Any() && recipientEmailsUnknownPersons.Any(a => a != null))
            {
                // For backwards compatibility, if no PersonIds where specified, but there are recipients that are only specified by EmailAddress, take a guess at the personIds by looking for matching email addresses
                recipientPersonList = new PersonService(( RockContext )Context)
                                      .Queryable()
                                      .Where(p => recipientEmailsUnknownPersons.Contains(p.Email))
                                      .ToList();
            }

            if (!recipientPersonList.Any())
            {
                return(null);
            }

            var communication = new Communication
            {
                CommunicationType     = CommunicationType.Email,
                Status                = CommunicationStatus.Approved,
                ReviewedDateTime      = RockDateTime.Now,
                ReviewerPersonAliasId = senderPersonAliasId,
                SenderPersonAliasId   = senderPersonAliasId
            };

            communication.FromName              = fromName.TrimForMaxLength(communication, "FromName");
            communication.FromEmail             = fromAddress.TrimForMaxLength(communication, "FromEmail");
            communication.ReplyToEmail          = replyTo.TrimForMaxLength(communication, "ReplyToEmail");
            communication.Subject               = subject.TrimForMaxLength(communication, "Subject");
            communication.Message               = message;
            communication.IsBulkCommunication   = bulkCommunication;
            communication.FutureSendDateTime    = null;
            communication.SendDateTime          = sendDateTime;
            communication.SystemCommunicationId = systemCommunicationId;
            Add(communication);

            // add each person as a recipient to the communication
            foreach (var person in recipientPersonList)
            {
                var personAliasId = person.PrimaryAliasId;
                if (!personAliasId.HasValue)
                {
                    continue;
                }

                var communicationRecipient = new CommunicationRecipient
                {
                    PersonAliasId = personAliasId.Value,
                    Status        = recipientStatus,
                    SendDateTime  = sendDateTime
                };
                communication.Recipients.Add(communicationRecipient);
            }

            return(communication);
        }
        /// <summary>
        /// Sends the communication.
        /// </summary>
        private void SendCommunication()
        {
            // create communication
            if ( this.CurrentPerson != null && _groupId != -1 && !string.IsNullOrWhiteSpace( GetAttributeValue( "CommunicationPage" ) ) )
            {
                var rockContext = new RockContext();
                var service = new Rock.Model.CommunicationService( rockContext );
                var communication = new Rock.Model.Communication();
                communication.IsBulkCommunication = false;
                communication.Status = Rock.Model.CommunicationStatus.Transient;

                communication.SenderPersonAliasId = this.CurrentPersonAliasId;

                service.Add( communication );

                var personAliasIds = new GroupMemberService( rockContext ).Queryable()
                                    .Where( m => m.GroupId == _groupId && m.GroupMemberStatus != GroupMemberStatus.Inactive )
                                    .ToList()
                                    .Select( m => m.Person.PrimaryAliasId )
                                    .ToList();

                // Get the primary aliases
                foreach ( int personAlias in personAliasIds )
                {
                    var recipient = new Rock.Model.CommunicationRecipient();
                    recipient.PersonAliasId = personAlias;
                    communication.Recipients.Add( recipient );
                }

                rockContext.SaveChanges();

                Dictionary<string, string> queryParameters = new Dictionary<string, string>();
                queryParameters.Add( "CommunicationId", communication.Id.ToString() );

                NavigateToLinkedPage( "CommunicationPage", queryParameters );
            }
        }
Beispiel #15
0
        /// <summary>
        /// Creates a new communication.
        /// </summary>
        /// <param name="fromPersonId">Person ID of the sender.</param>
        /// <param name="fromPersonName">Name of from person.</param>
        /// <param name="toPersonId">The Person ID of the recipient.</param>
        /// <param name="message">The message to send.</param>
        /// <param name="transportPhone">The transport phone.</param>
        /// <param name="responseCode">The reponseCode to use for tracking the conversation.</param>
        /// <param name="rockContext">A context to use for database calls.</param>
        private void CreateCommunication( int fromPersonId, string fromPersonName, int toPersonId, string message, string transportPhone, string responseCode, Rock.Data.RockContext rockContext )
        {

            // add communication for reply
            var communication = new Rock.Model.Communication();
            communication.IsBulkCommunication = false;
            communication.Status = CommunicationStatus.Approved;
            communication.SenderPersonId = fromPersonId;
            communication.Subject = string.Format( "From: {0}", fromPersonName );

            communication.SetChannelDataValue( "Message", message );
            communication.SetChannelDataValue( "FromValue", transportPhone );

            communication.ChannelEntityTypeId = EntityTypeCache.Read( "Rock.Communication.Channel.Sms" ).Id;

            var recipient = new Rock.Model.CommunicationRecipient();
            recipient.Status = CommunicationRecipientStatus.Pending;
            recipient.PersonId = toPersonId;
            recipient.ResponseCode = responseCode;
            communication.Recipients.Add( recipient );

            var communicationService = new Rock.Model.CommunicationService( rockContext );
            communicationService.Add( communication );
            rockContext.SaveChanges();

            // queue the sending
            var transaction = new Rock.Transactions.SendCommunicationTransaction();
            transaction.CommunicationId = communication.Id;
            transaction.PersonAlias = null;
            Rock.Transactions.RockQueue.TransactionQueue.Enqueue( transaction );
        }