-
Notifications
You must be signed in to change notification settings - Fork 20
/
ExportCSV.ascx.cs
252 lines (218 loc) · 9.42 KB
/
ExportCSV.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
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Modules.UserDefinedTable.CSV;
using DotNetNuke.Modules.UserDefinedTable.Components;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Skins.Controls;
using System.Globalization;
using DotNetNuke.Entities.Portals;
namespace DotNetNuke.Modules.UserDefinedTable
{
public partial class ExportCsv : PortalModuleBase
{
#region Private Members
int _moduleId = Convert.ToInt32(-1);
#endregion
#region Event Handlers
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
cmdCancel.Click += cmdCancel_Click;
cmdExport.Click += cmdExport_Click;
Load += Page_Load;
}
void Page_Load(object sender, EventArgs e)
{
try
{
if (Request.QueryString["moduleid"] != null)
{
_moduleId = int.Parse(Request.QueryString["moduleid"]);
}
if (!Page.IsPostBack)
{
cboFolders.Items.Insert(0,
new ListItem(
string.Format("<{0}>", Localization.GetString("None_Specified")), "-"));
var folders = FolderManager.Instance.GetFolders(UserInfo, "READ, WRITE");
foreach (FolderInfo folder in folders)
{
var folderItem = new ListItem
{
Text = folder.FolderPath == Null.NullString
? Localization.GetString("Root", LocalResourceFile)
: folder.FolderPath,
Value = folder.FolderPath
};
cboFolders.Items.Add(folderItem);
}
txtInitialDate.Attributes.Add("readonly", "readonly");
txtFinalDate.Attributes.Add("readonly", "readonly");
var moduleController = new ModuleController();
var objModule = moduleController.GetModule(_moduleId, TabId, false);
if (objModule != null)
{
txtFile.Text = CleanName(objModule.ModuleTitle);
}
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
void cmdCancel_Click(object sender, EventArgs e)
{
try
{
Response.Redirect(Globals.NavigateURL(), true);
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
void cmdExport_Click(object sender, EventArgs e)
{
try
{
if (cboFolders.SelectedIndex != 0 && txtFile.Text != "")
{
var strFile = CleanName(string.Format("{0}.csv", txtFile.Text));
var strMessage = ExportModule(_moduleId, strFile, cboFolders.SelectedItem.Value,
rblDelimiter.SelectedValue, txtInitialDate.Text, txtFinalDate.Text);
if (strMessage == "")
{
Response.Redirect(Globals.NavigateURL(), true);
}
else
{
UI.Skins.Skin.AddModuleMessage(this, strMessage, ModuleMessage.ModuleMessageType.RedError);
}
}
else
{
UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("Validation", LocalResourceFile),
ModuleMessage.ModuleMessageType.RedError);
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
#endregion
#region Private Methods
string ExportModule(int moduleId, string fileName, string folder, string delimiter, string initialDate, string finalDate)
{
var strMessage = "";
var moduleController = new ModuleController();
var moduleInfo = moduleController.GetModule(moduleId, TabId, false);
var extension = Path.GetExtension(fileName);
if (extension != null && (extension.ToUpper() == ".CSV" && moduleInfo != null))
{
try
{
ExportData(folder, fileName, delimiter, initialDate, finalDate);
}
catch (Exception ex)
{
strMessage = string.Format("Error occurred: {0}", ex.Message);
}
}
return strMessage;
}
static string CleanName(string name)
{
var strName = name;
var badChars = Path.GetInvalidPathChars();
return badChars.Aggregate(strName, (current, badChar) => current.Replace(badChar.ToString(), ""));
}
void ExportData(string folder, string strFileName, string delimiter, string initialDate, string finalDate)
{
DataSet ds;
if(!string.IsNullOrEmpty(initialDate) && !string.IsNullOrEmpty(finalDate))
{
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "yyyy/MM/dd";
var serverTimeZone = PortalController.Instance.GetCurrentPortalSettings().TimeZone;
var timeZone = serverTimeZone;
DateTime startDate = DateTime.ParseExact(initialDate, format, provider);
startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, 0, 0, 0);
startDate = TimeZoneInfo.ConvertTimeToUtc(startDate, timeZone);
DateTime endDate = DateTime.ParseExact(finalDate, format, provider);
endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, 23, 59, 59);
endDate = TimeZoneInfo.ConvertTimeToUtc(endDate, timeZone);
ds = new UserDefinedTableController(_moduleId, TabId, UserInfo).GetDataSetWithDates(true,startDate, endDate);
}
else
{
ds = new UserDefinedTableController(_moduleId, TabId, UserInfo).GetDataSet(true);
}
var data = ds.Tables[0];
var fields = ds.Tables[1];
WriteData(data, fields, folder, strFileName, delimiter);
}
void WriteData(DataTable data, DataTable fields, string folder, string fileName, string delimiter)
{
using (var sw = new StringWriter())
{
var columns = new List<string>();
//Writing top line with column names
foreach (DataRow row in fields.Rows)
{
var typeName = (row[FieldsTableColumn.Type].ToString());
//ignore system fields
if (DataType.ByName(typeName).IsUserDefinedField || cbSystemFields.Checked)
{
columns.Add(row[FieldsTableColumn.Title].ToString());
}
}
//write colums name as first line
CSVWriter.WriteCSV(columns.ToArray(), sw, delimiter);
//writing data
foreach (DataRow row in data.Rows)
{
var values = new List<string>();
//getting values for all colums
foreach (var fieldTitle in columns)
{
var valueName = ((data.Columns.Contains(fieldTitle + DataTableColumn.Appendix_Original))
? fieldTitle + DataTableColumn.Appendix_Original
: fieldTitle);
var value = row[valueName].AsString();
if (fieldTitle == "Created at" || fieldTitle == "Changed at")
{
DateTime valueDate = DateTime.Parse(value);
value = valueDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
}
values.Add(value);
}
CSVWriter.WriteCSV(values.ToArray(), sw, delimiter);
}
WriteFile(folder, fileName, sw);
}
}
void WriteFile(string folder, string fileName, StringWriter sw)
{
using (var memStream = new MemoryStream())
{
var bytes = Encoding.UTF8.GetBytes(sw.ToString());
memStream.Write(bytes, 0, bytes.Length);
var f = FolderManager.Instance.GetFolder(PortalId, folder);
FileManager.Instance.AddFile(f, fileName, memStream, true);
}
}
#endregion
}
}