-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTemplate_DataSource.ascx.cs
182 lines (151 loc) · 5.94 KB
/
Template_DataSource.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
#region Copyright
//
// Copyright (c) 2014
// by SatraBel
//
#endregion
#region Using Statements
using System;
using DotNetNuke.Entities.Modules;
using System.Web.Hosting;
using System.Collections.Generic;
using System.IO;
using Satrabel.OpenBlocks.Token;
using DotNetNuke.UI.Skins.Controls;
using Satrabel.OpenBlocks.DataSource;
using System.Collections;
using System.Web.UI.WebControls;
#endregion
namespace SatraBel.OpenBlocks
{
public partial class Template_DataSource : PortalModuleBase
{
#region Event Handlers
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
try
{
foreach (var provider in DataSourceProvider.GetProviderList())
{
if (!string.IsNullOrEmpty(provider.Configurator))
{
var ctrl = this.Page.LoadControl(provider.Configurator);
DataSourceConfigurator conf = ctrl as DataSourceConfigurator;
if (conf != null)
{
conf.PortalId = PortalId;
conf.LocalResourceFile = LocalResourceFile;
conf.ID = provider.FriendlyName;
phConfigurator.Controls.Add(ctrl);
}
}
}
}
catch (Exception ex)
{
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, TemplateFileName + "/" + ex.Message, ModuleMessage.ModuleMessageType.RedError);
}
}
string TemplateFileName = "";
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (Request.QueryString["template"] != null)
{
TemplateFileName = Request.QueryString["template"];
}
if (!Page.IsPostBack)
{
try
{
foreach (var item in DataSourceProvider.GetProviderList())
{
if (item.Configurator != null)
{
ddlDataSource.Items.Add(new ListItem(item.FriendlyName));
}
}
string realfilename = HostingEnvironment.MapPath(TemplateFileName);
Hashtable settings = new Hashtable();
string dsFileName = Path.ChangeExtension(realfilename, ".datasource");
//string token = "";
if (File.Exists(dsFileName))
{
string[] lines = File.ReadAllLines(dsFileName);
foreach (var item in lines)
{
string[] par = item.Split(new char[] { '=' }, 2);
settings.Add(par[0], par[1]);
}
ddlDataSource.SelectedValue = (string)settings["dataprovider"];
}
foreach (DataSourceConfigurator conf in phConfigurator.Controls)
{
conf.Visible = ddlDataSource.SelectedValue == conf.ID;
conf.LoadSettings(settings);
//conf.setToken(token);
}
}
catch (Exception ex)
{
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, TemplateFileName + "/" + ex.Message, ModuleMessage.ModuleMessageType.RedError);
}
}
}
protected void ddlDataSource_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (DataSourceConfigurator conf in phConfigurator.Controls)
{
conf.Visible = ddlDataSource.SelectedValue == conf.ID;
}
}
#endregion
protected void btnSave_Click(object sender, EventArgs e)
{
string realfilename = HostingEnvironment.MapPath(TemplateFileName);
string dsFileName = Path.ChangeExtension(realfilename, ".datasource");
foreach (DataSourceConfigurator conf in phConfigurator.Controls)
{
if (conf.Visible)
{
var ht = conf.SaveSettings();
List<string> lst = new List<string>();
var ModelParameters = new Dictionary<string, string>();
foreach (DictionaryEntry item in ht)
{
lst.Add(item.Key + "=" + item.Value);
ModelParameters.Add((string)item.Key, (string)item.Value);
}
File.WriteAllLines(dsFileName, lst.ToArray());
UpdateRazorFile(realfilename, ModelParameters);
}
}
Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
}
private void UpdateRazorFile(string realfilename, Dictionary<string, string> Parameters)
{
if (Path.GetExtension(realfilename) == ".cshtml")
{
try
{
object Model = DataSourceUtils.GetModel(Parameters);
string[] Lines = File.ReadAllLines(realfilename);
if (Lines.Length > 0 && Lines[0].Contains("@inherits"))
{
Lines[0] = "@inherits Satrabel.OpenBlocks.TemplateEngine.TemplateWebPage<" + Model.GetType().FullName + ">";
File.WriteAllLines(realfilename, Lines);
}
}
catch (Exception)
{
//throw;
}
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
}
}
}