예제 #1
0
        /// <summary>
        /// Handles the Click event of the mdSaveTemplate 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 mdSaveTemplate_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid || !CommunicationId.HasValue)
            {
                return;
            }

            using (var rockContext = new RockContext())
            {
                var communication = new CommunicationService(rockContext).Get(CommunicationId.Value);
                if (communication == null)
                {
                    return;
                }

                var template = new CommunicationTemplate
                {
                    SenderPersonAliasId = CurrentPersonAliasId,
                    Name                  = tbTemplateName.Text,
                    CategoryId            = cpTemplateCategory.SelectedValue.AsIntegerOrNull(),
                    Description           = tbTemplateDescription.Text,
                    Subject               = communication.Subject,
                    FromName              = communication.FromName,
                    FromEmail             = communication.FromEmail,
                    ReplyToEmail          = communication.ReplyToEmail,
                    CCEmails              = communication.CCEmails,
                    BCCEmails             = communication.BCCEmails,
                    Message               = "{% raw %}" + communication.Message + "{% endraw %}",
                    MessageMetaData       = communication.MessageMetaData,
                    SMSFromDefinedValueId = communication.SMSFromDefinedValueId,
                    SMSMessage            = communication.SMSMessage,
                    PushTitle             = communication.PushTitle,
                    PushMessage           = communication.PushMessage,
                    PushSound             = communication.PushSound
                };

                foreach (var attachment in communication.Attachments.ToList())
                {
                    var newAttachment = new CommunicationTemplateAttachment
                    {
                        BinaryFileId      = attachment.BinaryFileId,
                        CommunicationType = attachment.CommunicationType
                    };
                    template.Attachments.Add(newAttachment);
                }

                var templateService = new CommunicationTemplateService(rockContext);
                templateService.Add(template);
                rockContext.SaveChanges();

                template = templateService.Get(template.Id);
                if (template != null)
                {
                    template.MakePrivate(Authorization.VIEW, CurrentPerson);
                    template.MakePrivate(Authorization.EDIT, CurrentPerson);
                    template.MakePrivate(Authorization.ADMINISTRATE, CurrentPerson);
                }

                nbTemplateCreated.Visible = true;
            }

            HideDialog();
        }
        /// <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);
        }
        /// <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);
        }