Okay....so i've created a custom form control called FormToEmail. The class file is:
FormToEmail.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Sitefinity.Modules.Forms.Web.UI;
using Telerik.Sitefinity.Modules.Forms.Web.UI.Fields;
using Telerik.Web.UI;
using Telerik.Sitefinity.Modules.Forms;
using Telerik.Sitefinity.Forms.Model;
using System.Net.Mail;
using System.ComponentModel;
namespace CustomFormControl
{
public class FormToEmail : FormsControl
{
private const string _layoutTemplateName = "CustomFormControl.Resources.Views.FormToEmailControl.ascx";
//private string _emailTo = "Enter the email addresses form submissions should be sent to";
private string _emailTo;
private string _emailFrom;
private string _emailCC = string.Empty;
[Category("Email Submission Details")]
/// <summary>
/// one or more email addresses the form submissions should go to.
/// If multiple email addresses, separate each one with a comma
/// </summary>
public String ToAddresses
{
get
{
if (!String.IsNullOrEmpty(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("FormSubmitToEmail")))
{
_emailTo = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("FormSubmitToEmail");
}
else
{
_emailTo = "myemail@my.com";
}
return _emailTo;
}
set { _emailTo = value; }
}
[Category("Email Submission Details")]
/// <summary>
/// Who the email should show as being from. By default, this value is eisu@da.ks.gov
/// Please note that this value should be a valid email address.
/// </summary>
public String FromAddress
{
get
{
if (!String.IsNullOrEmpty(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("FormSubmitFromEmail")))
{
_emailFrom = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("FormSubmitFromEmail");
}
else
{
_emailFrom ="myemail@my.com"";
}
return _emailFrom;
}
set { _emailFrom = value; }
}
[Category("Email Submission Details")]
/// <summary>
/// Who the email should be cc'ed to.
/// If multiple email addresses, separate each one with a comma. This can be left blank.
/// </summary>
public String CCAddresses
{
get { return _emailCC; }
set { _emailCC = value; }
}
protected override string LayoutTemplateName
{
get
{
return _layoutTemplateName;
}
}
protected override void InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container)
{
base.InitializeControls(container);
}
protected override void ConfigureSubmitButton(System.Web.UI.Control control, string validationGroup)
{
var submit = control as FormSubmitButton;
submit.Click += new EventHandler(submit_Click);
base.ConfigureSubmitButton(control, validationGroup);
}
void submit_Click(object sender, EventArgs e)
{
Guid formId = this.FormId;
FormsManager formMgr = new FormsManager();
FormDescription formDescr = formMgr.GetForm(formId);
String msgBody = string.Empty;
foreach (var fld in this.FieldControls)
{
//fld.GetType().
if (fld is FormTextBox)
{
msgBody += String.Format("<p><strong>{0}:</strong> {1}</p>", ((FormTextBox)fld).Title, ((FormTextBox)fld).Value);
}
else if (fld is FormParagraphTextBox)
{
msgBody += String.Format("<p><strong>{0}:</strong> {1}</p>", ((FormParagraphTextBox)fld).Title, ((FormParagraphTextBox)fld).Value);
}
else if (fld is FormCheckboxes)
{
string choices = string.Empty;
foreach (string item in ((List<String>)((FormCheckboxes)fld).Value))
{
choices += String.Format("{0},", item);
}
msgBody += String.Format("<p><strong>{0}:</strong> {1}</p>", ((FormCheckboxes)fld).Title, choices);
}
else if (fld is FormChoiceField)
{
msgBody += String.Format("<p><strong>{0}:</strong> {1}</p>", ((FormChoiceField)fld).Title, ((FormChoiceField)fld).Value);
}
}
try
{
string subject = String.Format("{0} {1} - Form Submission", formDescr.ApplicationName.TrimEnd('/'), formDescr.Title);
SendEmailMsg(this.FromAddress, this.ToAddresses, this.CCAddresses, subject, msgBody);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Attempts to sends an email message. If success, returns true. Else returns false.
/// </summary>
/// <param name="fromAddr">Who the email is from</param>
/// <param name="toAddr">Who the email is to (comma separate if more than one)</param>
/// <param name="ccAddr">Who should be cc'ed (comma separate if more than one)</param>
/// <param name="subject">Subject line of the email</param>
/// <param name="msgBody">content of the email</param>
/// <returns></returns>
private bool SendEmailMsg(string fromAddr, string toAddr, string ccAddr, string subject, string msgBody)
{
SmtpClient mailServer = new SmtpClient();
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(fromAddr);
mailMsg.To.Add(toAddr);
if (!String.IsNullOrEmpty(ccAddr))
{
mailMsg.CC.Add(ccAddr);
}
mailMsg.IsBodyHtml = true;
mailMsg.Subject = subject;
mailMsg.Body = "<div style='font-family: verdana;font-size: .8em;'>" + msgBody + "</div>";
try
{
mailServer.Send(mailMsg);
}
catch (Exception)
{
return false;
}
return true;
}
}
}
NOTE: Right now I am pulling the from and to address from the web.config file
The template for this custom control is:
FormToEmailControl.ascx
The class file for the designer is:
FormToEmailDesigner.cs
namespace CustomFormControl
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Sitefinity.Web.UI;
using Telerik.Sitefinity.Web.UI.ControlDesign;
using Telerik.Sitefinity.Modules.Forms;
using Telerik.Sitefinity.Modules.Forms.Web.UI;
using Telerik.Sitefinity.Modules.Forms.Web.UI.Designers;
using Telerik.Sitefinity.Forms.Model;
using System.Web.UI;
using Telerik.Web.UI;
class FormToEmailDesigner : FormsControlDesigner
{
/// <summary>
/// Gets the name of the embedded layout template.
/// </summary>
/// <value></value>
/// <remarks>
/// Override this property to change the embedded template to be used with the dialog
/// </remarks>
protected override string LayoutTemplateName
{
get
{
return "CustomFormControl.Resources.Views.FormToEmailControlDesigner.ascx";
}
}
/// <summary>
/// Gets a reference to the form selector
/// </summary>
protected ContentSelector FormSelector
{
get
{
return Container.GetControl<ContentSelector>("selector", true);
}
}
/// <summary>
/// Gets a type from the resource assembly.
/// Resource assembly is an assembly that contains embedded resources such as templates, images, CSS files and etc.
/// By default this is Telerik.Sitefinity.Resources.dll.
/// </summary>
/// <value>The resources assembly info.</value>
protected override Type ResourcesAssemblyInfo
{
get
{
return this.GetType();
}
}
public override IEnumerable<ScriptReference> GetScriptReferences()
{
var res = new List<ScriptReference>(base.GetScriptReferences());
var assemblyName = this.GetType().Assembly.GetName().ToString();
res.Add(new ScriptReference("CustomFormControl.Resources.FormToEmailDesigner.js", assemblyName));
return res.ToArray();
}
/// <summary>
/// Gets a collection of script descriptors that represent ECMAScript (JavaScript) client components.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerable"/> collection of <see cref="T:System.Web.UI.ScriptDescriptor"/> objects.
/// </returns>
public override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
{
ScriptControlDescriptor desc = new ScriptControlDescriptor(this.GetType().FullName, this.ClientID);
desc.AddComponentProperty("formSelector", this.FormSelector.ClientID);
return new[] { desc };
}
/// <summary>
/// Initializes the controls.
/// </summary>
/// <param name="container">The control container.</param>
/// <remarks>
/// Initialize your controls in this method. Do not override CreateChildControls method.
/// </remarks>
protected override void InitializeControls(GenericContainer container)
{
this.DesignerMode = ControlDesignerModes.Simple;
//this.FormSelector.ServiceUrl = "~/Sitefinity/Services/Forms/FormsService.svc/";
//this.FormSelector.ItemType = typeof(Form).FullName;
}
}
}
The template file for the designer is:
FormToEmailControlDesigner.ascx
<%@ Control Language="C#" %>
<%@ Register Assembly="Telerik.Sitefinity" TagPrefix="designers" Namespace="Telerik.Sitefinity.Web.UI.ControlDesign" %>
<%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI"
TagPrefix="sitefinity" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<sitefinity:ResourceLinks ID="resourcesLinks" runat="server" UseEmbeddedThemes="True">
<sitefinity:ResourceFile AssemblyInfo="CustomFormControl.FormToEmail, CustomFormControl"
Name="CustomFormControl.Resources.Stylesheets.Designer.css" Static="true" />
</sitefinity:ResourceLinks>
<%--<telerik:RadWindowManager ID="windowManager" runat="server" Height="100%" Width="100%"
Behaviors="None" Skin="Sitefinity" ShowContentDuringLoad="false" VisibleStatusbar="false">
<windows>
<telerik:RadWindow ID="widgetEditorDialog" runat="server" Height="100" Width="100"
ReloadOnShow="true" Behaviors="Close" Modal="true" />
</windows>
</telerik:RadWindowManager>
--%>
<div id="selectorTag" style="display: none;" class="sfFlatDialogSelector">
<designers:ContentSelector ID="formSelector" runat="server" ItemType="Telerik.Sitefinity.Forms.Model.FormDescription"
ItemsFilter="Visible == true AND Status == Live" TitleText="Choose Form to Email"
BindOnLoad="false" AllowMultipleSelection="false" WorkMode="List" SearchBoxInnerText=""
SearchBoxTitleText="<%$Resources:Labels, NarrowByTyping %>" ServiceUrl="~/Sitefinity/Services/Forms/FormsService.svc"
ListModeClientTemplate="<strong class='sfItemTitle'>{{Title}}</strong>">
</designers:ContentSelector>
</div>
<div id="emailOptions">
<ul class="sfTargetList">
<li>
<label for="ToAddresses">
Email Address(es) to Send Form Submissions To</label>
<input type="text" id="ToAddresses" class="sfTxt" />
</li>
<li>
<label for="FromAddress">
Sender Email Address</label>
<input type="text" id="FromAddress" class="sfTxt" />
</li>
<li>
<label for="CCAddresses">
Email Address(es) to CC on Form Submissions</label>
<input type="text" id="CCAddresses" class="sfTxt" />
</li>
</ul>
</div>
And finally the javascript file for the designer is:
FormToEmailDesigner.js
Type.registerNamespace("CustomFormControl");
CustomFormControl.FormToEmailDesigner = function (element) {
CustomFormControl.FormToEmailDesigner.initializeBase(this, [element]);
this._parentDesigner = null;
this._formSelector = null;
}
CustomFormControl.FormToEmailDesigner.prototype =
{
/* --------------------------------- set up and tear down --------------------------------- */
initialize: function () {
CustomFormControl.FormToEmailDesigner.callBaseMethod(this, 'initialize');
this._toogleGroupSettingsDelegate = Function.createDelegate(this, function ()
{ dialogBase.resizeToContent(); });
},
dispose: function () {
CustomFormControl.FormToEmailDesigner.callBaseMethod(this, 'dispose');
},
/* --------------------------------- public methods --------------------------------- */
// implementation of IControlDesigner: Forces the control to refersh from the control Data
refreshUI: function () {
var data = this.get_controlData();
jQuery("#ToAddresses").val(data.ToAddresses);
jQuery("#FromAddress").val(data.FromAddress);
jQuery("#CCAddresses").val(data.CCAddresses);
},
// implementation of IControlDesigner: forces the designer view to apply the changes on UI to the control Data
applyChanges: function () {
var controlData = this.get_controlData();
controlData.ToAddresses = jQuery("#ToAddresses").val();
controlData.FromAddress = jQuery("#FromAddress").val();
controlData.CCAddresses = jQuery("#CCAddresses").val();
},
get_controlData: function () {
var parent = this.get_parentDesigner();
if (parent) {
var pe = parent.get_propertyEditor();
if (pe) { return pe.get_control(); }
}
alert('Control designer cannot find the control properties object!');
},
/* --------------------------------- event handlers --------------------------------- */
// this is an event handler for page selected event
// pageSelectedDelegate: function (items) {
// jQuery(this.get_element()).find('#selectorTag').hide();
// if (items == null) return;
// var selectedItems = this.get_pageSelector().getSelectedItems();
// if (selectedItems != null) {
// if (selectedItems.length > 0) {
// jQuery("#selectedPage").val(selectedItems[0].Title)
// jQuery("#selectedPageId").val(selectedItems[0].Id)
// }
// }
// dialogBase.resizeToContent();
// },
// _showPageSelector: function () {
// var filterItems = [];
// var filter = filterItems.join("OR");
// this.get_pageSelector().set_itemsFilter(filter);
// this.get_pageSelector().dataBind();
// jQuery(this.get_element()).find('#selectorTag').show();
// dialogBase.resizeToContent();
// },
// /* --------------------------------- private methods --------------------------------- */
/* --------------------------------- properties --------------------------------- */
// gets the reference to the parent designer control
get_parentDesigner: function () { return this._parentDesigner; },
// sets the reference fo the parent designer control
set_parentDesigner: function (value) { this._parentDesigner = value; },
// gets the reference to the propertyEditor control
get_propertyEditor: function () { return this._propertyEditor; },
// sets the reference fo the propertyEditor control
set_propertyEditor: function (value) { this._propertyEditor = value; },
// gets the reference to the page selector used to choose a page for showing the detail mode
get_formSelector: function () { return this._formSelector; },
// sets the reference to the page selector used to choose a page for showing the detail mode
set_formSelector: function (value) { this._formSelector = value; },
}
CustomFormControl.FormToEmailDesigner.registerClass('CustomFormControl.FormToEmailDesigner', Sys.UI.Control, Telerik.Sitefinity.Web.UI.ControlDesign.ControlDesignerBase);
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
With the above code, I still cannot see my public properties of ToAddresses, CCAddresses, FromAddress in the designer for the FormToEmail control. I'm sure I've got something wrong as, I confess, I understand some, but not all of the code and I have just modeled the above on things i've read in this forum. If you can give any guidance on where I have gone wrong, it would be much appreciated. This is much needed functionality that all our "sitefinity" clients are needing and I'm hoping I'm not too off the mark so I can get it to them. Thanks!!