Skip to content

Commit 8cc8a2e

Browse files
committed
Merge branch 'develop' into feature/v4
# Conflicts: # OpenContent/Components/OpenContentAPIController.cs # OpenContent/js/builder/formbuilder.js
2 parents a1a3e30 + 4f4743f commit 8cc8a2e

File tree

9 files changed

+240
-154
lines changed

9 files changed

+240
-154
lines changed

OpenContent/Components/Handlebars/HandlebarsEngine.cs

+20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public void Compile(string source)
4747
RegisterTruncateWordsHelper(hbs);
4848
RegisterReplaceHelper(hbs);
4949
RegisterReplaceNewlineHelper(hbs);
50+
RegisterTemplateHelper(hbs);
5051
_template = hbs.Compile(source);
5152
}
5253
catch (Exception ex)
@@ -122,6 +123,7 @@ private static void RegisterHelpers(IHandlebars hbs)
122123
RegisterTruncateWordsHelper(hbs);
123124
RegisterReplaceHelper(hbs);
124125
RegisterReplaceNewlineHelper(hbs);
126+
RegisterTemplateHelper(hbs);
125127
}
126128

127129
private static void RegisterTruncateWordsHelper(HandlebarsDotNet.IHandlebars hbs)
@@ -992,5 +994,23 @@ private static void RegisterConvertToJsonHelper(HandlebarsDotNet.IHandlebars hbs
992994
});
993995
}
994996

997+
private static void RegisterTemplateHelper(HandlebarsDotNet.IHandlebars hbs)
998+
{
999+
hbs.RegisterHelper("template", (writer, context, parameters) =>
1000+
{
1001+
try
1002+
{
1003+
string html = parameters[0].ToString();
1004+
HandlebarsEngine hbs2 = new HandlebarsEngine();
1005+
var res = hbs2.Execute(html, parameters[1]);
1006+
writer.WriteSafeString(res);
1007+
}
1008+
catch (Exception ex)
1009+
{
1010+
writer.WriteSafeString("");
1011+
}
1012+
});
1013+
}
1014+
9951015
}
9961016
}

OpenContent/Components/Json/JsonUtils.cs

+3-111
Original file line numberDiff line numberDiff line change
@@ -387,100 +387,7 @@ public static void LookupJson(JObject o, JObject additionalData, JObject schema,
387387
}
388388
}
389389
}
390-
391-
public static void LookupSelect2InOtherModule(JObject o, JObject options)
392-
{
393-
foreach (var child in o.Children<JProperty>().ToList())
394-
{
395-
JObject opt = null;
396-
if (options?["fields"] != null)
397-
{
398-
opt = options["fields"][child.Name] as JObject;
399-
}
400-
if (opt == null) continue;
401-
bool lookup =
402-
opt["type"] != null &&
403-
opt["type"].ToString() == "select2" &&
404-
opt["dataService"]?["data"]?["moduleId"] != null &&
405-
opt["dataService"]?["data"]?["tabId"] != null;
406-
407-
string dataMember = "";
408-
string valueField = "Id";
409-
string moduleId = "";
410-
string tabId = "";
411-
if (lookup)
412-
{
413-
dataMember = opt["dataService"]["data"]["dataMember"]?.ToString() ?? "";
414-
valueField = opt["dataService"]["data"]["valueField"]?.ToString() ?? "Id";
415-
moduleId = opt["dataService"]["data"]["moduleId"]?.ToString() ?? "0";
416-
tabId = opt["dataService"]["data"]["tabId"]?.ToString() ?? "0";
417-
}
418-
419-
var childProperty = child;
420-
421-
if (childProperty.Value is JArray)
422-
{
423-
var array = childProperty.Value as JArray;
424-
JArray newArray = new JArray();
425-
foreach (var value in array)
426-
{
427-
var obj = value as JObject;
428-
if (obj != null)
429-
{
430-
LookupSelect2InOtherModule(obj, opt["items"] as JObject);
431-
}
432-
else if (lookup)
433-
{
434-
var val = value as JValue;
435-
if (val != null)
436-
{
437-
try
438-
{
439-
var module = OpenContentModuleConfig.Create(int.Parse(moduleId), int.Parse(tabId), PortalSettings.Current);
440-
var ds = DataSourceManager.GetDataSource(module.Settings.Manifest.DataSource);
441-
var dsContext = OpenContentUtils.CreateDataContext(module);
442-
IDataItem dataItem = ds.Get(dsContext, val.ToString());
443-
newArray.Add(GenerateObject(dataItem, val.ToString()));
444-
}
445-
catch (System.Exception)
446-
{
447-
Debugger.Break();
448-
}
449-
}
450-
}
451-
}
452-
if (lookup)
453-
{
454-
childProperty.Value = newArray;
455-
}
456-
}
457-
else if (childProperty.Value is JObject)
458-
{
459-
var obj = childProperty.Value as JObject;
460-
LookupSelect2InOtherModule(obj, opt);
461-
}
462-
else if (childProperty.Value is JValue)
463-
{
464-
if (lookup)
465-
{
466-
string val = childProperty.Value.ToString();
467-
try
468-
{
469-
var module = OpenContentModuleConfig.Create(int.Parse(moduleId), int.Parse(tabId), PortalSettings.Current);
470-
var ds = DataSourceManager.GetDataSource(module.Settings.Manifest.DataSource);
471-
var dsContext = OpenContentUtils.CreateDataContext(module);
472-
IDataItem dataItem = ds.Get(dsContext, val);
473-
o[childProperty.Name] = GenerateObject(dataItem, val);
474-
}
475-
catch (System.Exception ex)
476-
{
477-
Debugger.Break();
478-
}
479-
}
480-
}
481-
}
482-
}
483-
390+
484391
/// <summary>
485392
/// Enhance data for all alpaca fields of type 'image2' and 'mlimage2'
486393
/// </summary>
@@ -584,22 +491,8 @@ private static string GetFileEditUrl(IFileInfo f)
584491
var portalFileUri = new PortalFileUri(f);
585492
return portalFileUri.EditUrl();
586493
}
587-
private static JObject GenerateObject(IDataItem additionalData, string id)
588-
{
589-
var json = additionalData?.Data;
590-
//if (!string.IsNullOrEmpty(dataMember))
591-
//{
592-
// json = json[dataMember];
593-
//}
594-
if (json != null)
595-
{
596-
return json as JObject;
597-
}
598-
JObject res = new JObject();
599-
res["Id"] = id;
600-
res["Title"] = "unknow";
601-
return res;
602-
}
494+
495+
603496

