-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSettings.ascx.cs
399 lines (352 loc) · 18.2 KB
/
Settings.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
// <copyright file="Settings.ascx.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.Globalization;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using Engage.Events;
using Telerik.Web.UI;
/// <summary>
/// This is the settings code behind for Event related Settings.
/// </summary>
public partial class Settings : SettingsBase
{
/// <summary>
/// Backing field for <see cref="Categories"/>
/// </summary>
private string[] categories;
/// <summary>
/// Backing field for the current module settings base selected.
/// </summary>
private ModuleSettingsBase specificSettingsControl;
/// <summary>
/// Gets the selected categories from module settings.
/// </summary>
private string[] Categories
{
get
{
if (this.categories == null)
{
var categoriesSettingValue = Dnn.Events.ModuleSettings.Categories.GetValueAsStringFor(this);
this.categories = categoriesSettingValue.Split(',');
}
return this.categories;
}
}
/// <summary>
/// Loads the settings.
/// </summary>
public override void LoadSettings()
{
base.LoadSettings();
try
{
if (this.IsPostBack)
{
return;
}
Localization.LocalizeGridView(ref this.DetailsDisplayModuleGrid, this.LocalResourceFile);
this.DetailsDisplayModuleGrid.DataSource = new ModuleController().GetModulesByDefinition(this.PortalId, Utility.ModuleDefinitionFriendlyName);
this.DetailsDisplayModuleGrid.DataBind();
this.CategoriesCheckBoxTreeView.DataTextField = "Name";
this.CategoriesCheckBoxTreeView.DataValueField = "Id";
this.CategoriesCheckBoxTreeView.DataFieldID = "Id";
this.CategoriesCheckBoxTreeView.DataFieldParentID = "ParentId";
var categoryList = (from category in CategoryCollection.Load(this.PortalId)
select new
{
Name = string.IsNullOrEmpty(category.Name)
? this.Localize("DefaultCategory", this.LocalSharedResourceFile)
: category.Name,
Id = category.Id.ToString(CultureInfo.InvariantCulture),
ParentId = category.ParentId.HasValue
? category.ParentId.Value.ToString(CultureInfo.InvariantCulture)
: string.Empty
}).ToList();
categoryList.Add(new { Name = this.Localize("All Categories.Text"), Id = string.Empty, ParentId = (string)null });
this.CategoriesCheckBoxTreeView.DataSource = categoryList;
this.CategoriesCheckBoxTreeView.DataBind();
this.SetOptions();
}
catch (Exception exc)
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
/// <summary>
/// Updates the settings.
/// </summary>
public override void UpdateSettings()
{
if (!this.Page.IsValid)
{
return;
}
try
{
Dnn.Events.ModuleSettings.FeaturedOnly.Set(this, this.FeaturedCheckBox.Checked);
Dnn.Events.ModuleSettings.HideFullEvents.Set(this, this.HideFullEventsCheckBox.Checked);
Dnn.Events.ModuleSettings.AllowRegistrationsByDefault.Set(this, this.AllowRegistrationsByDefaultCheckBox.Checked);
Dnn.Events.ModuleSettings.DetailsDisplayTabId.Set(this, this.GetSelectedDetailsDisplayTabId());
Dnn.Events.ModuleSettings.DetailsDisplayModuleId.Set(this, this.GetSelectedDetailsDisplayModuleId());
string cats;
var nodes = this.CategoriesCheckBoxTreeView.Nodes.Cast<RadTreeNode>().ToList();
if (nodes.Any() && nodes.First().Checked)
{
cats = string.Empty;
}
else
{
var selectedCategoryIds = this.CategoriesCheckBoxTreeView.CheckedNodes.Select(node => node.Value);
cats = string.Join(",", selectedCategoryIds.ToArray());
}
Dnn.Events.ModuleSettings.Categories.Set(this, cats);
this.specificSettingsControl.UpdateSettings();
}
catch (Exception exc)
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += this.Page_Load;
this.CategoriesListValidator.ServerValidate += this.CategoriesListValidator_ServerValidate;
this.DetailsDisplayModuleValidator.ServerValidate += this.DetailsDisplayModuleValidator_ServerValidate;
this.CategoriesCheckBoxTreeView.NodeCreated += this.CategoriesCheckBoxTreeView_NodeCreated;
this.CategoriesCheckBoxTreeView.NodeDataBound += this.CategoriesCheckBoxTreeView_NodeDataBound;
}
/// <summary>
/// Handles the <see cref="CheckBox.CheckedChanged"/> event of the <c>DetailsDisplayModuleRadioButton</c> control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void DetailsDisplayModuleRadioButton_CheckedChanged(object sender, EventArgs e)
{
var selectedRow = Engage.Utility.FindParentControl<TableRow>((RadioButton)sender);
this.SetDetailsDisplayModuleGridSelection(GetTabIdFromRow(selectedRow), GetModuleIdFromRow(selectedRow));
}
/// <summary>
/// Gets the tab ID of the given <paramref name="row"/>.
/// </summary>
/// <param name="row">The row from which to get the tab ID.</param>
/// <returns>The tab ID of the given <paramref name="row"/>.</returns>
private static int GetTabIdFromRow(TableRow row)
{
return int.Parse(row.Cells[2].Text, NumberStyles.Integer, CultureInfo.CurrentCulture);
}
/// <summary>
/// Gets the module ID of the given <paramref name="row"/>.
/// </summary>
/// <param name="row">The row from which to get the module ID.</param>
/// <returns>The module ID of the given <paramref name="row"/>.</returns>
private static int GetModuleIdFromRow(TableRow row)
{
return int.Parse(row.Cells[4].Text, NumberStyles.Integer, CultureInfo.CurrentCulture);
}
/// <summary>
/// Disables / enables the all nodes.
/// </summary>
/// <param name="nodes">The nodes.</param>
/// <param name="enabled">if set to <c>true</c> [enabled].</param>
private void SetEnabledAllNodes(IEnumerable<RadTreeNode> nodes, bool enabled)
{
foreach (var node in nodes)
{
node.Enabled = enabled;
node.Checked = enabled;
if (node.Nodes != null && node.Nodes.Count > 0)
{
this.SetEnabledAllNodes(node.Nodes.Cast<RadTreeNode>(), enabled);
}
}
}
/// <summary>
/// Handles the NodeDataBound event of the CategoriesCheckBoxTreeView control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.Web.UI.RadTreeNodeEventArgs"/> instance containing the event data.</param>
private void CategoriesCheckBoxTreeView_NodeDataBound(object sender, RadTreeNodeEventArgs e)
{
if (this.Categories != null & this.Categories.Contains(e.Node.Value))
{
e.Node.Checked = true;
}
}
/// <summary>
/// Handles the NodeCreated event of the CategoriesCheckBoxTreeView control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.Web.UI.RadTreeNodeEventArgs"/> instance containing the event data.</param>
private void CategoriesCheckBoxTreeView_NodeCreated(object sender, RadTreeNodeEventArgs e)
{
e.Node.Expanded = true;
}
/// <summary>
/// Handles the <see cref="CustomValidator.ServerValidate"/> event of the <see cref="CategoriesListValidator"/> control.
/// </summary>
/// <param name="source">The source of the event.</param>
/// <param name="args">The <see cref="ServerValidateEventArgs"/> instance containing the event data.</param>
private void CategoriesListValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = this.CategoriesCheckBoxTreeView.CheckedNodes.Any();
}
/// <summary>
/// Handles the <see cref="CustomValidator.ServerValidate"/> event of the <see cref="DetailsDisplayModuleValidator"/> control.
/// </summary>
/// <param name="source">The source of the event.</param>
/// <param name="args">The <see cref="System.Web.UI.WebControls.ServerValidateEventArgs"/> instance containing the event data.</param>
private void DetailsDisplayModuleValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
var checkedCount = this.DetailsDisplayModuleGrid.Rows.Cast<GridViewRow>()
.Select(row => (RadioButton)row.FindControl("DetailsDisplayModuleRadioButton"))
.Count(detailsDisplayModuleRadioButton => detailsDisplayModuleRadioButton.Checked);
args.IsValid = checkedCount == 1;
}
/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void Page_Load(object sender, EventArgs e)
{
this.DisplaySettingsControl();
}
/// <summary>
/// Sets the options.
/// </summary>
private void SetOptions()
{
this.FeaturedCheckBox.Checked = Dnn.Events.ModuleSettings.FeaturedOnly.GetValueAsBooleanFor(this).Value;
this.HideFullEventsCheckBox.Checked = Dnn.Events.ModuleSettings.HideFullEvents.GetValueAsBooleanFor(this).Value;
this.AllowRegistrationsByDefaultCheckBox.Checked = Dnn.Events.ModuleSettings.AllowRegistrationsByDefault.GetValueAsBooleanFor(this).Value;
this.SetDetailsDisplayModuleGridSelection(
Dnn.Events.ModuleSettings.DetailsDisplayTabId.GetValueAsInt32For(this) ?? this.TabId,
Dnn.Events.ModuleSettings.DetailsDisplayModuleId.GetValueAsInt32For(this) ?? this.ModuleId);
var categoriesSettingValue = Dnn.Events.ModuleSettings.Categories.GetValueAsStringFor(this);
if (!string.IsNullOrEmpty(categoriesSettingValue))
{
return;
}
var all = this.CategoriesCheckBoxTreeView.Nodes.Cast<RadTreeNode>().FirstOrDefault();
if (all == null)
{
return;
}
all.Checked = true;
this.SetEnabledAllNodes(all.Nodes.Cast<RadTreeNode>(), false);
}
/// <summary>
/// Selects the <see cref="RadioButton"/> for the row in <see cref="DetailsDisplayModuleGrid"/> with the given <paramref name="selectedTabId"/> and <paramref name="selectedModuleId"/>.
/// </summary>
/// <param name="selectedTabId">The Tab ID of the row to select.</param>
/// /// <param name="selectedModuleId">The Module ID of the row to select.</param>
private void SetDetailsDisplayModuleGridSelection(int selectedTabId, int selectedModuleId)
{
foreach (GridViewRow row in this.DetailsDisplayModuleGrid.Rows)
{
var detailsDisplayModuleRadioButton = (RadioButton)row.FindControl("DetailsDisplayModuleRadioButton");
detailsDisplayModuleRadioButton.Checked = GetTabIdFromRow(row) == selectedTabId && GetModuleIdFromRow(row) == selectedModuleId;
}
}
/// <summary>
/// Gets the selected Tab ID for the DetailsDisplayTabId setting.
/// </summary>
/// <returns>The TabID that was selected for the Details Display TabID setting</returns>
/// <exception cref="InvalidOperationException">This method cannot be called until validation has run, ensuring that a Tab ID has been selected</exception>
private int GetSelectedDetailsDisplayTabId()
{
foreach (GridViewRow row in this.DetailsDisplayModuleGrid.Rows)
{
var detailsDisplayModuleRadioButton = (RadioButton)row.FindControl("DetailsDisplayModuleRadioButton");
if (detailsDisplayModuleRadioButton.Checked)
{
return GetTabIdFromRow(row);
}
}
throw new InvalidOperationException("This method cannot be called until validation has run, ensuring that a Tab ID has been selected");
}
/// <summary>
/// Gets the selected Module ID for the DetailsDisplayModuleId setting.
/// </summary>
/// <returns>The ModuleID that was selected for the Details Display ModuleID setting</returns>
/// <exception cref="InvalidOperationException">This method cannot be called until validation has run, ensuring that a Module ID has been selected</exception>
private int GetSelectedDetailsDisplayModuleId()
{
foreach (GridViewRow row in this.DetailsDisplayModuleGrid.Rows)
{
var detailsDisplayModuleRadioButton = (RadioButton)row.FindControl("DetailsDisplayModuleRadioButton");
if (detailsDisplayModuleRadioButton.Checked)
{
return GetModuleIdFromRow(row);
}
}
throw new InvalidOperationException("This method cannot be called until validation has run, ensuring that a Module ID has been selected");
}
/// <summary>
/// Displays the settings control.
/// </summary>
private void DisplaySettingsControl()
{
switch (Dnn.Events.ModuleSettings.DisplayType.GetValueAsStringFor(this).ToUpperInvariant())
{
case "LIST":
this.LoadSettingsControl("Display/TemplateDisplayOptions.ascx", "List Settings.Header");
break;
case "CALENDAR":
this.LoadSettingsControl("Display/CalendarDisplayOptions.ascx", "Calendar Settings.Header");
break;
default:
break;
}
}
/// <summary>
/// Loads the settings control.
/// </summary>
/// <param name="controlName">Name of the control.</param>
/// <param name="headerResourceKey">The resource key for the specific settings control's header.</param>
private void LoadSettingsControl(string controlName, string headerResourceKey)
{
this.SpecificSettingsPlaceholder.EnableViewState = false;
this.SpecificSettingsPlaceholder.Controls.Clear();
this.specificSettingsControl = this.CreateSettingsControl(controlName);
this.SpecificSettingsPlaceholder.Controls.Add(this.specificSettingsControl);
this.SpecificSettingsHeaderLiteral.Text = this.Localize(headerResourceKey);
}
/// <summary>
/// Creates the settings control.
/// </summary>
/// <param name="controlName">Name of the control.</param>
/// <returns>Module Settings Base</returns>
private ModuleSettingsBase CreateSettingsControl(string controlName)
{
var settingsControl = (ModuleSettingsBase)this.LoadControl(controlName);
ModuleInfo mi = new ModuleController().GetModule(this.ModuleId, this.TabId);
settingsControl.ModuleConfiguration = mi;
settingsControl.ID = Path.GetFileNameWithoutExtension(controlName);
settingsControl.LoadSettings();
return settingsControl;
}
}
}