/// <summary>
        /// Handles the Click event of the btnSave 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 btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            var rockContext = new RockContext();

            var communicationTemplateService           = new CommunicationTemplateService(rockContext);
            var communicationTemplateAttachmentService = new CommunicationTemplateAttachmentService(rockContext);
            var binaryFileService = new BinaryFileService(rockContext);

            CommunicationTemplate communicationTemplate = null;
            var communicationTemplateId = hfCommunicationTemplateId.Value.AsIntegerOrNull();

            if (communicationTemplateId.HasValue)
            {
                communicationTemplate = communicationTemplateService.Get(communicationTemplateId.Value);
            }

            var newTemplate = false;

            if (communicationTemplate == null)
            {
                newTemplate           = true;
                communicationTemplate = new CommunicationTemplate();
                communicationTemplateService.Add(communicationTemplate);
            }

            communicationTemplate.Name        = tbName.Text;
            communicationTemplate.IsActive    = cbIsActive.Checked;
            communicationTemplate.Description = tbDescription.Text;

            if (communicationTemplate.ImageFileId != imgTemplatePreview.BinaryFileId)
            {
                var oldImageTemplatePreview = binaryFileService.Get(communicationTemplate.ImageFileId ?? 0);
                if (oldImageTemplatePreview != null)
                {
                    // the old image template preview won't be needed anymore, so make it IsTemporary and have it get cleaned up later
                    oldImageTemplatePreview.IsTemporary = true;
                }
            }

            communicationTemplate.ImageFileId = imgTemplatePreview.BinaryFileId;

            // Ensure that the ImagePreview is not set as IsTemporary=True
            if (communicationTemplate.ImageFileId.HasValue)
            {
                var imageTemplatePreview = binaryFileService.Get(communicationTemplate.ImageFileId.Value);
                if (imageTemplatePreview != null && imageTemplatePreview.IsTemporary)
                {
                    imageTemplatePreview.IsTemporary = false;
                }
            }

            // Note: If the Logo has changed, we can't get rid of it since existing communications might use it
            communicationTemplate.LogoBinaryFileId = imgTemplateLogo.BinaryFileId;

            // Ensure that the ImagePreview is not set as IsTemporary=True
            if (communicationTemplate.LogoBinaryFileId.HasValue)
            {
                var newImageTemplateLogo = binaryFileService.Get(communicationTemplate.LogoBinaryFileId.Value);
                if (newImageTemplateLogo != null && newImageTemplateLogo.IsTemporary)
                {
                    newImageTemplateLogo.IsTemporary = false;
                }
            }

            communicationTemplate.FromName           = tbFromName.Text;
            communicationTemplate.FromEmail          = tbFromAddress.Text;
            communicationTemplate.ReplyToEmail       = tbReplyToAddress.Text;
            communicationTemplate.CCEmails           = tbCCList.Text;
            communicationTemplate.BCCEmails          = tbBCCList.Text;
            communicationTemplate.LavaFields         = kvlMergeFields.Value.AsDictionaryOrNull();
            communicationTemplate.CssInliningEnabled = cbCssInliningEnabled.Checked;

            var binaryFileIds = hfAttachedBinaryFileIds.Value.SplitDelimitedValues().AsIntegerList();

            // delete any attachments that are no longer included
            foreach (var attachment in communicationTemplate.Attachments
                     .Where(a => !binaryFileIds.Contains(a.BinaryFileId)).ToList())
            {
                communicationTemplate.Attachments.Remove(attachment);
                communicationTemplateAttachmentService.Delete(attachment);
            }

            // add any new attachments that were added
            foreach (var attachmentBinaryFileId in binaryFileIds.Where(a => communicationTemplate.Attachments.All(x => x.BinaryFileId != a)))
            {
                communicationTemplate.Attachments.Add(new CommunicationTemplateAttachment {
                    BinaryFileId = attachmentBinaryFileId
                });
            }

            communicationTemplate.Subject = tbEmailSubject.Text;
            communicationTemplate.Message = ceEmailTemplate.Text;

            communicationTemplate.SMSFromDefinedValueId = ddlSMSFrom.SelectedValue.AsIntegerOrNull();
            communicationTemplate.SMSMessage            = tbSMSTextMessage.Text;

            communicationTemplate.CategoryId = cpCategory.SelectedValueAsInt();

            rockContext.SaveChanges();

            var personalView = GetAttributeValue("PersonalTemplatesView").AsBoolean();

            if (newTemplate)
            {
                communicationTemplate = communicationTemplateService.Get(communicationTemplate.Id);
                if (communicationTemplate != null)
                {
                    if (personalView)
                    {
                        // If editing personal templates, make the new template is private/personal to current user
                        communicationTemplate.MakePrivate(Authorization.VIEW, CurrentPerson);
                        communicationTemplate.MakePrivate(Authorization.EDIT, CurrentPerson);
                        communicationTemplate.MakePrivate(Authorization.ADMINISTRATE, CurrentPerson);
                    }
                    else
                    {
                        // Otherwise, make sure user can view and edit the new template.
                        if (!communicationTemplate.IsAuthorized(Authorization.VIEW, CurrentPerson))
                        {
                            communicationTemplate.AllowPerson(Authorization.VIEW, CurrentPerson);
                        }

                        // Make sure user can edit the new template.
                        if (!communicationTemplate.IsAuthorized(Authorization.EDIT, CurrentPerson))
                        {
                            communicationTemplate.AllowPerson(Authorization.EDIT, CurrentPerson);
                        }
                    }

                    // Always make sure RSR-Admin and Communication Admin can see
                    var groupService = new GroupService(rockContext);
                    var communicationAdministrators = groupService.Get(Rock.SystemGuid.Group.GROUP_COMMUNICATION_ADMINISTRATORS.AsGuid());
                    if (communicationAdministrators != null)
                    {
                        communicationTemplate.AllowSecurityRole(Authorization.VIEW, communicationAdministrators, rockContext);
                        communicationTemplate.AllowSecurityRole(Authorization.EDIT, communicationAdministrators, rockContext);
                        communicationTemplate.AllowSecurityRole(Authorization.ADMINISTRATE, communicationAdministrators, rockContext);
                    }

                    var rockAdministrators = groupService.Get(Rock.SystemGuid.Group.GROUP_ADMINISTRATORS.AsGuid());
                    if (rockAdministrators != null)
                    {
                        communicationTemplate.AllowSecurityRole(Authorization.VIEW, rockAdministrators, rockContext);
                        communicationTemplate.AllowSecurityRole(Authorization.EDIT, rockAdministrators, rockContext);
                        communicationTemplate.AllowSecurityRole(Authorization.ADMINISTRATE, rockAdministrators, rockContext);
                    }
                }
            }

            NavigateToParentPage();
        }
