/// <summary>
 /// Adds the attachment.
 /// </summary>
 /// <param name="communicationAttachment">The communication attachment.</param>
 /// <param name="communicationType">Type of the communication.</param>
 public void AddAttachment(CommunicationAttachment communicationAttachment, CommunicationType communicationType)
 {
     communicationAttachment.CommunicationType = communicationType;
     this.Attachments.Add(communicationAttachment);
 }
Exemple #2
0
        /// <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);
        }