-
Notifications
You must be signed in to change notification settings - Fork 0
/
NbStoreUrlRuleProvider.cs
233 lines (213 loc) · 11.3 KB
/
NbStoreUrlRuleProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using DotNetNuke.Common.Utilities;
using NEvoWeb.Modules.NB_Store;
using DotNetNuke.Services.Localization;
using Satrabel.HttpModules.Provider;
using System.Text.RegularExpressions;
using DotNetNuke.Entities.Modules;
namespace Satrabel.OpenUrlRewriter.NbStore
{
public class NbStoreUrlRuleProvider : UrlRuleProvider
{
public NbStoreUrlRuleProvider() { }
public override List<UrlRule> GetRules(int PortalId)
{
List<UrlRule> Rules = new List<UrlRule>();
Dictionary<string, Locale> dicLocales = LocaleController.Instance.GetLocales(PortalId);
ProductController pc = new ProductController();
CategoryController cc = new CategoryController();
ModuleController mc = new ModuleController();
ArrayList modules = mc.GetModulesByDefinition(PortalId, "NB_Store_ProductList");
foreach (ModuleInfo module in modules.OfType<ModuleInfo>().Where(m=> m.IsDeleted == false))
{
Hashtable modSettings = mc.GetModuleSettings(module.ModuleID);
int DetailTabId = Null.NullInteger;
if (modSettings["lstProductTabs"] != null)
{
string lstProductTabs = modSettings["lstProductTabs"].ToString();
int.TryParse(lstProductTabs, out DetailTabId);
}
bool RewriteProducts = module.TabID == DetailTabId;
bool BrowseCategory = Null.NullBoolean;
string chkBrowseCategory = modSettings["chkBrowseCategory"] == null ? "" : modSettings["chkBrowseCategory"].ToString();
Boolean.TryParse(chkBrowseCategory, out BrowseCategory);
bool IndexProducts = true;
if (modSettings["chkIndexProducts"] != null)
{
Boolean.TryParse(modSettings["chkIndexProducts"].ToString(), out IndexProducts);
}
int CatID = Null.NullInteger;
string ddlDefaultCategory = modSettings["ddlDefaultCategory"] == null ? "" : modSettings["ddlDefaultCategory"].ToString();
int.TryParse(ddlDefaultCategory, out CatID);
foreach (KeyValuePair<string, Locale> key in dicLocales)
{
string CultureCode = key.Value.Code;
string RuleCultureCode = (dicLocales.Count > 1 ? CultureCode : null);
bool chkCascadeResults = false;
bool.TryParse(modSettings["chkCascadeResults"] == null ? "" : modSettings["chkCascadeResults"].ToString(), out chkCascadeResults);
if (RewriteProducts)
{
var prodLst = GetProductList(PortalId, pc, cc, CatID, CultureCode, chkCascadeResults);
//var prodLst = pc.GetProductList(PortalId, CatID, CultureCode, false);
foreach (ProductListInfo prod in prodLst)
{
var rule = new UrlRule
{
RuleType = UrlRuleType.Module,
CultureCode = RuleCultureCode,
TabId = module.TabID,
Parameters = "ProdID=" + prod.ProductID + (CatID == Null.NullInteger ? "" : "&" + "CatID=" + CatID),
Action = UrlRuleAction.Rewrite,
Url = CleanupUrl(prod.SEOName == "" ? prod.ProductName : prod.SEOName),
InSitemap = IndexProducts
};
Rules.Add(rule);
}
}
if (BrowseCategory)
{
var CatRules = GetRulesForCategory(PortalId, CultureCode, module.TabID, CatID, "", pc, cc, RuleCultureCode, chkCascadeResults, RewriteProducts, IndexProducts);
Rules.AddRange(CatRules);
}
}
}
return Rules;
}
private static ArrayList GetProductList(int PortalId, ProductController pc, CategoryController cc, int CatID, string CultureCode, bool chkCascadeResults)
{
ArrayList prodLst;
string CategoryList = "";
if (chkCascadeResults && CatID > 0)
{
var aryList = cc.GetCategories(PortalId, CultureCode);
CategoryList = cc.GetSubCategoryList(aryList, CatID);
CategoryList += CatID.ToString();
prodLst = pc.GetProductList(PortalId, CatID, CultureCode, "", false, false, "", false, 0, 1, 99999, false, false, CategoryList, false);
}
else {
prodLst = pc.GetProductList(PortalId, CatID, CultureCode, false);
}
return prodLst;
}
private List<UrlRule> GetRulesForCategory(int PortalId, string CultureCode, int TabId, int CatID, string CategoryUrl, ProductController pc, CategoryController cc, string RuleCultureCode, bool chkCascadeResults, bool RewriteProducts, bool IndexProducts)
{
List<UrlRule> Rules = new List<UrlRule>();
var catLst = cc.GetCategories(PortalId, CultureCode, CatID == Null.NullInteger ? 0 : CatID);
foreach (NB_Store_CategoriesInfo cat in catLst)
{
var CatRule = new UrlRule
{
RuleType = UrlRuleType.Module,
CultureCode = RuleCultureCode,
TabId = TabId,
Parameters = "CatID=" + cat.CategoryID,
Action = UrlRuleAction.Rewrite,
Url = (CategoryUrl == "" ? "" : CategoryUrl + "/") + CleanupUrl(cat.SEOName == "" ? cat.CategoryName : cat.SEOName),
InSitemap = IndexProducts
};
CatRule.RedirectDestination = CatRule.Parameters.Replace('=', '/').Replace('&', '/') + "/" + CleanupSEO(cat.SEOName == "" ? cat.CategoryName : cat.SEOName);
CatRule.RedirectDestination = CatRule.RedirectDestination.ToLower();
if (string.IsNullOrEmpty(CatRule.Url))
{
//continue;
}
Rules.Add(CatRule);
if (RewriteProducts)
{
var productLst = GetProductList(PortalId, pc, cc, cat.CategoryID, CultureCode, chkCascadeResults);
//var productLst = pc.GetProductList(PortalId, cat.CategoryID, CultureCode, false);
foreach (ProductListInfo prod in productLst)
{
var rule = new UrlRule
{
RuleType = UrlRuleType.Module,
CultureCode = RuleCultureCode,
TabId = TabId,
Parameters = "ProdID=" + prod.ProductID + "&" + "CatID=" + cat.CategoryID,
Action = UrlRuleAction.Rewrite,
Url = (CategoryUrl == "" ? "" : CategoryUrl + "/") + CleanupUrl(cat.SEOName == "" ? cat.CategoryName : cat.SEOName) + "/" + CleanupUrl(prod.SEOName == "" ? prod.ProductName : prod.SEOName),
InSitemap = IndexProducts
};
Rules.Add(rule);
}
}
var CatRules = GetRulesForCategory(PortalId, CultureCode, TabId, cat.CategoryID, CatRule.Url, pc, cc, RuleCultureCode, chkCascadeResults, RewriteProducts, IndexProducts);
Rules.AddRange(CatRules);
}
return Rules;
}
public List<UrlRule> GetRulesOld(int PortalId)
{
List<UrlRule> Rules = new List<UrlRule>();
Dictionary<string, Locale> dicLocales = LocaleController.Instance.GetLocales(PortalId);
foreach (KeyValuePair<string, Locale> key in dicLocales)
{
string CultureCode = key.Value.Code;
CategoryController cc = new CategoryController();
ProductController pc = new ProductController();
// products alone
var prodLst = pc.GetProductList(PortalId, Null.NullInteger, CultureCode, false);
foreach (ProductListInfo prod in prodLst)
{
var rule = new UrlRule
{
RuleType = UrlRuleType.Module,
CultureCode = (dicLocales.Count > 1 ? CultureCode : null),
Parameters = "ProdID=" + prod.ProductID ,
Action = UrlRuleAction.Rewrite,
Url = CleanupUrl(prod.SEOName == "" ? prod.ProductName : prod.SEOName)
};
Rules.Add(rule);
}
// products and categories
var catLst = cc.GetCategories(PortalId, CultureCode);
foreach (NB_Store_CategoriesInfo cat in catLst)
{
var CatRule = new UrlRule
{
RuleType = UrlRuleType.Module,
CultureCode = (dicLocales.Count > 1 ? CultureCode : null),
Parameters = "CatID=" + cat.CategoryID,
Action = UrlRuleAction.Rewrite,
Url = CleanupUrl(cat.SEOName == "" ? cat.CategoryName : cat.SEOName)
};
CatRule.RedirectDestination = CatRule.Parameters.Replace('=', '/').Replace('&', '/') + "/" + CleanupSEO(cat.SEOName == "" ? cat.CategoryName : cat.SEOName);
CatRule.RedirectDestination = CatRule.RedirectDestination.ToLower();
Rules.Add(CatRule);
var productLst = pc.GetProductList(PortalId, cat.CategoryID, CultureCode, false);
foreach (ProductListInfo prod in productLst)
{
var rule = new UrlRule
{
RuleType = UrlRuleType.Module,
CultureCode = (dicLocales.Count > 1 ? CultureCode : null),
Parameters = "ProdID=" + prod.ProductID + "&" + "CatID=" + cat.CategoryID,
Action = UrlRuleAction.Rewrite,
Url = CleanupUrl(cat.SEOName == "" ? cat.CategoryName : cat.SEOName) +"/"+CleanupUrl(prod.SEOName == "" ? prod.ProductName : prod.SEOName)
};
Rules.Add(rule);
}
}
}
return Rules;
}
private string CleanupSEO(string SEOName){
if (SEOName != "") {
SEOName = SEOName.Replace( " ", "_");
SEOName = Regex.Replace(SEOName, @"[\W]", "");
}
return SEOName;
}
}
}