/
core.cs
385 lines (286 loc) · 15.2 KB
/
core.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
using lecoati.uMobile;
using lecoati.uMobile.umComponents;
using lecoati.uMobile.umCore;
using lecoati.uMobile.umHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Enums;
using Umbraco.Forms.Core.Services;
using Umbraco.Forms.Data.Storage;
namespace uMobile4Contour
{
[umClass(category: "Contour", icon: lecoati.uMobile.umComponents.GenericIcon.ListAlt)]
public class core : uMobile
{
enum Range
{
hour,
Day,
Month,
Year,
Decade
};
enum Show
{
five = 5,
ten = 10,
fifty = 50,
thousand = 100
};
[umMethod(title: "Forms", subtitle: "Form managment", icon: lecoati.uMobile.umComponents.GenericIcon.ListAlt)]
public static string ListForm()
{
FormStorage fs = new FormStorage();
System.Collections.Generic.List<Umbraco.Forms.Core.Form> forms = fs.GetAllForms();
List calls = new List();
foreach (Umbraco.Forms.Core.Form form in forms)
{
calls.AddListItem(new ListItem(form.Name,
subtitle: "Created: " + form.Created.ToString(),
icon: GenericIcon.FolderClose,
action: new Call("ListRecord", new string[] { form.Id.ToString(), "1" })));
}
return calls.UmGo();
}
[umMethod(visible: false, title: "Records")]
public static string ListRecord(String formGuid, String pageStr)
{
FormState state = FormState.Approved;
Range range = Range.Day;
Show show = Show.five;
int page = int.Parse(pageStr);
if (Utils.GetPostParameter("State") != null && !string.IsNullOrEmpty(Utils.GetPostParameter("State").ToString()))
HttpContext.Current.Session["State"] = Utils.GetPostParameter("State").ToString();
if (Utils.GetPostParameter("Range") != null && !string.IsNullOrEmpty(Utils.GetPostParameter("Range").ToString()))
HttpContext.Current.Session["Range"] = Utils.GetPostParameter("Range").ToString();
if (Utils.GetPostParameter("Show") != null && !string.IsNullOrEmpty(Utils.GetPostParameter("Show").ToString()))
HttpContext.Current.Session["Show"] = Utils.GetPostParameter("Show").ToString();
if (HttpContext.Current.Session["State"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["State"].ToString()))
state = (FormState)Enum.Parse(typeof(FormState), HttpContext.Current.Session["State"].ToString());
if (HttpContext.Current.Session["Range"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Range"].ToString()))
range = (Range)Enum.Parse(typeof(Range), HttpContext.Current.Session["Range"].ToString());
if (HttpContext.Current.Session["Show"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Show"].ToString()))
show = (Show)Enum.Parse(typeof(Show), HttpContext.Current.Session["Show"].ToString());
FormStorage fs = new FormStorage();
RecordsViewer rv = new RecordsViewer();
RecordStorage recordStorage = new RecordStorage();
Umbraco.Forms.Core.Form form = fs.GetForm(Guid.Parse(formGuid));
System.Collections.Generic.List<Record> records = rv.GetRecords(0, 0, form, Sorting.descending, new List<FormState> { state });
List calls = new List();
calls.AddListItem(new ListItem("<b>Filter</b>",
subtitle: state.ToString() + ", Last " + range.ToString() + ", " + (int)show + " records per page",
icon: GenericIcon.Filter,
action: new Call("ConfigFilter", new string[] { formGuid })));
DateTime minDate = DateTime.Now;
switch (range)
{
case Range.hour:
minDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0);
break;
case Range.Month:
minDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
break;
case Range.Year:
minDate = new DateTime(DateTime.Now.Year, 1, 1);
break;
case Range.Decade:
minDate = DateTime.MinValue;
break;
case Range.Day:
default:
minDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
break;
}
if (records.Where(r => r.Created > minDate).Any())
{
int total = records.Where(r => r.Created > minDate).Count();
IEnumerable<Record> results = records.Where(r => r.Created > minDate).OrderByDescending(r => r.Created).Skip((page - 1) * (int)show).Take((int)show);
Button nextButton = null;
if (total > (results.Count() + (page - 1) * (int)show))
nextButton = new Button(icon: GenericIcon.ArrowRight, action: new Call("ListRecord", new string[] { formGuid, (page + 1).ToString() }));
calls.AddListItem(new ListItem("<b>Total result:</b> " + total.ToString(), subtitle: "Page <b>" + page.ToString() + "</b> to <b>" + (((int)(total / (int)show)) + 1).ToString() + "</b>", contextual_button: nextButton));
foreach (Record record in results)
{
calls.AddListItem(new ListItem(record.Created.ToString(),
subtitle: "Ip: " + record.IP,
action: new Call("ViewData", new string[] { record.Id.ToString() })));
}
}
else
{
calls.AddListItem(new ListItem("<b>Total result:</b> 0", subtitle: "Page <b>0</b> to <b>0</b>"));
}
return calls.UmGo();
}
[umMethod(visible: false, title: "Filter")]
public static string ConfigFilter(String formGuid)
{
FormState state = FormState.Approved;
Range range = Range.Day;
Show show = Show.five;
if (HttpContext.Current.Session["State"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["State"].ToString()))
state = (FormState)Enum.Parse(typeof(FormState), HttpContext.Current.Session["State"].ToString());
if (HttpContext.Current.Session["Range"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Range"].ToString()))
range = (Range)Enum.Parse(typeof(Range), HttpContext.Current.Session["Range"].ToString());
if (HttpContext.Current.Session["Show"] != null && !string.IsNullOrEmpty(HttpContext.Current.Session["Show"].ToString()))
show = (Show)Enum.Parse(typeof(Show), HttpContext.Current.Session["Show"].ToString());
lecoati.uMobile.umComponents.Form form = new lecoati.uMobile.umComponents.Form();
form.primary = new Button("Refresh", new Call("ListRecord", new string[] { formGuid, "1" }));
FormFieldset fieldSet = new FormFieldset(title: "Filter by");
form.AddFieldset(fieldSet);
SelectField selectFilter = new SelectField("State", "State", state.ToString());
foreach (FormState stateIndex in Enum.GetValues(typeof(FormState)))
selectFilter.AddOption(stateIndex.ToString(), stateIndex.ToString());
fieldSet.AddFormItem(selectFilter);
SelectField selectRange = new SelectField("Range", "Range", range.ToString());
foreach (Range rangeIndex in Enum.GetValues(typeof(Range)))
selectRange.AddOption(rangeIndex.ToString(), rangeIndex.ToString());
fieldSet.AddFormItem(selectRange);
FormFieldset fieldSetEntries = new FormFieldset(title: "Entries");
form.AddFieldset(fieldSetEntries);
SelectField selecShow = new SelectField("Show", "Show", ((int)show).ToString());
foreach (Show showIndex in Enum.GetValues(typeof(Show)))
selecShow.AddOption(((int)showIndex).ToString(), showIndex.ToString());
fieldSetEntries.AddFormItem(selecShow);
return form.UmGo();
}
[umMethod(visible: false, title: "Record detail")]
public static string ViewData(String recordId)
{
FormStorage fs = new FormStorage();
RecordStorage rs = new RecordStorage();
Record record = rs.GetRecord(Guid.Parse(recordId));
Umbraco.Forms.Core.Form form = fs.GetForm(record.Form);
List calls = new List();
calls.AddListItem(new ListItem("<b>Approve</b>",
subtitle: "Approve record",
icon: GenericIcon.Check,
action: new Call("CheckRecordConfirm", new string[] { recordId })));
calls.AddListItem(new ListItem("<b>Delete</b>",
subtitle: "Delete record",
icon: GenericIcon.Trash,
action: new Call("DeleteRecordConfirm", new string[] { recordId })));
calls.AddListItem(new ListItem("<b>Email</b>",
subtitle: "Email record",
icon: GenericIcon.EnvelopeAlt,
action: new Call("EmailRecordForm", new string[] { recordId })));
String data = string.Empty;
data += "<p><b>State:</b><br/>" + record.State.ToString() + "</p><br />";
data += "<p><b>Created:</b><br/>" + record.Created.ToString() + "</p><br />";
data += "<p><b>Ip:</b><br/>" + record.IP + "</p><br />";
foreach (Field field in form.AllFields)
{
string value = string.Empty;
if (record.GetRecordField(field.Id) != null && record.GetRecordField(field.Id).Values.Count > 0)
{
value = record.GetRecordField(field.Id).Values[0].ToString();
}
data += "<p><b>" + field.Caption + ":</b><br/>" + value + "</p><br />";
}
calls.AddListItem(new ListItem("<b>Record</b><br/><br />",
subtitle: data));
return calls.UmGo();
}
[umMethod(visible: false, title: "Approve Record Confirm")]
public static string CheckRecordConfirm(String recordId)
{
return new MessageBox("Are you sure you want to approve this record", new Button("Ok", new Call("CheckRecord", new string[] { recordId })), new Button("Cancel"))
.UmGo();
}
[umMethod(visible: false, title: "Approve Record")]
public static string CheckRecord(String recordId)
{
try
{
RecordStorage rs = new RecordStorage();
Record record = rs.GetRecord(Guid.Parse(recordId));
RecordService s = new RecordService(record);
s.Approve();
}
catch (Exception ex)
{
if (ex.Message.Contains("This license is invalid"))
return new MessageBox("Record approved successfully", new Button("Ok", new Call("ViewData", new string[] { recordId }))).UmGo();
else
return new MessageBox("Error to approved this record", new Button("Close")).UmGo("Warning");
}
return new MessageBox("Record approved successfully", new Button("Ok", new Call("ViewData", new string[] { recordId })))
.UmGo();
}
[umMethod(visible: false, title: "Delete Record Confirm")]
public static string DeleteRecordConfirm(String recordId)
{
return new MessageBox("Are you sure you want to delete this record", new Button("Ok", new Call("DeleteRecord", new string[] { recordId })), new Button("Cancel"))
.UmGo();
}
[umMethod(visible: false, title: "Delete Record")]
public static string DeleteRecord(String recordId)
{
string formGuid = null;
try
{
RecordStorage rs = new RecordStorage();
Record record = rs.GetRecord(Guid.Parse(recordId));
RecordService s = new RecordService(record);
formGuid = record.Form.ToString();
s.Delete();
}
catch (Exception ex)
{
if (ex.Message.Contains("This license is invalid"))
return new MessageBox("Record deleted successfully", new Button("Ok", new Call("ListRecord", new string[] { formGuid, "1" }))).UmGo();
else
return new MessageBox("Error to delete this record", new Button("Close")).UmGo("Warning");
}
return new MessageBox("Record deleted successfully", new Button("Ok", new Call("ListRecord", new string[] { formGuid, "1" })))
.UmGo();
}
[umMethod(visible: false, title: "Email record Form")]
public static string EmailRecordForm(String recordId)
{
TextField tFEmail = new TextField("email", "Email");
lecoati.uMobile.umComponents.Form form = new lecoati.uMobile.umComponents.Form();
form.primary = new Button("Send", new Call("EmailRecord", new string[] { recordId }));
FormFieldset fieldSet = new FormFieldset(title: "Email record");
form.AddFieldset(fieldSet);
fieldSet.AddFormItem(tFEmail);
return form.UmGo();
}
[umMethod(visible: false, title: "Email record")]
public static string EmailRecord(String recordId)
{
String emailTo = Utils.GetPostParameter("email").ToString();
String emailFrom = umbraco.UmbracoSettings.NotificationEmailSender;
FormStorage fs = new FormStorage();
RecordStorage rs = new RecordStorage();
Record record = rs.GetRecord(Guid.Parse(recordId));
Umbraco.Forms.Core.Form form = fs.GetForm(record.Form);
string data = "<p><b>Form:</b> " + form.Name + "</p>";
data += "<p><b>State:</b><br/>" + record.State.ToString() + "</p>";
data += "<p><b>Created:</b><br/>" + record.Created.ToString() + "</p>";
data += "<p><b>Ip:</b><br/>" + record.IP + "</p>";
foreach (Field field in form.AllFields)
{
string value = string.Empty;
if (record.GetRecordField(field.Id) != null && record.GetRecordField(field.Id).Values.Count > 0)
{
value = record.GetRecordField(field.Id).Values[0].ToString();
}
data += "<p><b>" + field.Caption + ":</b><br/>" + value + "</p>";
}
try
{
umbraco.library.SendMail(emailFrom, emailTo, "Contour record", data, true);
}
catch
{
return new MessageBox("Error to send record ", new Button("Close")).UmGo("Warning");
}
return new MessageBox("Record sent successfully", new Button("Ok", new Call("ViewData", new string[] { recordId })))
.UmGo();
}
}
}