/// <summary> /// Handles the Copy event of the gCommunicationTemplates control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param> protected void gCommunicationTemplates_Copy(object sender, RowEventArgs e) { var rockContext = new RockContext(); var service = new CommunicationTemplateService(rockContext); var template = service.Get(e.RowKeyId); if (template != null) { var templateCopy = template.Clone(false); templateCopy.Id = 0; int copyNumber = 0; var copyName = "Copy of " + template.Name; while (service.Queryable().Any(a => a.Name == copyName)) { copyNumber++; copyName = string.Format("Copy({0}) of {1}", copyNumber, template.Name); } templateCopy.Name = copyName.Truncate(100); templateCopy.IsSystem = false; templateCopy.Guid = Guid.NewGuid(); service.Add(templateCopy); rockContext.SaveChanges(); } BindGrid(); }
/// <summary> /// Handles the Delete event of the gCommunicationTemplates control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs"/> instance containing the event data.</param> protected void gCommunicationTemplates_Delete(object sender, RowEventArgs e) { var rockContext = new RockContext(); var service = new CommunicationTemplateService(rockContext); var template = service.Get(e.RowKeyId); if (template != null) { if (!template.IsAuthorized(Authorization.EDIT, CurrentPerson)) { maGridWarning.Show("You are not authorized to delete this template", ModalAlertType.Information); return; } string errorMessage; if (!service.CanDelete(template, out errorMessage)) { maGridWarning.Show(errorMessage, ModalAlertType.Information); return; } service.Delete(template); rockContext.SaveChanges(); } BindGrid(); }
/// <summary> /// Handles the Delete event of the gCommunication control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs"/> instance containing the event data.</param> protected void gCommunication_Delete( object sender, Rock.Web.UI.Controls.RowEventArgs e ) { var rockContext = new RockContext(); var service = new CommunicationTemplateService( rockContext ); var template = service.Get( e.RowKeyId ); if ( template != null ) { if ( !template.IsAuthorized( Authorization.EDIT, this.CurrentPerson ) ) { maGridWarning.Show( "You are not authorized to delete this template", ModalAlertType.Information ); return; } string errorMessage; if ( !service.CanDelete( template, out errorMessage ) ) { maGridWarning.Show( errorMessage, ModalAlertType.Information ); return; } service.Delete( template ); rockContext.SaveChanges(); } BindGrid(); }
/// <summary> /// Handles the Copy event of the gCommunicationTemplates control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param> protected void gCommunicationTemplates_Copy(object sender, RowEventArgs e) { var rockContext = new RockContext(); var service = new CommunicationTemplateService(rockContext); var template = service.Get(e.RowKeyId); if (template != null) { if (!template.IsAuthorized(Authorization.EDIT, this.CurrentPerson)) { maGridWarning.Show("You are not authorized to copy this template", ModalAlertType.Information); return; } var templateCopy = template.Clone(false); templateCopy.Id = 0; int copyNumber = 0; var copyName = "Copy of " + template.Name; while (service.Queryable().Where(a => a.Name == copyName).Any()) { copyNumber++; copyName = string.Format("Copy({0}) of {1}", copyNumber, template.Name); } template.Name = copyName.Truncate(100); templateCopy.Guid = Guid.NewGuid(); service.Add(templateCopy); rockContext.SaveChanges(); } BindGrid(); }
/// <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 service = new CommunicationTemplateService(rockContext); Rock.Model.CommunicationTemplate template = null; if (CommunicationTemplateId.HasValue) { template = service.Get(CommunicationTemplateId.Value); } bool newTemplate = false; if (template == null) { newTemplate = true; template = new Rock.Model.CommunicationTemplate(); service.Add(template); } template.Name = tbName.Text; template.Description = tbDescription.Text; template.MediumEntityTypeId = MediumEntityTypeId; template.MediumData.Clear(); GetMediumData(); foreach (var keyVal in MediumData) { if (!string.IsNullOrEmpty(keyVal.Value)) { template.MediumData.Add(keyVal.Key, keyVal.Value); } } if (template.MediumData.ContainsKey("Subject")) { template.Subject = template.MediumData["Subject"]; template.MediumData.Remove("Subject"); } else { template.Subject = string.Empty; } if (template != null) { rockContext.SaveChanges(); NavigateToParentPage(); } if (newTemplate && !_canEdit) { template.MakePrivate(Authorization.VIEW, CurrentPerson); template.MakePrivate(Authorization.EDIT, CurrentPerson); } } }
/// <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 service = new CommunicationTemplateService( rockContext ); Rock.Model.CommunicationTemplate template = null; if ( CommunicationTemplateId.HasValue ) { template = service.Get( CommunicationTemplateId.Value ); } bool newTemplate = false; if ( template == null ) { newTemplate = true; template = new Rock.Model.CommunicationTemplate(); service.Add( template ); } template.Name = tbName.Text; template.Description = tbDescription.Text; template.ChannelEntityTypeId = ChannelEntityTypeId; template.ChannelData.Clear(); GetChannelData(); foreach(var keyVal in ChannelData) { if (!string.IsNullOrEmpty(keyVal.Value)) { template.ChannelData.Add(keyVal.Key, keyVal.Value); } } if ( template.ChannelData.ContainsKey( "Subject" ) ) { template.Subject = template.ChannelData["Subject"]; template.ChannelData.Remove( "Subject" ); } if ( template != null ) { rockContext.SaveChanges(); NavigateToParentPage(); } if ( newTemplate && !_canEdit ) { template.MakePrivate( Authorization.VIEW, CurrentPerson ); template.MakePrivate( Authorization.EDIT, CurrentPerson ); } } }
/// <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(); }
/// <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> /// 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); } } }