using System; using System.Data; using System.Collections; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.CMSHelper; using CMS.Controls; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.SiteProvider; using CMS.DocumentEngine; using CMS.UIControls; using CultureInfo = System.Globalization.CultureInfo; public partial class CMSAdminControls_UI_System_ViewObject : CMSAdminEditControl { #region "Variables" protected object mObject = null; // List of tables [DataTable, UniGridPager] protected ArrayList tables = new ArrayList(); #endregion #region "Properties" /// /// Gets or sets the object to display. /// public object Object { get { return mObject; } set { mObject = value; ReloadData(); } } #endregion" protected override void OnInit(EventArgs e) { base.OnInit(e); ReloadData(); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); WriteTablesContent(); } public override void ReloadData() { tables.Clear(); pnlContent.Controls.Clear(); WriteObject(Object); } protected void WriteObject(object obj) { // Write the objects if (obj != null) { if (obj == DBNull.Value) { WriteObjectType("DBNull.Value"); } else if (obj is DataView) { WriteObjectType(obj.ToString()); WriteDataTable(((DataView)obj).Table); } else if (obj is DataTable) { WriteObjectType(obj.ToString()); WriteDataTable((DataTable)obj); } else if (obj is DataSet) { WriteObjectType(obj.ToString()); WriteDataSet((DataSet)obj); } else if (obj is IDataContainer) { WriteObjectType(obj.ToString()); WriteDataContainer((IDataContainer)obj); } else if (obj is string) { WriteObjectType("string"); string content = "
"; pnlContent.Controls.Add(new LiteralControl(content)); } else if (obj is object[]) { pnlContent.Controls.Add(new LiteralControl("

Object type: " + obj.ToString() + "

")); // Write all objects in array foreach (object child in (object[])obj) { WriteObject(child); } } else { WriteObjectType(obj.ToString()); string objString = DataHelper.GetObjectString(obj); if (objString != obj.ToString()) { pnlContent.Controls.Add(new LiteralControl("
" + objString + "
")); } } } pnlContent.Controls.Add(new LiteralControl("

")); } protected void WriteTablesContent() { foreach (object[] table in tables) { // Prepare the components DataTable dt = (DataTable)table[0]; LiteralControl ltlContent = (LiteralControl)table[1]; UniGridPager pagerElem = (UniGridPager)table[2]; UniPagerConnector connectorElem = (UniPagerConnector)table[3]; // Handle the different types of direct page selector int currentPageSize = pagerElem.CurrentPageSize; if (currentPageSize > 0) { if ((float)connectorElem.PagerForceNumberOfResults / (float)currentPageSize > 20.0f) { pagerElem.DirectPageControlID = "txtPage"; } else { pagerElem.DirectPageControlID = "drpPage"; } } // Bind the pager first connectorElem.RaiseOnPageBinding(null, null); // Prepare the string builder StringBuilder sb = new StringBuilder(); // Prepare the indexes for paging int pageSize = pagerElem.CurrentPageSize; int startIndex = (pagerElem.CurrentPage - 1) * pageSize + 1; int endIndex = startIndex + pageSize; // Process all items int index = 0; bool all = (endIndex <= startIndex); if (dt.Columns.Count > 6) { // Write all rows foreach (DataRow dr in dt.Rows) { index++; if (all || (index >= startIndex) && (index < endIndex)) { sb.Append(""); // Add header sb.Append(""); // Add values int i = 0; foreach (DataColumn dc in dt.Columns) { object value = dr[dc.ColumnName]; // Binary columns string content = null; if ((dc.DataType == typeof(byte[])) && (value != DBNull.Value)) { byte[] data = (byte[])value; content = "<" + GetString("General.BinaryData") + ", " + DataHelper.GetSizeString(data.Length) + ">"; } else { content = ValidationHelper.GetString(value, ""); } if (!String.IsNullOrEmpty(content)) { ++i; string className = ((i % 2) == 0) ? "OddRow" : "EvenRow"; sb.Append(""); } } sb.Append("
" + GetString("General.FieldName") + "" + GetString("General.Value") + "
"); sb.Append("" + dc.ColumnName + ""); sb.Append(""); // Possible DataTime columns if ((dc.DataType == typeof(DateTime)) && (value != DBNull.Value)) { DateTime dateTime = Convert.ToDateTime(content); CultureInfo cultureInfo = new CultureInfo(CMSContext.CurrentUser.PreferredUICultureCode); content = dateTime.ToString(cultureInfo); } // Process content ProcessContent(sb, dr, dc.ColumnName, ref content); sb.Append("
\n"); } } } else { sb.Append(""); // Add header sb.Append(""); int h = 1; foreach (DataColumn column in dt.Columns) { string style = null; if (h == dt.Columns.Count) { style = "style=\"width:100%;\""; } sb.Append(""); h++; } sb.Append(""); // Write all rows foreach (DataRow dr in dt.Rows) { index++; if (all || (index >= startIndex) && (index < endIndex)) { string className = ((index % 2) == 0) ? "OddRow" : "EvenRow"; sb.Append(""); // Add values foreach (DataColumn dc in dt.Columns) { object value = dr[dc.ColumnName]; // Possible DataTime columns if ((dc.DataType == typeof(DateTime)) && (value != DBNull.Value)) { DateTime dateTime = Convert.ToDateTime(value); CultureInfo cultureInfo = new CultureInfo(CMSContext.CurrentUser.PreferredUICultureCode); value = dateTime.ToString(cultureInfo); } string content = ValidationHelper.GetString(value, ""); content = HTMLHelper.HTMLEncode(content); string cellStyle = null; if (content.Length < 100) { cellStyle = " style=\"white-space:nowrap;\""; } sb.Append("" + content + ""); } sb.Append(""); } } sb.Append("
" + column.ColumnName + "
\n"); } ltlContent.Text = sb.ToString(); } } /// /// Writes the object type to the output. /// /// Object type protected void WriteObjectType(string objectType) { pnlContent.Controls.Add(new LiteralControl("
Object type: " + objectType + "
")); } /// /// Writes the table content to the output. /// /// Table to write protected void WriteDataTable(DataTable dt) { pnlContent.Controls.Add(new LiteralControl("

