-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathModuleBase.cs
498 lines (443 loc) · 19.5 KB
/
ModuleBase.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// <copyright file="ModuleBase.cs" company="Engage Software">
// Engage: Events - http://www.EngageSoftware.com
// Copyright (c) 2004-2011
// by Engage Software ( http://www.engagesoftware.com )
// </copyright>
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
namespace Engage.Dnn.Events
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using DotNetNuke.Common;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.WebControls;
using Engage.Dnn.Events.Components;
using Engage.Events;
/// <summary>
/// This class extends the framework version in order for developers to add on any specific methods/behavior.
/// </summary>
public class ModuleBase : Framework.ModuleBase, IActionable
{
/// <summary>
/// Backing field for <see cref="CategoryIds"/>
/// </summary>
private IEnumerable<int> categoryIds;
/// <summary>
/// Backing field for <see cref="PermissionsService"/>
/// </summary>
private PermissionsService permissionsService;
/// <summary>
/// Gets the name of the this module's desktop module record in DNN.
/// </summary>
/// <value>The name of this module's desktop module record in DNN.</value>
public override string DesktopModuleName
{
get { return Utility.DesktopModuleName; }
}
/// <summary>
/// Gets the module actions.
/// </summary>
/// <value>The module actions.</value>
public ModuleActionCollection ModuleActions
{
get
{
var actions = new ModuleActionCollection();
if (this.PermissionsService.CanManageEvents)
{
actions.Add(
new ModuleAction(this.GetNextActionID())
{
Title = this.Localize("Add Event.Action", this.LocalSharedResourceFile),
CommandName = ModuleActionType.AddContent,
Url = this.BuildLinkUrl(this.ModuleId, "EventEdit"),
Secure = SecurityAccessLevel.View,
Icon = "add.gif"
});
actions.Add(
new ModuleAction(this.GetNextActionID())
{
Title = this.Localize("Manage Events.Action", this.LocalSharedResourceFile),
CommandName = ModuleActionType.EditContent,
Url = this.BuildLinkUrl(this.ModuleId, "EventListingAdmin"),
Secure = SecurityAccessLevel.View
});
}
if (this.PermissionsService.CanManageCategories)
{
actions.Add(
new ModuleAction(this.GetNextActionID())
{
Title = this.Localize("Manage Categories.Action", this.LocalSharedResourceFile),
CommandName = ModuleActionType.EditContent,
Url = this.BuildLinkUrl(this.ModuleId, "ManageCategories"),
Secure = SecurityAccessLevel.View
});
}
if (this.PermissionsService.CanViewResponses)
{
actions.Add(
new ModuleAction(this.GetNextActionID())
{
Title = this.Localize("View Responses.Action", this.LocalSharedResourceFile),
CommandName = ModuleActionType.EditContent,
Url = this.BuildLinkUrl(this.ModuleId, "ResponseSummary"),
Secure = SecurityAccessLevel.View,
Icon = "view.gif"
});
}
if (this.PermissionsService.CanManageDisplay)
{
actions.Add(
new ModuleAction(this.GetNextActionID())
{
Title = this.Localize("Manage Display.Action", this.LocalSharedResourceFile),
CommandName = ModuleActionType.ModuleSettings,
Url = this.BuildLinkUrl(this.ModuleId, "ChooseDisplay"),
Secure = SecurityAccessLevel.View,
Icon = "icon_skins_16px.gif"
});
}
return actions;
}
}
/// <summary>
/// Gets the event id.
/// </summary>
/// <value>The event id.</value>
protected int? EventId
{
get
{
if (this.Request.QueryString["eventId"] != null)
{
int eventId;
if (int.TryParse(this.Request.QueryString["eventId"], NumberStyles.Integer, CultureInfo.InvariantCulture, out eventId))
{
return eventId;
}
}
return null;
}
}
/// <summary>
/// Gets the occurrence date.
/// </summary>
/// <value>The date when the event occurs.</value>
/// <exception cref="InvalidOperationException">EventStart is not present on QueryString</exception>
protected DateTime EventStart
{
get
{
if (this.Request.QueryString["start"] != null)
{
long startTicks;
if (long.TryParse(this.Request.QueryString["start"], NumberStyles.Integer, CultureInfo.InvariantCulture, out startTicks))
{
return new DateTime(startTicks);
}
}
throw new InvalidOperationException("EventStart is not present on query-string: " + this.Request.RawUrl);
}
}
/// <summary>
/// Gets the register URL.
/// </summary>
/// <value>The register URL.</value>
[SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "Backwards compatibility")]
protected string RegisterUrl
{
get
{
int? eventId = this.EventId;
if (eventId.HasValue)
{
return this.BuildLinkUrl(this.ModuleId, "Register", Utility.GetEventParameters(eventId.Value, this.EventStart));
}
return null;
}
}
/// <summary>
/// Gets the Response URL.
/// </summary>
/// <value>The Response URL.</value>
[SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "Backwards compatability")]
protected string ResponseUrl
{
get
{
int? eventId = this.EventId;
if (eventId.HasValue)
{
return this.BuildLinkUrl(this.ModuleId, "Response", Utility.GetEventParameters(eventId.Value, this.EventStart));
}
return null;
}
}
/// <summary>
/// Gets the list of category IDs by which this display should filter its events list.
/// </summary>
/// <value>The category IDs.</value>
protected IEnumerable<int> CategoryIds
{
get
{
if (this.categoryIds == null)
{
var categoriesSettingValue = ModuleSettings.Categories.GetValueAsStringFor(this);
this.categoryIds = string.IsNullOrEmpty(categoriesSettingValue)
? Enumerable.Empty<int>()
: categoriesSettingValue.Split(',').Select(id => int.Parse(id, CultureInfo.InvariantCulture));
}
return this.categoryIds;
}
}
/// <summary>
/// Gets a value indicating whether this instance should display only featured events.
/// </summary>
/// <value>
/// <c>true</c> if this instance should only display featured events; otherwise, <c>false</c>.
/// </value>
protected bool IsFeatured
{
get { return ModuleSettings.FeaturedOnly.GetValueAsBooleanFor(this).Value; }
}
/// <summary>
/// Gets a value indicating whether this instance should show events which have hit their registration cap.
/// </summary>
/// <value>
/// <c>true</c> if this instance should not display events which have hit their registration cap; otherwise, <c>false</c>.
/// </value>
protected bool HideFullEvents
{
get { return ModuleSettings.HideFullEvents.GetValueAsBooleanFor(this).Value; }
}
/// <summary>
/// Gets a value indicating whether this instance is configured.
/// </summary>
/// <value>
/// <c>true</c> if this instance is configured; otherwise, <c>false</c>.
/// </value>
protected override bool IsConfigured
{
get
{
return "CALENDAR".Equals(ModuleSettings.DisplayType.GetValueAsStringFor(this), StringComparison.OrdinalIgnoreCase) ||
(Engage.Utility.HasValue(ModuleSettings.Template.GetValueAsStringFor(this)) &&
Engage.Utility.HasValue(ModuleSettings.SingleItemTemplate.GetValueAsStringFor(this)));
}
}
/// <summary>
/// Gets the index of the current page from the QueryString.
/// </summary>
/// <value>The index of the current page.</value>
protected virtual int CurrentPageIndex
{
get
{
int index;
if (!int.TryParse(this.Request.QueryString[this.PageIndexQueryStringKey], NumberStyles.Integer, CultureInfo.InvariantCulture, out index))
{
index = 1;
}
return index;
}
}
/// <summary>
/// Gets the query-string key for the index of the current page.
/// </summary>
protected string PageIndexQueryStringKey
{
get
{
return string.Format(CultureInfo.InvariantCulture, this.Localize("PageIndexKey.Format", this.LocalSharedResourceFile), this.ModuleId);
}
}
/// <summary>
/// Gets the <see cref="Components.PermissionsService"/>.
/// </summary>
/// <value>The permissions service.</value>
protected PermissionsService PermissionsService
{
get
{
if (this.permissionsService == null)
{
if (this.ModuleConfiguration == null)
{
this.SetModuleConfiguration();
}
this.permissionsService = new PermissionsService(this.ModuleConfiguration);
}
return this.permissionsService;
}
}
/// <summary>
/// Gets the Tab ID to use when displaying module details.
/// </summary>
/// <value>The Tab ID to use when displaying module details.</value>
protected int DetailsTabId
{
get { return ModuleSettings.DetailsDisplayTabId.GetValueAsInt32For(this) ?? this.TabId; }
}
/// <summary>
/// Gets the Module ID to use when displaying module details.
/// </summary>
/// <value>The Module ID to use when displaying module details.</value>
protected int DetailsModuleId
{
get { return ModuleSettings.DetailsDisplayModuleId.GetValueAsInt32For(this) ?? this.ModuleId; }
}
/// <summary>
/// Generates a list of QueryString parameters for the given list of <paramref name="queryStringKeys"/>.
/// </summary>
/// <param name="request">The current request.</param>
/// <param name="queryStringKeys">The keys for which to generate parameters.</param>
/// <returns>
/// A list of QueryString parameters for the given list of <paramref name="queryStringKeys"/>
/// </returns>
protected static string GenerateQueryStringParameters(HttpRequest request, params string[] queryStringKeys)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
if (queryStringKeys == null)
{
throw new ArgumentNullException("queryStringKeys");
}
var queryString = new StringBuilder(64);
foreach (string key in queryStringKeys)
{
if (Engage.Utility.HasValue(request.QueryString[key]))
{
if (queryString.Length > 0)
{
queryString.Append("&");
}
queryString.Append(key).Append("=").Append(request.QueryString[key]);
}
}
return queryString.ToString();
}
/// <summary>
/// Sends an <c>iCalendar</c> to the client to download.
/// </summary>
/// <param name="response">The response to use to send the <c>iCalendar</c>.</param>
/// <param name="content">The content of the <c>iCalendar</c>.</param>
/// <param name="name">The name of the file.</param>
protected static void SendICalendarToClient(HttpResponse response, string content, string name)
{
if (response == null)
{
throw new ArgumentNullException("response");
}
response.Clear();
// Stream The ICalendar
response.Buffer = true;
response.ContentType = "text/calendar";
response.ContentEncoding = Encoding.UTF8;
response.Charset = "utf-8";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(name) + ".ics" + "\"");
response.Write(content);
response.End();
}
/// <summary>
/// Raises the <see cref="Control.Init"/> event.
/// </summary>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.SetModuleConfiguration();
this.LocalResourceFile = this.AppRelativeTemplateSourceDirectory + Localization.LocalResourceDirectory + "/" + Path.GetFileNameWithoutExtension(this.TemplateControl.AppRelativeVirtualPath);
}
/// <summary>
/// Determines whether this instance of the module can display the given event (based on the Categories setting).
/// </summary>
/// <param name="event">The event to check.</param>
/// <returns>
/// <c>true</c> if this instance can show the event; otherwise, <c>false</c>.
/// </returns>
protected bool CanShowEvent(Event @event)
{
if (@event == null)
{
throw new ArgumentNullException("event");
}
return !this.CategoryIds.Any() || this.CategoryIds.Contains(@event.CategoryId);
}
/// <summary>
/// Clears the cached value for <see cref="CategoryIds"/>. This should be called when the setting is updated and needs to be read in the same request.
/// </summary>
protected void ClearCategoryIdsCache()
{
this.categoryIds = null;
}
/// <summary>
/// Denies the user access to this control, showing them an "Access Denied" message is they're logged in, or the login page if they aren't.
/// </summary>
protected void DenyAccess()
{
this.Response.Redirect(Framework.ModuleBase.IsLoggedIn ? Globals.NavigateURL("Access Denied") : Dnn.Utility.GetLoginUrl(this.PortalSettings, this.Request));
}
/// <summary>
/// Sets the <see cref="PortalModuleBase.ModuleConfiguration"/> for controls not loaded by DNN (i.e. when it's <c>null</c>),
/// getting it from the parent control
/// </summary>
protected void SetModuleConfiguration()
{
PortalModuleBase parentControl = this;
while (this.ModuleConfiguration == null)
{
parentControl = Engage.Utility.FindParentControl<PortalModuleBase>(parentControl);
if (parentControl == null)
{
break;
}
this.ModuleConfiguration = parentControl.ModuleConfiguration;
}
}
/// <summary>
/// Sets up a DNN <see cref="PagingControl"/>.
/// </summary>
/// <param name="pagingControl">The pager control.</param>
/// <param name="totalRecords">The total records.</param>
/// <param name="queryStringKeys">The QueryString keys which should be persisted when the paging links are clicked.</param>
protected void SetupPagingControl(PagingControl pagingControl, int totalRecords, params string[] queryStringKeys)
{
if (pagingControl == null)
{
throw new ArgumentNullException("pagingControl");
}
pagingControl.Visible = totalRecords != 0;
pagingControl.TotalRecords = totalRecords;
pagingControl.CurrentPage = this.CurrentPageIndex;
pagingControl.TabID = this.TabId;
pagingControl.QuerystringParams = GenerateQueryStringParameters(this.Request, queryStringKeys);
}
/// <summary>
/// Gets the name of the default category.
/// </summary>
/// <returns>The default category's display name</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Does not represent object state")]
protected string GetDefaultCategoryName()
{
return this.Localize("DefaultCategory.Text", this.LocalSharedResourceFile);
}
}
}