File: D:/HostingSpaces/TDijk1/erp-apps.eu/wwwroot/CMSWebParts/Pux/Documents/PuxInfiniteRepeater.ascx.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using CMS.Helpers;
using CMS.PortalControls;
using CMS.DocumentEngine;
using CMS.Controls;
using System.Data;
using CMS.DataEngine;
using Newtonsoft.Json;
using CMS.MacroEngine;
public partial class CMSWebParts_Pux_Ferdus_Product_PuxInfiniteRepeater : CMSAbstractWebPart
{
#region "Properties"
public string PageType
{
get
{
return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("PageType"), "CMS.MenuItem"), "CMS.MenuItem");
}
set
{
SetValue("PageType", value);
}
}
public string Path
{
get
{
return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("Path"), "/%"), "/%");
}
set
{
SetValue("Path", value);
}
}
public int NestingLevel
{
get
{
return ValidationHelper.GetInteger(GetValue("NestingLevel"), -1);
}
set
{
SetValue("NestingLevel", value);
}
}
public string Transformation
{
get
{
return ValidationHelper.GetString(GetValue("Transformation"), "");
}
set
{
SetValue("Transformation", value);
}
}
public int TopN
{
get
{
return ValidationHelper.GetInteger(GetValue("TopN"), 0);
}
set
{
SetValue("TopN", value);
}
}
public string Columns
{
get
{
return ValidationHelper.GetString(GetValue("Columns"), "");
}
set
{
SetValue("Columns", value);
}
}
public string Where
{
get
{
return ValidationHelper.GetString(GetValue("Where"), "");
}
set
{
SetValue("Where", value);
}
}
public string OrderBy
{
get
{
return ValidationHelper.GetString(GetValue("OrderBy"), "");
}
set
{
SetValue("OrderBy", value);
}
}
public int PageSize
{
get
{
return ValidationHelper.GetInteger(GetValue("PageSize"), -1);
}
set
{
SetValue("PageSize", value);
}
}
public string FilterName
{
get
{
return ValidationHelper.GetString(GetValue("FilterName"), "");
}
set
{
SetValue("FilterName", value);
}
}
public string DataSourceName
{
get
{
return ValidationHelper.GetString(GetValue("DataSourceName"), String.Empty);
}
set
{
SetValue("DataSourceName", value);
}
}
public string SiteName
{
get
{
string _siteName = ValidationHelper.GetString(GetValue("SiteName"), String.Empty);
if (String.IsNullOrEmpty(_siteName))
{
_siteName = CurrentSite.SiteName;
}
return _siteName;
}
set
{
SetValue("SiteName", value);
}
}
public bool ShowResultCount
{
get
{
return ValidationHelper.GetBoolean(GetValue("ShowResultCount"), true);
}
set
{
SetValue("ShowResultCount", value);
}
}
public string ShowResultCountText
{
get
{
return ValidationHelper.GetString(GetValue("ShowResultCountText"), "");
}
set
{
SetValue("ShowResultCountText", value);
}
}
public string CustomLoadMoreText
{
get
{
return ValidationHelper.GetString(GetValue("CustomLoadMoreText"), "Pux.MoreBtn");
}
set
{
SetValue("CustomLoadMoreText", value);
}
}
public string NoDataText
{
get
{
return ValidationHelper.GetString(GetValue("NoDataText"), "Pux.InfiniteRepeater.NoData");
}
set
{
SetValue("NoDataText", value);
}
}
public bool LoadMoreOnScrollToBottom
{
get
{
return ValidationHelper.GetBoolean(GetValue("LoadMoreOnScrollToBottom"), false);
}
set
{
SetValue("LoadMoreOnScrollToBottom", value);
}
}
public string OnAfterRequest
{
get
{
return ValidationHelper.GetString(GetValue("OnAfterRequest"), String.Empty);
}
set
{
SetValue("OnAfterRequest", value);
}
}
/// <summary>
/// Name of configuration variable
/// </summary>
public string ConfigurationName
{
get
{
return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("ConfigurationName"), String.Empty), "infiniteRepeaterConfig");
}
set
{
SetValue("ConfigurationName", value);
}
}
public bool EnableCache
{
get
{
return ValidationHelper.GetBoolean(GetValue("EnableCache"), true);
}
set
{
SetValue("EnableCache", value);
}
}
public int MaxFirstLoadPages
{
get
{
return ValidationHelper.GetInteger(GetValue("MaxFirstLoadPages"), 10);
}
set
{
SetValue("MaxFirstLoadPages", value);
}
}
public bool CombineWithDefaultCulture
{
get
{
return ValidationHelper.GetBoolean(GetValue("CombineWithDefaultCulture"), true);
}
set
{
SetValue("CombineWithDefaultCulture", value);
}
}
public bool GoBackOutOfList
{
get
{
return ValidationHelper.GetBoolean(GetValue("GoBackOutOfList"), false);
}
set
{
SetValue("GoBackOutOfList", value);
}
}
public bool UseMasonry
{
get
{
return ValidationHelper.GetBoolean(GetValue("UseMasonry"), false);
}
set
{
SetValue("UseMasonry", value);
}
}
public bool FilterOutDuplicates
{
get
{
return ValidationHelper.GetBoolean(GetValue("FilterOutDuplicates"), false);
}
set
{
SetValue("FilterOutDuplicates", value);
}
}
/// <summary>
/// Automatically call javascript initialization function after app.ready
/// </summary>
public bool AutoInitialize
{
get
{
return ValidationHelper.GetBoolean(GetValue("AutoInitialize"), false);
}
set
{
SetValue("AutoInitialize", value);
}
}
public bool SelectOnlyPublished
{
get
{
return ValidationHelper.GetBoolean(GetValue("SelectOnlyPublished"), true);
}
set
{
SetValue("SelectOnlyPublished", value);
}
}
public bool DelayedLoading
{
get
{
return ValidationHelper.GetBoolean(GetValue("DelayedLoading"), false);
}
set
{
SetValue("DelayedLoading", value);
}
}
public bool ShowDebugLabels
{
get
{
return ValidationHelper.GetBoolean(GetValue("ShowDebugLabels"), false);
}
set
{
SetValue("ShowDebugLabels", value);
}
}
public bool NoCachePages
{
get
{
return ValidationHelper.GetBoolean(GetValue("NoCachePages"), false);
}
set
{
SetValue("NoCachePages", value);
}
}
public bool UseCanonical
{
get
{
return ValidationHelper.GetBoolean(GetValue("UseCanonical"), false);
}
set
{
SetValue("UseCanonical", value);
}
}
public bool LoadPagesIndividually
{
get
{
return ValidationHelper.GetBoolean(GetValue("LoadPagesIndividually"), false);
}
set
{
SetValue("LoadPagesIndividually", value);
}
}
public bool ShowPager
{
get
{
return ValidationHelper.GetBoolean(GetValue("ShowPager"), false);
}
set
{
SetValue("ShowPager", value);
}
}
public bool ShowTopPager
{
get
{
return ValidationHelper.GetBoolean(GetValue("ShowTopPager"), false);
}
set
{
SetValue("ShowTopPager", value);
}
}
public int PagerMaxNumbers
{
get
{
return ValidationHelper.GetInteger(GetValue("PagerMaxNumbers"), 7);
}
set
{
SetValue("PagerMaxNumbers", value);
}
}
public string HeaderHelper
{
get
{
return ValidationHelper.GetString(GetValue("HeaderHelper"), "");
}
set
{
SetValue("HeaderHelper", value);
}
}
#endregion
#region "UniPager Template properties"
/// <summary>
/// Gets or sets the pages template.
/// </summary>
public string PagesTemplate
{
get
{
return ValidationHelper.GetString(GetValue("Pages"), "CMS.PagerTransformations.General-Pages");
}
set
{
SetValue("Pages", value);
}
}
/// <summary>
/// Gets or sets the current page template.
/// </summary>
public string CurrentPageTemplate
{
get
{
return ValidationHelper.GetString(GetValue("CurrentPage"), "CMS.PagerTransformations.General-CurrentPage");
}
set
{
SetValue("CurrentPage", value);
}
}
/// <summary>
/// Gets or sets the separator template.
/// </summary>
public string SeparatorTemplate
{
get
{
return ValidationHelper.GetString(GetValue("PageSeparator"), "");
}
set
{
SetValue("PageSeparator", value);
}
}
/// <summary>
/// Gets or sets the first page template.
/// </summary>
public string FirstPageTemplate
{
get
{
return ValidationHelper.GetString(GetValue("FirstPage"), "CMS.PagerTransformations.General-FirstPage");
}
set
{
SetValue("FirstPage", value);
}
}
/// <summary>
/// Gets or sets the last page template.
/// </summary>
public string LastPageTemplate
{
get
{
return ValidationHelper.GetString(GetValue("LastPage"), "CMS.PagerTransformations.General-LastPage");
}
set
{
SetValue("LastPage", value);
}
}
/// <summary>
/// Gets or sets the previous page template.
/// </summary>
public string PreviousPageTemplate
{
get
{
return ValidationHelper.GetString(GetValue("PreviousPage"), "CMS.PagerTransformations.General-PreviousPage");
}
set
{
SetValue("PreviousPage", value);
}
}
/// <summary>
/// Gets or sets the next page template.
/// </summary>
public string NextPageTemplate
{
get
{
return ValidationHelper.GetString(GetValue("NextPage"), "CMS.PagerTransformations.General-NextPage");
}
set
{
SetValue("NextPage", value);
}
}
/// <summary>
/// Gets or sets the previous group template.
/// </summary>
public string PreviousGroupTemplate
{
get
{
return ValidationHelper.GetString(GetValue("PreviousGroup"), "CMS.PagerTransformations.General-PreviousGroup");
}
set
{
SetValue("PreviousGroup", value);
}
}
/// <summary>
/// Gets or sets the next group template.
/// </summary>
public string NextGroupTemplate
{
get
{
return ValidationHelper.GetString(GetValue("NextGroup"), "CMS.PagerTransformations.General-NextGroup");
}
set
{
SetValue("NextGroup", value);
}
}
/// <summary>
/// Gets or sets the layout template.
/// </summary>
public string LayoutTemplate
{
get
{
return ValidationHelper.GetString(GetValue("PagerLayout"), "CMS.PagerTransformations.Simple-PagerLayout");
}
set
{
SetValue("PagerLayout", value);
}
}
/// <summary>
/// Gets or sets the direct page template.
/// </summary>
public string DirectPageTemplate
{
get
{
return ValidationHelper.GetString(GetValue("DirectPage"), "");
}
set
{
SetValue("DirectPage", value);
}
}
#endregion
public int ResultsCount { get; set; }
// get dictionary from query array (splitted by ';')
private Dictionary<string, string> _filterQueryDict = null;
public Dictionary<string, string> FilterQueryDict
{
get
{
if (_filterQueryDict == null)
{
string filterQueryStr = SqlHelper.EscapeQuotes(QueryHelper.GetString("filter", String.Empty));
if (!String.IsNullOrEmpty(filterQueryStr))
{
string[] filterArr = filterQueryStr.Split(';');
_filterQueryDict = new Dictionary<string, string>();
foreach (string s in filterArr)
{
if (s.Contains(":"))
{
string[] sKeyValue = s.Split(':');
_filterQueryDict.Add(sKeyValue[0], sKeyValue[1]);
}
}
}
}
return _filterQueryDict;
}
}
// where conditions in dictionary loaded from webpart settings
private Dictionary<string, string> _whereDict = null;
public Dictionary<string, string> WhereDict
{
get
{
if (_whereDict == null)
{
_whereDict = new Dictionary<string, string>();
if (Where != null)
{
foreach (string i in Where.Split('\n'))
{
if (i.Contains(";"))
{
string[] j = i.Split(';');
_whereDict.Add(j[0], j[1]);
}
}
}
}
return _whereDict;
}
}
private string _resultWhereCondition = "init";
public string ResultWhereCondition
{
get
{
if (_resultWhereCondition == "init")
{
if (!Where.Contains(';'))
{
_resultWhereCondition = Where;
}
else
{
_resultWhereCondition = String.Empty;
foreach (KeyValuePair<string, string> d in WhereDict)
{
// there are two possibilities - filter has its own query string or can be as a part of "filter" filter query string array
string queryStringValue = SqlHelper.EscapeQuotes(QueryHelper.GetString(d.Key, String.Empty));
if (!String.IsNullOrEmpty(queryStringValue))
{
_resultWhereCondition = SqlHelper.AddWhereCondition(_resultWhereCondition, String.Format(d.Value, queryStringValue), "AND");
}
else if (FilterQueryDict != null && FilterQueryDict.ContainsKey(d.Key))
{
if (FilterQueryDict[d.Key].Contains(","))
{
string tempWhereCondition = String.Empty;
foreach (string tmpKey in FilterQueryDict[d.Key].Split(',').Select(a => a.Trim()).Where(a => !String.IsNullOrEmpty(a)))
{
tempWhereCondition = SqlHelper.AddWhereCondition(tempWhereCondition, String.Format(d.Value, tmpKey, "OR"));
}
if (!String.IsNullOrEmpty(tempWhereCondition))
{
_resultWhereCondition = SqlHelper.AddWhereCondition(_resultWhereCondition, "(" + tempWhereCondition + ")", "AND");
}
}
else
{
_resultWhereCondition = SqlHelper.AddWhereCondition(_resultWhereCondition, String.Format(d.Value, FilterQueryDict[d.Key]), "AND");
}
}
}
}
// if nothing matched, try to load default param
if (_resultWhereCondition == String.Empty)
{
if (WhereDict.ContainsKey("default"))
{
_resultWhereCondition = WhereDict["default"];
}
}
return _resultWhereCondition;
}
else
{
return _resultWhereCondition;
}
}
}
private Dictionary<string, string> _orderByDict = null;
public Dictionary<string, string> OrderByDict
{
get
{
if (_orderByDict == null)
{
_orderByDict = new Dictionary<string, string>();
if (OrderBy != null)
{
foreach (string i in OrderBy.Split('\n'))
{
if (i.Contains(";"))
{
string[] j = i.Split(';');
_orderByDict.Add(j[0], j[1]);
}
}
}
}
return _orderByDict;
}
set
{
_orderByDict = value;
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.SQLProperties += "where;";
}
protected void Page_Load(object sender, EventArgs e)
{
var rptConfig = new
{
layout = UseMasonry ? "masonry" : "standard",
enableCache = EnableCache,
goBackOutOfList = GoBackOutOfList,
onAfterRequest = OnAfterRequest,
textLoadMore = DataHelper.GetNotEmpty(CustomLoadMoreText, ResHelper.GetString("Pux.MoreBtn")),
loadMoreOnScroll = LoadMoreOnScrollToBottom,
rootContainer = pnlData.ClientID,
id = ClientID,
showPager = ShowPager,
pagerMaxNumbers = PagerMaxNumbers,
headerHelper = HeaderHelper
};
var autoInitialize = AutoInitialize ? "app.ready(function() { pux.components.infiniteRepeater(infiniteRepeaterConfig); });" : string.Empty;
ltlScript.Text = string.Format("<script>{0} = {1};{2}</script>", ConfigurationName, JsonConvert.SerializeObject(rptConfig), autoInitialize);
}
public bool IsAjaxRequest(HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
return (request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
}
#region "Methods"
/// <summary>
/// Content loaded event handler.
/// </summary>
public override void OnContentLoaded()
{
base.OnContentLoaded();
//rpt.DataBinding += new EventHandler(repItems_DataBinding);
SetupControl();
//rpt.DataBind();
}
/// <summary>
/// Initializes the control properties.
/// </summary>
protected void SetupControl()
{
if (this.StopProcessing)
{
// Do not process
}
else
{
#region "UniPager template properties"
// UniPager template properties
if (!String.IsNullOrEmpty(PagesTemplate))
{
pagerElem.PageNumbersTemplate = CMSDataProperties.LoadTransformation(pagerElem, PagesTemplate);
}
if (!String.IsNullOrEmpty(CurrentPageTemplate))
{
pagerElem.CurrentPageTemplate = CMSDataProperties.LoadTransformation(pagerElem, CurrentPageTemplate);
}
if (!String.IsNullOrEmpty(SeparatorTemplate))
{
pagerElem.PageNumbersSeparatorTemplate = CMSDataProperties.LoadTransformation(pagerElem, SeparatorTemplate);
}
if (!String.IsNullOrEmpty(FirstPageTemplate))
{
pagerElem.FirstPageTemplate = CMSDataProperties.LoadTransformation(pagerElem, FirstPageTemplate);
}
if (!String.IsNullOrEmpty(LastPageTemplate))
{
pagerElem.LastPageTemplate = CMSDataProperties.LoadTransformation(pagerElem, LastPageTemplate);
}
if (!String.IsNullOrEmpty(PreviousPageTemplate))
{
pagerElem.PreviousPageTemplate = CMSDataProperties.LoadTransformation(pagerElem, PreviousPageTemplate);
}
if (!String.IsNullOrEmpty(NextPageTemplate))
{
pagerElem.NextPageTemplate = CMSDataProperties.LoadTransformation(pagerElem, NextPageTemplate);
}
if (!String.IsNullOrEmpty(PreviousGroupTemplate))
{
pagerElem.PreviousGroupTemplate = CMSDataProperties.LoadTransformation(pagerElem, PreviousGroupTemplate);
}
if (!String.IsNullOrEmpty(NextGroupTemplate))
{
pagerElem.NextGroupTemplate = CMSDataProperties.LoadTransformation(pagerElem, NextGroupTemplate);
}
if (!String.IsNullOrEmpty(DirectPageTemplate))
{
pagerElem.DirectPageTemplate = CMSDataProperties.LoadTransformation(pagerElem, DirectPageTemplate);
}
if (!String.IsNullOrEmpty(LayoutTemplate))
{
pagerElem.LayoutTemplate = CMSDataProperties.LoadTransformation(pagerElem, LayoutTemplate);
}
#endregion
ltrNoDataTitle.Text = ResHelper.GetString(MacroResolver.Resolve(NoDataText));
rpt.ItemTemplate = CMSDataProperties.LoadTransformation(this, Transformation);
rpt.ClassNames = PageType;
rpt.Path = Path;
rpt.MaxRelativeLevel = NestingLevel;
rpt.TopN = TopN;
rpt.EnablePaging = true;
rpt.PagerControl.PagerPosition = PagingPlaceTypeEnum.Top;
rpt.DataSourceName = DataSourceName;
rpt.FilterOutDuplicates = FilterOutDuplicates;
rpt.SiteName = SiteName;
rpt.DelayedLoading = DelayedLoading;
rpt.SelectOnlyPublished = SelectOnlyPublished;
rpt.LoadPagesIndividually = LoadPagesIndividually;
//head meta
if (NoCachePages && (RequestContext.CurrentQueryString.ToLower().Contains("page") || RequestContext.CurrentQueryString.ToLower().Contains("pages")))
{
string noCache = "<meta http-equiv='pragma' content='no-cache' />";
Page.Header.Controls.Add(new LiteralControl(noCache));
}
if (UseCanonical && (RequestContext.CurrentQueryString.ToLower().Contains("page") || RequestContext.CurrentQueryString.ToLower().Contains("pages")))
{
string canonical = "<link rel='canonical' href='" + CurrentDocument.AbsoluteURL.ToLower() + "' />";
Page.Header.Controls.Add(new LiteralControl(canonical));
}
// rpt.PageSize = PageSize;
if (IsAjaxRequest(Request))
{
rpt.PageSize = PageSize;
}
else
{
int pages = QueryHelper.GetInteger("pages", 1);
if (pages > MaxFirstLoadPages)
{
string newUrl = URLHelper.AddParameterToUrl(RequestContext.CurrentURL, "pages", MaxFirstLoadPages.ToString());
URLHelper.Redirect(newUrl);
}
//if (QueryHelper.GetInteger("onepage", 0) == 1)
//{
// rpt.PageSize = PageSize;
//}
//else
//{
rpt.PageSize = PageSize * pages;
//}
}
if (!String.IsNullOrEmpty(Columns))
{
rpt.Columns = Columns;
}
rpt.WhereCondition = ResultWhereCondition;
if (!OrderBy.Contains('\n') && !OrderBy.Contains(';') && OrderByDict == null)
{
rpt.OrderBy = OrderBy;
}
else
{
string queryOrderBy = SqlHelper.EscapeQuotes(QueryHelper.GetString("orderBy", String.Empty));
if (!String.IsNullOrEmpty(queryOrderBy))
{
if (OrderByDict.ContainsKey(queryOrderBy))
{
rpt.OrderBy = OrderByDict[queryOrderBy];
}
}
else
{
if (OrderByDict.ContainsKey("default"))
{
rpt.OrderBy = OrderByDict["default"];
}
}
}
if (!String.IsNullOrEmpty(FilterName))
{
rpt.FilterName = FilterName;
}
if (ShowDebugLabels)
{
plcDebugLabels.Visible = true;
lblOrderBy.Text = "orderby: " + rpt.OrderBy;
lblWhereCondition.Text = "where: " + rpt.WhereCondition;
}
else
{
plcDebugLabels.Visible = false;
}
pagerElem.PageControl = rpt.ID;
pagerElem.PageSize = rpt.PageSize;
pnlBottomPager.Visible = ShowPager;
pnlTopPager.Visible = ShowTopPager;
}
}
/// <summary>
/// Reloads the control data.
/// </summary>
public override void ReloadData()
{
base.ReloadData();
SetupControl();
}
protected void rpt_DataBinding(object sender, EventArgs e)
{
if (!DataHelper.DataSourceIsEmpty(rpt.DataSource))
{
bool haveCMSTot = false;
foreach (DataRowView rw in rpt.DataSource as PagedDataSource)
{
if (rw.Row.Table.Columns.Contains("CMS_TOT"))
{
int cmsTot = ValidationHelper.GetInteger(rw["CMS_TOT"], -1);
if (cmsTot != -1)
{
ResultsCount = cmsTot;
haveCMSTot = true;
}
}
break;
}
//var ds = rpt.DataSource as System.Web.UI.WebControls.PagedDataSource;
//if (ds != null)
//{
// ResultsCount = ds.DataSourceCount;
//}
if (!haveCMSTot)
{
ResultsCount = pagerElem.DataSourceItemsCount;
}
}
else
{
ResultsCount = 0;
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Page.PreRenderComplete += Page_PreRenderComplete;
pnlResultCount.Visible = ShowResultCount;
string resultCount = ResultsCount.ToString();
if (pnlResultCount.Visible)
{
lblResultCountLabel.Text = ShowResultCountText + ": ";
lblResultCount.Text = resultCount;
}
if (!IsAjaxRequest(Request))
{
if (ResultsCount < 1)
{
pnlNoData.Visible = true;
}
}
ltrSystemPageSize.Text = rpt.PageSize.ToString();
lblSystemResultCount.Text = resultCount;
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
if (IsAjaxRequest(Request))
{
if (Request.HttpMethod == "GET")
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter writer = new HtmlTextWriter(sw);
if (ResultsCount > 0)
{
pnlNoData.Visible = false;
// render repeater
rpt.PagerControl.Visible = false;
rpt.RenderControl(writer);
}
else
{
pnlNoData.Visible = true;
pnlNoData.RenderControl(writer);
}
// create JSON to be returned to the client
JObject o = new JObject();
// number of pages
o["TotalPages"] = (int)Math.Ceiling(ResultsCount / (decimal)rpt.PageSize);
o["TotalResults"] = ResultsCount > 0 ? ResultsCount : 0;
o["PageSize"] = rpt.PageSize;
// search results for current page
o["Data"] = sb.ToString();
// send response
Response.Write(o.ToString());
RequestHelper.EndResponse();
}
}
}
#endregion
}