using System; using CMS.FormControls; using CMS.GlobalHelper; public partial class CMSAdminControls_UI_Selectors_SelectValidity : FormEngineUserControl { #region "Backing fields" private string mValidForFieldName = "ValidFor"; private string mValidUntilFieldName = "ValidUntil"; private bool mAutoPostBack = false; #endregion #region "Properties - values" /// /// Gets or sets the validity period value. /// public override object Value { get { EnsureChildControls(); return DateTimeHelper.GetValidityString(Validity); } set { EnsureChildControls(); this.Validity = DateTimeHelper.GetValidityEnum(ValidationHelper.GetString(value, null)); } } /// /// Gets or sets the validity period value. /// public ValidityEnum Validity { get { if (radDays.Checked) { return ValidityEnum.Days; } else if (radWeeks.Checked) { return ValidityEnum.Weeks; } else if (radYears.Checked) { return ValidityEnum.Years; } else if (radMonths.Checked) { return ValidityEnum.Months; } else { return ValidityEnum.Until; } } set { radDays.Checked = (value == ValidityEnum.Days); radWeeks.Checked = (value == ValidityEnum.Weeks); radMonths.Checked = (value == ValidityEnum.Months); radYears.Checked = (value == ValidityEnum.Years); radUntil.Checked = (value == ValidityEnum.Until); DisableInactiveControl(); } } /// /// Gets or sets the "valid for" units. /// public int? ValidFor { get { if (string.IsNullOrEmpty(txtValidFor.Text)) { return null; } return ValidationHelper.GetInteger(txtValidFor.Text.Trim(), 0); } set { txtValidFor.Text = value.HasValue ? value.ToString() : null; } } /// /// Gets or sets the "valid until" date and time. /// public DateTime? ValidUntil { get { if (untilDateElem.SelectedDateTime == DateTimeHelper.ZERO_TIME) { return null; } return untilDateElem.SelectedDateTime; } set { untilDateElem.SelectedDateTime = value.HasValue ? value.Value : DateTimeHelper.ZERO_TIME; } } /// /// Gets or sets the name of the "valid for" form field. /// Default value is "ValidFor". /// public string ValidForFieldName { get { return ValidationHelper.GetString(GetValue("ValidForFieldName"), mValidForFieldName); } set { SetValue("ValidForFieldName", value); mValidForFieldName = value; } } /// /// Gets or sets the name of the "valid until" form field. /// Default value is "ValidUntil". /// public string ValidUntilFieldName { get { return ValidationHelper.GetString(GetValue("ValidUntilFieldName"), mValidUntilFieldName); } set { SetValue("ValidUntilFieldName", value); mValidUntilFieldName = value; } } #endregion #region "Properties - general" /// /// Indicates if validation error message is displayed by the control itself. /// public bool DisplayErrorMessage { get; set; } /// /// Indicates if post back automatically occurs when validity is changed. /// public bool AutoPostBack { get { return ValidationHelper.GetBoolean(GetValue("AutoPostBack"), mAutoPostBack); } set { SetValue("AutoPostBack", value); mAutoPostBack = value; } } /// /// Automatically disables inactive control. If time interval validity is selected (day, week, month, year) /// then time interval text box is enabled and date time picker is disabled and vice versa. /// public bool AutomaticallyDisableInactiveControl { get; set; } #endregion #region "Events" public event EventHandler OnValidityChanged; #endregion #region "Lifecycle" protected override void CreateChildControls() { DisableInactiveControl(); } protected override void OnInit(EventArgs e) { base.OnInit(e); EnsureChildControls(); InitByForm(); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); // Set auto postback radDays.AutoPostBack = AutoPostBack; radWeeks.AutoPostBack = AutoPostBack; radMonths.AutoPostBack = AutoPostBack; radYears.AutoPostBack = AutoPostBack; radUntil.AutoPostBack = AutoPostBack; // Display error message if required lblError.Text = ErrorMessage; lblError.Visible = DisplayErrorMessage && !string.IsNullOrEmpty(lblError.Text); } #endregion #region "Initialization" /// /// Attempts to initialize the control by form data. /// private void InitByForm() { if (Form == null) { return; } if (Form.Data.ContainsColumn(ValidForFieldName)) { ValidFor = Form.Data.GetValue(ValidForFieldName) as int?; } if (Form.Data.ContainsColumn(ValidUntilFieldName)) { ValidUntil = Form.Data.GetValue(ValidUntilFieldName) as DateTime?; } } #endregion #region "Validation" public override bool IsValid() { return string.IsNullOrEmpty(Validate()); } /// /// Validates values and returns error message if there is any. /// public string Validate() { ErrorMessage = null; // Validate valid for multiplier if (string.IsNullOrEmpty(ErrorMessage) && !radUntil.Checked) { if (!ValidationHelper.IsInteger(txtValidFor.Text.Trim()) || !(ValidationHelper.GetInteger(txtValidFor.Text.Trim(), 0) >= 1)) { ErrorMessage = GetString("general.selectvalidity.validforerror"); } } // Validate until date and time if (string.IsNullOrEmpty(ErrorMessage) && radUntil.Checked) { if (!string.IsNullOrEmpty(untilDateElem.DateTimeTextBox.Text) && (!untilDateElem.IsValidRange() || !ValidationHelper.IsDateTime(untilDateElem.DateTimeTextBox.Text))) { ErrorMessage = GetString("general.selectvalidity.validuntilerror"); } } return ErrorMessage; } #endregion #region "Methods" /// /// Returns an array of values of related fields. /// The first dimension of the array is the field name and the second dimension is its value. /// public override object[,] GetOtherValues() { EnsureChildControls(); object[,] values = new object[1, 2]; // Set only value relevant to actual validity if (Validity == ValidityEnum.Until) { values[0, 0] = ValidUntilFieldName; values[0, 1] = ValidUntil; } else { values[0, 0] = ValidForFieldName; values[0, 1] = ValidFor; } return values; } protected void ValidityRadioGroup_CheckedChanged(object sender, EventArgs e) { DisableInactiveControl(); if (OnValidityChanged != null) { OnValidityChanged(this, null); } } /// /// Enables/disables inactive control. /// private void DisableInactiveControl() { if (AutomaticallyDisableInactiveControl && AutoPostBack) { bool dateSelected = radUntil.Checked; txtValidFor.Enabled = !dateSelected; untilDateElem.Enabled = dateSelected; } } #endregion }