604497
private static JObject GenerateObject(JObject additionalData, string key, string id, string dataMember, string valueField, string childerenField)
605498
{
@@ -749,7 +642,6 @@ public static void RemoveType(JToken o)
749642
}
750643
}
751644
}
752-
753645
}
754646
class DictionaryNoCase : Dictionary<string, object>
755647
{

OpenContent/Components/Render/ModelFactoryBase.cs

+151-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
using DotNetNuke.Entities.Portals;
1+
using DotNetNuke.Common;
2+
using DotNetNuke.Entities.Portals;
23
using Newtonsoft.Json.Linq;
34
using Satrabel.OpenContent.Components.Datasource;
5+
using Satrabel.OpenContent.Components.Datasource.Search;
6+
using Satrabel.OpenContent.Components.Dnn;
7+
using Satrabel.OpenContent.Components.Handlebars;
48
using Satrabel.OpenContent.Components.Json;
9+
using Satrabel.OpenContent.Components.Localization;
510
using Satrabel.OpenContent.Components.Manifest;
11+
using Satrabel.OpenContent.Components.Querying;
12+
using Satrabel.OpenContent.Components.TemplateHelpers;
613
using System;
714
using System.Collections.Generic;
15+
using System.Diagnostics;
16+
using System.IO;
817
using System.Linq;
9-
using Satrabel.OpenContent.Components.Datasource.Search;
10-
using Satrabel.OpenContent.Components.Dnn;
11-
using Satrabel.OpenContent.Components.Localization;
12-
using Satrabel.OpenContent.Components.Querying;
18+
using System.Web;
19+
1320

1421
namespace Satrabel.OpenContent.Components.Render
1522
{
@@ -167,10 +174,12 @@ protected void EnhanceSelect2(JObject model, bool onlyData)
167174
}
168175
if (_optionsJson != null)
169176
{
170-
JsonUtils.LookupSelect2InOtherModule(model, _optionsJson);
177+
LookupSelect2InOtherModule(model, _optionsJson, onlyData);
171178
}
172179
}
173180

181+
182+
174183
protected void ExtendModel(JObject model, bool onlyData, bool onlyMainData)
175184
{
176185
if (_module.CanvasUnavailable) onlyData = true;
@@ -208,18 +217,17 @@ protected void ExtendModel(JObject model, bool onlyData, bool onlyMainData)
208217
foreach (var dataItem in dataItems.Items)
209218
{
210219
var json = dataItem.Data;
211-
212-
if (json != null && DnnLanguageUtils.GetPortalLocales(_portalId).Count > 1)
220+
if (json != null )
213221
{
214222
JsonUtils.SimplifyJson(json, GetCurrentCultureCode());
215223
}
216-
217224
if (json is JObject)
218225
{
219226
JObject context = new JObject();
220227
json["Context"] = context;
221228
context["Id"] = dataItem.Id;
222229
EnhanceSelect2(json as JObject, onlyData);
230+
JsonUtils.SimplifyJson(json, GetCurrentCultureCode());
223231
}
224232
colDataJson.Add(json);
225233
}
@@ -389,5 +397,139 @@ protected bool IsEditMode
389397
}
390398
}
391399

