eng.rupalikulkarni-gmail
-
7/24/2012 5:19:58 PM
RE:searched Records are not displaying
Hi,
Below is my CMScustom.cs file code in which all macro's definition is there
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
using CMS.GlobalHelper; using CMS.Compatibility; using CMS.SettingsProvider; using CMS.DataEngine; using CMS.SiteProvider; using CMS.CMSHelper; using CMS.Scheduler; using CMS.EventLog;
using System.Collections.Generic;
/// <summary> /// CMSCustom methods /// </summary> public static class CMSCustom { /// <summary> /// Initializes the custom methods /// </summary> public static void Init() {
MacroResolverCompatibility.OnResolveCustomMacro += new MacroResolverCompatibility.MacroHandler(ResolveCustomMacro); ClassHelperCompatibility.OnGetCustomClass += new ClassHelperCompatibility.GetClassEventHandler(GetCustomClass);
}
/// <summary> /// Custom macro handler /// </summary> /// <param name="sender">Sender (active macro resolver)</param> /// <param name="expression">Expression to resolve</param> /// <param name="match">Returns true if the macro matches (was resolved)</param> public static string ResolveCustomMacro(MacroResolver sender, string expression, out bool match) {
match = false; string result = expression; string[] expr = expression.Split('|'); string param = String.Empty;
//loop through all parameters for (int i = 0; i < expr.Length; i++) { //param=expr; if (expr.ToUpper().StartsWith("(COLUMN)")) { //only looking for column parameter param = expr.Remove(0, 8).Trim(); } if (expr.ToUpper().StartsWith("(PARAM)")) { param = expr.Remove(0, 7).Trim(); } }
// Add your custom macro evaluation switch (expr[0].ToLower()) { case "preownedsearch": match = true; result = ResolveWhereCondition(param); break; case "preownedorderbylink": match = true; result = ResolveOrderByLink(param); break; case "preownedorderby": match = true; result = ResolveOrderByParam(param); break; case "recentlyviewedwhere": match = true; result = ResolveRecentlyViewedWhereCondition(param); break; }
return result; }
private static string ResolveRecentlyViewedWhereCondition(string param) { string s = String.Empty; string qry = CMS.CMSHelper.CMSContext.ResolveMacros(param); //string qry = param;
if (String.IsNullOrEmpty(qry)) { s = "eqStockNumber IS NULL"; } else { s = "eqStockNumber IN (" + qry + ")"; } return s; }
private static string ResolveOrderByParam(string param) { string s = String.Empty; string qry = CMS.CMSHelper.CMSContext.ResolveMacros(param); if(!String.IsNullOrEmpty(qry)) { switch(qry.ToLower()) { case "make": s = "eqModel"; break; case "manufacturer": s = "ManufacturerName"; break; case "price": s = "eqPrice"; break; case "location": s = "LocationName"; break; case "year": s = "eqYear"; break; case "hours": s = "eqHours"; break; } } return s; } private static string ResolveOrderByLink(string col) { string cUrl = UrlHelper.RawUrl; string[] p = {"make", "manufacturer", "price", "location", "year", "hours"}; string s = String.Empty; //remove all possible order by params cUrl = UrlHelper.RemoveParameterFromUrl(cUrl, "order");
if(!String.IsNullOrEmpty(col)) { switch(col.ToUpper()) { case "EQMODEL": s = cUrl = UrlHelper.AddParameterToUrl(cUrl, "order", p[0]); break; case "MANUFACTURERNAME": s = cUrl = UrlHelper.AddParameterToUrl(cUrl, "order", p[1]); break; case "EQPRICE": s = cUrl = UrlHelper.AddParameterToUrl(cUrl, "order", p[2]); break; case "LOCATIONNAME": s = cUrl = UrlHelper.AddParameterToUrl(cUrl, "order", p[3]); break; case "EQYEAR": s = cUrl = UrlHelper.AddParameterToUrl(cUrl, "order", p[4]); break; case "EQHOURS": s = cUrl = UrlHelper.AddParameterToUrl(cUrl, "order", p[5]); break; } } return s; } private static string ResolveWhereCondition(string col) { string text = CMS.CMSHelper.CMSContext.ResolveMacros("{%searchtext%}"); string mode = CMS.CMSHelper.CMSContext.ResolveMacros("{%searchmode%}"); string s = String.Empty;
if (!String.IsNullOrEmpty(col)) { string[] w = text.Split(' '); switch (mode) { case "anyword": for (int i = 0; i < w.Length; i++) { s += "searchtext" + " LIKE '% " + SQLEncode(w) + " %' OR "; } s = s.Remove(s.Length - 4); break; case "allwords": for (int i = 0; i < w.Length; i++) { s += "searchtext" + " LIKE ''% " + SQLEncode(w) + " %'' AND "; } s = s.Remove(s.Length - 5); break; case "exactphrase": s = "searchtext" + " LIKE ''%" + SQLEncode(text) + "%''"; break; default: s = "SearchText = NULL"; break; } } else { //default column s = "SearchText = NULL"; }
return s; }
private static string SQLEncode(string str) { string characters = "'%";
foreach(char s in characters) { switch(s) { case '\'': str = str.Replace(s.ToString(), "''"); break; case '%': str = str.Replace(s.ToString(), "[" + s.ToString() + "]"); break; } } return str; }
/// <summary> /// Gets the custom class object based on the given class name. This handler is called when the assembly name is App_Code /// </summary> /// <param name="className">Class name</param> public static object GetCustomClass(string className) { // Provide your custom classes switch (className) { // Define the class MyTask implementing ITask and you can provide your scheduled tasks out of App_Code case "Custom.MyTask": return new MyTask(); }
return null; }
/// <summary> /// Sample task class /// </summary> public class MyTask : ITask { /// <summary> /// Executes the task /// </summary> /// <param name="ti">Task info</param> public string Execute(TaskInfo ti) { EventLogProvider ev = new EventLogProvider(); ev.LogEvent("I", DateTime.Now, "MyTask", "Execute", null, "This task was executed from '~/App_Code/Global/CMS/CMSCustom.cs'.");
return null; } } }
Please let me know, do i need to make any changes to get it work??
Thanks!
|