File: D:/HostingSpaces/TDijk1/erp-apps.eu/wwwroot/App_Code/ERPApps/ERPTransformationHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.TreeEngine;
using CMS.GlobalHelper;
using CMS.SiteProvider;
using CMS.CMSHelper;
using CMS.DocumentEngine;
using CMS.Helpers;
using CMS.CustomTables;
using CMS.Membership;
using CMS.DataEngine;
/// <summary>
/// Helper methods used in transformations and in data processing.
/// </summary>
public class ERPTransformationHelper
{
/// <summary>
/// Returns parent node for given node.
/// </summary>
/// <param name="currentNode"></param>
/// <returns></returns>
public static TreeNode GetParentNode(object currentNode)
{
return TreeHelper.SelectSingleNode(ValidationHelper.GetInteger(currentNode, 0));
}
/// <summary>
/// Returns names of items in many-to-many relationship
/// </summary>
/// <param name="data">Relationship values in form "|X|Y|Z|", where X, Y, Z are ID in Custom table</param>
/// <param name="table">Name of custom table to be used as a source</param>
/// <returns></returns>
public static string GetListLinks(string data, string table)
{
return GetListLinks(data, table, "ItemID");
}
/// <summary>
/// Returns names of items in many-to-many relationship
/// </summary>
/// <param name="data">Relationship values in form "|X|Y|Z|" where X, Y, Z are code values (strings) in Custom table</param>
/// <param name="table">Name of custom table to be used as a source</param>
/// <returns></returns>
public static string GetListLinksByCode(string data, string table)
{
return GetListLinks(data, table, "ItemCode");
}
/// <summary>
/// Returns names of items in many-to-many relationship based on custom source field
/// </summary>
/// <param name="data">Relationship values in form "|X|Y|Z|" where X, Y, Z are values in "keyField" column</param>
/// <param name="table">Name of custom table to be used as a source</param>
/// <param name="keyField">Column in custom table to be used as the data source</param>
/// <returns></returns>
public static string GetListLinks(string data, string table, string keyField)
{
Dictionary<string, string> customData = null;
//caching
using ( CachedSection<Dictionary<string, string>> cs = new CachedSection<Dictionary<string, string>>(ref customData, 15, true, null, "GetListLinks" + table + "." + keyField) )
{
if ( cs.LoadData )
{
// Get data class using custom table name
DataClassInfo customTableClassInfo = DataClassInfoProvider.GetDataClassInfo(table);
customData = new Dictionary<string, string>();
// Check if data class info exists
if ( customTableClassInfo != null )
{
// Initialize custom table item provider with current user info and general connection
System.Data.DataSet ds = CustomTableItemProvider.GetItems(table, null, null);
if ( !DataHelper.DataSourceIsEmpty(ds) )
{
foreach ( System.Data.DataRow item in ds.Tables[0].Rows )
{
customData[ValidationHelper.GetString(item[keyField], "")] = ValidationHelper.GetString(item["ItemName"], "");
}
}
}
cs.CacheDependency = CacheHelper.GetCacheDependency("customtable|" + table.ToLower());
cs.Data = customData;
}
}
// build result
System.Text.StringBuilder sb = new System.Text.StringBuilder();
// convert |X|Y|Z| to readable string "value 1, value 2, value 3"
data = data.Trim('|');
string[] ids = data.Split('|');
foreach ( string item in ids )
{
string id = ValidationHelper.GetString(item, "");
if ( customData != null && customData.ContainsKey(id) )
{
sb.Append(customData[id] + ", ");
}
}
return sb.ToString().TrimEnd(' ').TrimEnd(',');
}
/// <summary>
/// Returns CSS class of body based on current page
/// </summary>
/// <returns></returns>
public static string GetPageBodyClass() {
string cssClass = null;
TreeNode doc = DocumentContext.CurrentDocument;
if ( !doc.DocumentMenuClassHighlighted.Equals("") )
{
cssClass = doc.DocumentMenuClassHighlighted;
}
else
{
switch ( doc.NodeClassName.ToLower() )
{
case "erp.application":
cssClass = "detail";
break;
case "erp.industrypage":
case "erp.platformpage":
case "erp.processpage":
cssClass = "category";
break;
}
}
if ( doc.NodeAliasPath.ToLower().StartsWith("/account/vendor/") )
{
cssClass = "vendor-section";
}
if ( doc.NodeAliasPath.ToLower().StartsWith("/news-editors/editor/") )
{
cssClass = "vendor-section";
}
if ( null != cssClass )
{
return " class=\"" + cssClass + "\"";
}
return null;
}
/// <summary>
/// Checks whether requested application is available (published) or if it belongs to the current user.
/// </summary>
/// <param name="published"></param>
/// <param name="vendorID"></param>
/// <returns></returns>
public static bool CheckAppDetailPage(bool published, int vendorID)
{
if ( published )
{
// application is published, no checks are necessary
return true;
}
// application is not published, check, whether current user is owner of the app
if (vendorID == ValidationHelper.GetInteger(MembershipContext.AuthenticatedUser.GetValue("UserVendorID"), -1))
{
// current user is the owner of the application, show the page, but with a warning
return false;
}
ERPDataHelper.Redirect404(true);
return false;
}
/// <summary>
/// Replaces line breaks with <p></p> elements.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string FormatLineBreaks(string value) {
return "<p>" + System.Text.RegularExpressions.Regex.Replace(value.Trim().Replace("\r\n", "\n"), "\n+", "</p><p>") + "</p>";
}
public static string ConcatStrings(string separator, params string[] strings)
{
string result = "";
int j = 0;
for (int i = 0; i < strings.Length; i++)
{
if (DataHelper.IsEmpty(strings[i]))
continue;
if (j > 0)
result += separator;
result += strings[i];
j++;
}
return result;
}
}