using System; using System.Collections.Generic; 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.Reporting; public partial class CMSModules_Reporting_FormControls_SelectReportCategory : FormEngineUserControl { #region "Variables" private bool mShowRootCategory = false; private bool mUseAutoPostBack = false; #endregion #region "Public properties" /// /// Gets or sets the enabled state of the control. /// public override bool Enabled { get { return base.Enabled; } set { EnsureChildControls(); base.Enabled = value; drpCategories.Enabled = value; } } /// /// Gets or sets field value. /// public override object Value { get { EnsureChildControls(); return drpCategories.SelectedValue; } set { EnsureChildControls(); drpCategories.SelectedValue = ValidationHelper.GetString(value, String.Empty); } } /// /// Gets the inner DropDownList control. /// public DropDownList DropDownList { get { return drpCategories; } } /// /// If true show root category. /// public bool ShowRootCategory { get { return mShowRootCategory; } set { mShowRootCategory = value; } } /// /// Indicates if use autopostback. /// public bool UseAutoPostBack { get { return mUseAutoPostBack; } set { mUseAutoPostBack = value; } } #endregion #region "Methods" protected void Page_Load(object sender, EventArgs e) { } /// /// Creates child controls. /// protected override void CreateChildControls() { if (StopProcessing) { return; } drpCategories.AutoPostBack = UseAutoPostBack; drpCategories.Items.Clear(); //// Get categories DataSet ds = ReportCategoryInfoProvider.GetCategories(String.Empty, String.Empty); if (!DataHelper.DataSourceIsEmpty(ds)) { int counter = 0; // Make special collection for "tree mapping" Dictionary> categories = new Dictionary>(); // Fill collection from dataset foreach (DataRow dr in ds.Tables[0].Rows) { int parentId = ValidationHelper.GetInteger(dr["CategoryParentID"], 0); int id = ValidationHelper.GetInteger(dr["CategoryID"], 0); string name = ResHelper.LocalizeString(ValidationHelper.GetString(dr["CategoryDisplayName"], String.Empty)); SortedList list; categories.TryGetValue(parentId, out list); try { // Sub categories list not created yet if (list == null) { list = new SortedList(); categories.Add(parentId, list); } list.Add(name + "_" + counter, new object[2] { id, name }); } catch { } counter++; } // Start filling the dropdown from the root(parentId = 0) AddSubCategories(categories, 0, 0); } } /// /// Add subcategories list items to drop down. /// /// Special "tree" collection /// Category parent ID /// Category level(recursion) private void AddSubCategories(Dictionary> categories, int parentId, int level) { if (categories != null) { SortedList categoryList; categories.TryGetValue(parentId, out categoryList); if (categoryList != null) { foreach (KeyValuePair category in categoryList) { // Make indentation for sub categories string indentation = String.Empty; //No root displayed - show one level less int levelIndentation = level - 1; if (ShowRootCategory) { levelIndentation = level; } for (int i = 0; i < levelIndentation; i++) { indentation += "\xA0\xA0\xA0"; } //If item is not root if ((parentId != 0) || (ShowRootCategory)) { // Create and add list item ListItem listItem = new ListItem(indentation + category.Value[1], category.Value[0].ToString()); drpCategories.Items.Add(listItem); } // Recursion AddSubCategories(categories, Convert.ToInt32(category.Value[0]), level + 1); } } } } #endregion }