using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CMS.CMSHelper;
using CMS.Controls;
using CMS.GlobalHelper;
using CMS.PortalControls;
using CMS.SettingsProvider;
using CMS.SiteProvider;
public partial class CMSWebParts_Ecommerce_Products_TopNProductsBySales : CMSAbstractWebPart
{
#region "Document properties"
///
/// Site name.
///
public string SiteName
{
get
{
return DataHelper.GetNotEmpty(GetValue("SiteName"), lstElem.SiteName);
}
set
{
SetValue("SiteName", value);
lstElem.SiteName = value;
}
}
///
/// Gets or sets path where random products will be find.
///
public string Path
{
get
{
string path = ValidationHelper.GetString(GetValue("Path"), "");
if (path == "")
{
return "/%";
}
else
{
return CMSContext.CurrentResolver.ResolvePath(path);
}
}
set
{
SetValue("Path", value);
lstElem.Path = value;
}
}
///
/// Maximal relative level.
///
public int MaxRelativeLevel
{
get
{
return ValidationHelper.GetInteger(GetValue("MaxRelativeLevel"), lstElem.MaxRelativeLevel);
}
set
{
SetValue("MaxRelativeLevel", value);
lstElem.MaxRelativeLevel = value;
}
}
///
/// Class names.
///
public string ClassNames
{
get
{
return DataHelper.GetNotEmpty(GetValue("Classnames"), lstElem.ClassNames);
}
set
{
SetValue("ClassNames", value);
lstElem.ClassNames = value;
}
}
///
/// Select top N items.
///
public int SelectTopN
{
get
{
return ValidationHelper.GetInteger(GetValue("SelectTopN"), lstElem.SelectTopN);
}
set
{
SetValue("SelectTopN", value);
lstElem.SelectTopN = value;
}
}
///
/// Where condition.
///
public string WhereCondition
{
get
{
return DataHelper.GetNotEmpty(GetValue("WhereCondition"), GetWhereCondition(""));
}
set
{
SetValue("WhereCondition", value);
lstElem.WhereCondition = GetWhereCondition(value);
}
}
///
/// Order by clause.
///
public string OrderBy
{
get
{
return DataHelper.GetNotEmpty(GetValue("OrderBy"), GetOrderByExpression(""));
}
set
{
SetValue("OrderBy", value);
lstElem.OrderBy = GetOrderByExpression(value);
}
}
///
/// Select only published nodes.
///
public bool SelectOnlyPublished
{
get
{
return ValidationHelper.GetBoolean(GetValue("SelectOnlyPublished"), lstElem.SelectOnlyPublished);
}
set
{
SetValue("SelectOnlyPublished", value);
lstElem.SelectOnlyPublished = value;
}
}
///
/// Culture code.
///
public string CultureCode
{
get
{
return DataHelper.GetNotEmpty(GetValue("CultureCode"), lstElem.CultureCode);
}
set
{
SetValue("CultureCode", value);
lstElem.CultureCode = value;
}
}
///
/// Combine with default culture.
///
public bool CombineWithDefaultCulture
{
get
{
return ValidationHelper.GetBoolean(GetValue("CombineWithDefaultCulture"), lstElem.CombineWithDefaultCulture);
}
set
{
SetValue("CombineWithDefaultCulture", value);
lstElem.CombineWithDefaultCulture = value;
}
}
#endregion
#region "System settings"
///
/// Gest or sest the cache item name.
///
public override string CacheItemName
{
get
{
return base.CacheItemName;
}
set
{
base.CacheItemName = value;
lstElem.CacheItemName = value;
}
}
///
/// Cache dependencies, each cache dependency on a new line.
///
public override string CacheDependencies
{
get
{
return ValidationHelper.GetString(base.CacheDependencies, lstElem.CacheDependencies);
}
set
{
base.CacheDependencies = value;
lstElem.CacheDependencies = value;
}
}
///
/// Gets or sets the cache minutes.
///
public override int CacheMinutes
{
get
{
return base.CacheMinutes;
}
set
{
base.CacheMinutes = value;
lstElem.CacheMinutes = value;
}
}
///
/// Gets or sets the value that indicates whether permissions should be checked.
///
public bool CheckPermissions
{
get
{
return ValidationHelper.GetBoolean(GetValue("CheckPermissions"), lstElem.CheckPermissions);
}
set
{
SetValue("CheckPermissions", value);
lstElem.CheckPermissions = value;
}
}
#endregion
#region "Layout"
///
/// Repeat columns.
///
public int RepeatColumns
{
get
{
return ValidationHelper.GetInteger(GetValue("RepeatColumns"), lstElem.RepeatColumns);
}
set
{
SetValue("RepeatColumns", value);
lstElem.RepeatColumns = value;
}
}
///
/// Repeat layout.
///
public RepeatLayout RepeatLayout
{
get
{
return CMSDataList.GetRepeatLayout(DataHelper.GetNotEmpty(GetValue("RepeatLayout"), lstElem.RepeatLayout.ToString()));
}
set
{
SetValue("RepeatLayout", value.ToString());
lstElem.RepeatLayout = value;
}
}
///
/// Repeat Direction.
///
public RepeatDirection RepeatDirection
{
get
{
return CMSDataList.GetRepeatDirection(DataHelper.GetNotEmpty(GetValue("RepeatDirection"), lstElem.RepeatDirection.ToString()));
}
set
{
SetValue("RepeatDirection", value.ToString());
lstElem.RepeatDirection = value;
}
}
#endregion
#region "Transformation properties"
///
/// Gets or sets the name of the transforamtion which is used for displaying the results.
///
public string TransformationName
{
get
{
return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("TransformationName"), lstElem.TransformationName), lstElem.TransformationName);
}
set
{
SetValue("TransformationName", value);
lstElem.TransformationName = value;
}
}
///
/// Gets or sets the name of the transforamtion which is used for displaying the alternate results.
///
public string AlternatingTransformationName
{
get
{
return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("AlternatingTransformationName"), lstElem.AlternatingTransformationName), lstElem.AlternatingTransformationName);
}
set
{
SetValue("AlternatingTransformationName", value);
lstElem.AlternatingTransformationName = value;
}
}
#endregion
#region "Public properties"
///
/// Hide control for zero rows.
///
public bool HideControlForZeroRows
{
get
{
return ValidationHelper.GetBoolean(GetValue("HideControlForZeroRows"), lstElem.HideControlForZeroRows);
}
set
{
SetValue("HideControlForZeroRows", value);
lstElem.HideControlForZeroRows = value;
}
}
///
/// Zero rows text.
///
public string ZeroRowsText
{
get
{
return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("ZeroRowsText"), lstElem.ZeroRowsText), lstElem.ZeroRowsText);
}
set
{
SetValue("ZeroRowsText", value);
lstElem.ZeroRowsText = value;
}
}
#endregion
///
/// Content loaded event handler.
///
public override void OnContentLoaded()
{
base.OnContentLoaded();
ReloadData();
}
///
/// Initializes the control properties.
///
protected void SetupControl()
{
if (StopProcessing)
{
lstElem.StopProcessing = true;
}
else
{
lstElem.ControlContext = ControlContext;
// System settings
lstElem.CacheItemName = CacheItemName;
lstElem.CacheDependencies = CacheDependencies;
lstElem.CacheMinutes = CacheMinutes;
lstElem.CheckPermissions = CheckPermissions;
// Document properties
lstElem.SiteName = SiteName;
lstElem.ClassNames = ClassNames;
lstElem.Path = Path;
lstElem.MaxRelativeLevel = MaxRelativeLevel;
lstElem.SelectOnlyPublished = SelectOnlyPublished;
lstElem.CombineWithDefaultCulture = CombineWithDefaultCulture;
lstElem.CultureCode = CultureCode;
lstElem.SelectTopN = SelectTopN;
lstElem.WhereCondition = GetWhereCondition(WhereCondition);
lstElem.OrderBy = GetOrderByExpression(OrderBy);
// Layout
lstElem.RepeatColumns = RepeatColumns;
lstElem.RepeatDirection = RepeatDirection;
lstElem.RepeatLayout = RepeatLayout;
// Transformations
lstElem.AlternatingTransformationName = AlternatingTransformationName;
lstElem.TransformationName = TransformationName;
//// Public
lstElem.HideControlForZeroRows = HideControlForZeroRows;
lstElem.ZeroRowsText = ZeroRowsText;
}
}
///
/// Reloads the data.
///
public override void ReloadData()
{
base.ReloadData();
SetupControl();
lstElem.ReloadData(true);
}
///
/// Returns where condition.
///
/// Custom WHERE condition
private string GetWhereCondition(string customWhere)
{
SiteInfo si = null;
// Get required site data
if (SiteName != "")
{
si = SiteInfoProvider.GetSiteInfo(SiteName);
}
else
{
si = CMSContext.CurrentSite;
}
if (si != null)
{
// Build where condition
string where = "SELECT OrderItemSKUID FROM COM_OrderItem JOIN COM_SKU ON COM_OrderItem.OrderItemSKUID = COM_SKU.SKUID JOIN COM_Order ON COM_OrderItem.OrderItemOrderID = COM_Order.OrderID WHERE (COM_SKU.SKUEnabled = 1) AND (COM_SKU.SKUOptionCategoryID IS NULL) AND (COM_Order.OrderSiteID = " + si.SiteID + ")";
// Get documents of the specified class only - without coupled data !!!
if (ClassNames != "")
{
int i = 0;
string tempWhere = "";
string[] classNames = ClassNames.Trim(';').Split(';');
foreach (string className in classNames)
{
if (i > 0)
{
tempWhere += " OR ";
}
tempWhere += "(ClassName = '" + SqlHelperClass.GetSafeQueryString(className, false) + "')";
i++;
}
if (tempWhere != "")
{
where += " AND (" + tempWhere + ")";
}
}
where = "NodeSKUID IN (" + where + ")";
// Add custom WHERE condition
if (customWhere != "")
{
where += " AND (" + customWhere + ")";
}
return where;
}
return "";
}
///
/// Returns ORDER BY expression.
///
///Custom order by expression
private string GetOrderByExpression(string customOrderBy)
{
// Required "ORDER BY" expression
string orderBy = "(SELECT Count(OrderItemSKUID) FROM COM_OrderItem WHERE COM_OrderItem.OrderItemSKUID = NodeSKUID GROUP BY OrderItemSKUID) DESC";
// Add cutom "ORDER BY" expression
if (customOrderBy != "")
{
orderBy += ", " + customOrderBy;
}
return orderBy;
}
///
/// OnPrerender override (Set visibility).
///
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Visible = lstElem.Visible;
if ((HideControlForZeroRows) && (DataHelper.DataSourceIsEmpty(lstElem.DataSource)))
{
Visible = false;
}
}
///
/// Clears cache.
///
public override void ClearCache()
{
lstElem.ClearCache();
}
}