using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.Controls; using CMS.ExtendedControls; using CMS.GlobalHelper; using CMS.PortalControls; using CMS.SettingsProvider; public partial class CMSWebParts_Viewers_Basic_BasicUniView : CMSAbstractWebPart { #region "Variables" // Base datasource instance private CMSBaseDataSource mDataSourceControl = null; // Indicates whether control was binded private bool binded = false; // BasicRepeter instance private BasicUniView basicUniView = new BasicUniView(); // Indicates whether current control was added to the filter collection private bool mFilterControlAdded = false; #endregion #region "Public properties" /// /// Gets or sets name of source. /// public string DataSourceName { get { return ValidationHelper.GetString(GetValue("DataSourceName"), String.Empty); } set { SetValue("DataSourceName", value); } } /// /// Control with data source. /// public CMSBaseDataSource DataSourceControl { get { // Check if control is empty and load it with the data if (mDataSourceControl == null) { if (!String.IsNullOrEmpty(DataSourceName)) { mDataSourceControl = CMSControlsHelper.GetFilter(DataSourceName) as CMSBaseDataSource; } } return mDataSourceControl; } set { mDataSourceControl = value; } } /// /// Gets or sets FirstItemTransformationName property. /// public string FirstItemTransformationName { get { return ValidationHelper.GetString(GetValue("FirstItemTransformationName"), ""); } set { SetValue("FirstItemTransformationName", value); } } /// /// Gets or sets LastItemTransformationName property. /// public string LastItemTransformationName { get { return ValidationHelper.GetString(GetValue("LastItemTransformationName"), ""); } set { SetValue("LastItemTransformationName", value); } } /// /// Gets or sets AlternatingItemTemplate property. /// public string AlternatingItemTransformationName { get { return ValidationHelper.GetString(GetValue("AlternatingItemTransformationName"), ""); } set { SetValue("AlternatingItemTransformationName", value); } } /// /// Gets or sets FooterTemplate property. /// public string FooterTransformationName { get { return ValidationHelper.GetString(GetValue("FooterTransformationName"), ""); } set { SetValue("FooterTransformationName", value); } } /// /// Gets or sets HeaderTemplate property. /// public string HeaderTransformationName { get { return ValidationHelper.GetString(GetValue("HeaderTransformationName"), ""); } set { SetValue("HeaderTransformationName", value); } } /// /// Gets or sets ItemTemplate property. /// public string TransformationName { get { return ValidationHelper.GetString(GetValue("TransformationName"), ""); } set { SetValue("TransformationName", value); } } /// /// Gets or sets SeparatorTemplate property. /// public string SeparatorTransformationName { get { return ValidationHelper.GetString(GetValue("SeparatorTransformationName"), ""); } set { SetValue("SeparatorTransformationName", value); } } /// /// Gets or sets HideControlForZeroRows property. /// public bool HideControlForZeroRows { get { return ValidationHelper.GetBoolean(GetValue("HideControlForZeroRows"), true); } set { SetValue("HideControlForZeroRows", value); } } /// /// Gets or sets ZeroRowsText property. /// public string ZeroRowsText { get { return ValidationHelper.GetString(GetValue("ZeroRowsText"), ""); } set { SetValue("ZeroRowsText", value); } } /// /// Gets or sets FooterTemplate for selected item. /// public string SelectedItemFooterTransformationName { get { return ValidationHelper.GetString(GetValue("SelectedItemFooterTransformationName"), ""); } set { SetValue("SelectedItemFooterTransformationName", value); } } /// /// Gets or sets HeaderTemplate for selected item. /// public string SelectedItemHeaderTransformationName { get { return ValidationHelper.GetString(GetValue("SelectedItemHeaderTransformationName"), ""); } set { SetValue("SelectedItemHeaderTransformationName", value); } } /// /// Gets or sets ItemTemplate for selected item. /// public string SelectedItemTransformationName { get { return ValidationHelper.GetString(GetValue("SelectedItemTransformationName"), ""); } set { SetValue("SelectedItemTransformationName", value); } } /// /// Gets or sets ItemTemplate for single item. /// public string SingleItemTransformationName { get { return ValidationHelper.GetString(GetValue("SingleItemTransformationName"), String.Empty); } set { SetValue("SingleItemTransformationName", value); } } /// /// Gets or sets the value that indicates whether header and footer items should be hidden if single item is displayed. /// public bool HideHeaderAndFooterForSingleItem { get { return ValidationHelper.GetBoolean(GetValue("HideHeaderAndFooterForSingleItem"), basicUniView.HideHeaderAndFooterForSingleItem); } set { SetValue("HideHeaderAndFooterForSingleItem", value); basicUniView.HideHeaderAndFooterForSingleItem = value; } } #endregion #region "Methods" /// /// On content loaded override. /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Initializes the control properties. /// protected void SetupControl() { if (StopProcessing) { // Do nothing } else { // Set properties if (!String.IsNullOrEmpty(ZeroRowsText)) { basicUniView.ZeroRowsText = ZeroRowsText; } basicUniView.HideControlForZeroRows = HideControlForZeroRows; basicUniView.HideHeaderAndFooterForSingleItem = HideHeaderAndFooterForSingleItem; basicUniView.DataBindByDefault = false; basicUniView.OnPageChanged += new EventHandler(BasicRepeater_OnPageChanged); EnsureFilterControl(); } } /// /// Ensures current control in the filters collection. /// protected void EnsureFilterControl() { if (!mFilterControlAdded) { // Add basic repeater to the filter collection CMSControlsHelper.SetFilter(ValidationHelper.GetString(GetValue("WebPartControlID"), ClientID), basicUniView); mFilterControlAdded = true; } } /// /// OnPageChaged event handler. /// /// Sender /// EventArg private void BasicRepeater_OnPageChanged(object sender, EventArgs e) { EnsureChildControls(); // Reload data if (DataSourceControl != null) { basicUniView.DataSource = DataSourceControl.DataSource; LoadTransformations(); basicUniView.DataBind(); binded = true; } } protected override void CreateChildControls() { // Add control to the control collection plcBasicUniView.Controls.Add(basicUniView); base.CreateChildControls(); } /// /// Loads and setups web part. /// protected override void OnLoad(EventArgs e) { EnsureChildControls(); // Check whether postback was executed from current transformation item if (RequestHelper.IsPostBack()) { // Indicates whether postback was fired from current control bool bindControl = false; // Check event target value and callback parameter value string eventTarget = ValidationHelper.GetString(Request.Form["__EVENTTARGET"], String.Empty); string callbackParam = ValidationHelper.GetString(Request.Form["__CALLBACKPARAM"], String.Empty); if (eventTarget.StartsWithCSafe(UniqueID) || callbackParam.StartsWithCSafe(UniqueID) || eventTarget.EndsWithCSafe(ContextMenu.CONTEXT_MENU_SUFFIX)) { bindControl = true; } // Check whether request key contains some control assigned to current control else { foreach (string key in Request.Form.Keys) { if ((key != null) && key.StartsWithCSafe(UniqueID)) { bindControl = true; break; } } } if (bindControl) { // Reload data if (DataSourceControl != null) { basicUniView.DataSource = DataSourceControl.DataSource; LoadTransformations(); basicUniView.DataBind(); binded = true; } } } base.OnLoad(e); } protected override void OnInit(EventArgs e) { //Handle filter change event if (DataSourceControl != null) { DataSourceControl.OnFilterChanged += new ActionEventHandler(DataSourceControl_OnFilterChanged); } base.OnInit(e); } /// /// Load transformations with dependence on datasource type and datasource state. /// protected void LoadTransformations() { CMSBaseDataSource docDataSource = DataSourceControl as CMSBaseDataSource; if ((docDataSource != null) && (docDataSource.IsSelected) && (!String.IsNullOrEmpty(SelectedItemTransformationName))) { basicUniView.ItemTemplate = CMSDataProperties.LoadTransformation(this, SelectedItemTransformationName, false); if (!String.IsNullOrEmpty(SelectedItemFooterTransformationName)) { basicUniView.FooterTemplate = CMSDataProperties.LoadTransformation(this, SelectedItemFooterTransformationName, false); } else { basicUniView.FooterTemplate = null; } if (!String.IsNullOrEmpty(SelectedItemHeaderTransformationName)) { basicUniView.HeaderTemplate = CMSDataProperties.LoadTransformation(this, SelectedItemHeaderTransformationName, false); } else { basicUniView.HeaderTemplate = null; } } else { // Apply transformations if they exist if (!String.IsNullOrEmpty(TransformationName)) { basicUniView.ItemTemplate = CMSDataProperties.LoadTransformation(this, TransformationName, false); } if (!String.IsNullOrEmpty(AlternatingItemTransformationName)) { basicUniView.AlternatingItemTemplate = CMSDataProperties.LoadTransformation(this, AlternatingItemTransformationName, false); } if (!String.IsNullOrEmpty(FooterTransformationName)) { basicUniView.FooterTemplate = CMSDataProperties.LoadTransformation(this, FooterTransformationName, false); } if (!String.IsNullOrEmpty(HeaderTransformationName)) { basicUniView.HeaderTemplate = CMSDataProperties.LoadTransformation(this, HeaderTransformationName, false); } if (!String.IsNullOrEmpty(SeparatorTransformationName)) { basicUniView.SeparatorTemplate = CMSDataProperties.LoadTransformation(this, SeparatorTransformationName, false); } if (!String.IsNullOrEmpty(FirstItemTransformationName)) { basicUniView.FirstItemTemplate = CMSDataProperties.LoadTransformation(this, FirstItemTransformationName, false); } if (!String.IsNullOrEmpty(LastItemTransformationName)) { basicUniView.LastItemTemplate = CMSDataProperties.LoadTransformation(this, LastItemTransformationName, false); } if (!String.IsNullOrEmpty(SingleItemTransformationName)) { basicUniView.SingleItemTemplate = CMSDataProperties.LoadTransformation(this, SingleItemTransformationName, false); } } } /// /// OnFilter change event handler. /// private void DataSourceControl_OnFilterChanged() { EnsureChildControls(); // Set forcibly visibility Visible = true; // Reload data if (DataSourceControl != null) { basicUniView.DataSource = DataSourceControl.DataSource; LoadTransformations(); basicUniView.DataBind(); binded = true; } } /// /// OnPreRender override. /// protected override void OnPreRender(EventArgs e) { // Datasource data object ds = null; // Set transformations if data source is not empty if (DataSourceControl != null) { // Get data from datasource ds = DataSourceControl.DataSource; // Check whether data exist if ((!DataHelper.DataSourceIsEmpty(ds)) && (!binded)) { // Initilaize related data if provided if (DataSourceControl.RelatedData != null) { RelatedData = DataSourceControl.RelatedData; } basicUniView.DataSource = DataSourceControl.DataSource; LoadTransformations(); basicUniView.DataBind(); } } base.OnPreRender(e); // Hide control for zero rows if (((DataSourceControl == null) || (DataHelper.DataSourceIsEmpty(ds))) && (HideControlForZeroRows)) { Visible = false; } } /// /// Reloads data. /// public override void ReloadData() { SetupControl(); EnsureFilterControl(); base.ReloadData(); } #endregion; }