Beispiel #1
0
        /// <summary>
        /// Creates the communication to the recipient's mobile device with attachments.
        /// </summary>
        /// <param name="fromPerson">From person.</param>
        /// <param name="toPersonAliasId">To person alias identifier.</param>
        /// <param name="message">The message.</param>
        /// <param name="fromPhone">From phone.</param>
        /// <param name="responseCode">The response code.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="attachments">The attachments.</param>
        public static void CreateCommunicationMobile(Person fromPerson, int?toPersonAliasId, string message, DefinedValueCache fromPhone, string responseCode, Rock.Data.RockContext rockContext, List <BinaryFile> attachments)
        {
            // NOTE: fromPerson should never be null since a Nameless Person record should have been created if a regular person record wasn't found
            string communicationName = fromPerson != null?string.Format("From: {0}", fromPerson.FullName) : "From: unknown person";

            var communicationService = new CommunicationService(rockContext);

            var createSMSCommunicationArgs = new CommunicationService.CreateSMSCommunicationArgs
            {
                FromPerson            = fromPerson,
                ToPersonAliasId       = toPersonAliasId,
                Message               = message,
                FromPhone             = fromPhone,
                CommunicationName     = communicationName,
                ResponseCode          = responseCode,
                SystemCommunicationId = null,
            };

            Rock.Model.Communication communication = communicationService.CreateSMSCommunication(createSMSCommunicationArgs);

            rockContext.SaveChanges();

            // Now that we have a communication ID we can add the attachments
            if (attachments != null && attachments.Any())
            {
                foreach (var attachment in attachments)
                {
                    var communicationAttachment = new CommunicationAttachment
                    {
                        BinaryFileId      = attachment.Id,
                        CommunicationId   = communication.Id,
                        CommunicationType = CommunicationType.SMS
                    };

                    communication.AddAttachment(communicationAttachment, CommunicationType.SMS);
                }

                rockContext.SaveChanges();
            }

            // queue the sending
            var transaction = new ProcessSendCommunication.Message()
            {
                CommunicationId = communication.Id,
            };

            transaction.Send();
        }
        public Attachment CommunicationAttachment2Attachment(CommunicationAttachment commAttach)
        {
            Attachment retVal = new Attachment();

            if (commAttach.AttachmentId != null)
            {
                retVal.AttachmentId = commAttach.AttachmentId;
            }
            retVal.CreatorName  = commAttach.CreatorName;
            retVal.DisplayName  = commAttach.DisplayName;
            retVal.FileType     = commAttach.FileType;
            retVal.FileUrl      = commAttach.FileUrl;
            retVal.LastModified = commAttach.LastModified;

            return(retVal);
        }
        /// <summary>
        /// Migrates communication data from the MediumDataJson field to the individual fields.
        /// </summary>
        /// <param name="updateTemplates">if set to <c>true</c> [update templates].</param>
        /// <param name="howManyToConvert">The how many to convert.</param>
        /// <returns></returns>
        public static bool UpdateCommunicationRecords(bool updateTemplates, int howManyToConvert)
        {
            bool anyRemaining = true;



            if (updateTemplates)
            {
                using (var rockContext = new RockContext())
                {
                    var binaryFileService = new BinaryFileService(rockContext);

                    foreach (var comm in new CommunicationTemplateService(rockContext)
                             .Queryable().Where(c =>
                                                c.MediumDataJson != null &&
                                                c.MediumDataJson != "" &&
                                                c.MediumDataJson != "{}"))
                    {
                        var attachmentBinaryFileIds = new List <int>();
                        SetPropertiesFromMediumDataJson(comm, comm.MediumDataJson, attachmentBinaryFileIds);

                        foreach (int binaryFileId in attachmentBinaryFileIds)
                        {
                            var binaryFile = binaryFileService.Get(binaryFileId);
                            if (binaryFile != null)
                            {
                                var attachment = new CommunicationTemplateAttachment();
                                attachment.BinaryFile        = binaryFile;
                                attachment.CommunicationType = CommunicationType.Email;
                                comm.AddAttachment(attachment, CommunicationType.Email);
                            }
                        }

                        comm.MediumDataJson = string.Empty;
                    }
                    rockContext.SaveChanges();
                }
            }

            int howManyLeft = howManyToConvert;

            while (howManyLeft > 0)
            {
                using (var rockContext = new RockContext())
                {
                    int take           = howManyLeft < 100 ? howManyLeft : 100;
                    var communications = new CommunicationService(rockContext)
                                         .Queryable().Where(c =>
                                                            c.MediumDataJson != null &&
                                                            c.MediumDataJson != "" &&
                                                            c.MediumDataJson != "{}")
                                         .OrderByDescending(c => c.Id)
                                         .Take(take)
                                         .ToList();

                    anyRemaining = communications.Count >= take;
                    howManyLeft  = anyRemaining ? howManyLeft - take : 0;

                    var binaryFileService = new BinaryFileService(rockContext);

                    foreach (var comm in communications)
                    {
                        var attachmentBinaryFileIds = new List <int>();
                        SetPropertiesFromMediumDataJson(comm, comm.MediumDataJson, attachmentBinaryFileIds);

                        foreach (int binaryFileId in attachmentBinaryFileIds)
                        {
                            var binaryFile = binaryFileService.Get(binaryFileId);
                            if (binaryFile != null)
                            {
                                var attachment = new CommunicationAttachment();
                                attachment.BinaryFile        = binaryFile;
                                attachment.CommunicationType = CommunicationType.Email;
                                comm.AddAttachment(attachment, CommunicationType.Email);
                            }
                        }

                        comm.MediumDataJson = string.Empty;
                    }
                    rockContext.SaveChanges();
                }
            }

            return(anyRemaining);
        }