示例#2
0
        /// <summary>
        /// Handles the Click event of the btnSave 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 btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                var rockContext = new RockContext();

                var communicationTemplateService           = new CommunicationTemplateService(rockContext);
                var communicationTemplateAttachmentService = new CommunicationTemplateAttachmentService(rockContext);

                CommunicationTemplate communicationTemplate = null;
                int?communicationTemplateId = hfCommunicationTemplateId.Value.AsIntegerOrNull();
                if (communicationTemplateId.HasValue)
                {
                    communicationTemplate = communicationTemplateService.Get(communicationTemplateId.Value);
                }

                bool newTemplate = false;
                if (communicationTemplate == null)
                {
                    newTemplate           = true;
                    communicationTemplate = new Rock.Model.CommunicationTemplate();
                    communicationTemplateService.Add(communicationTemplate);
                }

                communicationTemplate.Name             = tbName.Text;
                communicationTemplate.IsActive         = cbIsActive.Checked;
                communicationTemplate.Description      = tbDescription.Text;
                communicationTemplate.ImageFileId      = imgTemplatePreview.BinaryFileId;
                communicationTemplate.LogoBinaryFileId = imgTemplateLogo.BinaryFileId;

                communicationTemplate.FromName           = tbFromName.Text;
                communicationTemplate.FromEmail          = tbFromAddress.Text;
                communicationTemplate.ReplyToEmail       = tbReplyToAddress.Text;
                communicationTemplate.CCEmails           = tbCCList.Text;
                communicationTemplate.BCCEmails          = tbBCCList.Text;
                communicationTemplate.LavaFields         = kvlMergeFields.Value.AsDictionaryOrNull();
                communicationTemplate.CssInliningEnabled = cbCssInliningEnabled.Checked;

                var binaryFileIds = hfAttachedBinaryFileIds.Value.SplitDelimitedValues().AsIntegerList();

                // delete any attachments that are no longer included
                foreach (var attachment in communicationTemplate.Attachments.Where(a => !binaryFileIds.Contains(a.BinaryFileId)).ToList())
                {
                    communicationTemplate.Attachments.Remove(attachment);
                    communicationTemplateAttachmentService.Delete(attachment);
                }

                // add any new attachments that were added
                foreach (var attachmentBinaryFileId in binaryFileIds.Where(a => !communicationTemplate.Attachments.Any(x => x.BinaryFileId == a)))
                {
                    communicationTemplate.Attachments.Add(new CommunicationTemplateAttachment {
                        BinaryFileId = attachmentBinaryFileId
                    });
                }

                communicationTemplate.Subject = tbEmailSubject.Text;
                communicationTemplate.Message = ceEmailTemplate.Text;

                communicationTemplate.SMSFromDefinedValueId = ddlSMSFrom.SelectedValue.AsIntegerOrNull();
                communicationTemplate.SMSMessage            = tbSMSTextMessage.Text;

                communicationTemplate.CategoryId = cpCategory.SelectedValueAsInt();

                if (communicationTemplate != null)
                {
                    rockContext.SaveChanges();
                    NavigateToParentPage();
                }

                if (newTemplate && !IsUserAuthorized(Authorization.EDIT))
                {
                    communicationTemplate.MakePrivate(Authorization.VIEW, CurrentPerson);
                    communicationTemplate.MakePrivate(Authorization.EDIT, CurrentPerson);
                }
            }
        }