Class FormSaveProvider
- Namespace
- Dynamicweb.DataManagement
- Assembly
- Dynamicweb.DataManagement.dll
Inherit this type and override the Save method to create your own form save handler.
public class FormSaveProvider : ConfigurableAddIn
- Inheritance
-
FormSaveProvider
- Derived
- Inherited Members
Examples
using System.Collections.Generic;
using System.Data;
using System.Net.Mail;
using System.Text;
using Dynamicweb.Configuration;
using Dynamicweb.Core;
using Dynamicweb.Core.Helpers;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;
using Dynamicweb.Mailing;
using Dynamicweb.Rendering;
namespace Dynamicweb.DataManagement
{
/// <summary>
/// Send the form over mail
/// </summary>
[
AddInName("DM.Form.SendMail"),
AddInLabel("Send the form over mail"),
AddInDescription("This will send an email with all form elements.")
]
public class MailFormSaveProvider : FormSaveProvider, IParameterOptions
{
private Encoding _mailEncoding = Encoding.UTF8;
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.SenderName"),
AddInLabel("Sender name"),
AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true")
]
public string MailFromName { get; set; }
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.UseFormFieldForSenderName"),
AddInLabel("Get from form"),
AddInParameterEditor(typeof(YesNoParameterEditor), "jsOnClick=ShowFieldSelector('MailFormSaveProvider.SenderName', this)")
]
public bool MailFromNameUseField { get; set; }
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.SenderEmail"),
AddInLabel("Sender e-mail"),
AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true")
]
public string MailFrom { get; set; }
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.UseFormFieldForSenderEmail"),
AddInLabel("Get from form"),
AddInParameterEditor(typeof(YesNoParameterEditor), "jsOnClick=ShowFieldSelector('MailFormSaveProvider.SenderEmail', this)")
]
public bool MailFromUseField { get; set; }
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.RecipientEmail"),
AddInLabel("Recipient e-mail"),
AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true")
]
public string MailTo { get; set; }
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.UseFormFieldForRecipient"),
AddInLabel("Get from form"),
AddInParameterEditor(typeof(YesNoParameterEditor), "jsOnClick=ShowFieldSelector('MailFormSaveProvider.RecipientEmail', this)")
]
public bool MailToUseField { get; set; }
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.RecipientCCEmail"),
AddInLabel("Recipient CC e-mail"),
AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true")
]
public string MailCC { get; set; }
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.RecipientBCCEmail"),
AddInLabel("Recipient BCC e-mail"),
AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true")
]
public string MailBCC { get; set; }
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.Subject"),
AddInLabel("Subject"),
AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true")
]
public string MailSubject { get; set; }
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.Template"),
AddInLabel("Template"),
AddInParameterEditor(typeof(TemplateParameterEditor), "folder=/Templates/DataManagement/Forms/Email;FullPath=false;NewGUI=true")
]
public string MailTemplate { get; set; }
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.Encoding"),
AddInLabel("Encoding"),
AddInParameterEditor(typeof(DropDownParameterEditor), "NewGUI=true;SortBy=Value")
]
public int MailEncodingCodePage
{
get
{
return _mailEncoding.CodePage;
}
set
{
Encoding encoding = Encoding.GetEncoding(value);
_mailEncoding = encoding;
}
}
[
AddInParameterGroup("Mail configuration"),
AddInParameter("MailFormSaveProvider.UseLogging"),
AddInLabel("Logging"),
AddInParameterEditor(typeof(YesNoParameterEditor), "")
]
public bool MailLogging { get; set; }
IEnumerable<ParameterOption> IParameterOptions.GetParameterOptions(string dropdownName)
{
var check = new HashSet<int>();
var ret = new List<ParameterOption>();
EncodingInfo[] encArray = Encoding.GetEncodings();
foreach (EncodingInfo encoding in encArray)
{
if (check.Add(encoding.CodePage))
{
ret.Add(new(encoding.DisplayName, encoding.CodePage));
}
}
return ret;
}
/// <summary>
/// Save form settings values
/// </summary>
/// <param name="formSettings">Form settings</param>
/// <param name="keysAndValues">settings names and values</param>
/// <returns>True if saved successFully</returns>
public override bool Save(FormSetting formSettings, Dictionary<string, object> keysAndValues)
{
StringBuilder stringBuilder = new StringBuilder();
bool isSaved = false;
string dwSmtpServer = string.Empty;
string mailFromEmail = string.Empty;
string mailFromNameValue = string.Empty;
string mailToEmail = string.Empty;
string mailTemplateContext = string.Empty;
try
{
Template template = new Template("DataManagement/Forms/Email/" + MailTemplate);
FormFieldCollection fields = formSettings.Fields;
FormFieldCollection temporaryFields = new FormFieldCollection();
temporaryFields.AddRange(fields);
foreach (FormField field in temporaryFields)
{
if (keysAndValues.ContainsKey(field.SystemName))
{
field.Value = Converter.ToString(keysAndValues[field.SystemName]);
}
else
{
formSettings.Fields.Remove(field);
}
}
FormRenderer render = new FormRenderer();
mailTemplateContext = render.RenderForm(formSettings, template);
dwSmtpServer = Converter.ToString(SystemConfiguration.Instance.GetValue("/Globalsettings/System/MailServer/Server"));
mailFromNameValue = MailFromName;
mailFromEmail = string.IsNullOrEmpty(MailFrom) ? Converter.ToString(SystemConfiguration.Instance.GetValue("/Globalsettings/Settings/CommonInformation/Email")) : MailFrom;
if (MailFromUseField || MailFromNameUseField || MailToUseField)
{
foreach (string key in keysAndValues.Keys)
{
if (key == MailFrom)
{
mailFromEmail = Converter.ToString(keysAndValues[key]);
}
if (MailFromNameUseField && key == MailFromName)
{
mailFromNameValue = Converter.ToString(keysAndValues[key]);
}
if (MailToUseField && key == MailTo)
{
MailTo = Converter.ToString(keysAndValues[key]);
}
}
}
if (string.IsNullOrEmpty(MailFrom))
{
MailFrom = EmailHandler.SystemMailFromAddress();
}
using MailMessage mailMessage = new MailMessage();
if (_mailEncoding != null)
{
mailMessage.BodyEncoding = _mailEncoding;
}
else
{
mailMessage.BodyEncoding = Encoding.UTF8;
}
if (!string.IsNullOrEmpty(MailTo))
{
if (MailTo.Contains(";"))
{
string[] MailToArray = MailTo.Split(';');
foreach (string address in MailToArray)
{
string addr = address.Trim();
if (StringHelper.IsValidEmailAddress(addr))
{
mailMessage.To.Add(addr);
}
}
}
else
{
if (StringHelper.IsValidEmailAddress(MailTo))
{
mailMessage.To.Add(MailTo);
}
}
}
if (!string.IsNullOrEmpty(MailCC))
{
if (MailCC.Contains(";"))
{
string[] mailCCArray = MailCC.Split(';');
foreach (string address in mailCCArray)
{
string addr = address.Trim();
if (StringHelper.IsValidEmailAddress(addr))
{
mailMessage.CC.Add(addr);
}
}
}
else
{
if (StringHelper.IsValidEmailAddress(MailCC))
{
mailMessage.CC.Add(MailCC);
}
}
}
if (!string.IsNullOrEmpty(MailBCC))
{
if (MailBCC.Contains(";"))
{
string[] mailBCCArray = MailBCC.Split(';');
foreach (string address in mailBCCArray)
{
string addr = address.Trim();
if (StringHelper.IsValidEmailAddress(addr))
{
mailMessage.Bcc.Add(addr);
}
}
}
else
{
if (StringHelper.IsValidEmailAddress(MailBCC))
{
mailMessage.Bcc.Add(MailBCC);
}
}
}
if (string.IsNullOrEmpty(mailFromEmail) || !StringHelper.IsValidEmailAddress(mailFromEmail))
{
mailFromEmail = MailTo;
if (string.IsNullOrEmpty(mailFromEmail) || !StringHelper.IsValidEmailAddress(mailFromEmail))
{
mailFromEmail = EmailHandler.SystemMailFromAddress();
}
}
if (string.IsNullOrEmpty(mailFromNameValue))
{
mailFromNameValue = mailFromEmail;
}
if (!string.IsNullOrEmpty(mailFromNameValue))
{
mailMessage.Sender = new MailAddress(mailFromEmail, mailFromNameValue);
}
else
{
mailMessage.Sender = new MailAddress(mailFromEmail);
}
mailMessage.From = mailMessage.Sender;
mailMessage.Subject = MailSubject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = mailTemplateContext;
isSaved = EmailHandler.Send(mailMessage, MailLogging);
}
catch (DataException)
{
isSaved = false;
}
return isSaved;
}
}
}
Methods
Save(FormSetting, Dictionary<string, object>)
Saves the specified form.
public virtual bool Save(FormSetting Form, Dictionary<string, object> KeysAndValues)
Parameters
Form
FormSettingThe form.
KeysAndValues
Dictionary<string, object>The keys and values.
Returns
Save(FormSetting, Dictionary<string, object>, string)
Saves the specified form.
public virtual bool Save(FormSetting Form, Dictionary<string, object> KeysAndValues, string Settings)
Parameters
Form
FormSettingThe form.
KeysAndValues
Dictionary<string, object>The keys and values.
Settings
stringThe settings.