forked from nbrightproject/NBrightPL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlConnector.ashx.cs
339 lines (277 loc) · 12.6 KB
/
XmlConnector.ashx.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Web;
using System.Xml;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using NBrightCore;
using NBrightCore.common;
using NBrightCore.render;
using NBrightDNN;
using NBrightPL.common;
using DataProvider = DotNetNuke.Data.DataProvider;
namespace Nevoweb.DNN.NBrightPL
{
/// <summary>
/// Summary description for XMLconnector
/// </summary>
public class XmlConnector : IHttpHandler
{
private String _lang = "";
public void ProcessRequest(HttpContext context)
{
#region "Initialize"
var strOut = "";
var paramCmd = Utils.RequestQueryStringParam(context, "cmd");
var itemId = Utils.RequestQueryStringParam(context, "itemid");
var lang = Utils.RequestQueryStringParam(context, "lang");
var language = Utils.RequestQueryStringParam(context, "language");
var baselang = Utils.RequestQueryStringParam(context, "baselang");
#region "setup language"
// because we are using a webservice the system current thread culture might not be set correctly,
// so use the lang/lanaguge param to set it.
if (lang == "") lang = language;
if (!string.IsNullOrEmpty(lang)) _lang = lang;
// default to current thread if we have no language.
if (_lang == "") _lang = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(_lang);
#endregion
#endregion
#region "Do processing of command"
var objCtrl = new NBrightDataController();
strOut = "ERROR!! - No Security rights for current user!";
switch (paramCmd)
{
case "test":
strOut = "<root>" + UserController.GetCurrentUserInfo().Username + "</root>";
break;
case "getformdata":
strOut = GetTabData(context, "viewbody.html");
break;
case "getbasedata":
strOut = GetTabData(context, "viewbodybase.html");
break;
case "saveformdata":
if (CheckRights()) strOut = SaveTabData(context);
break;
case "getsetting":
if (CheckRights()) strOut = GetSettings(context, "settings.html");
break;
case "savesetting":
if (CheckRights()) strOut = SaveSettings(context);
break;
case "translate":
if (CheckRights())
{
TranslateForm(context);
strOut = "reload";
}
break;
}
#endregion
#region "return results"
//send back xml as plain text
context.Response.Clear();
context.Response.ContentType = "text/plain";
context.Response.Write(strOut);
context.Response.End();
#endregion
}
public bool IsReusable
{
get
{
return false;
}
}
#region "Methods"
private String SaveSettings(HttpContext context)
{
try
{
//get uploaded params
var ajaxInfo = GetAjaxFields(context);
var tabid = ajaxInfo.GetXmlProperty("genxml/hidden/tabid");
var selectlang = ajaxInfo.GetXmlProperty("genxml/hidden/selectlang");
var objCtrl = new NBrightDataController();
var dataRecord = objCtrl.GetByGuidKey(PortalSettings.Current.PortalId, -1, "SETTINGS", "NBrightPL");
if (dataRecord == null)
{
dataRecord = new NBrightInfo(true); // populate empty XML so we can update nodes.
dataRecord.GUIDKey = "NBrightPL";
dataRecord.PortalId = PortalSettings.Current.PortalId;
dataRecord.TypeCode = "SETTINGS";
dataRecord.Lang = "";
}
dataRecord.ModuleId = -99; // always use -99, so we don't get picked up as data by NBrightMod.
var strIn = HttpUtility.UrlDecode(Utils.RequestParam(context, "inputxml"));
dataRecord.UpdateAjax(strIn);
objCtrl.Update(dataRecord);
return "";
}
catch (Exception ex)
{
return ex.ToString();
}
}
private String GetSettings(HttpContext context, String templateName)
{
try
{
var strOut = "";
//get uploaded params
var ajaxInfo = GetAjaxFields(context);
var tabid = ajaxInfo.GetXmlProperty("genxml/hidden/itemid");
var selectlang = ajaxInfo.GetXmlProperty("genxml/hidden/selectlang");
var lang = ajaxInfo.GetXmlProperty("genxml/hidden/lang");
var baselang = ajaxInfo.GetXmlProperty("genxml/hidden/baselang");
// get template
var objCtrl = new NBrightDataController();
var bodyTempl = GetTemplateData(templateName, lang);
var dataRecord = objCtrl.GetByGuidKey(PortalSettings.Current.PortalId, -1, "SETTINGS", "NBrightPL");
strOut = GenXmlFunctions.RenderRepeater(dataRecord, bodyTempl);
return strOut;
}
catch (Exception ex)
{
return ex.ToString();
}
}
private String GetTabData(HttpContext context, String templateName)
{
try
{
var strOut = "";
//get uploaded params
var ajaxInfo = GetAjaxFields(context);
var tabid = ajaxInfo.GetXmlProperty("genxml/hidden/itemid");
var selectlang = ajaxInfo.GetXmlProperty("genxml/hidden/selectlang");
var lang = ajaxInfo.GetXmlProperty("genxml/hidden/lang");
var baselang = ajaxInfo.GetXmlProperty("genxml/hidden/baselang");
if (Utils.IsNumeric(tabid))
{
if (templateName == "viewbodybase.html") selectlang = baselang;
if (selectlang == "") selectlang = lang; // use current culture
// get template
var bodyTempl = GetTemplateData(templateName, lang);
//get data
var tabData = new TabData(tabid, selectlang);
strOut = GenXmlFunctions.RenderRepeater(tabData.Info, bodyTempl);
}
return strOut;
}
catch (Exception ex)
{
return ex.ToString();
}
}
private String SaveTabData(HttpContext context)
{
try
{
//get uploaded params
var ajaxInfo = GetAjaxFields(context);
var tabid = ajaxInfo.GetXmlProperty("genxml/hidden/tabid");
var selectlang = ajaxInfo.GetXmlProperty("genxml/hidden/selectlang");
if (Utils.IsNumeric(tabid))
{
var lang = ajaxInfo.GetXmlProperty("genxml/hidden/lang");
if (selectlang == "") selectlang = lang;
var tabData = new TabData(tabid, selectlang);
//save data
if (tabData.Exists)
{
var strIn = HttpUtility.UrlDecode(Utils.RequestParam(context, "inputxml"));
tabData.DataRecord.UpdateAjax(strIn);
tabData.DataLangRecord.UpdateAjax(strIn);
tabData.Save();
}
}
return "";
}
catch (Exception ex)
{
return ex.ToString();
}
}
private void TranslateForm(HttpContext context)
{
try
{
var objCtrl = new NBrightDataController();
var settings = objCtrl.GetByGuidKey(PortalSettings.Current.PortalId, -1, "SETTINGS", "NBrightPL");
var clientId = settings.GetXmlProperty("genxml/textbox/bingclientid");
var clientSecret = settings.GetXmlProperty("genxml/textbox/bingclientsecret");
var headerValue = Utils.GetTranslatorHeaderValue(clientId, clientSecret);
//get uploaded params
var ajaxInfo = GetAjaxFields(context);
var tabid = ajaxInfo.GetXmlProperty("genxml/hidden/tabid");
var selectlang = ajaxInfo.GetXmlProperty("genxml/hidden/lang");
var baselang = ajaxInfo.GetXmlProperty("genxml/hidden/baselangtrans");
if (selectlang == "") selectlang = Utils.GetCurrentCulture();
if (baselang == "") baselang = Utils.GetCurrentCulture();
if (Utils.IsNumeric(tabid) && (baselang != selectlang))
{
var baseData = new TabData(tabid, baselang);
var tabData = new TabData(tabid, selectlang);
//save data
if (tabData.Exists && baseData.Exists)
{
baselang = baselang.Substring(0, 2);
selectlang = selectlang.Substring(0, 2);
var nodList = baseData.DataLangRecord.XMLDoc.SelectNodes("genxml/textbox/*");
if (nodList != null)
{
foreach (XmlNode nod in nodList)
{
var newText = Utils.GetTranslatedText(headerValue, nod.InnerText, baselang, selectlang);
tabData.DataLangRecord.SetXmlProperty("genxml/textbox/" + nod.Name, newText);
}
tabData.Save();
}
}
}
}
catch (Exception ex)
{
// ignore
}
}
#endregion
#region "functions"
private String GetTemplateData(String templatename,String lang)
{
var controlMapPath = HttpContext.Current.Server.MapPath("/DesktopModules/NBright/NBrightPL");
var templCtrl = new NBrightCore.TemplateEngine.TemplateGetter(PortalSettings.Current.HomeDirectoryMapPath, controlMapPath, "Themes\\config", "");
var templ = templCtrl.GetTemplateData(templatename, lang);
templ = Utils.ReplaceUrlTokens(templ);
return templ;
}
private NBrightInfo GetAjaxFields(HttpContext context)
{
var strIn = HttpUtility.UrlDecode(Utils.RequestParam(context, "inputxml"));
var xmlData = GenXmlFunctions.GetGenXmlByAjax(strIn, "");
var objInfo = new NBrightInfo();
objInfo.ItemID = -1;
objInfo.TypeCode = "AJAXDATA";
objInfo.XMLData = xmlData;
var dic = objInfo.ToDictionary();
// set langauge if we have it passed.
if (dic.ContainsKey("lang") && dic["lang"] != "") _lang = dic["lang"];
// set the context culturecode, so any DNN functions use the correct culture (entryurl tag token)
if (_lang != "" && _lang != System.Threading.Thread.CurrentThread.CurrentCulture.ToString()) System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(_lang);
return objInfo;
}
private Boolean CheckRights()
{
if (UserController.GetCurrentUserInfo().IsInRole("Manager") || UserController.GetCurrentUserInfo().IsInRole("Editor") || UserController.GetCurrentUserInfo().IsInRole("Administrators"))
{
return true;
}
return false;
}
#endregion
}
}