Skip to content

Commit b0df699

Browse files
committed
Merge branch 'release/v3.2'
2 parents 16cd70b + 38f0d3d commit b0df699

23 files changed

+565
-225
lines changed

AlpacaFormBuilder.ascx

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@
119119
var data = form.getValue();
120120
var schema = getSchema(data);
121121
var options = getOptions(data);
122-
var postData = JSON.stringify({ 'data': data, 'schema': schema, 'options': options });
122+
var view = getView(data);
123+
var postData = JSON.stringify({ 'data': data, 'schema': schema, 'options': options, 'view': view });
123124
var action = "UpdateBuilder";
124125
$.ajax({
125126
type: "POST",

AlpacaFormBuilder.ascx.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected override void OnInit(EventArgs e)
3030
base.OnInit(e);
3131
hlCancel.NavigateUrl = Globals.NavigateURL();
3232
cmdSave.NavigateUrl = Globals.NavigateURL();
33-
AlpacaEngine alpaca = new AlpacaEngine(Page, ModuleContext, "" /*settings.Template.Uri().FolderPath*/, "builder");
33+
AlpacaEngine alpaca = new AlpacaEngine(Page, ModuleContext.PortalId, "" /*settings.Template.Uri().FolderPath*/, "builder");
3434
alpaca.RegisterAll(true);
3535
ClientResourceManager.RegisterScript(Page, "~/DesktopModules/OpenForm/js/builder/formbuilder.js", FileOrder.Js.DefaultPriority);
3636
ClientResourceManager.RegisterStyleSheet(Page, "~/DesktopModules/OpenForm/js/builder/formbuilder.css", FileOrder.Css.DefaultPriority);

App_LocalResources/View.ascx.fr-FR.resx

+3
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,7 @@
153153
<data name="ShareTemplate.Action" xml:space="preserve">
154154
<value>Template Exchange</value>
155155
</data>
156+
<data name="Sending.Text" xml:space="preserve">
157+
<value>Envoi en cours ...</value>
158+
</data>
156159
</root>

App_LocalResources/View.ascx.nl-BE.resx

+3
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,7 @@
123123
<data name="cmdSave.Text" xml:space="preserve">
124124
<value>Verzenden</value>
125125
</data>
126+
<data name="Sending.Text" xml:space="preserve">
127+
<value>Verzending...</value>
128+
</data>
126129
</root>

App_LocalResources/View.ascx.resx

+3
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,7 @@
162162
<data name="EditData.Action" xml:space="preserve">
163163
<value>Edit Raw Data</value>
164164
</data>
165+
<data name="Sending.Text" xml:space="preserve">
166+
<value>Sending...</value>
167+
</data>
165168
</root>

Components/OpenFormAPIController.cs

+56-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
using RecaptchaV2.NET;
3535
using DotNetNuke.Security;
3636
using Satrabel.OpenContent.Components;
37+
using Satrabel.OpenContent.Components.Form;
3738
using Satrabel.OpenContent.Components.Logging;
3839

3940
#endregion
@@ -63,8 +64,11 @@ public HttpResponseMessage Form()
6364
string templateFilename = HostingEnvironment.MapPath("~/" + template);
6465
string schemaFilename = Path.GetDirectoryName(templateFilename) + "\\" + "schema.json";
6566

66-
JObject schemaJson = JsonUtils.GetJsonFromFile(schemaFilename);
67-
json["schema"] = schemaJson;
67+
json["schema"] = JsonUtils.GetJsonFromFile(schemaFilename);
68+
if (UserInfo.UserID > 0 && json["schema"] is JObject)
69+
{
70+
json["schema"] = FormUtils.InitFields(json["schema"] as JObject, UserInfo);
71+
}
6872

6973
// default options
7074
string optionsFilename = Path.GetDirectoryName(templateFilename) + "\\" + "options.json";
@@ -99,6 +103,8 @@ public HttpResponseMessage Form()
99103
json["view"] = viewJson;
100104
}
101105
}
106+
107+
102108
}
103109
return Request.CreateResponse(HttpStatusCode.OK, json);
104110
}
@@ -188,10 +194,40 @@ public HttpResponseMessage Submit(JObject form)
188194
form.Remove("recaptcha");
189195
}
190196

