/CMSAdminControls/UI/UniGrid/UniGrid.ascx.cs
C# | 2818 lines | 1963 code | 388 blank | 467 comment | 394 complexity | 7cd997de0b11da87452ab4c9c81277d1 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Xml;
- using CMS.CMSHelper;
- using CMS.Controls;
- using CMS.DataEngine;
- using CMS.EventLog;
- using CMS.ExtendedControls;
- using CMS.FormControls;
- using CMS.FormEngine;
- using CMS.GlobalHelper;
- using CMS.IO;
- using CMS.SettingsProvider;
- using CMS.UIControls;
- using CMS.UIControls.UniGridConfig;
- using CMS.URLRewritingEngine;
- using Action = CMS.UIControls.UniGridConfig.Action;
- public partial class CMSAdminControls_UI_UniGrid_UniGrid : UniGrid, IUniPageable
- {
- #region "Constants"
- private const int halfPageCountLimit = 1000;
- #endregion
- #region "Variables"
- private Button showButton = null;
- private int rowIndex = 1;
- private bool resetSelection = false;
- private bool visiblePagesSet = false;
- private static string DEFAULT_ACTIONS_MENU = "~/CMSAdminControls/UI/UniGrid/Controls/UniGridMenu.ascx";
- private bool mShowObjectMenu = true;
- private bool mCheckRelative = false;
- #endregion
- #region "Properties"
- /// <summary>
- /// Indicates if control is used on live site
- /// </summary>
- public override bool IsLiveSite
- {
- get
- {
- return base.IsLiveSite;
- }
- set
- {
- base.IsLiveSite = value;
- plcMess.IsLiveSite = value;
- }
- }
- /// <summary>
- /// Messages placeholder
- /// </summary>
- public override MessagesPlaceHolder MessagesPlaceHolder
- {
- get
- {
- return plcMess;
- }
- }
- /// <summary>
- /// Gets or sets GridView control of UniGrid.
- /// </summary>
- public override GridView GridView
- {
- get
- {
- return UniGridView;
- }
- }
- /// <summary>
- /// Hidden field containing selected items.
- /// </summary>
- public override HiddenField SelectionHiddenField
- {
- get
- {
- return hidSelection;
- }
- }
- /// <summary>
- /// Gets or sets UniGrid pager control of UniGrid.
- /// </summary>
- public override UniGridPager Pager
- {
- get
- {
- return pagerElem;
- }
- }
- /// <summary>
- /// If true, relative ancestor div is checked in context menu
- /// </summary>
- public bool CheckRelative
- {
- get
- {
- return mCheckRelative;
- }
- set
- {
- mCheckRelative = value;
- }
- }
- /// <summary>
- /// Gets selected items from UniGrid.
- /// </summary>
- public override List<string> SelectedItems
- {
- get
- {
- return GetSelectedItems();
- }
- set
- {
- SetSectedItems(value);
- }
- }
- /// <summary>
- /// Gets selected items from UniGrid.
- /// </summary>
- public override List<string> DeselectedItems
- {
- get
- {
- return GetDeselectedItems();
- }
- }
- /// <summary>
- /// Gets selected items from UniGrid.
- /// </summary>
- public override List<string> NewlySelectedItems
- {
- get
- {
- return GetNewlySelectedItems();
- }
- }
- /// <summary>
- /// Gets filter placeHolder from Unigrid.
- /// </summary>
- public override PlaceHolder FilterPlaceHolder
- {
- get
- {
- return filter;
- }
- }
- /// <summary>
- /// Gets page size dropdown from Unigrid Pager.
- /// </summary>
- public override DropDownList PageSizeDropdown
- {
- get
- {
- return Pager.PageSizeDropdown;
- }
- }
- /// <summary>
- /// Defines whether to show object menu (menu containing relationships, export/backup, destroy object, ... functionality)
- /// </summary>
- public bool ShowObjectMenu
- {
- get
- {
- return mShowObjectMenu;
- }
- set
- {
- mShowObjectMenu = value;
- }
- }
- /// <summary>
- /// Gets filter form.
- /// </summary>
- public override BasicForm FilterForm
- {
- get
- {
- return filterForm;
- }
- }
- #endregion
- #region "Page events"
- /// <summary>
- /// Control's init event handler.
- /// </summary>
- protected void Page_Init(object sender, EventArgs e)
- {
- advancedExportElem.UniGrid = this;
- }
- /// <summary>
- /// Control's load event handler.
- /// </summary>
- protected void Page_Load(object sender, EventArgs e)
- {
- if (StopProcessing)
- {
- Visible = false;
- FilterForm.StopProcessing = true;
- }
- else
- {
- SetPager();
- if (LoadGridDefinition())
- {
- // Do not load on callback
- if (!RequestHelper.IsCallback())
- {
- ActionsHidden = hidActions;
- ActionsHashHidden = hidActionsHash;
- // Check whether current request is row action command and if so, raise action
- if (!String.IsNullOrEmpty(hidCmdName.Value) && (Request.Form["__EVENTTARGET"] == UniqueID) && ((Request.Form["__EVENTARGUMENT"] == "UniGridAction")))
- {
- HandleAction(hidCmdName.Value, hidCmdArg.Value);
- }
- // Set order by clause
- ProcessSorting();
- // Set filter form
- if (!string.IsNullOrEmpty(FilterFormName))
- {
- SetBasicFormFilter();
- if (!EventRequest() && !DelayedReload)
- {
- ReloadData();
- }
- }
- // Get data from database and set them to the grid view
- else if (FilterByQueryString)
- {
- if (displayFilter)
- {
- SetFilter(true);
- }
- else
- {
- if (!EventRequest() && !DelayedReload)
- {
- ReloadData();
- }
- }
- }
- else
- {
- // Load the default filter value
- if (!RequestHelper.IsPostBack() && displayFilter)
- {
- SetFilter(true);
- }
- else if (!EventRequest() && !DelayedReload)
- {
- ReloadData();
- }
- }
- }
- }
- }
- // Clear hidden action on load event. If unigrid is invisible, page pre render is not fired
- ClearActions();
- }
- /// <summary>
- /// Control's PreRender event handler.
- /// </summary>
- protected override void OnPreRender(EventArgs e)
- {
- base.OnPreRender(e);
- if (FilterIsSet)
- {
- // Check for FilteredZeroRowsText
- if ((GridView.Rows.Count == 0) && !String.IsNullOrEmpty(FilteredZeroRowsText))
- {
- // Display filter zero rows text
- lblInfo.Text = FilteredZeroRowsText;
- lblInfo.Visible = true;
- pagerElem.Visible = false;
- }
- else
- {
- lblInfo.Visible = false;
- pagerElem.Visible = true;
- }
- }
- else
- {
- // Check for ZeroRowsText
- if (GridView.Rows.Count == 0)
- {
- if (!HideControlForZeroRows && !String.IsNullOrEmpty(ZeroRowsText))
- {
- // Display zero rows text
- lblInfo.Text = ZeroRowsText;
- lblInfo.Visible = true;
- pagerElem.Visible = false;
- // Check additional filter visibility
- CheckFilterVisibility();
- }
- else
- {
- lblInfo.Visible = false;
- pagerElem.Visible = false;
- filter.Visible = false;
- }
- }
- else
- {
- lblInfo.Visible = false;
- pagerElem.Visible = true;
- // Check additional filter visibility
- CheckFilterVisibility();
- }
- }
- if (Visible && !StopProcessing)
- {
- RegisterCmdScripts();
- }
- if (Pager.CurrentPage > halfPageCountLimit)
- {
- // Enlarge direct page textbox
- TextBox txtPage = ControlsHelper.GetChildControl(pagerElem, typeof(TextBox), "txtPage") as TextBox;
- if (txtPage != null)
- {
- txtPage.Style.Add(HtmlTextWriterStyle.Width, "50px");
- }
- }
- advancedExportElem.Visible = ShowActionsMenu;
- // Hide info label when error message is displayed
- lblInfo.Visible = lblInfo.Visible && string.IsNullOrEmpty(plcMess.ErrorText);
- }
- #endregion
- #region "Public methods"
- /// <summary>
- /// Clears UniGrid's information on recently performed action. Under normal circumstances there is no need to perform this action.
- /// However sometimes forcing grid to clear the actions is required.
- /// </summary>
- public void ClearActions()
- {
- // Clear hidden fields
- hidCmdName.Value = null;
- hidCmdArg.Value = null;
- }
- /// <summary>
- /// Clears all selected items from hidden values.
- /// </summary>
- public void ClearSelectedItems()
- {
- ClearHiddenValues(SelectionHiddenField);
- }
- /// <summary>
- /// Loads the XML configuration of the grid.
- /// </summary>
- public bool LoadXmlConfiguration()
- {
- // If no configuration is given, do not process
- if (string.IsNullOrEmpty(GridName))
- {
- return true;
- }
- string xmlFilePath = Server.MapPath(GridName);
- // Check the configuration file
- if (!File.Exists(xmlFilePath))
- {
- ShowError(String.Format(GetString("unigrid.noxmlfile"), xmlFilePath));
- return false;
- }
- // Load the XML configuration
- XmlDocument document = new XmlDocument();
- document.Load(xmlFilePath);
- XmlNode node = document.DocumentElement;
- if (node != null)
- {
- // Load options definition
- XmlNode optionNode = node.SelectSingleNode("options");
- if (optionNode != null)
- {
- GridOptions = new UniGridOptions(optionNode);
- }
- // Load actions definition
- XmlNode actionsNode = node.SelectSingleNode("actions");
- if (actionsNode != null)
- {
- GridActions = new UniGridActions(actionsNode);
- }
- // Load pager definition
- XmlNode pagerNode = node.SelectSingleNode("pager");
- if (pagerNode != null)
- {
- PagerConfig = new UniGridPagerConfig(pagerNode);
- }
- // Select list of "column" nodes
- XmlNode columnsNode = node.SelectSingleNode("columns");
- if (columnsNode != null)
- {
- GridColumns = new UniGridColumns(columnsNode);
- }
- // Try to get ObjectType from definition
- XmlNode objectTypeNode = node.SelectSingleNode("objecttype");
- if (objectTypeNode != null)
- {
- // Get object type information
- LoadObjectTypeDefinition(objectTypeNode);
- }
- else
- {
- // Get query information
- XmlNode queryNode = node.SelectSingleNode("query");
- LoadQueryDefinition(queryNode);
- }
- return true;
- }
- return false;
- }
- /// <summary>
- /// Loads the grid definition.
- /// </summary>
- public override bool LoadGridDefinition()
- {
- if (GridView.Columns.Count == 0)
- {
- using (Table filterTable = new Table())
- {
- filterTable.CssClass = "UniGridFilterTable";
- filter.Controls.Clear();
- // Clear all columns from the grid view
- UniGridView.Columns.Clear();
- UniGridView.GridLines = GridLines.Horizontal;
- if (!LoadXmlConfiguration())
- {
- return false;
- }
- // Load options
- if (GridOptions != null)
- {
- LoadOptionsDefinition(GridOptions, filterTable);
- }
- if (GridActions == null && ShowActionsMenu)
- {
- EmptyAction emptyAction = new EmptyAction();
- GridActions = new UniGridActions();
- GridActions.Actions.Add(emptyAction);
- }
- // Actions
- if (GridActions != null)
- {
- LoadActionsDefinition(GridActions);
- }
- // Load pager configuration
- if (PagerConfig != null)
- {
- LoadPagerDefinition(PagerConfig);
- }
- // Set direct page control id from viewstate
- if (ViewState["DirectPageControlID"] != null)
- {
- Pager.DirectPageControlID = ViewState["DirectPageControlID"].ToString();
- }
- // Raise load columns event
- RaiseLoadColumns();
- // Load columns
- if (GridColumns != null)
- {
- foreach (Column col in GridColumns.Columns)
- {
- // Load column definition
- LoadColumnDefinition(col, filterTable);
- }
- }
- if (displayFilter)
- {
- // Finish filter form with "Show" button
- CreateFilterButton(filterTable);
- }
- }
- }
- return true;
- }
- /// <summary>
- /// Reloads the grid data.
- /// </summary>
- public override void ReloadData()
- {
- try
- {
- // Ensure grid definition before realod data
- LoadGridDefinition();
- RestoreState();
- RaiseOnBeforeDataReload();
- rowIndex = 1;
- // Get Current TOP N
- if (CurrentPageSize > 0)
- {
- int currentPageIndex = Pager.CurrentPage;
- int pageSize = (CurrentPageSize > 0) ? CurrentPageSize : UniGridView.PageSize;
- CurrentTopN = pageSize * (currentPageIndex + Pager.CurrentPagesGroupSize);
- }
- if (CurrentTopN < TopN)
- {
- CurrentTopN = TopN;
- }
- // If first/last button and direct page contol in pager is hidden use current topN for better performance
- if (!Pager.ShowDirectPageControl && !Pager.ShowFirstLastButtons)
- {
- TopN = CurrentTopN;
- }
- // Retrieve data
- UniGridView.DataSource = RetrieveData();
- RaiseOnAfterDataReload();
- SetUnigridControls();
- // Check if datasource is loaded
- if (DataHelper.DataSourceIsEmpty(GridView.DataSource) && (pagerElem.CurrentPage > 1))
- {
- pagerElem.UniPager.CurrentPage = 1;
- ReloadData();
- }
- // Resolve the edit action URL
- if (!String.IsNullOrEmpty(EditActionUrl))
- {
- EditActionUrl = CMSContext.ResolveMacros(EditActionUrl);
- }
- SortColumns.Clear();
- UniGridView.DataBind();
- mRowsCount = DataHelper.GetItemsCount(UniGridView.DataSource);
- CheckFilterVisibility();
- }
- catch (Exception ex)
- {
- // Display tooltip only development mode is enabled
- string desc = null;
- if (SettingsKeyProvider.DevelopmentMode)
- {
- desc = ex.Message;
- }
- ShowError(GetString("unigrid.error.reload"), desc, null);
- // Log exception
- EventLogProvider ev = new EventLogProvider();
- ev.LogEvent("UniGrid", "RELOADDATA", ex.InnerException ?? ex);
- }
- }
- /// <summary>
- /// Gets a dataset with data based on UniGrid's settings.
- /// </summary>
- /// <returns>DataSet with data</returns>
- public override DataSet RetrieveData()
- {
- DataSet ds = null;
- // If datasource for unigrid is query (not dataset), then execute query
- if (!string.IsNullOrEmpty(Query))
- {
- // Reload the data with current parameters
- ds = ConnectionHelper.ExecuteQuery(Query, QueryParameters, CompleteWhereCondition, CurrentOrder, TopN, Columns, CurrentOffset, CurrentPageSize, ref pagerForceNumberOfResults);
- }
- // If UniGrid is in ObjectType mode, get the data according to given object type.
- else if (InfoObject != null)
- {
- // Get the result set
- ds = InfoObject.GetData(QueryParameters, CompleteWhereCondition, CurrentOrder, TopN, Columns, false, CurrentOffset, CurrentPageSize, ref pagerForceNumberOfResults);
- }
- // External dataset is used
- else
- {
- ds = RaiseDataReload();
- SortUniGridDataSource(ds);
- ds = DataHelper.TrimDataSetPage(ds, CurrentOffset, CurrentPageSize, ref pagerForceNumberOfResults);
- }
- // Add empty dataset
- if (ds == null)
- {
- ds = new DataSet();
- ds.Tables.Add();
- }
- // Raise event 'OnRetrieveData'
- ds = RaiseAfterRetrieveData(ds);
- return ds;
- }
- /// <summary>
- /// Returns where condition from unigrid filters.
- /// </summary>
- public override string GetFilter(bool isDataTable)
- {
- string where = string.Empty;
- // Count of the conditions in the 'where clause'
- int whereConditionCount = 0;
- // Process all filter fields
- foreach (UniGridFilterField filterField in FilterFields.Values)
- {
- string filterFormat = filterField.Format;
- // AND in 'where clause'
- string andExpression;
- Control mainControl = filterField.OptionsControl;
- Control valueControl = filterField.ValueControl;
- string filterPath = filterField.ControlPath;
- if (valueControl is CMSAbstractBaseFilterControl)
- {
- // Custom filters (loaded controls)
- CMSAbstractBaseFilterControl customFilter = (CMSAbstractBaseFilterControl)valueControl;
- string customWhere = customFilter.WhereCondition;
- if (!String.IsNullOrEmpty(customWhere))
- {
- andExpression = (whereConditionCount > 0) ? " AND " : "";
- where += andExpression + customWhere;
- whereConditionCount++;
- }
- // Prepare query string
- if (FilterByQueryString && RequestHelper.IsPostBack())
- {
- queryStringHashTable[customFilter.ID] = customFilter.Value;
- }
- }
- else if (mainControl is DropDownList)
- {
- // Dropdown list filter
- DropDownList ddlistControl = (DropDownList)mainControl;
- TextBox txtControl = (TextBox)valueControl;
- string textboxValue = txtControl.Text;
- string textboxID = txtControl.ID;
- // Empty field -> no filter is set for this field
- if (textboxValue != "")
- {
- string op = ddlistControl.SelectedValue;
- string value = textboxValue.Replace("\'", "''");
- string columnName = ddlistControl.ID.Trim().TrimStart('[').TrimEnd(']').Trim();
- // Format {0} = column name, {1} = operator, {2} = value, {3} = default condition
- string defaultFormat = null;
- if (textboxID.EndsWithCSafe("TextValue"))
- {
- switch (op.ToLowerCSafe())
- {
- // LIKE operators
- case "like":
- defaultFormat = isDataTable ? "[{0}] {1} '%{2}%'" : "[{0}] {1} N'%{2}%'";
- break;
- case "not like":
- defaultFormat = isDataTable ? "([{0}] is null or [{0}] {1} '%{2}%')" : "([{0}] is null or [{0}] {1} N'%{2}%')";
- break;
- case "<>":
- defaultFormat = isDataTable ? "([{0}] is null or [{0}] {1} '{2}')" : "([{0}] is null or [{0}] {1} N'{2}')";
- break;
- // Standard operators
- default:
- defaultFormat = isDataTable ? "[{0}] {1} '{2}'" : "[{0}] {1} N'{2}'";
- break;
- }
- }
- else // textboxID.EndsWithCSafe("NumberValue")
- {
- if (ValidationHelper.IsDouble(value) || ValidationHelper.IsInteger(value))
- {
- defaultFormat = "[{0}] {1} {2}";
- if (op == "<>")
- {
- defaultFormat = "([{0}] is null or [{0}] {1} {2})";
- }
- }
- }
- if (!String.IsNullOrEmpty(defaultFormat))
- {
- string defaultCondition = String.Format(defaultFormat, columnName, op, value);
- string condition = defaultCondition;
- if (filterFormat != null)
- {
- condition = String.Format(filterFormat, columnName, op, value, defaultCondition);
- }
- andExpression = (whereConditionCount > 0 ? " AND " : string.Empty);
- // ddlistControl.ID - column name
- // ddlistControl.SelectedValue - condition option
- // textboxSqlValue - condition value
- where += String.Format("{0}({1})", andExpression, condition);
- whereConditionCount++;
- }
- }
- // Prepare query string
- if (FilterByQueryString)
- {
- queryStringHashTable[ddlistControl.ID] = String.Format("{0};{1}", ddlistControl.SelectedValue, textboxValue);
- }
- }
- else if (valueControl is DropDownList)
- {
- // Checkbox filter
- DropDownList currentControl = (DropDownList)valueControl;
- string value = currentControl.SelectedValue;
- if (value != "")
- {
- string columnName = currentControl.ID;
- string condition = String.Format("{0} = {1}", columnName, value);
- if (filterFormat != null)
- {
- condition = String.Format(filterFormat, columnName, "=", value, condition);
- }
- andExpression = (whereConditionCount > 0 ? " AND " : string.Empty);
- where += String.Format("{0}({1})", andExpression, condition);
- whereConditionCount++;
- }
- // Prepare query string
- if (FilterByQueryString)
- {
- queryStringHashTable[currentControl.ID] = ";" + value;
- }
- }
- }
- return where;
- }
- /// <summary>
- /// Uncheck all checkboxes in selection column.
- /// </summary>
- public override void ResetSelection()
- {
- ResetSelection(true);
- }
- /// <summary>
- /// Uncheck all checkboxes in selection column.
- /// </summary>
- /// <param name="reset">Indicates if reset selection javascript should be registered</param>
- public void ResetSelection(bool reset)
- {
- SelectionHiddenField.Value = String.Empty;
- hidNewSelection.Value = String.Empty;
- hidDeSelection.Value = String.Empty;
- resetSelection = reset;
- }
- #endregion
- #region "UniGrid events"
- /// <summary>
- /// Process data from filter.
- /// </summary>
- protected void ShowButton_Click(object sender, EventArgs e)
- {
- ApplyFilter(sender, e);
- }
- protected void ResetButton_Click(object sender, EventArgs e)
- {
- Reset();
- }
- protected void pageSizeDropdown_SelectedIndexChanged(object sender, EventArgs e)
- {
- RaisePageSizeChanged();
- }
- protected void UniGridView_Sorting(object sender, EventArgs e)
- {
- RaiseBeforeSorting(sender, e);
- }
- /// <summary>
- /// After data bound event.
- /// </summary>
- protected void UniGridView_DataBound(object sender, EventArgs e)
- {
- // Set actions hash into hidden field
- SetActionsHash();
- SetPager();
- // Call page binding event
- if (OnPageBinding != null)
- {
- OnPageBinding(this, null);
- }
- }
- protected void UniGridView_RowCreating(object sender, GridViewRowEventArgs e)
- {
- // If row type is header
- if (e.Row.RowType == DataControlRowType.Header)
- {
- // Add sorting definition to list of sort columns
- SortColumns.Add(SortDirect.ToLowerCSafe());
- // Parse the sort expression
- string sort = SortDirect.ToLowerCSafe().Replace("[", "").Replace("]", "").Trim();
- if (sort.StartsWithCSafe("cast("))
- {
- sort = sort.Substring(5);
- }
- Match sortMatch = OrderByRegex.Match(sort);
- string sortColumn = null;
- string sortDirection = null;
- if (sortMatch.Success)
- {
- // Get column name
- if (sortMatch.Groups[1].Success)
- {
- sortColumn = sortMatch.Groups[1].Value;
- }
- // Get sort direction
- sortDirection = sortMatch.Groups[2].Success ? sortMatch.Groups[2].Value : "asc";
- }
- else
- {
- // Get column name from sort expression
- int space = sort.IndexOfAny(new char[] { ' ', ',' });
- sortColumn = space > -1 ? sort.Substring(0, space) : sort;
- sortDirection = "asc";
- }
- // Check if displaying arrow indicating sorting is requested
- if (showSortDirection)
- {
- // Prepare the columns
- foreach (TableCell Cell in e.Row.Cells)
- {
- // If there is some sorting expression
- DataControlFieldCell dataField = (DataControlFieldCell)Cell;
- string fieldSortExpression = dataField.ContainingField.SortExpression;
- if (!DataHelper.IsEmpty(fieldSortExpression))
- {
- SortColumns.Add(fieldSortExpression.ToLowerCSafe());
- // If actual sorting expressions is this cell
- if (CMSString.Equals(sortColumn, fieldSortExpression.Replace("[", "").Replace("]", "").Trim(), true))
- {
- // Initialize sort arrow
- Literal sortArrow = new Literal()
- {
- Text = String.Format("<span class=\"UniGridSort{0}\" > </span>", ((sortDirection == "desc") ? "Down" : "Up"))
- };
- if (DataHelper.IsEmpty(Cell.Text))
- {
- if ((Cell.Controls.Count != 0) && (Cell.Controls[0] != null))
- {
- // Add original text
- Cell.Controls[0].Controls.Add(new LiteralControl(String.Format("<span class=\"UniGridSortLabel\">{0}</span>", ((LinkButton)(Cell.Controls[0])).Text)));
- // Add one space before image
- Cell.Controls[0].Controls.Add(new LiteralControl(" "));
- Cell.Controls[0].Controls.Add(sortArrow);
- }
- else
- {
- // Add one space before image
- Cell.Controls.Add(new LiteralControl(" "));
- Cell.Controls.Add(sortArrow);
- }
- }
- }
- }
- }
- }
- }
- else if (e.Row.RowType == DataControlRowType.DataRow)
- {
- e.Row.CssClass = (rowIndex % 2 == 0) ? "OddRow" : "EvenRow";
- rowIndex++;
- }
- else if (e.Row.RowType == DataControlRowType.Footer)
- {
- e.Row.CssClass = "UniGridFooter";
- }
- else if (e.Row.RowType == DataControlRowType.Pager)
- {
- e.Row.CssClass = "UniGridPager";
- }
- }
- /// <summary>
- /// Handles the action event.
- /// </summary>
- /// <param name="cmdName">Command name</param>
- /// <param name="cmdValue">Command value</param>
- public void HandleAction(string cmdName, string cmdValue)
- {
- string action = cmdName.ToLowerCSafe();
- // Check action security and redirect if user not authorized
- CheckActionAndRedirect(action);
- GeneralizedInfo infoObj = null;
- int objectId = 0;
- switch (action)
- {
- case "#delete":
- case "#destroyobject":
- {
- // Delete the object
- objectId = ValidationHelper.GetInteger(cmdValue, 0);
- if (objectId > 0)
- {
- infoObj = CMSObjectHelper.GetReadOnlyObject(ObjectType);
- string objectType = infoObj.TypeInfo.Inherited ? infoObj.TypeInfo.OriginalObjectType : infoObj.ObjectType;
- infoObj = BaseAbstractInfoProvider.GetInfoById(objectType, objectId);
- if (infoObj != null)
- {
- switch (action)
- {
- case "#delete":
- // Check the dependencies
- AbstractProvider providerObj = infoObj.TypeInfo.ProviderObject;
- List<string> names = new List<string>();
- if (providerObj.CheckObjectDependencies(objectId, ref names))
- {
- string description = null;
- if (names.Count > 0)
- {
- // Encode and localize names
- StringBuilder sb = new StringBuilder();
- names.ForEach(item => sb.Append("<br />", HTMLHelper.HTMLEncode(ResHelper.LocalizeString(item))));
- description = GetString(objectType.Replace(".", "_") + ".objectlist|unigrid.objectlist") + sb.ToString();
- }
- ShowError(GetString("ecommerce.deletedisabledwithoutenable"), description, null);
- return;
- }
- // Delete the object
- infoObj.DeleteObject();
- break;
- case "#destroyobject":
- if (CMSContext.CurrentUser.IsAuthorizedPerObject(PermissionsEnum.Destroy, infoObj.ObjectType, CMSContext.CurrentSiteName))
- {
- using (CMSActionContext context = new CMSActionContext())
- {
- context.CreateVersion = false;
- Action ac = GridActions.GetAction("#delete");
- if (ac != null)
- {
- HandleAction("#delete", cmdValue);
- }
- else
- {
- ac = GridActions.GetAction("delete");
- if (ac != null)
- {
- RaiseAction("delete", cmdValue);
- }
- else
- {
- ShowError(GetString("objectversioning.destroyobject.nodeleteaction"));
- return;
- }
- }
- }
- }
- break;
- }
- }
- }
- }
- break;
- default:
- RaiseAction(cmdName, cmdValue);
- break;
- }
- }
- #endregion
- #region "Private methods"
- /// <summary>
- /// Sets unigrid controls.
- /// </summary>
- private void SetUnigridControls()
- {
- filter.Visible = displayFilter;
- // Indicates whether unigrid datasource is empty or not
- isEmpty = DataHelper.DataSourceIsEmpty(UniGridView.DataSource);
- if (isEmpty)
- {
- // Try to reload data for previous page if action was used and no data loaded (mostly delete)
- if (onActionUsed && Pager.CurrentPage > 1)
- {
- Pager.UniPager.CurrentPage = Pager.CurrentPage - 1;
- ReloadData();
- }
- else if (HideControlForZeroRows && (WhereClause == ""))
- {
- // Hide filter
- filter.Visible = false;
- }
- }
- else
- {
- // Disable GridView paging because UniGridPager will provide paging
- UniGridView.AllowPaging = false;
- // Process paging if pager is displayed
- if (Pager.DisplayPager)
- {
- if (CurrentPageSize > 0)
- {
- if (!visiblePagesSet)
- {
- Pager.VisiblePages = (((CurrentOffset / CurrentPageSize) + 1)) > halfPageCountLimit ? 5 : 10;
- }
- Pager.DirectPageControlID = ((float)PagerForceNumberOfResults / Pager.CurrentPageSize > 20.0f) ? "txtPage" : "drpPage";
- // Save direct page control id in viewstate
- ViewState["DirectPageControlID"] = Pager.DirectPageControlID;
- }
- }
- }
- }
- /// <summary>
- /// Load options definition.
- /// </summary>
- /// <param name="options">Options configuration</param>
- /// <param name="filterTable">Table for filter</param>
- private void LoadOptionsDefinition(UniGridOptions options, Table filterTable)
- {
- // Create filter table according to the key value "DisplayFilter"
- displayFilter = options.DisplayFilter;
- if (displayFilter)
- {
- filter.Controls.Add(filterTable);
- }
- // Filter limit
- if (options.FilterLimit > -1)
- {
- FilterLimit = options.FilterLimit;
- }
- // Display sort direction images
- showSortDirection = options.ShowSortDirection;
- // Display selection column with checkboxes
- showSelection = options.ShowSelection;
- if (showSelection)
- {
- TemplateField chkColumn = new TemplateField();
- using (CheckBox headerBox = new CheckBox { ID = "headerBox" })
- {
- using (CheckBox itemBox = new CheckBox { ID = "itemBox" })
- {
- // Set selection argument
- itemBox.Attributes["selectioncolumn"] = options.SelectionColumn;
- chkColumn.HeaderTemplate = new GridViewTemplate(ListItemType.Header, this, headerBox);
- chkColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, this, itemBox);
- }
- }
- UniGridView.Columns.Add(chkColumn);
- }
- // Get pagesize options
- if (!String.IsNullOrEmpty(options.PageSize))
- {
- Pager.PageSizeOptions = options.PageSize;
- }
- // Set pagging acording to the key value "DisplayPageSizeDropdown"
- if (options.DisplayPageSizeDropdown != null)
- {
- Pager.ShowPageSize = options.DisplayPageSizeDropdown.Value;
- }
- }
- /// <summary>
- /// Loads actions definition.
- /// </summary>
- /// <param name="actions">Configuration of the actions</param>
- private void LoadActionsDefinition(UniGridActions actions)
- {
- // Custom template field of the grid view
- TemplateField actionsColumn = new TemplateField();
- // Ensure width of the column
- if (!String.IsNullOrEmpty(actions.Width))
- {
- actionsColumn.ItemStyle.Width = new Unit(actions.Width);
- }
- // Add object menu if possible
- if ((actions.Actions.Count > 0 && !(actions.Actions.FirstOrDefault() is EmptyAction)) && ShowObjectMenu && UniGridFunctions.ShowUniGridObjectContextMenu(CMSObjectHelper.GetReadOnlyObject(ObjectType)))
- {
- actions.Actions.RemoveAll(a => a is EmptyAction);
- // Check if object menu already contained
- var menus = from action in actions.Actions.OfType<Action>()
- where (action.Name.ToLowerCSafe() == "#objectmenu") || (!String.IsNullOrEmpty(action.ContextMenu))
- select action;
- // Add object menu of necessary
- if ((menus.Count() == 0) && !IsLiveSite)
- {
- Action action = new Action("#objectmenu");
- action.ExternalSourceName = "#objectmenu";
- actions.Actions.Add(action);
- }
- }
- // Show header?
- if (actions.ShowHeader)
- {
- if (ShowActionsMenu && string.IsNullOrEmpty(actions.ContextMenu))
- {
- actions.ContextMenu = DEFAULT_ACTIONS_MENU;
- actions.Caption = "General.OtherActions";
- }
- // Fill in the custom template field
- string label = (ShowActionsLabel ? GetString("unigrid.actions") : "");
- GridViewTemplate headerTemplate = new GridViewTemplate(ListItemType.Header, this, actions, label, ImageDirectoryPath, DefaultImageDirectoryPath, Page);
- headerTemplate.ContextMenuParent = plcContextMenu;
- headerTemplate.CheckRelative = CheckRelative;
- actionsColumn.HeaderTemplate = headerTemplate;
- if (ShowActionsMenu)
- {
- if (actions.Actions.FirstOrDefault() is EmptyAction)
- {
- actionsColumn.HeaderStyle.CssClass = "EmptyAC";
- }
- else
- {
- actionsColumn.HeaderStyle.CssClass = "AC";
- }
- }
- }
- GridViewTemplate actionsTemplate = new GridViewTemplate(ListItemType.Item, this, actions, null, ImageDirectoryPath, DefaultImageDirectoryPath, Page);
- actionsTemplate.OnExternalDataBound += RaiseExternalDataBound;
- actionsTemplate.ContextMenuParent = plcContextMenu;
- actionsTemplate.CheckRelative = CheckRelative;
- actionsColumn.ItemTemplate = actionsTemplate;
- if (!IsLiveSite)
- {
- actionsColumn.ItemStyle.CssClass = "NW UniGridActions";
- }
- else
- {
- actionsColumn.ItemStyle.CssClass = "UniGridActions";
- actionsColumn.ItemStyle.Wrap = false;
- }
- // CSS class name
- string cssClass = actions.CssClass;
- if (cssClass != null)
- {
- actionsColumn.HeaderStyle.CssClass += " " + cssClass;
- actionsColumn.ItemStyle.CssClass += " " + cssClass;
- actionsColumn.FooterStyle.CssClass += " " + cssClass;
- }
- // Add custom column to grid view
- UniGridView.Columns.Add(actionsColumn);
- }
- /// <summary>
- /// Load unigrid pager configuration.
- /// </summary>
- /// <param name="config">Pager configuration</param>
- private void LoadPagerDefinition(UniGridPagerConfig config)
- {
- if (config.DisplayPager != null)
- {
- Pager.DisplayPager = config.DisplayPager.Value;
- }
- // Load definition only if pager is displayed
- if (Pager.DisplayPager)
- {
- if (config.PageSizeOptions != null)
- {
- Pager.PageSizeOptions = config.PageSizeOptions;
- }
- if (config.ShowDirectPageControl != null)
- {
- Pager.ShowDirectPageControl = config.ShowDirectPageControl.Value;
- }
- if (config.ShowFirstLastButtons != null)
- {
- Pager.ShowFirstLastButtons = config.ShowFirstLastButtons.Value;
- }
- if (config.ShowPageSize != null)
- {
- Pager.ShowPageSize = config.ShowPageSize.Value;
- }
- if (config.ShowPreviousNextButtons != null)
- {
- Pager.ShowPreviousNextButtons = config.ShowPreviousNextButtons.Value;
- }
- if (config.ShowPreviousNextPageGroup != null)
- {
- Pager.ShowPreviousNextPageGroup = config.ShowPreviousNextPageGroup.Value;
- }
- if (config.VisiblePages > 0)
- {
- Pager.VisiblePages = config.VisiblePages;
- visiblePagesSet = true;
- }
- if (config.DefaultPageSize > 0)
- {
- Pager.DefaultPageSize = config.DefaultPageSize;
- }
- // Try to get page size from request
- string selectedPageSize = Request.Form[Pager.PageSizeDropdown.UniqueID];
- int pageSize = 0;
- if (selectedPageSize != null)
- {
- pageSize = ValidationHelper.GetInteger(selectedPageSize, 0);
- }
- else if (config.DefaultPageSize > 0)
- {
- pageSize = config.DefaultPageSize;
- }
- if ((pageSize > 0) || (pageSize == -1))
- {
- Pager.CurrentPageSize = pageSize;
- }
- }
- else
- {
- // Reset page size
- Pager.CurrentPageSize = -1;
- }
- }
- /// <summary>
- /// Load single column definition.
- /// </summary>
- /// <param name="column">Column to use</param>
- /// <param name="filterTable">Table for filter</param>
- private void LoadColumnDefinition(Column column, Table filterTable)
- {
- DataControlField field = null;
- string cssClass = column.CssClass;
- string columnCaption = null;
- // Process the column type Hyperlink or BoundColumn based on the parameters
- if ((column.HRef != null) ||
- (column.ExternalSourceName != null) ||
- (column.Localize) ||
- (column.Icon != null) ||
- (column.Tooltip != null) ||
- (column.Action != null) ||
- (column.Style != null) ||
- (column.MaxLength > 0))
- {
- ExtendedBoundField linkColumn = new ExtendedBoundField();
- field = linkColumn;
- // Attribute "source"
- if (column.Source != null)
- {
- linkColumn.DataField = column.Source;
- if (column.AllowSorting)
- {
- if (!String.IsNullOrEmpty(column.Sort))
- {
- linkColumn.SortExpression = column.Sort;
- }
- else if (column.Source.ToLowerCSafe() != ExtendedBoundField.ALL_DATA.ToLowerCSafe())
- {
- linkColumn.SortExpression = column.Source;
- }
- }
- }
- // Action parameters
- if (column.Action != null)
- {
- linkColumn.Action = column.Action;
- // Action parameters
- if (column.CommandArgument != null)
- {
- linkColumn.CommandArgument = column.CommandArgument;
- }
- }
- // Action parameters
- if (column.Parameters != null)
- {
- linkColumn.ActionParameters = column.Parameters;
- }
- // Navigate URL
- if (column.HRef != null)
- {
- linkColumn.NavigateUrl = column.HRef;
- }
- // External source
- if (column.ExternalSourceName != null)
- {
- linkColumn.ExternalSourceName = column.ExternalSourceName;
- linkColumn.OnExternalDataBound += RaiseExternalDataBound;
- }
- // Localize strings?
- linkColumn.LocalizeStrings = column.Localize;
- // Style
- if (column.Style != null)
- {
- linkColumn.Style = column.Style;
- }
- // Class name
- if (cssClass != null)
- {
- linkColumn.HeaderStyle.CssClass += " " + cssClass;
- linkColumn.ItemStyle.CssClass += " " + cssClass;
- linkColumn.FooterStyle.CssClass += " " + cssClass;
- }
- // Icon
- if (column.Icon != null)
- {
- if (linkColumn.DataField == "")
- {
- linkColumn.DataField = ExtendedBoundField.ALL_DATA;
- }
- linkColumn.Icon = GetActionImage(column.Icon);
- }
- // Max length
- if (column.MaxLength > 0)
- {
- linkColumn.MaxLength = column.MaxLength;
- }
- // Process "tooltip" node
- ColumnTooltip tooltip = column.Tooltip;
- if (tooltip != null)
- {
- // If there is some tooltip register TooltipScript
- if ((tooltip.Source != null) || (tooltip.ExternalSourceName != null))
- {
- ScriptHelper.RegisterTooltip(Page);
- }
- // Tooltip source
- if (tooltip.Source != null)
- {
- linkColumn.TooltipSourceName = tooltip.Source;
- }
- // Tooltip external source
- if (tooltip.ExternalSourceName != null)
- {
- linkColumn.TooltipExternalSourceName = tooltip.ExternalSourceName;
- // Ensure external data bound event handler
- if (string.IsNullOrEmpty(column.ExternalSourceName))
- {
- linkColumn.OnExternalDataBound += RaiseExternalDataBound;
- }
- }
- // Tooltip width
- if (tooltip.Width != null)
- {
- linkColumn.TooltipWidth = tooltip.Width;
- }
- // Encode tooltip
- linkColumn.TooltipEncode = tooltip.Encode;
- }
- }
- else
- {
- BoundField userColumn = new BoundField(); // Custom column of the grid view
- field = userColumn;
- // Attribute "source"
- if (column.Source != null)
- {
- userColumn.DataField = column.Source;
- // Allow sorting
- if (column.AllowSorting)
- {
- if (column.Source.ToLowerCSafe() != ExtendedBoundField.ALL_DATA.…
Large files files are truncated, but you can click here to view the full file