forked from sachatrauwaen/OpenContent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditData.ascx.cs
435 lines (414 loc) · 17.8 KB
/
EditData.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#region Copyright
//
// Copyright (c) 2015
// by Satrabel
//
#endregion
#region Using Statements
using System;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common;
using System.Web.UI.WebControls;
using System.IO;
using Satrabel.OpenContent.Components;
using Newtonsoft.Json.Linq;
using Satrabel.OpenContent.Components.Json;
using Satrabel.OpenContent.Components.Manifest;
using Satrabel.OpenContent.Components.Datasource;
#endregion
namespace Satrabel.OpenContent
{
public partial class EditData : PortalModuleBase
{
private const string cData = "Data";
private const string cSettings = "Settings";
private const string cFilter = "Filter";
#region Event Handlers
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
cmdSave.Click += cmdSave_Click;
cmdCancel.Click += cmdCancel_Click;
cmdImport.Click += cmdImport_Click;
cmdRestApi.NavigateUrl = Globals.NavigateURL("Swagger", "mid=" + ModuleContext.ModuleId) + "?popUp=true";
//ServicesFramework.Instance.RequestAjaxScriptSupport();
//ServicesFramework.Instance.RequestAjaxAntiForgerySupport();
sourceList.SelectedIndexChanged += sourceList_SelectedIndexChanged;
ddlVersions.SelectedIndexChanged += ddlVersions_SelectedIndexChanged;
}
private void cmdImport_Click(object sender, EventArgs e)
{
OpenContentSettings settings = this.OpenContentSettings();
switch (sourceList.SelectedValue)
{
case cData:
{
txtSource.Text = File.ReadAllText(settings.TemplateDir.PhysicalFullDirectory + "\\data.json");
}
break;
}
}
private void ddlVersions_SelectedIndexChanged(object sender, EventArgs e)
{
OpenContentSettings settings = this.OpenContentSettings();
int ModId = settings.IsOtherModule ? settings.ModuleId : ModuleId;
var ds = DataSourceManager.GetDataSource("OpenContent");
var dsContext = new DataSourceContext()
{
ModuleId = ModId,
ActiveModuleId = ModuleContext.ModuleId,
TemplateFolder = settings.TemplateDir.FolderPath,
Single = true
};
var dsItem = ds.Get(dsContext, null);
if (dsItem != null)
{
var ticks = long.Parse(ddlVersions.SelectedValue);
var ver = ds.GetVersion(dsContext, dsItem, new DateTime(ticks));
//var ver = data.Versions.Single(v => v.CreatedOnDate == d);
txtSource.Text = ver.ToString();
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Page.IsPostBack)
{
InitEditor();
}
}
private void InitEditor()
{
LoadFiles();
DisplayFile(cData);
}
private void DisplayFile(string selectedDataType)
{
cmdImport.Visible = false;
string json = string.Empty;
switch (selectedDataType)
{
case cData:
{
TemplateManifest template = null;
OpenContentSettings settings = this.OpenContentSettings();
int ModId = settings.IsOtherModule ? settings.ModuleId : ModuleId;
if (settings.TemplateAvailable)
{
template = settings.Template;
}
var ds = DataSourceManager.GetDataSource(settings.Manifest.DataSource);
var dsContext = new DataSourceContext()
{
ModuleId = ModId,
ActiveModuleId = ModuleContext.ModuleId,
TemplateFolder = settings.TemplateDir.FolderPath,
Config = settings.Manifest.DataSourceConfig
};
if (template != null && template.IsListTemplate)
{
ddlVersions.Visible = false;
cmdRestApi.Visible = true;
string itemId = Request.QueryString["id"];
if (!string.IsNullOrEmpty(itemId))
{
var dsItem = ds.Get(dsContext, itemId);
if (dsItem != null)
{
json = dsItem.Data.ToString();
var versions = ds.GetVersions(dsContext, dsItem);
if (versions != null)
foreach (var ver in versions)
{
ddlVersions.Items.Add(new ListItem()
{
//Text = ver.CreatedOnDate.ToShortDateString() + " " + ver.CreatedOnDate.ToShortTimeString(),
//Value = ver.CreatedOnDate.Ticks.ToString()
Text = ver["text"].ToString(),
Value = ver["ticks"].ToString()
});
}
}
}
else
{
var dataList = ds.GetAll(dsContext, null);
if (dataList != null)
{
JArray lst = new JArray();
foreach (var item in dataList.Items)
{
lst.Add(item.Data);
}
json = lst.ToString();
}
}
}
else
{
ddlVersions.Visible = true;
cmdRestApi.Visible = false;
dsContext.Single = true;
var dsItem = ds.Get(dsContext, null);
if (dsItem != null)
{
json = dsItem.Data.ToString();
var versions = ds.GetVersions(dsContext, dsItem);
if (versions != null)
foreach (var ver in versions)
{
ddlVersions.Items.Add(new ListItem()
{
//Text = ver.CreatedOnDate.ToShortDateString() + " " + ver.CreatedOnDate.ToShortTimeString(),
//Value = ver.CreatedOnDate.Ticks.ToString()
Text = ver["text"].ToString(),
Value = ver["ticks"].ToString()
});
}
}
}
cmdImport.Visible = string.IsNullOrEmpty(json) && File.Exists(settings.TemplateDir.PhysicalFullDirectory + "\\data.json");
}
break;
case cSettings:
json = ModuleContext.Settings["data"] as string;
break;
case cFilter:
json = ModuleContext.Settings["query"] as string;
break;
default:
{
OpenContentSettings settings = this.OpenContentSettings();
int ModId = settings.IsOtherModule ? settings.ModuleId : ModuleId;
var manifest = settings.Manifest;
string key = selectedDataType;
var dataManifest = manifest.GetAdditionalData(key);
var ds = DataSourceManager.GetDataSource(manifest.DataSource);
var dsContext = new DataSourceContext()
{
PortalId = PortalSettings.PortalId,
TabId = TabId,
ModuleId = ModId,
TabModuleId = this.TabModuleId,
UserId = UserInfo.UserID,
TemplateFolder = settings.TemplateDir.FolderPath,
Config = manifest.DataSourceConfig,
//Options = reqOptions
};
var dsItem = ds.GetData(dsContext, dataManifest.ScopeType, dataManifest.StorageKey ?? key);
json = dsItem == null ? "" : dsItem.Data.ToString();
break;
}
}
txtSource.Text = json;
}
private void LoadFiles()
{
TemplateManifest template = ModuleContext.OpenContentSettings().Template;
sourceList.Items.Clear();
sourceList.Items.Add(new ListItem(cData, cData));
sourceList.Items.Add(new ListItem(cSettings, cSettings));
sourceList.Items.Add(new ListItem(cFilter, cFilter));
if (template != null && template.Manifest != null && template.Manifest.AdditionalDataExists())
{
foreach (var addData in template.Manifest.AdditionalData)
{
string title = string.IsNullOrEmpty(addData.Value.Title) ? addData.Key : addData.Value.Title;
sourceList.Items.Add(new ListItem(title, addData.Key));
}
}
}
protected void cmdSave_Click(object sender, EventArgs e)
{
if (sourceList.SelectedValue == cData)
{
SaveData();
}
else if (sourceList.SelectedValue == cSettings)
{
SaveSettings();
}
else if (sourceList.SelectedValue == cFilter)
{
SaveFilter();
}
else
{
SaveAdditionalData(sourceList.SelectedValue);
}
Response.Redirect(Globals.NavigateURL(), true);
}
private void SaveAdditionalData(string key)
{
OpenContentSettings settings = this.OpenContentSettings();
int ModId = settings.IsOtherModule ? settings.ModuleId : ModuleId;
var manifest = settings.Manifest;
var dataManifest = manifest.GetAdditionalData(key);
var ds = DataSourceManager.GetDataSource(manifest.DataSource);
var dsContext = new DataSourceContext()
{
PortalId = PortalSettings.PortalId,
TabId = TabId,
ModuleId = ModId,
TabModuleId = this.TabModuleId,
UserId = UserInfo.UserID,
TemplateFolder = settings.TemplateDir.FolderPath,
Config = manifest.DataSourceConfig,
//Options = reqOptions
};
var dsItem = ds.GetData(dsContext, dataManifest.ScopeType, dataManifest.StorageKey ?? key);
if (dsItem == null)
{
ds.AddData(dsContext, dataManifest.ScopeType, dataManifest.StorageKey ?? key, JToken.Parse(txtSource.Text));
}
else
{
ds.UpdateData(dsContext, dsItem, JToken.Parse(txtSource.Text));
}
}
private void SaveFilter()
{
ModuleController mc = new ModuleController();
if (!string.IsNullOrEmpty(txtSource.Text))
mc.UpdateModuleSetting(ModuleId, "query", txtSource.Text);
}
private void SaveData()
{
TemplateManifest template = null;
OpenContentSettings settings = this.OpenContentSettings();
int ModId = settings.IsOtherModule ? settings.ModuleId : ModuleId;
bool index = false;
if (settings.TemplateAvailable)
{
template = settings.Template;
index = settings.Template.Manifest.Index;
}
/*
FieldConfig indexConfig = null;
if (index)
{
indexConfig = OpenContentUtils.GetIndexConfig(settings.Template.Key.TemplateDir);
}
*/
var ds = DataSourceManager.GetDataSource(settings.Manifest.DataSource);
var dsContext = new DataSourceContext()
{
ModuleId = ModId,
ActiveModuleId = ModuleContext.ModuleId,
TemplateFolder = settings.TemplateDir.FolderPath,
Index = index,
UserId = UserInfo.UserID,
Config = settings.Manifest.DataSourceConfig
};
if (template != null && template.IsListTemplate)
{
string itemId = Request.QueryString["id"];
if (!string.IsNullOrEmpty(itemId))
{
var dsItem = ds.Get(dsContext, itemId);
if (string.IsNullOrEmpty(txtSource.Text))
{
if (dsItem != null)
{
ds.Delete(dsContext, dsItem);
}
}
else
{
var json = txtSource.Text.ToJObject("Saving txtSource");
if (dsItem == null)
{
ds.Add(dsContext, json);
}
else
{
ds.Update(dsContext, dsItem, json);
}
if (json["ModuleTitle"] != null && json["ModuleTitle"].Type == JTokenType.String)
{
string ModuleTitle = json["ModuleTitle"].ToString();
OpenContentUtils.UpdateModuleTitle(ModuleContext.Configuration, ModuleTitle);
}
else if (json["ModuleTitle"] != null && json["ModuleTitle"].Type == JTokenType.Object)
{
string ModuleTitle = json["ModuleTitle"][DnnLanguageUtils.GetCurrentCultureCode()].ToString();
OpenContentUtils.UpdateModuleTitle(ModuleContext.Configuration, ModuleTitle);
}
}
}
else
{
JArray lst = null;
if (!string.IsNullOrEmpty(txtSource.Text))
{
lst = JArray.Parse(txtSource.Text);
}
var dataList = ds.GetAll(dsContext, null).Items;
foreach (var item in dataList)
{
ds.Delete(dsContext, item);
}
if (lst != null)
{
foreach (JObject json in lst)
{
ds.Add(dsContext, json);
}
}
}
}
else
{
dsContext.Single = true;
var dsItem = ds.Get(dsContext, null);
if (string.IsNullOrEmpty(txtSource.Text))
{
if (dsItem != null)
{
ds.Delete(dsContext, dsItem);
}
}
else
{
var json = txtSource.Text.ToJObject("Saving txtSource");
if (dsItem == null)
{
ds.Add(dsContext, json);
}
else
{
ds.Update(dsContext, dsItem, json);
}
if (json["ModuleTitle"] != null && json["ModuleTitle"].Type == JTokenType.String)
{
string ModuleTitle = json["ModuleTitle"].ToString();
OpenContentUtils.UpdateModuleTitle(ModuleContext.Configuration, ModuleTitle);
}
else if (json["ModuleTitle"] != null && json["ModuleTitle"].Type == JTokenType.Object)
{
string ModuleTitle = json["ModuleTitle"][DnnLanguageUtils.GetCurrentCultureCode()].ToString();
OpenContentUtils.UpdateModuleTitle(ModuleContext.Configuration, ModuleTitle);
}
}
}
}
private void SaveSettings()
{
ModuleController mc = new ModuleController();
if (string.IsNullOrEmpty(txtSource.Text))
mc.DeleteModuleSetting(ModuleId, "data");
else
mc.UpdateModuleSetting(ModuleId, "data", txtSource.Text);
}
protected void cmdCancel_Click(object sender, EventArgs e)
{
Response.Redirect(Globals.NavigateURL(), true);
}
private void sourceList_SelectedIndexChanged(object sender, EventArgs e)
{
phVersions.Visible = sourceList.SelectedValue == cData;
DisplayFile(sourceList.SelectedValue);
}
#endregion
}
}