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.DataEngine;
using CMS.FormControls;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
public partial class CMSModules_PortalEngine_Controls_WebParts_SelectWebpart : FormEngineUserControl
{
#region "Variables"
private DataSet ds = null;
private bool mShowRoot = false;
private bool mShowEmptyCategories = false;
private bool mShowWebparts = true;
private bool mEnableCategorySelection = false;
private bool mShowInheritedWebparts = true;
private bool dataLoaded = false;
#endregion
#region "Public properties"
///
/// Gets or sets field value.
///
public override object Value
{
get
{
EnsureChildControls();
return drpWebpart.SelectedValue;
}
set
{
EnsureChildControls();
drpWebpart.SelectedValue = ValidationHelper.GetString(value, String.Empty);
}
}
///
/// Enables or disables root category in drop down list.
///
public bool ShowRoot
{
get
{
return mShowRoot;
}
set
{
mShowRoot = value;
}
}
///
/// Shows or hide inherited webparts.
///
public bool ShowInheritedWebparts
{
get
{
return mShowInheritedWebparts;
}
set
{
mShowInheritedWebparts = value;
}
}
///
/// Enables or disables hiding of empty categories in drop down list.
///
public bool ShowEmptyCategories
{
get
{
return mShowEmptyCategories;
}
set
{
mShowEmptyCategories = value;
}
}
///
/// If enabled, webparts are shown in dropdownlist.
///
public bool ShowWebparts
{
get
{
return mShowWebparts;
}
set
{
mShowWebparts = value;
}
}
///
/// If enabled, category can be selected, otherwise categories are disabled.
///
public bool EnableCategorySelection
{
get
{
return mEnableCategorySelection;
}
set
{
mEnableCategorySelection = value;
}
}
///
/// Gets the drop down list control.
///
public DropDownList DropDownListControl
{
get
{
return drpWebpart;
}
}
///
/// Root category path
///
public string RootPath
{
get;
set;
}
#endregion
#region "Methods"
///
/// Handles the Load event of the Page control.
///
protected void Page_Load(object sender, EventArgs e)
{
}
///
/// Creates child controls.
///
protected override void CreateChildControls()
{
if (StopProcessing)
{
return;
}
ReloadData(false);
}
///
/// Reloads the data.
///
/// If true, the data is reloaded even when already loaded
public void ReloadData(bool forceReload)
{
if (!dataLoaded || forceReload)
{
drpWebpart.Items.Clear();
// Do not retrieve webparts
string whereCond = null;
if (!ShowWebparts)
{
whereCond = "ObjectType = 'webpartcategory'";
}
if (!ShowInheritedWebparts)
{
whereCond = SqlHelperClass.AddWhereCondition(whereCond, "WebPartParentID IS NULL");
}
if (!String.IsNullOrEmpty(RootPath))
{
whereCond = SqlHelperClass.AddWhereCondition(whereCond, String.Format("ObjectPath = '{0}' OR ObjectPath LIKE '{0}/%'", SqlHelperClass.GetSafeQueryString(RootPath.TrimEnd('/'), false)));
}
ds = ConnectionHelper.ExecuteQuery("cms.webpartcategory.selectallview", null, whereCond, "DisplayName", 0, "ObjectID, DisplayName, ParentID, ObjectType");
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["ParentID"], 0);
int id = ValidationHelper.GetInteger(dr["ObjectID"], 0);
string name = ResHelper.LocalizeString(ValidationHelper.GetString(dr["DisplayName"], String.Empty));
string type = ValidationHelper.GetString(dr["ObjectType"], String.Empty);
// Skip webpart, take only WebpartCategory
if (type == "webpart")
{
continue;
}
SortedList list;
categories.TryGetValue(parentId, out list);
// 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 });
counter++;
}
// Start filling the dropdown from the root(parentId = 0)
int level = 0;
// Root is not shown, start indentation later
if (!ShowRoot)
{
level = -1;
}
AddSubCategories(categories, 0, level);
}
dataLoaded = true;
}
}
///
/// 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;
for (int i = 0; i < level; i++)
{
indentation += "\xA0\xA0\xA0";
}
// Create and add list item
ListItem listItem = null;
// Hide root
if (((parentId == 0) && ShowRoot) || (parentId > 0))
{
if (EnableCategorySelection)
{
listItem = new ListItem(indentation + category.Value[1], category.Value[0].ToString());
if (ShowRoot && (level == 0))
{
listItem.Attributes.Add("style", "background-color: #DDDDDD;");
}
}
else
{
listItem = new ListItem(indentation + category.Value[1], "-10");
listItem.Attributes.Add("style", "background-color: #DDDDDD; color: #000000;");
listItem.Attributes.Add("disabled", "disabled");
}
drpWebpart.Items.Add(listItem);
}
if (ShowWebparts)
{
// Add webparts under category
int webparts = AddWebparts(indentation += "\xA0\xA0\xA0", Convert.ToInt32(category.Value[0]));
// Remove empty categories
if (!ShowEmptyCategories && (webparts == 0) && (listItem != null))
{
drpWebpart.Items.Remove(listItem);
}
}
// Recursion
AddSubCategories(categories, Convert.ToInt32(category.Value[0]), level + 1);
}
}
}
}
///
/// Add webparts under current category.
///
/// Indentation of items
/// Parent category
/// Number of added webparts
private int AddWebparts(string indentation, int parentCategory)
{
if (ds != null)
{
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "ParentID = " + parentCategory + " AND ObjectType = 'webpart'";
foreach (DataRowView drv in dv)
{
// Create and add list item
ListItem listItem = new ListItem(indentation + drv["DisplayName"], drv["ObjectID"].ToString());
drpWebpart.Items.Add(listItem);
}
// Return number of webparts
return dv.Count;
}
return 0;
}
#endregion
}