191-
data = OpenFormUtils.GenerateFormData(form.ToString(), out formData);
197+
198+
string template = (string)ActiveModule.ModuleSettings["template"];
199+
string templateFilename = HostingEnvironment.MapPath("~/" + template);
200+
string schemaFilename = Path.GetDirectoryName(templateFilename) + "\\" + "schema.json";
201+
JObject schemaJson = JsonUtils.GetJsonFromFile(schemaFilename);
202+
//form["schema"] = schemaJson;
203+
// default options
204+
string optionsFilename = Path.GetDirectoryName(templateFilename) + "\\" + "options.json";
205+
JObject optionsJson = null;
206+
if (File.Exists(optionsFilename))
207+
{
208+
string fileContent = File.ReadAllText(optionsFilename);
209+
if (!string.IsNullOrWhiteSpace(fileContent))
210+
{
211+
optionsJson = JObject.Parse(fileContent);
212+
//form["options"] = optionsJson;
213+
}
214+
}
215+
// language options
216+
optionsFilename = Path.GetDirectoryName(templateFilename) + "\\" + "options." + DnnUtils.GetCurrentCultureCode() + ".json";
217+
if (File.Exists(optionsFilename))
218+
{
219+
string fileContent = File.ReadAllText(optionsFilename);
220+
if (!string.IsNullOrWhiteSpace(fileContent))
221+
{
222+
optionsJson = JObject.Parse(fileContent);
223+
//form["options"] = optionsJson;
224+
}
225+
}
226+
var enhancedForm = form.DeepClone() as JObject;
227+
OpenFormUtils.ResolveLabels(enhancedForm, schemaJson, optionsJson);
228+
data = OpenFormUtils.GenerateFormData(enhancedForm.ToString(), out formData);
192229
}
193230

