1
- using DotNetNuke . Entities . Portals ;
1
+ using DotNetNuke . Common ;
2
+ using DotNetNuke . Entities . Portals ;
2
3
using Newtonsoft . Json . Linq ;
3
4
using Satrabel . OpenContent . Components . Datasource ;
5
+ using Satrabel . OpenContent . Components . Datasource . Search ;
6
+ using Satrabel . OpenContent . Components . Dnn ;
7
+ using Satrabel . OpenContent . Components . Handlebars ;
4
8
using Satrabel . OpenContent . Components . Json ;
9
+ using Satrabel . OpenContent . Components . Localization ;
5
10
using Satrabel . OpenContent . Components . Manifest ;
11
+ using Satrabel . OpenContent . Components . Querying ;
12
+ using Satrabel . OpenContent . Components . TemplateHelpers ;
6
13
using System ;
7
14
using System . Collections . Generic ;
15
+ using System . Diagnostics ;
16
+ using System . IO ;
8
17
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
+
13
20
14
21
namespace Satrabel . OpenContent . Components . Render
15
22
{
@@ -167,10 +174,12 @@ protected void EnhanceSelect2(JObject model, bool onlyData)
167
174
}
168
175
if ( _optionsJson != null )
169
176
{
170
- JsonUtils . LookupSelect2InOtherModule ( model , _optionsJson ) ;
177
+ LookupSelect2InOtherModule ( model , _optionsJson , onlyData ) ;
171
178
}
172
179
}
173
180
181
+
182
+
174
183
protected void ExtendModel ( JObject model , bool onlyData , bool onlyMainData )
175
184
{
176
185
if ( _module . CanvasUnavailable ) onlyData = true ;
@@ -208,18 +217,17 @@ protected void ExtendModel(JObject model, bool onlyData, bool onlyMainData)
208
217
foreach ( var dataItem in dataItems . Items )
209
218
{
210
219
var json = dataItem . Data ;
211
-
212
- if ( json != null && DnnLanguageUtils . GetPortalLocales ( _portalId ) . Count > 1 )
220
+ if ( json != null )
213
221
{
214
222
JsonUtils . SimplifyJson ( json , GetCurrentCultureCode ( ) ) ;
215
223
}
216
-
217
224
if ( json is JObject )
218
225
{
219
226
JObject context = new JObject ( ) ;
220
227
json [ "Context" ] = context ;
221
228
context [ "Id" ] = dataItem . Id ;
222
229
EnhanceSelect2 ( json as JObject , onlyData ) ;
230
+ JsonUtils . SimplifyJson ( json , GetCurrentCultureCode ( ) ) ;
223
231
}
224
232
colDataJson . Add ( json ) ;
225
233
}
@@ -389,5 +397,139 @@ protected bool IsEditMode
389
397
}
390
398
}
391
399
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
+ }
392
534
}
393
535
}
0 commit comments