using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using System.Collections.Generic; using CMS.CMSHelper; using CMS.Controls; using CMS.DataEngine; using CMS.ExtendedControls; using CMS.GlobalHelper; using CMS.PortalControls; using CMS.SettingsProvider; public partial class CMSWebParts_Viewers_Documents_cmsdatagrid : CMSAbstractWebPart { #region "Variables" protected string[] mColumnList; protected string[] mGlobalNameID = null; #endregion #region "Document properties" /// /// Gets or sets the cache item name. /// public override string CacheItemName { get { return base.CacheItemName; } set { base.CacheItemName = value; gridElem.CacheItemName = value; } } /// /// Cache dependencies, each cache dependency on a new line. /// public override string CacheDependencies { get { return ValidationHelper.GetString(base.CacheDependencies, gridElem.CacheDependencies); } set { base.CacheDependencies = value; gridElem.CacheDependencies = value; } } /// /// Gets or sets the cache minutes. /// public override int CacheMinutes { get { return base.CacheMinutes; } set { base.CacheMinutes = value; gridElem.CacheMinutes = value; } } /// /// Gets or sets the value that indicates whether first page is sets when sort of some column is changed. /// public bool SetFirstPageAfterSortChange { get { return ValidationHelper.GetBoolean(GetValue("SetFirstPageAfterSortChange"), gridElem.SetFirstPageAfterSortChange); } set { SetValue("SetFirstPageAfterSortChange", value); gridElem.SetFirstPageAfterSortChange = value; } } /// /// Gets or sets the value that indicates whether permissions should be checked. /// public bool CheckPermissions { get { return ValidationHelper.GetBoolean(GetValue("CheckPermissions"), gridElem.CheckPermissions); } set { SetValue("CheckPermissions", value); } } /// /// Gets or sets the class names which should be displayed. /// public string ClassNames { get { return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("ClassNames"), gridElem.ClassNames), gridElem.ClassNames); } set { SetValue("ClassNames", value); gridElem.ClassNames = value; } } /// /// Code name of the category to display documents from. /// public string CategoryName { get { return ValidationHelper.GetString(GetValue("CategoryName"), gridElem.CategoryName); } set { SetValue("CategoryName", value); gridElem.CategoryName = value; } } /// /// Gets or sets the value that indicates whether documents are combined with default culture version. /// public bool CombineWithDefaultCulture { get { return ValidationHelper.GetBoolean(GetValue("CombineWithDefaultCulture"), gridElem.CombineWithDefaultCulture); } set { SetValue("CombineWithDefaultCulture", value); gridElem.CombineWithDefaultCulture = value; } } /// /// Gets or sets the culture code of the documents. /// public string CultureCode { get { return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("CultureCode"), gridElem.CultureCode), gridElem.CultureCode); } set { SetValue("CultureCode", value); gridElem.CultureCode = value; } } /// /// Gets or sets the maximal relative level of the documents to be shown. /// public int MaxRelativeLevel { get { return ValidationHelper.GetInteger(GetValue("MaxRelativeLevel"), gridElem.MaxRelativeLevel); } set { SetValue("MaxRelativeLevel", value); gridElem.MaxRelativeLevel = value; } } /// /// Gets or sets the order by clause. /// public string OrderBy { get { return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("OrderBy"), gridElem.OrderBy), gridElem.OrderBy); } set { SetValue("OrderBy", value); gridElem.OrderBy = value; } } /// /// Gets or sets the path of the documents. /// public string Path { get { return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("Path"), null), null); } set { SetValue("Path", value); gridElem.Path = value; } } /// /// Gets or sets the value that indicates whether only published documents are selected. /// public bool SelectOnlyPublished { get { return ValidationHelper.GetBoolean(GetValue("SelectOnlyPublished"), gridElem.SelectOnlyPublished); } set { SetValue("SelctOnlyPublished", value); gridElem.SelectOnlyPublished = value; } } /// /// Gets or sets the value that indicates whether filter out duplicate documents. /// public bool FilterOutDuplicates { get { return ValidationHelper.GetBoolean(GetValue("FilterOutDuplicates"), gridElem.FilterOutDuplicates); } set { SetValue("FilterOutDuplicates", value); gridElem.FilterOutDuplicates = value; } } /// /// Gets or sets the site name. /// public string SiteName { get { return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("SiteName"), gridElem.SiteName), gridElem.SiteName); } set { SetValue("SiteName", value); gridElem.SiteName = value; } } /// /// Gets or sets the where condition. /// public string WhereCondition { get { return DataHelper.GetNotEmpty(GetValue("WhereCondition"), gridElem.WhereCondition); } set { SetValue("WhereCondition", value); gridElem.WhereCondition = value; } } /// /// Gets or sets the number which indicates how many documents should be displayed. /// public int SelectTopN { get { return ValidationHelper.GetInteger(GetValue("SelectTopN"), gridElem.SelectTopN); } set { SetValue("SelectTopN", value); gridElem.SelectTopN = value; } } /// /// Gets or sets the columns to get. /// public string SelectedColumns { get { return DataHelper.GetNotEmpty(GetValue("Columns"), gridElem.SelectedColumns); } set { SetValue("Columns", value); gridElem.SelectedColumns = value; } } #endregion #region "Relationships properties" /// /// Gets or sets the value that indicates whether related node is on the left side. /// public bool RelatedNodeIsOnTheLeftSide { get { return ValidationHelper.GetBoolean(GetValue("RelatedNodeIsOnTheLeftSide"), gridElem.RelatedNodeIsOnTheLeftSide); } set { SetValue("RelatedNodeIsOnTheLeftSide", value); gridElem.RelatedNodeIsOnTheLeftSide = value; } } /// /// Gets or sets the relationship name. /// public string RelationshipName { get { return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("RelationshipName"), gridElem.RelationshipName), gridElem.RelationshipName); } set { SetValue("RelationshipName", value); gridElem.RelationshipName = value; } } /// /// Gets or sets the relationship with node GUID. /// public Guid RelationshipWithNodeGUID { get { return ValidationHelper.GetGuid(GetValue("RelationshipWithNodeGuid"), gridElem.RelationshipWithNodeGuid); } set { SetValue("RelationshipWithNodeGuid", value); gridElem.RelationshipWithNodeGuid = value; } } #endregion #region "Transformation properties" /// /// Gets or sets the name of the transformation which is used for displaying the results of selected item. /// public string SelectedItemTransformationName { get { return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("SelectedItemTransformationName"), gridElem.SelectedItemTransformationName), gridElem.SelectedItemTransformationName); } set { SetValue("SelectedItemTransformationName", value); gridElem.SelectedItemTransformationName = value; } } #endregion #region "Public properties" /// /// Gets or sets the value that indicates whether control should be hidden if no data found. /// public bool HideControlForZeroRows { get { return ValidationHelper.GetBoolean(GetValue("HideControlForZeroRows"), gridElem.HideControlForZeroRows); } set { SetValue("HideControlForZeroRows", value); gridElem.HideControlForZeroRows = value; } } /// /// Gets or sets the text which is displayed for zero rows result. /// public string ZeroRowsText { get { return ValidationHelper.GetString(GetValue("ZeroRowsText"), gridElem.ZeroRowsText); } set { SetValue("ZeroRowsText", value); gridElem.ZeroRowsText = value; } } /// /// Gets or sets the value that indicates whether sorting is ascending at default. /// public bool SortAscending { get { return (ValidationHelper.GetBoolean(GetValue("SortAscending"), gridElem.SortAscending)); } set { SetValue("SortAscending", value); gridElem.SortAscending = value; } } /// /// Gets or sets the value that indicates whether sorting is allowed. /// public bool AllowSorting { get { return ValidationHelper.GetBoolean(GetValue("AllowSorting"), gridElem.AllowSorting); } set { SetValue("AllowSorting", value); gridElem.AllowSorting = value; } } /// /// Gets or sets the value that indicates whether sorting process is proceeded in the code. /// public bool ProcessSorting { get { return ValidationHelper.GetBoolean(GetValue("ProcessSorting"), gridElem.ProcessSorting); } set { SetValue("ProcessSorting", value); gridElem.ProcessSorting = value; } } /// /// Gets or sets the default sort field. /// public string SortField { get { return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("SortField"), gridElem.SortField), gridElem.SortField); } set { SetValue("SortField", value); gridElem.SortField = value; } } /// /// Gets or sets the value that indicates whether paging will be allowed. /// public bool AllowPaging { get { return ValidationHelper.GetBoolean(GetValue("AllowPaging"), gridElem.AllowPaging); } set { SetValue("AllowPaging", value); gridElem.AllowPaging = value; } } /// /// Gets or sets the value that indicates whether custom paging is enabled. /// public bool AllowCustomPaging { get { return ValidationHelper.GetBoolean(GetValue("AllowCustomPaging"), gridElem.AllowCustomPaging); } set { SetValue("AllowCustomPaging", value); gridElem.AllowCustomPaging = value; } } /// /// Gets or sets the size of the page if the paging is allowed. /// public int PageSize { get { return ValidationHelper.GetInteger(GetValue("PageSize"), gridElem.PageSize); } set { SetValue("PageSize", value); gridElem.PageSize = value; } } /// /// Gets or sets the value that indicates whether DataBind will be proceeded automatically. /// public bool DataBindByDefault { get { return ValidationHelper.GetBoolean(GetValue("DataBindByDefault"), gridElem.DataBindByDefault); } set { SetValue("DataBindByDefault", value); gridElem.DataBindByDefault = value; } } /// /// Gets or sets the paging mode. /// public PagerMode PagingMode { get { return gridElem.GetPagerMode(ValidationHelper.GetString(GetValue("Mode"), "")); } set { SetValue("Mode", value); } } /// /// Gets or sets the value that indicates whether header will be displayed. /// public bool ShowHeader { get { return ValidationHelper.GetBoolean(GetValue("ShowHeader"), gridElem.ShowHeader); } set { SetValue("ShowHeader", value); gridElem.ShowHeader = value; } } /// /// Gets or sets the value that indicates whether footer will be displayed. /// public bool ShowFooter { get { return ValidationHelper.GetBoolean(GetValue("ShowFooter"), gridElem.ShowFooter); } set { SetValue("ShowFooter", value); gridElem.ShowFooter = value; } } /// /// Gets or sets the tool tip text. /// public string ToolTip { get { return DataHelper.GetNotEmpty(GetValue("ToolTip"), gridElem.ToolTip); } set { SetValue("ToolTip", value); gridElem.ToolTip = value; } } /// /// Gets or sets the value that indicates whether 'New button' should be displayed. /// public bool ShowNewButton { get { return ValidationHelper.GetBoolean(GetValue("ShowNewButton"), btnAdd.Visible); } set { SetValue("ShowNewButton", value); btnAdd.Visible = value; } } /// /// Gets or sets the new button text. /// public string NewButtonText { get { return DataHelper.GetNotEmpty(GetValue("NewButtonText"), btnAdd.Text); } set { SetValue("NewButtonText", value); btnAdd.Text = value; } } /// /// Gets or sets the value that represent column selector settings. /// public string ColumnsSelector { get { return DataHelper.GetNotEmpty(GetValue("ColumnSelector"), ""); } set { SetValue("ColumnSelector", value); } } /// /// Filter name. /// public string FilterName { get { return ValidationHelper.GetString(GetValue("FilterName"), gridElem.FilterName); } set { SetValue("FilterName", value); gridElem.FilterName = value; } } /// /// Gets or sets the SkinID which should be used. /// public override string SkinID { get { return base.SkinID; } set { base.SkinID = value; if ((gridElem != null) && (PageCycle < PageCycleEnum.Initialized)) { gridElem.SkinID = SkinID; } } } /// /// Grid control. /// public CMSDataGrid GridControl { get { return gridElem; } } #endregion #region "Stop processing" /// /// Returns true if the control processing should be stopped. /// public override bool StopProcessing { get { return base.StopProcessing; } set { base.StopProcessing = value; gridElem.StopProcessing = value; } } #endregion /// /// Content loaded event handler. /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Initializes the control properties. /// protected void SetupControl() { if (StopProcessing) { gridElem.StopProcessing = true; } else { gridElem.ControlContext = ControlContext; gridElem.EnableViewState = true; // Set properties from web part form gridElem.CacheItemName = CacheItemName; gridElem.CacheDependencies = CacheDependencies; gridElem.CacheMinutes = CacheMinutes; gridElem.CheckPermissions = CheckPermissions; gridElem.ClassNames = ClassNames; gridElem.CategoryName = CategoryName; gridElem.CombineWithDefaultCulture = CombineWithDefaultCulture; gridElem.FilterOutDuplicates = FilterOutDuplicates; gridElem.CultureCode = CultureCode; gridElem.MaxRelativeLevel = MaxRelativeLevel; gridElem.OrderBy = OrderBy; gridElem.RelatedNodeIsOnTheLeftSide = RelatedNodeIsOnTheLeftSide; gridElem.RelationshipName = RelationshipName; gridElem.RelationshipWithNodeGuid = RelationshipWithNodeGUID; gridElem.SelectedItemTransformationName = SelectedItemTransformationName; gridElem.Path = Path; gridElem.SelectTopN = SelectTopN; gridElem.SelectedColumns = SelectedColumns; gridElem.SelectOnlyPublished = SelectOnlyPublished; gridElem.SiteName = SiteName; gridElem.WhereCondition = WhereCondition; gridElem.HideControlForZeroRows = HideControlForZeroRows; gridElem.ZeroRowsText = ZeroRowsText; if (!StandAlone && (PageCycle < PageCycleEnum.Initialized) && (ValidationHelper.GetString(Page.StyleSheetTheme, "") == "")) { gridElem.SkinID = SkinID; } gridElem.SortAscending = SortAscending; gridElem.AllowSorting = AllowSorting; gridElem.ProcessSorting = ProcessSorting; gridElem.SortField = SortField; gridElem.AllowPaging = AllowPaging; gridElem.AllowCustomPaging = AllowCustomPaging; gridElem.PageSize = PageSize; gridElem.DataBindByDefault = DataBindByDefault; gridElem.SelectTopN = SelectTopN; gridElem.PagerStyle.Mode = PagingMode; gridElem.FilterName = FilterName; gridElem.ShowHeader = ShowHeader; gridElem.ShowFooter = ShowFooter; gridElem.ToolTip = ToolTip; gridElem.SetFirstPageAfterSortChange = SetFirstPageAfterSortChange; // CMSEditModeButtonAdd btnAdd.Visible = ShowNewButton; btnAdd.Text = NewButtonText; string[] mClassNames = gridElem.ClassNames.Split(';'); btnAdd.ClassName = DataHelper.GetNotEmpty(mClassNames[0], ""); string mPath = ""; if (gridElem.Path.EndsWithCSafe("/%")) { mPath = gridElem.Path.Remove(gridElem.Path.Length - 2); } if (gridElem.Path.EndsWithCSafe("/")) { mPath = gridElem.Path.Remove(gridElem.Path.Length - 1); } btnAdd.Path = DataHelper.GetNotEmpty(mPath, ""); LoadFromDataClass(gridElem.ClassNames); InitColumns(ColumnsSelector); /*if (RequestHelper.IsPostBack()) { this.gridElem.ReloadData(true); }*/ gridElem.DataBindByDefault = false; } } /// /// Applies given StyleSheet skin. /// public override void ApplyStyleSheetSkin(Page page) { gridElem.SkinID = SkinID; base.ApplyStyleSheetSkin(page); } /// /// Reload data. /// public override void ReloadData() { base.ReloadData(); SetupControl(); gridElem.ReloadData(true); } /// /// Page load. /// protected void Page_Load(object sender, EventArgs e) { gridElem.ReloadData(true); } /// /// Gets information from XML and add it to columns. /// /// Input XML string /// Return false if params is null or empty or when Xml haven't any node protected bool InitColumns(string mXML) { // Check if data are valid if (gridElem.Columns.Count > 0) { return false; } if (string.IsNullOrEmpty(mXML)) { return false; } if ((mColumnList == null) || (mColumnList.Length == 0)) { return false; } // Load XML from string XmlDocument mXMLDocument = new XmlDocument(); mXMLDocument.LoadXml(mXML); XmlNodeList NodeList = mXMLDocument.DocumentElement.GetElementsByTagName("column"); if (NodeList.Count == 0) { return false; } gridElem.AutoGenerateColumns = false; gridElem.ItemDataBound += gridElem_ItemDataBound; // Go through all nodes foreach (XmlNode node in NodeList) { string mName = XmlHelper.GetXmlAttributeValue(node.Attributes["name"], ""); string mHeader = XmlHelper.GetXmlAttributeValue(node.Attributes["header"], ""); string mType = XmlHelper.GetXmlAttributeValue(node.Attributes["type"], ""); // If name is not empty if (DataHelper.GetNotEmpty(mName, "") != "") { // And it is in the table if (IsInTable(mName)) { // Create new column DataGridColumn column = null; if ((mType != null) && (mType == "link")) { if (mGlobalNameID == null) { mGlobalNameID = new string[0]; } string[] mHelpGlobal = new string[mGlobalNameID.Length + 1]; mHelpGlobal[mGlobalNameID.Length] = mName; for (int i = 0; i < mGlobalNameID.Length; i++) { mHelpGlobal[i] = mGlobalNameID[i]; } mGlobalNameID = mHelpGlobal; TemplateColumn col = new TemplateColumn(); col.ItemTemplate = new LinkItemTemplate(mName); column = col; } else { BoundColumn col = new BoundColumn(); column = col; col.DataField = mName; } // Load header if (DataHelper.GetNotEmpty(mHeader, "") != "") { column.HeaderText = ResHelper.LocalizeString(mHeader); } else { column.HeaderText = mName; } if (gridElem.AllowSorting) { column.SortExpression = mName; } gridElem.Columns.Add(column); } } } return true; } /// /// Check if column is valid for classnames. /// /// Name of column /// Return true if column is in table protected bool IsInTable(string mColumn) { if ((DataHelper.GetNotEmpty(mColumn, "") == "") || (mColumnList == null)) { // If column isn't set return false return false; } // Find column in table for (int i = mColumnList.GetLowerBound(0); i <= mColumnList.GetUpperBound(0); i++) { if (mColumnList[i] == mColumn) { return true; } } return false; } /// /// Load Columns names from Tree. /// protected void LoadFromDataClass(string classNames) { List columnList = new List(); List classesList = new List(); if (!string.IsNullOrEmpty(classNames)) { classesList = new List(classNames.Split(';')); } // If more than 1 DataClasses are in classesList, get columns only from CMS.Tree and CMS.Document. //if (classesList.Count > 1) //{ // classesList = new List(); //} classesList.Add("CMS.Tree"); classesList.Add("CMS.Document"); // Fill columnList with column names from all classes. foreach (string className in classesList) { try { if (className != "") { IDataClass dc = DataClassFactory.NewDataClass(className); DataClassInfo dci = DataClassInfoProvider.GetDataClass(className); // Get columns only from couplet classes. if (dci.ClassIsCoupledClass) { foreach (string col in dc.StructureInfo.ColumnNames) { columnList.Add(col); } } } } catch { } } // Set string array. mColumnList = columnList.ToArray(); } /// /// Item data bound handler. /// private void gridElem_ItemDataBound(object sender, DataGridItemEventArgs e) { if (mGlobalNameID != null) { // For all items for (int i = mGlobalNameID.GetLowerBound(0); i <= mGlobalNameID.GetUpperBound(0); i++) { if (!string.IsNullOrEmpty(mGlobalNameID[i])) { Control ctrl = e.Item.FindControl(mGlobalNameID[i]); if (ctrl != null) { // Create hyperlink control ((HyperLink)ctrl).Text = ((DataRowView)e.Item.DataItem)[mGlobalNameID[i]].ToString(); ((HyperLink)ctrl).NavigateUrl = CMSContext.GetUrl(((DataRowView)e.Item.DataItem)["NodeAliasPath"].ToString(), ((DataRowView)e.Item.DataItem)["DocumentUrlPath"].ToString()); } } } } } /// /// OnPrerender override (Set visibility). /// protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); // Set visibility of control Visible = gridElem.Visible && !StopProcessing; if (DataHelper.DataSourceIsEmpty(gridElem.DataSource) && (HideControlForZeroRows)) { Visible = false; } } /// /// Clear cache. /// public override void ClearCache() { gridElem.ClearCache(); } }