194-
195231
if (settings != null && settings.Notifications != null)
196232
{
197233
foreach (var notification in settings.Notifications)
@@ -208,6 +244,7 @@ public HttpResponseMessage Submit(JObject form)
208244
string body = formData;
209245
if (!string.IsNullOrEmpty(notification.EmailBody))
210246
{
247+
211248
body = hbs.Execute(notification.EmailBody, data);
212249
}
213250

@@ -287,19 +324,22 @@ public HttpResponseMessage UpdateBuilder(JObject json)
287324
{
288325
string templateFilename = HostingEnvironment.MapPath("~/" + Template);
289326
string dataDirectory = Path.GetDirectoryName(templateFilename) + "\\";
290-
if (json["data"] != null && json["schema"] != null && json["options"] != null)
327+
if (json["data"] != null && json["schema"] != null && json["options"] != null && json["view"] != null)
291328
{
292329
var schema = json["schema"].ToString();
293330
var options = json["options"].ToString();
331+
var view = json["view"].ToString();
294332
var data = json["data"].ToString();
295333
var datafile = dataDirectory + "builder.json";
296334
var schemafile = dataDirectory + "schema.json";
297335
var optionsfile = dataDirectory + "options.json";
336+
var viewfile = dataDirectory + "view.json";
298337
try
299338
{
300339
File.WriteAllText(datafile, data);
301340
File.WriteAllText(schemafile, schema);
302341
File.WriteAllText(optionsfile, options);
342+
File.WriteAllText(viewfile, view);
303343
}
304344
catch (Exception ex)
305345
{
@@ -334,12 +374,14 @@ private MailAddress GenerateMailAddress(string TypeOfAddress, string Email, stri
334374

335375
if (TypeOfAddress == "host")
336376
{
337-
adr = new MailAddress(Host.HostEmail, Host.HostTitle);
377+
if (Validate.IsValidEmail(Host.HostEmail))
378+
adr = new MailAddress(Host.HostEmail, Host.HostTitle);
338379
}
339380
else if (TypeOfAddress == "admin")
340381
{
341382
var user = UserController.GetUserById(PortalSettings.PortalId, PortalSettings.AdministratorId);
342-
adr = new MailAddress(user.Email, user.DisplayName);
383+
if (Validate.IsValidEmail(user.Email))
384+
adr = new MailAddress(user.Email, user.DisplayName);
343385
}
344386
else if (TypeOfAddress == "form")
345387
{
@@ -348,19 +390,21 @@ private MailAddress GenerateMailAddress(string TypeOfAddress, string Email, stri
348390
if (string.IsNullOrEmpty(FormEmailField))
349391
FormEmailField = "email";
350392

351-
string FormEmail = GetProperty(form, FormEmailField);
352-
string FormName = GetProperty(form, FormNameField);
353-
adr = new MailAddress(FormEmail, FormName);
393+
string formEmail = GetProperty(form, FormEmailField);
394+
string formName = GetProperty(form, FormNameField);
395+
if (Validate.IsValidEmail(formEmail))
396+
adr = new MailAddress(formEmail, formName);
354397
}
355398
else if (TypeOfAddress == "custom")
356399
{
357-
adr = new MailAddress(Email, Name);
400+
if (Validate.IsValidEmail(Email))
401+
adr = new MailAddress(Email, Name);
358402
}
359403
else if (TypeOfAddress == "current")
360404
{
361405
if (UserInfo == null)
362406
throw new Exception(string.Format("Can't send email to current user, as there is no current user. Parameters were TypeOfAddress: [{0}], Email: [{1}], Name: [{2}], FormEmailField: [{3}], FormNameField: [{4}], FormNameField: [{5}]", TypeOfAddress, Email, Name, FormEmailField, FormNameField, form));
363-
if (string.IsNullOrEmpty(UserInfo.Email))
407+
if (Validate.IsValidEmail(UserInfo.Email))
364408
throw new Exception(string.Format("Can't send email to current user, as email address of current user is unknown. Parameters were TypeOfAddress: [{0}], Email: [{1}], Name: [{2}], FormEmailField: [{3}], FormNameField: [{4}], FormNameField: [{5}]", TypeOfAddress, Email, Name, FormEmailField, FormNameField, form));
365409

366410
adr = new MailAddress(UserInfo.Email, UserInfo.DisplayName);

Components/OpenFormUtils.cs

+81
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Satrabel.OpenContent.Components.Json;
55
using System;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.IO;
89
using System.Linq;
910
using System.Text;
@@ -146,6 +147,86 @@ public static string ReverseMapPath(string path)
146147
if (!res.StartsWith("/")) res = "/" + res;
147148
return res;
148149
}
150+
151+
public static void ResolveLabels(JObject o, JObject schema, JObject options)
152+
{
153+
foreach (var child in o.Children<JProperty>().ToList())
154+
{
155+
JObject sch = null;
156+
if (schema?["properties"] != null)
157+
{
158+
sch = schema["properties"][child.Name] as JObject;
159+
}
160+
JObject opt = null;
161+
if (options?["fields"] != null)
162+
{
163+
opt = options["fields"][child.Name] as JObject;
164+
}
165+
if (opt == null) continue;
166+
167+
JArray optionLabels = opt["optionLabels"] as JArray;
168+
169+
var childProperty = child;
170+
if (childProperty.Value is JArray)
171+
{
172+
var array = childProperty.Value as JArray;
173+
JArray newArray = new JArray();
174+
foreach (var value in array)
175+
{
176+
var obj = value as JObject;
177+
if (obj != null)
178+
{
179+
ResolveLabels(obj, sch["items"] as JObject, opt["items"] as JObject);
180+
}
181+
else if (optionLabels != null)
182+
{
183+
var val = value as JValue;
184+
if (val != null)
185+
{
186+
try
187+
{
188+
var aEnum = sch["enum"] as JArray;
189+
var idx = Array.IndexOf(aEnum.Select(e => e.ToString()).ToArray(), val.ToString());
190+
newArray.Add(optionLabels[idx]);
191+
}
192+
catch (System.Exception)
193+
{
194+
}
195+
}
196+
}
197+
}
198+
199+
if (optionLabels != null)
200+
{
201+
childProperty.Value = newArray;
202+
}
203+
}
204+
else if (childProperty.Value is JObject)
205+
{
206+
var obj = childProperty.Value as JObject;
207+
ResolveLabels(obj, sch, opt);
208+
}
209+
else if (childProperty.Value is JValue)
210+
{
211+
if (optionLabels != null)
212+
{
213+
string val = childProperty.Value.ToString();
214+
try
215+
{
216+
var aEnum = sch["enum"] as JArray;
217+
var idx = Array.IndexOf(aEnum.Select(e => e.ToString()).ToArray(), val.ToString());
218+
o[childProperty.Name] = optionLabels[idx];
219+
220+
}
221+
catch (System.Exception)
222+
{
223+
Debugger.Break();
224+
}
225+
}
226+
}
227+
}
228+
}
229+
149230
}
150231

151232
}

Components/Validate.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Text.RegularExpressions;
4+
5+
namespace Satrabel.OpenForm.Components
6+
{
7+
public static class Validate
8+
{
9+
10+
/// <summary>
11+
/// Determines whether email is valid.
12+
/// </summary>
13+
/// <remarks>
14+
/// https://technet.microsoft.com/nl-be/library/01escwtf(v=vs.110).aspx
15+
/// </remarks>
16+
public static bool IsValidEmail(string strIn)
17+
{
18+
bool invalid = false;
19+
if (string.IsNullOrEmpty(strIn))
20+
return false;
21+
22+
// Use IdnMapping class to convert Unicode domain names.
23+
try
24+
{
25+
strIn = Regex.Replace(strIn, @"(@)(.+)$", DomainMapper);
26+
}
27+
catch (Exception e)
28+
{
29+
invalid = true;
30+
}
31+
32+
if (invalid)
33+
return false;
34+
35+
// Return true if strIn is in valid e-mail format.
36+
return Regex.IsMatch(strIn,
37+
@"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
38+
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",
39+
RegexOptions.IgnoreCase);
40+
}
41+
42+
private static string DomainMapper(Match match)
43+
{
44+
// IdnMapping class with default property values.
45+
IdnMapping idn = new IdnMapping();
46+
string domainName = match.Groups[2].Value;
47+
domainName = idn.GetAscii(domainName);
48+
return match.Groups[1].Value + domainName;
49+
}
50+
}
51+
}

Edit.ascx

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<%@ Import Namespace="DotNetNuke.Services.Localization" %>
33
<%@ Register TagPrefix="dnn" TagName="label" Src="~/controls/LabelControl.ascx" %>
44
<div class="dnnForm dnnEditBasicSettings" id="dnnEditBasicSettings">
5+
<p><asp:LinkButton ID="excelDownload" runat="server" Text="Download as Excel" OnClick="ExcelDownload_Click"/></p>
56
<fieldset>
67
<div class="dnnFormItem">
78
<asp:GridView ID="gvData" runat="server" CssClass="dnnGrid" GridLines="None"

0 commit comments

Comments
 (0)