400+
protected void LookupSelect2InOtherModule(JObject model, JObject options, bool onlyData)
401+
{
402+
403+
foreach (var child in model.Children<JProperty>().ToList())
404+
{
405+
JObject opt = null;
406+
if (options?["fields"] != null)
407+
{
408+
opt = options["fields"][child.Name] as JObject;
409+
}
410+
if (opt == null) continue;
411+
bool lookup =
412+
opt["type"] != null &&
413+
opt["type"].ToString() == "select2" &&
414+
opt["dataService"]?["action"] != null &&
415+
opt["dataService"]?["action"].ToString() == "Lookup";
416+
417+
//opt["dataService"]?["data"]?["moduleId"] != null &&
418+
//opt["dataService"]?["data"]?["tabId"] != null;
419+
420+
string dataMember = "";
421+
string valueField = "Id";
422+
string moduleId = "";
423+
string tabId = "";
424+
if (lookup)
425+
{
426+
dataMember = opt["dataService"]["data"]["dataMember"]?.ToString() ?? "";
427+
valueField = opt["dataService"]["data"]["valueField"]?.ToString() ?? "Id";
428+
moduleId = opt["dataService"]["data"]["moduleId"]?.ToString() ?? "0";
429+
tabId = opt["dataService"]["data"]["tabId"]?.ToString() ?? "0";
430+
}
431+
432+
var childProperty = child;
433+
434+
if (childProperty.Value is JArray)
435+
{
436+
var array = childProperty.Value as JArray;
437+
JArray newArray = new JArray();
438+
foreach (var value in array)
439+
{
440+
var obj = value as JObject;
441+
if (obj != null)
442+
{
443+
LookupSelect2InOtherModule(obj, opt["items"] as JObject, onlyData);
444+
}
445+
else if (lookup)
446+
{
447+
var val = value as JValue;
448+
if (val != null)
449+
{
450+
try
451+
{
452+
newArray.Add(GenerateObject(val.ToString(), int.Parse(tabId), int.Parse(moduleId), onlyData));
453+
}
454+
catch (System.Exception)
455+
{
456+
Debugger.Break();
457+
}
458+
}
459+
}
460+
}
461+
if (lookup)
462+
{
463+
childProperty.Value = newArray;
464+
}
465+
}
466+
else if (childProperty.Value is JObject)
467+
{
468+
var obj = childProperty.Value as JObject;
469+
LookupSelect2InOtherModule(obj, opt, onlyData);
470+
}
471+
else if (childProperty.Value is JValue)
472+
{
473+
if (lookup)
474+
{
475+
string val = childProperty.Value.ToString();
476+
try
477+
{
478+
model[childProperty.Name] = GenerateObject(val, int.Parse(tabId), int.Parse(moduleId), onlyData);
479+
}
480+
catch (System.Exception ex)
481+
{
482+
Debugger.Break();
483+
}
484+
}
485+
}
486+
}
487+
}
488+
489+
private JToken GenerateObject(string id, int tabId, int moduleId, bool onlyData)
490+
{
491+
var module = moduleId> 0 ? OpenContentModuleConfig.Create(moduleId, tabId, PortalSettings.Current) : _module;
492+
var ds = DataSourceManager.GetDataSource(module.Settings.Manifest.DataSource);
493+
var dsContext = OpenContentUtils.CreateDataContext(module);
494+
IDataItem dataItem = ds.Get(dsContext, id);
495+
if (dataItem != null)
496+
{
497+
var json = dataItem?.Data?.DeepClone() as JObject;
498+
//if (!string.IsNullOrEmpty(dataMember))
499+
//{
500+
// json = json[dataMember];
501+
//}
502+
if (json != null)
503+
{
504+
JsonUtils.SimplifyJson(json, GetCurrentCultureCode());
505+
if (!onlyData)
506+
{
507+
var context = new JObject();
508+
json["Context"] = context;
509+
context["Id"] = dataItem.Id;
510+
context["DetailUrl"] = GenerateDetailUrl(dataItem, json, module.Settings.Manifest, tabId > 0 ? tabId : _detailTabId);
511+
}
512+
return json;
513+
}
514+
}
515+
JObject res = new JObject();
516+
res["Id"] = id;
517+
res["Title"] = "unknow";
518+
return res;
519+
}
520+
521+
protected string GenerateDetailUrl(IDataItem item, JObject dyn, Manifest.Manifest manifest, int detailTabId)
522+
{
523+
string url = "";
524+
if (!string.IsNullOrEmpty(manifest.DetailUrl))
525+
{
526+
HandlebarsEngine hbEngine = new HandlebarsEngine();
527+
var dynForHBS = JsonUtils.JsonToDictionary(dyn.ToString());
528+
529+
url = hbEngine.Execute(manifest.DetailUrl, dynForHBS);
530+
url = HttpUtility.HtmlDecode(url);
531+
}
532+
return _module.GetUrl(_detailTabId, url.CleanupUrl(), "id=" + item.Id);
533+
}
392534
}
393535
}

0 commit comments

Comments
 (0)