-
Notifications
You must be signed in to change notification settings - Fork 2
/
CreateModule.ascx.cs
465 lines (410 loc) · 20.7 KB
/
CreateModule.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace Dnn.Module.ModuleCreator
{
using System;
using System.IO;
using System.Web.UI.WebControls;
using DotNetNuke.Abstractions;
using DotNetNuke.Abstractions.Application;
using DotNetNuke.Abstractions.Portals;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Content.Taxonomy;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Definitions;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Installer.Packages;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.UI.Skins.Controls;
using Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Codebehind for the CreateModule view.
/// </summary>
public partial class CreateModule : PortalModuleBase
{
private readonly INavigationManager navigationManager;
private readonly IHostSettingsService hostSettingsService;
private readonly IApplicationStatusInfo applicationStatusInfo;
private readonly IPortalSettings portalSettings;
/// <summary>
/// Initializes a new instance of the <see cref="CreateModule"/> class.
/// </summary>
public CreateModule()
{
this.navigationManager = this.DependencyProvider.GetRequiredService<INavigationManager>();
this.hostSettingsService = this.DependencyProvider.GetRequiredService<IHostSettingsService>();
this.applicationStatusInfo = this.DependencyProvider.GetRequiredService<IApplicationStatusInfo>();
this.portalSettings = PortalController.Instance.GetCurrentSettings();
}
/// <inheritdoc/>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.optLanguage.SelectedIndexChanged += this.OptLanguageSelectedIndexChanged;
this.cboTemplate.SelectedIndexChanged += this.CboTemplateSelectedIndexChanged;
this.cmdCreate.Click += this.CmdCreateClick;
}
/// <inheritdoc/>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.UserInfo.IsSuperUser)
{
if (!this.Page.IsPostBack)
{
var hostSettings = this.hostSettingsService.GetSettingsDictionary();
if (hostSettings.ContainsKey("Owner"))
{
this.txtOwner.Text = hostSettings["Owner"];
}
this.LoadLanguages();
this.LoadModuleTemplates();
this.txtControl.Text = "View";
}
}
else
{
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("SuperUser.ErrorMessage", this.LocalResourceFile), ModuleMessage.ModuleMessageType.YellowWarning);
this.createForm.Visible = false;
}
}
/// <summary>
/// Fires up when the language is changed.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The event arguments.</param>
protected void OptLanguageSelectedIndexChanged(object sender, EventArgs e)
{
this.LoadModuleTemplates();
}
/// <summary>
/// Fires up when the template selection is changed.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The event arguments.</param>
protected void CboTemplateSelectedIndexChanged(object sender, EventArgs e)
{
this.LoadReadMe();
}
/// <summary>
/// Fires up when the create button is clicked.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The event arguments.</param>
protected void CmdCreateClick(object sender, EventArgs e)
{
if (this.UserInfo.IsSuperUser)
{
if (!string.IsNullOrEmpty(this.txtOwner.Text) && !string.IsNullOrEmpty(this.txtModule.Text) && this.cboTemplate.SelectedIndex > 0 && !string.IsNullOrEmpty(this.txtControl.Text))
{
this.hostSettingsService.Update("Owner", this.txtOwner.Text, false);
if (this.CreateModuleDefinition())
{
this.Response.Redirect(this.navigationManager.NavigateURL(), true);
}
}
else
{
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("InputValidation.ErrorMessage", this.LocalResourceFile), ModuleMessage.ModuleMessageType.YellowWarning);
}
}
}
private void LoadReadMe()
{
var readMePath = this.Server.MapPath(this.ControlPath) + "Templates\\" + this.optLanguage.SelectedValue + "\\" + this.cboTemplate.SelectedItem.Value + "\\readme.txt";
if (File.Exists(readMePath))
{
var readMe = Null.NullString;
using (TextReader tr = new StreamReader(readMePath))
{
readMe = tr.ReadToEnd();
tr.Close();
this.lblDescription.Text = readMe.Replace("\n", "<br/>");
}
}
else
{
this.lblDescription.Text = string.Empty;
}
}
private void LoadLanguages()
{
this.optLanguage.Items.Clear();
var moduleTemplatePath = this.Server.MapPath(this.ControlPath) + "Templates";
string[] folderList = Directory.GetDirectories(moduleTemplatePath);
foreach (string folderPath in folderList)
{
this.optLanguage.Items.Add(new ListItem(Path.GetFileName(folderPath)));
}
this.optLanguage.SelectedIndex = 0;
}
private void LoadModuleTemplates()
{
this.cboTemplate.Items.Clear();
var moduleTemplatePath = this.Server.MapPath(this.ControlPath) + "Templates\\" + this.optLanguage.SelectedValue;
string[] folderList = Directory.GetDirectories(moduleTemplatePath);
foreach (string folderPath in folderList)
{
if (Path.GetFileName(folderPath).ToLowerInvariant().StartsWith("module"))
{
this.cboTemplate.Items.Add(new ListItem(Path.GetFileName(folderPath)));
}
}
this.cboTemplate.Items.Insert(0, new ListItem("<" + Localization.GetString("Not_Specified", Localization.SharedResourceFile) + ">", string.Empty));
if (this.cboTemplate.Items.FindByText("Module - User Control") != null)
{
this.cboTemplate.Items.FindByText("Module - User Control").Selected = true;
}
else
{
this.cboTemplate.SelectedIndex = 0;
}
this.LoadReadMe();
}
private void CreateModuleFolder()
{
var moduleFolderPath = this.applicationStatusInfo.ApplicationMapPath + "\\DesktopModules\\" + this.GetFolderName().Replace("/", "\\");
if (!Directory.Exists(moduleFolderPath))
{
Directory.CreateDirectory(moduleFolderPath);
}
}
private string CreateModuleControl()
{
var moduleTemplatePath = this.Server.MapPath(this.ControlPath) + "Templates\\" + this.optLanguage.SelectedValue + "\\" + this.cboTemplate.SelectedValue + "\\";
EventLogController.Instance.AddLog("Processing Template Folder", moduleTemplatePath, this.portalSettings, -1, EventLogController.EventLogType.HOST_ALERT);
var controlName = Null.NullString;
var fileName = Null.NullString;
var modulePath = string.Empty;
var sourceCode = Null.NullString;
// iterate through files in template folder
string[] fileList = Directory.GetFiles(moduleTemplatePath);
foreach (string filePath in fileList)
{
modulePath = this.Server.MapPath("DesktopModules/" + this.GetFolderName() + "/");
// open file
using (TextReader tr = new StreamReader(filePath))
{
sourceCode = tr.ReadToEnd();
tr.Close();
}
// replace tokens
sourceCode = sourceCode.Replace("_OWNER_", this.GetOwner());
sourceCode = sourceCode.Replace("_MODULE_", this.GetModule());
sourceCode = sourceCode.Replace("_CONTROL_", this.GetControl());
sourceCode = sourceCode.Replace("_YEAR_", DateTime.Now.Year.ToString());
// get filename
fileName = Path.GetFileName(filePath);
fileName = fileName.Replace("template", this.GetControl());
fileName = fileName.Replace("_OWNER_", this.GetOwner());
fileName = fileName.Replace("_MODULE_", this.GetModule());
fileName = fileName.Replace("_CONTROL_", this.GetControl());
switch (Path.GetExtension(filePath).ToLowerInvariant())
{
case ".ascx":
controlName = fileName;
break;
case ".vbhtml":
controlName = fileName;
break;
case ".cshtml":
controlName = fileName;
break;
case ".resx":
modulePath = modulePath + "\\App_LocalResources\\";
break;
case ".vb":
if (filePath.ToLowerInvariant().IndexOf(".ascx") == -1)
{
modulePath = modulePath.Replace("DesktopModules", "App_Code");
}
break;
case ".cs":
if (filePath.ToLowerInvariant().IndexOf(".ascx") == -1)
{
modulePath = modulePath.Replace("DesktopModules", "App_Code");
}
break;
case ".js":
modulePath = modulePath + "\\js\\";
break;
}
// check if folder exists
if (!Directory.Exists(modulePath))
{
Directory.CreateDirectory(modulePath);
}
// check if file already exists
if (!File.Exists(modulePath + fileName))
{
// create file
using (TextWriter tw = new StreamWriter(modulePath + fileName))
{
tw.WriteLine(sourceCode);
tw.Close();
}
EventLogController.Instance.AddLog("Created File", modulePath + fileName, this.portalSettings, -1, EventLogController.EventLogType.HOST_ALERT);
}
}
return controlName;
}
private string GetOwner()
{
return this.txtOwner.Text.Replace(" ", string.Empty);
}
private string GetModule()
{
return this.txtModule.Text.Replace(" ", string.Empty);
}
private string GetControl()
{
return this.txtControl.Text.Replace(" ", string.Empty);
}
private string GetFolderName()
{
var strFolder = Null.NullString;
strFolder += this.txtOwner.Text + "/" + this.txtModule.Text;
// return folder and remove any spaces that might appear in folder structure
return strFolder.Replace(" ", string.Empty);
}
private string GetClassName()
{
var strClass = Null.NullString;
strClass += this.txtOwner.Text + "." + this.txtModule.Text;
// return class and remove any spaces that might appear in class name
return strClass.Replace(" ", string.Empty);
}
/// <summary>
/// Creates the module definition.
/// </summary>
/// <returns>A value indicating whether the operation succeeded.</returns>
private bool CreateModuleDefinition()
{
try
{
if (PackageController.Instance.GetExtensionPackage(Null.NullInteger, p => p.Name == this.GetClassName()) == null)
{
var controlName = Null.NullString;
// Create module folder
this.CreateModuleFolder();
// Create module control
controlName = this.CreateModuleControl();
if (controlName != string.Empty)
{
// Create package
var objPackage = new PackageInfo();
objPackage.Name = this.GetClassName();
objPackage.FriendlyName = this.txtModule.Text;
objPackage.Description = this.txtDescription.Text;
objPackage.Version = new Version(1, 0, 0);
objPackage.PackageType = "Module";
objPackage.License = string.Empty;
objPackage.Owner = this.txtOwner.Text;
objPackage.Organization = this.txtOwner.Text;
objPackage.FolderName = "DesktopModules/" + this.GetFolderName();
objPackage.License = "The license for this package is not currently included within the installation file, please check with the vendor for full license details.";
objPackage.ReleaseNotes = "This package has no Release Notes.";
PackageController.Instance.SaveExtensionPackage(objPackage);
// Create desktopmodule
var objDesktopModule = new DesktopModuleInfo();
objDesktopModule.DesktopModuleID = Null.NullInteger;
objDesktopModule.ModuleName = this.GetClassName();
objDesktopModule.FolderName = this.GetFolderName();
objDesktopModule.FriendlyName = this.txtModule.Text;
objDesktopModule.Description = this.txtDescription.Text;
objDesktopModule.IsPremium = false;
objDesktopModule.IsAdmin = false;
objDesktopModule.Version = "01.00.00";
objDesktopModule.BusinessControllerClass = string.Empty;
objDesktopModule.CompatibleVersions = string.Empty;
objDesktopModule.AdminPage = string.Empty;
objDesktopModule.HostPage = string.Empty;
objDesktopModule.Dependencies = string.Empty;
objDesktopModule.Permissions = string.Empty;
objDesktopModule.PackageID = objPackage.PackageID;
objDesktopModule.DesktopModuleID = DesktopModuleController.SaveDesktopModule(objDesktopModule, false, true);
objDesktopModule = DesktopModuleController.GetDesktopModule(objDesktopModule.DesktopModuleID, Null.NullInteger);
// Add OwnerName to the DesktopModule taxonomy and associate it with this module
var vocabularyId = -1;
var termId = -1;
var objTermController = DotNetNuke.Entities.Content.Common.Util.GetTermController();
var objTerms = objTermController.GetTermsByVocabulary("Module_Categories");
foreach (Term term in objTerms)
{
vocabularyId = term.VocabularyId;
if (term.Name == this.txtOwner.Text)
{
termId = term.TermId;
}
}
if (termId == -1)
{
termId = objTermController.AddTerm(new Term(vocabularyId) { Name = this.txtOwner.Text });
}
var objTerm = objTermController.GetTerm(termId);
var objContentController = DotNetNuke.Entities.Content.Common.Util.GetContentController();
var objContent = objContentController.GetContentItem(objDesktopModule.ContentItemId);
objTermController.AddTermToContent(objTerm, objContent);
// Add desktopmodule to all portals
DesktopModuleController.AddDesktopModuleToPortals(objDesktopModule.DesktopModuleID);
// Create module definition
var objModuleDefinition = new ModuleDefinitionInfo();
objModuleDefinition.ModuleDefID = Null.NullInteger;
objModuleDefinition.DesktopModuleID = objDesktopModule.DesktopModuleID;
// need core enhancement to have a unique DefinitionName
objModuleDefinition.FriendlyName = this.GetClassName();
// objModuleDefinition.FriendlyName = txtModule.Text;
// objModuleDefinition.DefinitionName = GetClassName();
objModuleDefinition.DefaultCacheTime = 0;
objModuleDefinition.ModuleDefID = ModuleDefinitionController.SaveModuleDefinition(objModuleDefinition, false, true);
// Create modulecontrol
var objModuleControl = new ModuleControlInfo();
objModuleControl.ModuleControlID = Null.NullInteger;
objModuleControl.ModuleDefID = objModuleDefinition.ModuleDefID;
objModuleControl.ControlKey = string.Empty;
objModuleControl.ControlSrc = "DesktopModules/" + this.GetFolderName() + "/" + controlName;
objModuleControl.ControlTitle = string.Empty;
objModuleControl.ControlType = SecurityAccessLevel.View;
objModuleControl.HelpURL = string.Empty;
objModuleControl.IconFile = string.Empty;
objModuleControl.ViewOrder = 0;
objModuleControl.SupportsPartialRendering = false;
objModuleControl.SupportsPopUps = false;
ModuleControlController.AddModuleControl(objModuleControl);
// Update current module to reference new moduledefinition
var objModule = ModuleController.Instance.GetModule(this.ModuleId, this.TabId, false);
objModule.ModuleDefID = objModuleDefinition.ModuleDefID;
objModule.ModuleTitle = this.txtModule.Text;
// HACK - need core enhancement to be able to update ModuleDefID
using (DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(
"UPDATE {databaseOwner}{objectQualifier}Modules SET ModuleDefID=" + objModule.ModuleDefID + " WHERE ModuleID=" + this.ModuleId))
{
}
ModuleController.Instance.UpdateModule(objModule);
return true;
}
else
{
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("TemplateProblem.ErrorMessage", this.LocalResourceFile), ModuleMessage.ModuleMessageType.YellowWarning);
return false;
}
}
else
{
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("AlreadyExists.ErrorMessage", this.LocalResourceFile), ModuleMessage.ModuleMessageType.YellowWarning);
return false;
}
}
catch (Exception exc)
{
Exceptions.LogException(exc);
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, exc.ToString(), ModuleMessage.ModuleMessageType.RedError);
return false;
}
}
}
}