using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.FormControls; using CMS.ExtendedControls; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.CMSHelper; public partial class CMSAdminControls_Basic_ThreeStateCheckBox : FormEngineUserControl { #region "Constants" public const string DEFAULT_KEY = "##DEFAULT##"; #endregion #region "Variables" private object mPositiveValue = 1; private object mNegativeValue = 0; private object mNotSetValue = -1; private object mDefaultValue = null; #endregion #region "Properties" /// /// Gets control representing three-state positive choice /// public LocalizedRadioButton PositiveChoice { get { return rbPositive; } } /// /// Gets control representing three-state negative choice /// public LocalizedRadioButton NegativeChoice { get { return rbNegative; } } /// /// Gets control representing three-state not set choice /// public LocalizedRadioButton NotSetChoice { get { return rbNotSet; } } /// /// Indicates if postback should be performed /// public bool AutoPostBack { get { return PositiveChoice.AutoPostBack; } set { PositiveChoice.AutoPostBack = value; NegativeChoice.AutoPostBack = value; NotSetChoice.AutoPostBack = value; } } /// /// Gets or sets value representing three-state not set choice /// public object NotSetValue { get { return mNotSetValue; } set { mNotSetValue = value; } } /// /// Gets or sets value representing three-state positive choice /// public object PositiveValue { get { return mPositiveValue; } set { mPositiveValue = value; } } /// /// Gets or sets value representing three-state negative choice /// public object NegativeValue { get { return mNegativeValue; } set { mNegativeValue = value; } } /// /// Gets or sets value of the control /// public override object Value { get { if (PositiveChoice.Checked) { return PositiveValue; } else if (NegativeChoice.Checked) { return NegativeValue; } else { return NotSetValue; } } set { if (value != null) { int hash = value.GetHashCode(); if (hash == PositiveValue.GetHashCode()) { PositiveChoice.Checked = true; NegativeChoice.Checked = false; NotSetChoice.Checked = false; } else if (hash == NegativeValue.GetHashCode()) { NegativeChoice.Checked = true; PositiveChoice.Checked = false; NotSetChoice.Checked = false; } else { NotSetChoice.Checked = true; NegativeChoice.Checked = false; PositiveChoice.Checked = false; } } } } /// /// Setting key code name used to initialize not set choice value /// public string SettingKeyName { get { return ValidationHelper.GetString(GetValue("SettingKeyName"), null); } } /// /// Gets or sets if control is enabled /// public override bool Enabled { get { base.Enabled = PositiveChoice.Enabled && NegativeChoice.Enabled && NotSetChoice.Enabled; return base.Enabled; } set { base.Enabled = value; PositiveChoice.Enabled = NegativeChoice.Enabled = NotSetChoice.Enabled = value; } } #endregion #region "Events" /// /// Event for control state change /// public event EventHandler CheckedChanged; #endregion #region "Control events" /// /// Page init event /// protected override void OnInit(EventArgs e) { if (!RequestHelper.IsPostBack()) { if (!NotSetChoice.Checked && !NegativeChoice.Checked && !PositiveChoice.Checked) { NotSetChoice.Checked = true; } } NotSetChoice.Text = GetString("general.sitesettings") + (string.IsNullOrEmpty(SettingKeyName) ? null : " (" + DEFAULT_KEY + ")"); PositiveChoice.Text = GetString("general.yes"); NegativeChoice.Text = GetString("general.no"); if (!string.IsNullOrEmpty(SettingKeyName)) { SetDefaultValue(SettingsKeyProvider.GetBoolValue(CMSContext.CurrentSiteName + "." + SettingKeyName)); } base.OnInit(e); } /// /// Page PreRender event /// protected override void OnLoad(EventArgs e) { base.OnLoad(e); NotSetChoice.CheckedChanged += RaiseCheckedChanged; PositiveChoice.CheckedChanged += RaiseCheckedChanged; NegativeChoice.CheckedChanged += RaiseCheckedChanged; } protected void RaiseCheckedChanged(object sender, EventArgs e) { if (CheckedChanged != null) { CheckedChanged(sender, e); } } #endregion #region "Public methods" /// /// Gets actual value considering actual default value /// public object GetActualValue() { object val = Value; if (val == NotSetValue) { return mDefaultValue; } return val; } /// /// Replace text ##DEFAULT## with default value in text description of not set RadioButton control /// /// Default value public void SetDefaultValue(object defaultValue) { mDefaultValue = defaultValue; string replaceString = null; int defaultHash = (defaultValue != null) ? defaultValue.GetHashCode() : 0; int positiveHash = (PositiveValue != null) ? PositiveValue.GetHashCode() : 0; int negativeHash = (NegativeValue != null) ? NegativeValue.GetHashCode() : 0; if (defaultHash == positiveHash) { replaceString = PositiveChoice.Text; } else if (defaultHash == negativeHash) { replaceString = NegativeChoice.Text; } NotSetChoice.Text = NotSetChoice.Text.Replace(DEFAULT_KEY, replaceString); } /// /// Set specified ForumInfo object property according to specified value /// /// Forum object to update with new value /// Property to be set public void SetThreeStateValue(BaseInfo infoObj, string propertyName) { int value = ValidationHelper.GetInteger(Value, -1); if (value == -1) { infoObj.SetValue(propertyName, null); } else { infoObj.SetValue(propertyName, ValidationHelper.GetBoolean(value, false)); } } /// /// Get ForumInfo object value to initialize ThreeStateControl object /// /// ForumInfo object used to initialize ThreeStateControl /// Object property used for initialization /// Integer value used to initialize ThreeStateControl public void InitFromThreeStateValue(BaseInfo infoObj, string propertyName) { object dbValue = infoObj.GetValue(propertyName); Value = (dbValue == null) ? -1 : ValidationHelper.GetBoolean(dbValue, true) ? 1 : 0; } #endregion }