Beispiel #4
0
        /// <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;
                    newCommunication.SendDateTime          = null;

                    communication.Recipients.ToList().ForEach(r =>
                                                              newCommunication.Recipients.Add(new CommunicationRecipient()
                    {
                        PersonAliasId             = r.PersonAliasId,
                        Status                    = CommunicationRecipientStatus.Pending,
                        StatusNote                = string.Empty,
                        AdditionalMergeValuesJson = r.AdditionalMergeValuesJson
                    }));


                    foreach (var attachment in communication.Attachments.ToList())
                    {
                        var newAttachment = new CommunicationAttachment();
                        newAttachment.BinaryFileId      = attachment.BinaryFileId;
                        newAttachment.CommunicationType = attachment.CommunicationType;
                        newCommunication.Attachments.Add(newAttachment);
                    }

                    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>
        /// Migrates communication data from the MediumDataJson field to the individual fields.
        /// </summary>
        /// <param name="updateTemplates">if set to <c>true</c> [update templates].</param>
        /// <param name="howManyToConvert">The how many to convert.</param>
        /// <param name="commandTimeout">The command timeout (seconds).</param>
        /// <returns></returns>
        public static bool UpdateCommunicationRecords(bool updateTemplates, int howManyToConvert, int?commandTimeout)
        {
            bool anyRemaining = true;

            if (updateTemplates)
            {
                using (var rockContext = new RockContext())
                {
                    if (commandTimeout.HasValue)
                    {
                        rockContext.Database.CommandTimeout = commandTimeout;
                    }

                    var binaryFileService = new BinaryFileService(rockContext);

                    // if there is any pre-v7 MediumDataJson data, it would be have a datalength of 2 or more (blank would be null, '', or '{}')
                    foreach (var comm in new CommunicationTemplateService(rockContext).Queryable()
                             .Where(c => SqlFunctions.DataLength(c.MediumDataJson) > 2))
                    {
                        var attachmentBinaryFileIds = new List <int>();
                        SetPropertiesFromMediumDataJson(comm, comm.MediumDataJson, attachmentBinaryFileIds);

                        foreach (int binaryFileId in attachmentBinaryFileIds)
                        {
                            var binaryFile = binaryFileService.Get(binaryFileId);
                            if (binaryFile != null)
                            {
                                var attachment = new CommunicationTemplateAttachment();
                                attachment.BinaryFile        = binaryFile;
                                attachment.CommunicationType = CommunicationType.Email;
                                comm.AddAttachment(attachment, CommunicationType.Email);
                            }
                        }

                        comm.MediumDataJson = string.Empty;
                    }
                    rockContext.SaveChanges();
                }
            }

            int howManyLeft = howManyToConvert;

            while (howManyLeft > 0)
            {
                using (var rockContext = new RockContext())
                {
                    int take = howManyLeft < 100 ? howManyLeft : 100;

                    // if there is any pre-v7 MediumDataJson data, it would be have a datalength of 2 or more (blank would be null, '', or '{}')
                    var communications = new CommunicationService(rockContext).Queryable()
                                         .Where(c => SqlFunctions.DataLength(c.MediumDataJson) > 2)
                                         .OrderByDescending(c => c.Id)
                                         .Take(take)
                                         .ToList();

                    anyRemaining = communications.Count >= take;
                    howManyLeft  = anyRemaining ? howManyLeft - take : 0;

                    var binaryFileService = new BinaryFileService(rockContext);

                    foreach (var comm in communications)
                    {
                        var attachmentBinaryFileIds = new List <int>();
                        SetPropertiesFromMediumDataJson(comm, comm.MediumDataJson, attachmentBinaryFileIds);

                        foreach (int binaryFileId in attachmentBinaryFileIds)
                        {
                            var binaryFile = binaryFileService.Get(binaryFileId);
                            if (binaryFile != null)
                            {
                                var attachment = new CommunicationAttachment();
                                attachment.BinaryFile        = binaryFile;
                                attachment.CommunicationType = CommunicationType.Email;
                                comm.AddAttachment(attachment, CommunicationType.Email);
                            }
                        }

                        comm.MediumDataJson = string.Empty;
                    }

                    rockContext.SaveChanges();
                }
            }

            return(anyRemaining);
        }