jxycms
2/9/2017 - 1:01 AM

customize email notification email address in code according to form field values this is cs file in bizform web part

customize email notification email address in code according to form field values this is cs file in bizform web part

using System;

using CMS.Helpers;
using CMS.Localization;
using CMS.PortalControls;
using CMS.SiteProvider;
using CMS.WebAnalytics;
using CMS.OnlineForms;
using CMS.DataEngine;
using DocumentFormat.OpenXml.Vml;
using System.Data;
using CMS.CustomTables;

public partial class CMSWebParts_BizForms_bizform : CMSAbstractWebPart
{
    #region "Properties"

    /// <summary>
    /// Gets or sets the form name of BizForm.
    /// </summary>
    public string BizFormName
    {
        get
        {
            return ValidationHelper.GetString(GetValue("BizFormName"), "");
        }
        set
        {
            SetValue("BizFormName", value);
        }
    }


    /// <summary>
    /// Gets or sets the alternative form full name (ClassName.AlternativeFormName).
    /// </summary>
    public string AlternativeFormName
    {
        get
        {
            return ValidationHelper.GetString(GetValue("AlternativeFormName"), "");
        }
        set
        {
            SetValue("AlternativeFormName", value);
        }
    }


    /// <summary>
    /// Gets or sets the site name.
    /// </summary>
    public string SiteName
    {
        get
        {
            return ValidationHelper.GetString(GetValue("SiteName"), "");
        }
        set
        {
            SetValue("SiteName", value);
        }
    }


    /// <summary>
    /// Gets or sets the value that indicates whether the WebPart use colon behind label.
    /// </summary>
    public bool UseColonBehindLabel
    {
        get
        {
            return ValidationHelper.GetBoolean(GetValue("UseColonBehindLabel"), true);
        }
        set
        {
            SetValue("UseColonBehindLabel", value);
        }
    }


    /// <summary>
    /// Gets or sets the message which is displayed after validation failed.
    /// </summary>
    public string ValidationErrorMessage
    {
        get
        {
            return ValidationHelper.GetString(GetValue("ValidationErrorMessage"), "");
        }
        set
        {
            SetValue("ValidationErrorMessage", value);
        }
    }


    /// <summary>
    /// Gets or sets the conversion track name used after successful registration.
    /// </summary>
    public string TrackConversionName
    {
        get
        {
            return ValidationHelper.GetString(GetValue("TrackConversionName"), "");
        }
        set
        {
            if (value.Length > 400)
            {
                value = value.Substring(0, 400);
            }
            SetValue("TrackConversionName", value);
        }
    }


    /// <summary>
    /// Gets or sets the conversion value used after successful registration.
    /// </summary>
    public double ConversionValue
    {
        get
        {
            return ValidationHelper.GetDoubleSystem(GetValue("ConversionValue"), 0);
        }
        set
        {
            SetValue("ConversionValue", value);
        }
    }

    #endregion


    #region "Methods"

    protected override void OnLoad(EventArgs e)
    {
        viewBiz.OnAfterSave += viewBiz_OnAfterSave;
        base.OnLoad(e);
    }


    /// <summary>
    /// Content loaded event handler.
    /// </summary>
    public override void OnContentLoaded()
    {
        base.OnContentLoaded();
        SetupControl();        
    }


    /// <summary>
    /// Reloads data for partial caching.
    /// </summary>
    public override void ReloadData()
    {
        base.ReloadData();
        SetupControl();
    }


    /// <summary>
    /// Initializes the control properties.
    /// </summary>
    protected void SetupControl()
    {
        if (StopProcessing)
        {
            // Do nothing
            viewBiz.StopProcessing = true;
        }
        else
        {
            // Set BizForm properties
            viewBiz.FormName = BizFormName;
            viewBiz.SiteName = SiteName;
            viewBiz.UseColonBehindLabel = UseColonBehindLabel;
            viewBiz.AlternativeFormFullName = AlternativeFormName;
            viewBiz.ValidationErrorMessage = ValidationErrorMessage;           

            // Set the live site context
            if (viewBiz != null)
            {
                viewBiz.ControlContext.ContextName = CMS.ExtendedControls.ControlContext.LIVE_SITE;
            }
        }
    }


    private void viewBiz_OnAfterSave(object sender, EventArgs e)
    {
        // Check current Form name
        if (this.viewBiz.FormName == "ContactUs")
        {
            // Get BizFormInfo object
            BizFormInfo bi = BizFormInfoProvider.GetBizFormInfo(this.BizFormName, SiteContext.CurrentSiteID);

            // Get selected value if a custom field
            var interest = this.viewBiz.GetDataValue("EnquiryType").ToString();
            if (interest == "1") {
                interest = "More information about HSGA and our providers";
            }
            var stateNum = this.viewBiz.GetDataValue("State").ToString();
            string state = "";
            string recipientEmailAddress = "";
            if (stateNum != "")
            {
                switch (stateNum)
                {
                    case "1": state = "New South Wales"; break;
                    case "2": state = "Victoria"; break;
                    case "3": state = "Queensland"; break;
                    case "4": state = "South Australia"; break;
                    case "5": state = "Western Australia"; break;
                    case "6": state = "Northern Territory"; break;
                    case "7": state = "Tasmania"; break;
                    case "8": state = "International"; break;
                    default: break;
                }
            }

            DataSet emailDatabase = CustomTableItemProvider.GetItems("customtable.StateInterestEmailAddress")
                                .Column("email")
                                .WhereEquals("state", state)
                                .WhereEquals("Interest", interest)
                                .Result;
            recipientEmailAddress = emailDatabase.Tables[0].Rows[0]["email"].ToString();
            bi.FormSendToEmail = recipientEmailAddress;
        }
    }

    #endregion
}