-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathView.ascx.cs
411 lines (370 loc) · 17.9 KB
/
View.ascx.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#region Copyright
//
// Copyright (c) 2015 by Satrabel
//
#endregion
#region Using Statements
using System;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Framework;
using DotNetNuke.Common;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using Satrabel.OpenForm.Components;
using System.IO;
using DotNetNuke.Web.Client;
using DotNetNuke.Web.Client.ClientResourceManagement;
using Newtonsoft.Json;
using Satrabel.OpenContent.Components.Handlebars;
using System.Web.UI;
using Satrabel.OpenContent.Components;
using Satrabel.OpenContent.Components.Alpaca;
using Localization = DotNetNuke.Services.Localization.Localization;
using System.Web.Hosting;
using System.Linq;
using Satrabel.OpenContent.Components.Razor;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Satrabel.OpenContent.Components.Json;
#endregion
namespace Satrabel.OpenForm
{
public partial class View : PortalModuleBase, IActionable
{
#region Event Handlers
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//cmdSave.NavigateUrl = Globals.NavigateURL("", "result=1");
ServicesFramework.Instance.RequestAjaxScriptSupport();
ServicesFramework.Instance.RequestAjaxAntiForgerySupport();
//JavaScript.RequestRegistration(CommonJs.DnnPlugins); ;
//JavaScript.RequestRegistration(CommonJs.jQueryFileUpload);
RegisterFields();
}
private void RegisterFields()
{
Scripts = new List<string>();
List<string> fieldTypes = new List<string>();
JToken options = GetOptions();
if (options != null)
{
fieldTypes = FieldTypes(options);
}
Scripts.Add(Page.ResolveUrl("~/DesktopModules/OpenContent/js/lib/handlebars/handlebars.min.js"));
Scripts.Add(Page.ResolveUrl("~/DesktopModules/OpenContent/js/alpaca/bootstrap/alpaca.min.js"));
Scripts.Add(Page.ResolveUrl("~/DesktopModules/OpenContent/alpaca/js/fields/dnn/CheckboxField.js"));
if (fieldTypes.Contains("date") || fieldTypes.Contains("datetime") || fieldTypes.Contains("time"))
{
//ClientResourceManager.RegisterScript(Page, "~/DesktopModules/OpenContent/alpaca/js/fields/dnn/DateField.js", FileOrder.Js.DefaultPriority+10, "DnnPageHeaderProvider");
//ClientResourceManager.RegisterScript(Page, "~/DesktopModules/OpenContent/js/lib/moment/min/moment-with-locales.min.js", FileOrder.Js.DefaultPriority+10, "DnnPageHeaderProvider");
//ClientResourceManager.RegisterScript(Page, "~/DesktopModules/OpenContent/js/lib/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js", FileOrder.Js.DefaultPriority + 11, "DnnPageHeaderProvider");
ClientResourceManager.RegisterStyleSheet(Page, "~/DesktopModules/OpenContent/js/lib/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.css", FileOrder.Css.DefaultPriority+11);
Scripts.Add(Page.ResolveUrl("~/DesktopModules/OpenContent/alpaca/js/fields/dnn/DateField.js"));
Scripts.Add(Page.ResolveUrl("~/DesktopModules/OpenContent/js/lib/moment/min/moment-with-locales.min.js"));
Scripts.Add(Page.ResolveUrl("~/DesktopModules/OpenContent/js/lib/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js"));
}
if (fieldTypes.Contains("summernote") || fieldTypes.Contains("mlsummernote"))
{
//ClientResourceManager.RegisterScript(Page, "~/DesktopModules/OpenContent/alpaca/js/fields/dnn/SummernoteField.js", FileOrder.Js.DefaultPriority+10, "DnnPageHeaderProvider");
//ClientResourceManager.RegisterScript(Page, "~/DesktopModules/OpenContent/js/summernote/summernote.min.js", FileOrder.Js.DefaultPriority+10, "DnnPageHeaderProvider");
ClientResourceManager.RegisterStyleSheet(Page, "~/DesktopModules/OpenContent/js/summernote/summernote.css", FileOrder.Css.DefaultPriority+10);
Scripts.Add(Page.ResolveUrl("~/DesktopModules/OpenContent/alpaca/js/fields/dnn/SummernoteField.js"));
Scripts.Add(Page.ResolveUrl( "~/DesktopModules/OpenContent/js/summernote/summernote.min.js"));
}
}
private JToken GetOptions()
{
string Template = ModuleContext.Settings["template"] as string;
string templateFilename = HostingEnvironment.MapPath("~/" + Template);
//string templateFilename = "~/" + Template;
string optionsFilename = Path.GetDirectoryName(templateFilename) + "\\" + "options.json";
// default options
JToken optionsJson = JsonUtils.GetJsonFromFile(optionsFilename);
// language options
optionsFilename = Path.GetDirectoryName(templateFilename) + "\\" + $"options.{DnnLanguageUtils.GetCurrentCultureCode()}.json";
if (File.Exists(optionsFilename))
{
JToken languageOptionsJson = JsonUtils.GetJsonFromFile(optionsFilename);
optionsJson = optionsJson.JsonMerge(languageOptionsJson);
}
return optionsJson;
}
private static List<string> FieldTypes(JToken options)
{
var types = new List<string>();
var fields = options["fields"];
if (fields != null)
{
foreach (JProperty fieldProp in fields.Children())
{
var field = fieldProp.First();
var fieldtype = field["type"];
if (fieldtype != null)
{
types.Add(fieldtype.ToString());
}
var subtypes = FieldTypes(field);
types.AddRange(subtypes);
}
}
else if (options["items"] != null)
{
if (options["items"]["type"] != null)
{
var fieldtype = options["items"]["type"] as JValue;
types.Add(fieldtype.Value.ToString());
}
var subtypes = FieldTypes(options["items"]);
types.AddRange(subtypes);
}
return types;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string Template = ModuleContext.Settings["template"] as string;
if (!Page.IsPostBack)
{
InitForm(Template);
}
else
{
if (Request.QueryString["result"] == "submit")
{
/*
int id = int.Parse(Request.QueryString["result"]);
OpenFormController ctrl =new OpenFormController();
var content = ctrl.GetContent(id, ModuleId);
*/
string json = Request["__OPENFORM" + ModuleId];
phForm.Visible = false;
phResult.Visible = true;
string formData = "";
dynamic data = OpenFormUtils.GenerateFormData(json, out formData);
string jsonSettings = Settings["data"] as string;
SettingsDTO settings = JsonConvert.DeserializeObject<SettingsDTO>(jsonSettings);
if (settings != null && settings.Settings != null)
{
if (!string.IsNullOrEmpty(settings.Settings.Message))
{
HandlebarsEngine hbs = new HandlebarsEngine();
lMessage.Text = hbs.Execute(settings.Settings.Message, data);
}
lTracking.Text = settings.Settings.Tracking;
}
var razorscript = new FileUri(Path.GetDirectoryName(Template), "aftersubmit.cshtml");
if (razorscript.FileExists)
{
data.FeedBackMessage = lMessage.Text;
data.IPAddress = Request.UserHostAddress;
lMessage.Text = ExecuteRazor(razorscript, data);
}
}
}
}
private string ExecuteRazor(FileUri template, dynamic model)
{
string webConfig = template.PhysicalFullDirectory;
webConfig = webConfig.Remove(webConfig.LastIndexOf("\\")) + "\\web.config";
if (!File.Exists(webConfig))
{
string filename = HostingEnvironment.MapPath("~/DesktopModules/OpenContent/Templates/web.config");
File.Copy(filename, webConfig);
}
var writer = new StringWriter();
try
{
var razorEngine = new RazorEngine("~/" + template.FilePath, ModuleContext, LocalResourceFile);
razorEngine.Render(writer, model);
}
catch (Exception ex)
{
//LoggingUtils.RenderEngineException(this, ex);
string stack = string.Join("\n", ex.StackTrace.Split('\n').Where(s => s.Contains("\\Portals\\") && s.Contains("in")).Select(s => s.Substring(s.IndexOf("in"))).ToArray());
//throw new TemplateException("Failed to render Razor template " + template.FilePath + "\n" + stack, ex, model, template.FilePath);
return "Failed to render Razor template " + template.FilePath + "\n" + stack;
}
return writer.ToString();
}
private void InitForm(string template)
{
bool templateDefined = !string.IsNullOrEmpty(template);
string settings = ModuleContext.Settings["data"] as string;
bool settingsDefined = !string.IsNullOrEmpty(settings);
if (!templateDefined || !settingsDefined)
{
pHelp.Visible = true;
}
if (ModuleContext.PortalSettings.UserInfo.IsSuperUser)
{
hlTempleteExchange.NavigateUrl = ModuleContext.EditUrl("ShareTemplate");
hlTempleteExchange.Visible = true;
}
if (pHelp.Visible && ModuleContext.EditMode)
{
hlEditSettings.NavigateUrl = ModuleContext.EditUrl("EditSettings");
hlEditSettings.Visible = true;
scriptList.Items.AddRange(OpenFormUtils.GetTemplatesFiles(PortalSettings, ModuleId, template).ToArray());
scriptList.Visible = true;
}
if (string.IsNullOrEmpty(template))
{
ScopeWrapper.Visible = false;
}
if (templateDefined)
{
IncludeResourses(template);
}
if (settingsDefined)
{
SettingsDTO set = JsonConvert.DeserializeObject<SettingsDTO>(settings);
if (!string.IsNullOrEmpty(set.Settings.SiteKey))
{
ClientResourceManager.RegisterScript(Page, "https://www.google.com/recaptcha/api.js", FileOrder.Js.DefaultPriority, "DnnPageHeaderProvider");
lReCaptcha.Text = "<div class=\"g-recaptcha\" data-sitekey=\"" + set.Settings.SiteKey + "\"></div>";
}
}
}
protected void cmdSave_Click(object sender, EventArgs e)
{
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, "Update Successful", DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
}
protected void cmdCancel_Click(object sender, EventArgs e)
{
}
#endregion
private void IncludeResourses(string template)
{
if (!(string.IsNullOrEmpty(template)))
{
var cssfilename = new FileUri(Path.GetDirectoryName(template), "template.css");
if (cssfilename.FileExists)
{
ClientResourceManager.RegisterStyleSheet(Page, Page.ResolveUrl(cssfilename.UrlFilePath), FileOrder.Css.PortalCss);
}
var jsfilename = new FileUri(Path.GetDirectoryName(template), "template.js");
if (jsfilename.FileExists)
{
ClientResourceManager.RegisterScript(Page, jsfilename.UrlFilePath, FileOrder.Js.DefaultPriority);
}
}
}
public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
{
get
{
var actions = new ModuleActionCollection();
actions.Add(ModuleContext.GetNextActionID(),
Localization.GetString(ModuleActionType.AddContent, LocalResourceFile),
ModuleActionType.AddContent,
"",
"~/DesktopModules/OpenContent/images/editcontent2.png",
ModuleContext.EditUrl(),
false,
SecurityAccessLevel.Edit,
true,
false);
actions.Add(ModuleContext.GetNextActionID(),
Localization.GetString("EditSettings.Action", LocalResourceFile),
ModuleActionType.ContentOptions,
"",
"~/DesktopModules/OpenContent/images/editsettings2.png",
ModuleContext.EditUrl("EditSettings"),
false,
SecurityAccessLevel.Edit,
true,
false);
var scriptFileSetting = Settings["template"] as string;
if (!string.IsNullOrEmpty(scriptFileSetting))
{
string templateFilename = Server.MapPath("~/" + scriptFileSetting);
string builderFilename = Path.GetDirectoryName(templateFilename) + "\\" + "builder.json";
if (File.Exists(builderFilename))
actions.Add(ModuleContext.GetNextActionID(),
Localization.GetString("Builder.Action", LocalResourceFile),
ModuleActionType.ContentOptions,
"",
"~/DesktopModules/OpenForm/images/formbuilder.png",
ModuleContext.EditUrl("FormBuilder"),
false,
SecurityAccessLevel.Edit,
true,
false);
actions.Add(ModuleContext.GetNextActionID(),
Localization.GetString("EditTemplate.Action", LocalResourceFile),
ModuleActionType.ContentOptions,
"",
"~/DesktopModules/OpenForm/images/edittemplate.png",
ModuleContext.EditUrl("EditTemplate"),
false,
SecurityAccessLevel.Host,
true,
false);
actions.Add(ModuleContext.GetNextActionID(),
Localization.GetString("EditData.Action", LocalResourceFile),
ModuleActionType.EditContent,
"",
"~/DesktopModules/OpenForm/images/edit.png",
//ModuleContext.EditUrl("EditData"),
ModuleContext.EditUrl("EditData"),
false,
SecurityAccessLevel.Host,
true,
false);
}
/*
Actions.Add(ModuleContext.GetNextActionID(),
Localization.GetString("EditData.Action", LocalResourceFile),
ModuleActionType.EditContent,
"",
"",
ModuleContext.EditUrl("EditData"),
false,
SecurityAccessLevel.Host,
true,
false);
*/
actions.Add(ModuleContext.GetNextActionID(),
Localization.GetString("ShareTemplate.Action", LocalResourceFile),
ModuleActionType.ContentOptions,
"",
"~/DesktopModules/OpenContent/images/exchange.png",
ModuleContext.EditUrl("ShareTemplate"),
false,
SecurityAccessLevel.Host,
true,
false);
return actions;
}
}
protected string AlpacaCulture
{
get
{
string cultureCode = LocaleController.Instance.GetCurrentLocale(PortalId).Code;
return AlpacaEngine.AlpacaCulture(cultureCode);
}
}
protected void scriptList_SelectedIndexChanged(object sender, EventArgs e)
{
ModuleController mc = new ModuleController();
mc.UpdateModuleSetting(ModuleId, "template", scriptList.SelectedValue);
}
protected void lbSave_Click(object sender, EventArgs e)
{
}
protected string PostBackStr()
{
//return Page.GetPostBackEventReference(lbSave);
PostBackOptions pb = new PostBackOptions(lbSave, null, Globals.NavigateURL("", "result=submit"), false, false, false, true, false, null);
return Page.ClientScript.GetPostBackEventReference(pb);
}
protected string GetString(string resourceKey)
{
return Localization.GetString(resourceKey, LocalResourceFile);
}
public List<string> Scripts { get; set; }
}
}