using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CMS.FormControls; using CMS.GlobalHelper; using CMS.SiteProvider; using CMS.UIControls; public partial class CMSFormControls_CountrySelector : FormEngineUserControl { #region "Variables" private bool mDisplayAllItems = true; private bool mAddAllItemsRecord = false; private bool mAddNoneRecord = false; private bool mUseCodeNameForSelection = true; private bool mAddSelectCountryRecord = true; private bool mEnableStateSelection = true; private ReturnType returnWhat = ReturnType.Both; /// /// Indicates what return value should be submited by the control. /// private enum ReturnType { /// /// Default value. Returns string value with both countryID and stateID separated by semicolumn. /// Both = 0, /// /// Returns integer value containing only countryID. /// CountryID = 1, /// /// Returns integer value containing only stateID. /// StateID = 2 } #endregion #region "Public properties" /// /// Gets client ID of the country drop down list. /// public override string ValueElementID { get { return CountryDropDown.ClientID; } } /// /// Gets or sets the enabled state of the control. /// public override bool Enabled { get { return base.Enabled; } set { base.Enabled = value; if (uniSelectorCountry != null) { uniSelectorCountry.Enabled = value; } if (uniSelectorState != null) { uniSelectorState.Enabled = value; } } } /// /// Gets or sets the value which indicates whether the selector should load all data to DDL. /// public bool DisplayAllItems { get { return mDisplayAllItems; } set { mDisplayAllItems = value; if ((uniSelectorCountry != null) && (uniSelectorState != null)) { uniSelectorCountry.MaxDisplayedItems = (value ? 300 : UniSelector.DefaultMaxDisplayedItems); uniSelectorState.MaxDisplayedItems = (value ? 100 : UniSelector.DefaultMaxDisplayedItems); uniSelectorCountry.MaxDisplayedTotalItems = uniSelectorCountry.MaxDisplayedItems + 50; uniSelectorState.MaxDisplayedTotalItems = uniSelectorState.MaxDisplayedItems + 50; } } } /// /// Gets or sets the value which determines, whether to add none item record to the dropdownlist. /// public bool AddAllItemsRecord { get { return mAddAllItemsRecord; } set { mAddAllItemsRecord = value; } } /// /// Add '(none)' record to the dropdownlist. /// public bool AddNoneRecord { get { return mAddNoneRecord; } set { mAddNoneRecord = value; } } /// /// Add '(select country)' record to the dropdownlist. /// public bool AddSelectCountryRecord { get { return mAddSelectCountryRecord; } set { mAddSelectCountryRecord = value; } } /// /// If set to true also state selection will be available in the control. /// public bool EnableStateSelection { get { return mEnableStateSelection; } set { mEnableStateSelection = value; } } /// /// Set/get Value property in the form 'CountryName;StateName' or 'CountryID;StateID' /// public bool UseCodeNameForSelection { get { return mUseCodeNameForSelection; } set { mUseCodeNameForSelection = value; } } /// /// Selected country ID. /// public int CountryID { get { if (UseCodeNameForSelection) { // Convert country name to ID CountryInfo ci = CountryInfoProvider.GetCountryInfo(ValidationHelper.GetString(uniSelectorCountry.Value, String.Empty)); if (ci != null) { return ci.CountryID; } } else { return ValidationHelper.GetInteger(uniSelectorCountry.Value, 0); } return 0; } set { if (value > 0) { // Check if code name is used for selection if (UseCodeNameForSelection) { CountryInfo ci = CountryInfoProvider.GetCountryInfo(value); if (ci != null) { uniSelectorCountry.Value = ci.CountryName; } } else { uniSelectorCountry.Value = value; } uniSelectorState.WhereCondition = "CountryID = " + value; } } } /// /// Selected State ID. Zero if not available. /// public int StateID { get { if (plcStates.Visible) { // Check id using code name for selection if (UseCodeNameForSelection) { // Convert state name to ID StateInfo si = StateInfoProvider.GetStateInfo(ValidationHelper.GetString(uniSelectorState.Value, String.Empty)); if (si != null) { return si.StateID; } } else { return ValidationHelper.GetInteger(uniSelectorState.Value, 0); } } return 0; } set { if (value > 0) { // Check id using code name for selection if (UseCodeNameForSelection) { // Convert state ID to name StateInfo si = StateInfoProvider.GetStateInfo(value); if (si != null) { uniSelectorState.Value = si.StateName; } } else { uniSelectorState.Value = value; } } } } /// /// Selected Country name. /// public string CountryName { get { if (UseCodeNameForSelection) { return ValidationHelper.GetString(uniSelectorCountry.Value, String.Empty); } else { CountryInfo ci = CountryInfoProvider.GetCountryInfo(ValidationHelper.GetInteger(uniSelectorCountry.Value, 0)); if (ci != null) { return ci.CountryName; } } return String.Empty; } set { if (UseCodeNameForSelection) { uniSelectorCountry.Value = value; } else { CountryInfo ci = CountryInfoProvider.GetCountryInfo(value); if (ci != null) { uniSelectorCountry.Value = ci.CountryID; } } } } /// /// Selected State name. /// public string StateName { get { if (plcStates.Visible) { if (UseCodeNameForSelection) { return ValidationHelper.GetString(uniSelectorState.Value, String.Empty); } else { StateInfo si = StateInfoProvider.GetStateInfo(ValidationHelper.GetInteger(uniSelectorState.Value, 0)); if (si != null) { return si.StateName; } } } return String.Empty; } set { if (UseCodeNameForSelection) { uniSelectorState.Value = value; } else { StateInfo si = StateInfoProvider.GetStateInfo(value); if (si != null) { uniSelectorState.Value = si.StateID; } } } } /// /// Gets or sets field value. /// public override object Value { get { returnWhat = (ReturnType)ValidationHelper.GetInteger(GetValue("ReturnType"), 0); // Return only country ID if (returnWhat == ReturnType.CountryID) { return CountryID; } // Return only state ID else if (returnWhat == ReturnType.StateID) { return StateID; } // Return string with country and state IDs else { string val = null; if (UseCodeNameForSelection) { val = (!string.IsNullOrEmpty(StateName)) ? CountryName + ";" + StateName : CountryName; } else { val = (StateID > 0) ? CountryID.ToString() + ";" + StateID.ToString() : CountryID.ToString(); } return (val == ";") ? null : val; } } set { // Return type returnWhat = (ReturnType)ValidationHelper.GetInteger(GetValue("ReturnType"), 0); // Load panel if ((uniSelectorCountry == null) || (uniSelectorState == null)) { pnlUpdate.LoadContainer(); } // Get only country ID if (returnWhat == ReturnType.CountryID) { CountryID = ValidationHelper.GetInteger(value, 0); } // Get only stateID else if (returnWhat == ReturnType.StateID) { StateID = ValidationHelper.GetInteger(value, 0); // Find country from state info StateInfo state = StateInfoProvider.GetStateInfo(StateID); if (state != null) { CountryID = state.CountryID; } } // Get both country and state IDs else { string[] ids = ValidationHelper.GetString(value, "").Split(';'); if (ids.Length >= 1) { if (UseCodeNameForSelection) { CountryName = ValidationHelper.GetString(ids[0], ""); } else { CountryID = ValidationHelper.GetInteger(ids[0], 0); } if (ids.Length == 2) { if (UseCodeNameForSelection) { StateName = ValidationHelper.GetString(ids[1], ""); } else { StateID = ValidationHelper.GetInteger(ids[1], 0); } } } } } } /// /// Returns the DDL with countries. /// public DropDownList CountryDropDown { get { return uniSelectorCountry.DropDownSingleSelect; } } /// /// Returns the DDL with states. /// public DropDownList StateDropDown { get { return uniSelectorState.DropDownSingleSelect; } } /// /// Indicates whether state is selected. Returns true if no states offered. /// public bool StateSelectionIsValid { get { return (!plcStates.Visible) || (StateDropDown.Items.Count == 0) || (StateID > 0); } } #endregion #region "Methods" /// /// Page load. /// protected void Page_Load(object sender, EventArgs e) { if (StopProcessing) { uniSelectorCountry.StopProcessing = true; uniSelectorState.StopProcessing = true; } else { // Init selector for countries uniSelectorCountry.IsLiveSite = IsLiveSite; uniSelectorCountry.OnSelectionChanged += new EventHandler(uniSelectorCountry_OnSelectionChanged); uniSelectorCountry.DropDownSingleSelect.AutoPostBack = true; uniSelectorCountry.SelectionMode = SelectionModeEnum.SingleDropDownList; uniSelectorCountry.ReturnColumnName = (UseCodeNameForSelection ? "CountryName" : "CountryID"); uniSelectorCountry.MaxDisplayedItems = (DisplayAllItems ? 300 : UniSelector.DefaultMaxDisplayedItems); uniSelectorCountry.MaxDisplayedTotalItems = uniSelectorCountry.MaxDisplayedItems + 50; string[,] fields = null; if (AddSelectCountryRecord && AddNoneRecord) { fields = new string[,] { { GetString("countryselector.selectcountryrecord"), "" }, { GetString("general.selectnone"), "" } }; } else { // Add 'none' record when requested if (AddNoneRecord) { fields = new string[,] { { GetString("general.selectnone"), "" } }; } else if (AddSelectCountryRecord) { fields = new string[,] { { GetString("countryselector.selectcountryrecord"), "" } }; } } // Add (all) record when requested if (AddAllItemsRecord) { uniSelectorCountry.AllowAll = true; uniSelectorState.AllowAll = true; } else { uniSelectorCountry.SpecialFields = fields; } // Init selector for states uniSelectorState.IsLiveSite = IsLiveSite; uniSelectorState.SelectionMode = SelectionModeEnum.SingleDropDownList; uniSelectorState.DropDownSingleSelect.AutoPostBack = true; uniSelectorState.ReturnColumnName = (UseCodeNameForSelection ? "StateName" : "StateID"); uniSelectorState.MaxDisplayedItems = (DisplayAllItems ? 100 : UniSelector.DefaultMaxDisplayedItems); uniSelectorState.MaxDisplayedTotalItems = uniSelectorState.MaxDisplayedItems + 50; uniSelectorState.WhereCondition = "CountryID = " + CountryID; if (UseCodeNameForSelection) { uniSelectorState.AllRecordValue = String.Empty; uniSelectorState.NoneRecordValue = String.Empty; uniSelectorCountry.AllRecordValue = String.Empty; uniSelectorCountry.NoneRecordValue = String.Empty; } } } /// /// Hide States DDL if there is no state for selected country. /// protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (CountryID > 0) { uniSelectorState.StopProcessing = false; plcStates.Visible = mEnableStateSelection && uniSelectorState.HasData; } else { plcStates.Visible = false; uniSelectorState.StopProcessing = true; } } /// /// Country DropDownList Selection change. /// protected void uniSelectorCountry_OnSelectionChanged(object sender, EventArgs e) { if (CountryID > 0) { uniSelectorState.WhereCondition = "CountryID = " + CountryID; uniSelectorState.StopProcessing = false; uniSelectorState.Reload(true); // Raise change event RaiseOnChanged(); } else { uniSelectorState.StopProcessing = true; } } /// /// Reloads the data in the selector. /// public void ReloadData() { ReloadData(false); } /// /// Reloads the data in the selector. /// /// If true uniselectors are reloaded public void ReloadData(bool forceReload) { uniSelectorCountry.Reload(forceReload); int countryId = 0; if (UseCodeNameForSelection) { CountryInfo ci = CountryInfoProvider.GetCountryInfo(ValidationHelper.GetString(uniSelectorCountry.Value, "")); if (ci != null) { countryId = ci.CountryID; } } else { countryId = ValidationHelper.GetInteger(uniSelectorCountry.Value, 0); } uniSelectorState.WhereCondition = "CountryID = " + countryId; uniSelectorState.Reload(forceReload); } #endregion }