" + dt.TableName + "

")); if (!DataHelper.DataSourceIsEmpty(dt)) { // Add literal for content LiteralControl ltlContent = new LiteralControl(); pnlContent.Controls.Add(ltlContent); // Add control for pager UniGridPager pagerElem = (UniGridPager)LoadUserControl("~/CMSAdminControls/UI/UniGrid/Controls/UniGridPager.ascx"); pagerElem.ID = dt.TableName + "_pager"; pagerElem.PageSizeOptions = "1,2,5,10,25,50,100,##ALL##"; if (dt.Columns.Count > 10) { pagerElem.DefaultPageSize = 1; } else if (dt.Columns.Count > 6) { pagerElem.DefaultPageSize = 2; } pnlContent.Controls.Add(pagerElem); // Add pager connector UniPagerConnector connectorElem = new UniPagerConnector(); connectorElem.PagerForceNumberOfResults = dt.Rows.Count; pagerElem.PagedControl = connectorElem; pnlContent.Controls.Add(connectorElem); tables.Add(new object[] { dt, ltlContent, pagerElem, connectorElem }); } } /// /// Writes the table content to the output. /// /// DataSet to write protected void WriteDataSet(DataSet ds) { // Write all tables foreach (DataTable dt in ds.Tables) { WriteDataTable(dt); } } /// /// Writes the data container to the output. /// /// Container to write protected void WriteDataContainer(IDataContainer dc) { StringBuilder sb = new StringBuilder(); sb.Append(""); // Add header sb.Append(""); // Add values int i = 0; foreach (string column in dc.ColumnNames) { try { object value = dc.GetValue(column); // Binary columns string content = null; if (value is byte[]) { byte[] data = (byte[])value; content = "<" + GetString("General.BinaryData") + ", " + DataHelper.GetSizeString(data.Length) + ">"; } else { content = ValidationHelper.GetString(value, ""); } if (!String.IsNullOrEmpty(content)) { ++i; string className = ((i % 2) == 0) ? "OddRow" : "EvenRow"; sb.Append(""); } } catch { } } sb.Append("
" + GetString("General.FieldName") + "" + GetString("General.Value") + "
"); sb.Append("" + column + ""); sb.Append(""); // Possible DataTime columns if (value is DateTime) { DateTime dateTime = Convert.ToDateTime(content); CultureInfo cultureInfo = new CultureInfo(CMSContext.CurrentUser.PreferredUICultureCode); content = dateTime.ToString(cultureInfo); } // Process content ProcessContent(sb, dc, column, ref content); sb.Append("

\n"); pnlContent.Controls.Add(new LiteralControl(sb.ToString())); } /// /// Processes the content. /// /// StringBuilder to write /// Source object /// Column /// Content protected void ProcessContent(StringBuilder sb, object source, string column, ref string content) { bool standard = true; switch (column.ToLowerCSafe()) { // Document content case "documentcontent": EditableItems items = new EditableItems(); items.LoadContentXml(content); // Add regions foreach (DictionaryEntry region in items.EditableRegions) { sb.Append("" + (string)region.Key + ""); string regionContent = HTMLHelper.ResolveUrls((string)region.Value, URLHelper.ApplicationPath); sb.Append("" + regionContent + ""); } // Add web parts foreach (DictionaryEntry part in items.EditableWebParts) { sb.Append("" + (string)part.Key + ""); string regionContent = HTMLHelper.ResolveUrls((string)part.Value, URLHelper.ApplicationPath); sb.Append("" + regionContent + ""); } standard = false; break; // XML columns case "pagetemplatewebparts": case "webpartproperties": case "reportparameters": case "classformdefinition": case "classxmlschema": case "classformlayout": case "userdialogsconfiguration": content = HTMLHelper.ReformatHTML(content); break; // File columns case "metafilename": { Guid metaFileGuid = ValidationHelper.GetGuid(GetValueFromSource(source, "MetaFileGuid"), Guid.Empty); if (metaFileGuid != Guid.Empty) { string metaFileName = ValidationHelper.GetString(GetValueFromSource(source, "MetaFileName"), ""); content = "" + HTMLHelper.HTMLEncode(metaFileName) + ""; sb.Append(content); standard = false; } } break; } // Standard rendering if (standard) { if (content.Length > 500) { content = TextHelper.EnsureMaximumLineLength(content, 50, "​", true); } else { content = HTMLHelper.HTMLEncode(content); } content = TextHelper.EnsureLineEndings(content, "
"); content = content.Replace("\t", "    "); sb.Append("
" + content + "
"); } } /// /// Gets the value from given source. /// /// Source object /// Column name protected object GetValueFromSource(object source, string column) { if (source is DataRow) { DataRow dr = (DataRow)source; return dr[column]; } else if (source is IDataContainer) { IDataContainer dc = (IDataContainer)source; return dc.GetValue(column); } return null; } }