using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.CMSHelper; using CMS.FormControls; using CMS.GlobalHelper; using CMS.SiteProvider; using CMS.UIControls; using CMS.WebAnalytics; using CMS.LicenseProvider; using CMS.SettingsProvider; public partial class CMSModules_WebAnalytics_FormControls_SelectCampaign : FormEngineUserControl { #region "Variables" private bool mPostbackOnChange = false; private int mSiteID = CMSContext.CurrentSiteID; private bool mCreateOnUnknownName = true; #endregion #region "Properties" /// /// Gets or sets the value of campaign. /// public override object Value { get { return usCampaign.Value; } set { usCampaign.Value = value; } } /// /// Return uniselector control. /// public UniSelector UniSelector { get { return usCampaign; } } /// /// Gets or sets site ID. /// public int SiteID { get { return mSiteID; } set { mSiteID = value; } } /// /// If true, full postback is raised when item changed. /// public bool PostbackOnChange { get { return mPostbackOnChange; } set { mPostbackOnChange = value; } } /// /// Selection mode of control (dropdown,multiselect...). /// public SelectionModeEnum SelectionMode { get { return usCampaign.SelectionMode; } set { usCampaign.SelectionMode = value; } } /// /// If true, allow all is enabled. /// public bool AllowAll { get { return usCampaign.AllowAll; } set { usCampaign.AllowAll = value; } } /// /// Gets or sets AllowEmpty value of uniselector. /// public bool AllowEmpty { get { return usCampaign.AllowEmpty; } set { usCampaign.AllowEmpty = value; } } /// /// Value for all record item. /// public string AllRecordValue { get { return usCampaign.AllRecordValue; } } /// /// Gets or sets value for (none) record. /// public string NoneRecordValue { set { usCampaign.NoneRecordValue = value; } get { return usCampaign.NoneRecordValue; } } /// /// Indicates whether selector should try to create a new row if unknown selected or not. /// public bool CreateOnUnknownName { get { return mCreateOnUnknownName; } set { mCreateOnUnknownName = value; } } #endregion protected void Page_Load(object sender, EventArgs e) { usCampaign.IsLiveSite = IsLiveSite; usCampaign.AllowEditTextBox = true; usCampaign.TextBoxSelect.MaxLength = 100; if ((SelectionMode == SelectionModeEnum.SingleTextBox) && CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.WebAnalytics", "managecampaigns")) { usCampaign.EditDialogWindowWidth = 800; usCampaign.EditDialogWindowHeight = 660; string url = "~/CMSModules/WebAnalytics/Pages/Tools/Campaign/Tab_General.aspx?campaignName=##ITEMID##&modaldialog=true"; usCampaign.EditItemPageUrl = url; url = "~/CMSModules/WebAnalytics/Pages/Tools/Campaign/Tab_General.aspx?modaldialog=true"; usCampaign.NewItemPageUrl = url; } if (PostbackOnChange) { usCampaign.DropDownSingleSelect.AutoPostBack = true; ScriptManager scr = ScriptManager.GetCurrent(Page); scr.RegisterPostBackControl(usCampaign); } usCampaign.WhereCondition = "CampaignSiteID = " + SiteID; if (!RequestHelper.IsPostBack()) { ReloadData(); } } /// /// Reloads uniselector. /// public void ReloadData() { usCampaign.WhereCondition = "CampaignSiteID = " + SiteID; usCampaign.Reload(true); } /// /// Test if campaign is valid. /// public override bool IsValid() { String value = ValidationHelper.GetString(usCampaign.Value, String.Empty).Trim(); if (value != String.Empty) { String domain = URLHelper.GetCurrentDomain(); if (DataHelper.GetNotEmpty(domain, "") != "") { string parsedDomain = LicenseKeyInfoProvider.ParseDomainName(domain); if (!LicenseKeyInfoProvider.IsFeatureAvailable(parsedDomain, FeatureEnum.CampaignAndConversions)) { ValidationError = GetString("campaignselector.nolicence"); return false; } } if (!ValidationHelper.IsCodeName(value)) { ValidationError = GetString("campaign.validcodename"); return false; } // If campaign object not found CampaignInfo ci = CampaignInfoProvider.GetCampaignInfo(value, SiteInfoProvider.GetSiteName(SiteID)); // Handle info not found if (ci == null) { if (!mCreateOnUnknownName) { // Selector is not set to create new campaigns so error out ValidationError = GetString("campaign.validcodename"); return false; } // Or check permissions .. if (!CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.WebAnalytics", "ManageCampaigns")) { ValidationError = GetString("campaign.notallowedcreate"); return false; } // .. and try to create a new one. ci = new CampaignInfo(); ci.CampaignName = value; ci.CampaignDisplayName = value; ci.CampaignEnabled = true; ci.CampaignSiteID = SiteID; CampaignInfoProvider.SetCampaignInfo(ci); } } return true; } }