DotNetNuke Module Settings Made Simple
If you create enough DotNetNuke modules, you will soon notice that you repeat code to retrieve and store settings. I got fed-up with this and wrote a common engine with an XML-driven UI to manage settings in my modules. Now, when I create a new module, I just change one XML file and I have the UI for the user to edit/save module settings ready to go.
I am not ready to share the complete UI yet, however, I thought I would start by sharing my code for managing the settings. For any DNN module, the code allows you to store and retrieve settings with three scope types — User, Module and Global.
“User” scope is a user-specific value that is persisted using the DNN core personalization support; “Module” scope is a value that is specific to one instance of a module; “Global” scope is a value that is specific to all instances of the current module type.
I suspect the User scope may have been rendered obsolete by the profile capabilities in DNN 3.3.4/4.3.4, however everything still works. There are some useful helper methods that can check if a setting exists and strong-typed methods for retrieving string, numeric and boolean values.
One item bears explaining — enumerated values. The enumeration is quite limited and is actually a “role enumeration.” It allows you to have a setting that indirectly refers to the roles granted view/edit rights at the module level. For example, if you store an enumerated value as “e”, when retrieving the value, GetEnumerated() will return true if the current user has edit permissions for the module. The enumerations are as follows:
“*” — All Users — always returns true
“r” — Registered Users — returns true if user is member of Registered Users role
“e” — Editors — returns true if user is member of a role that has edit permissions
“a” — Administrators — returns true if user has administrator rights for the portal
“n” — No access — returns false
I use this to set a module’s boolean property to True or False. This approach makes it a lot easier to deal with simple permissions instead of having the user select actual Roles for various settings.
The code is simple enough that you should be able to figure it out quickly (sorry for the lack of comments). The key methods are “Save,” “Get” and “Delete.”
Here is the code. Use it as you wish, but please do not delete the copyright line.
Speerio Module Settings Controller
SettingsController.cs
using System;
using System.Web;
using System.Collections;
using DotNetNuke.Security;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.Personalization;
// Copyright 2006 Speerio, Inc. (Author: Nik Kalyani)
namespace Speerio.DNN.Common
{
public class SettingsController
{
private int moduleId = -1;
private int globalId = -1;
private Hashtable moduleSettings = null;
private Hashtable globalSettings = null;
private Hashtable userSettings = null;
private UserInfo userInfo = UserController.GetCurrentUserInfo();
private PortalSettings portalSettings = (PortalSettings) HttpContext.Current.Items["PortalSettings"];
private PersonalizationController personalizationController = new PersonalizationController();
private ModuleController moduleController = new ModuleController();
private ModuleInfo moduleInfo = null;
private bool createMissing = false;
public enum SettingScope { User, Module, Global };
public SettingsController(int moduleId) : this(moduleId, false)
{
}
public SettingsController(int moduleId, bool createMissing)
{
ModuleId = moduleId;
moduleInfo = moduleController.GetModuleByDefinition(portalSettings.PortalId,"Site Settings");
GlobalId = moduleInfo.ModuleID;
if (HttpContext.Current.Request.IsAuthenticated)
userSettings = personalizationController.LoadProfile(userInfo.UserID, portalSettings.PortalId).Profile;
moduleSettings = moduleController.GetModuleSettings(ModuleId);
globalSettings = moduleController.GetModuleSettings(GlobalId);
}
private bool CreateMissing
{
get{ return(createMissing); }
set{ createMissing = value; }
}
public Hashtable GlobalSettings
{
get{ return(globalSettings); }
}
public Hashtable ModuleSettings
{
get{ return(moduleSettings); }
}
public Hashtable UserSettings
{
get{ return(userSettings); }
}
private int ModuleId
{
get{ return(moduleId); }
set{ moduleId = value; }
}
private int GlobalId
{
get{ return(globalId); }
set{ globalId = value; }
}
public void Delete(string key)
{
Delete(key, SettingScope.Module);
}
public void Delete(string key, SettingScope scope)
{
try
{
switch(scope)
{
case SettingScope.Module :
moduleController.DeleteModuleSetting(ModuleId, key);
break;
case SettingScope.Global :
moduleController.DeleteModuleSetting(GlobalId, key);
break;
case SettingScope.User :
PersonalizationInfo pInfo = personalizationController.LoadProfile(userInfo.UserID, portalSettings.PortalId);
Hashtable userProfile = pInfo.Profile;
pInfo.Profile.Remove(key);
personalizationController.SaveProfile(pInfo, userInfo.UserID, portalSettings.PortalId);
break;
}
}
catch
{
}
}
public string Get(string key)
{
return(Get(key, "", SettingScope.Module, false));
}
public string Get(string key, bool replaceTokens)
{
return(Get(key, "", SettingScope.Module, replaceTokens));
}
public string Get(string key, SettingScope scope)
{
return(Get(key, "", scope, false));
}
public string Get(string key, SettingScope scope, bool replaceTokens)
{
return(Get(key, "", scope, replaceTokens));
}
public string Get(string key, string defaultValue)
{
return(Get(key, defaultValue, SettingScope.Module, false));
}
public string Get(string key, string defaultValue, bool replaceTokens)
{
return(Get(key, defaultValue, SettingScope.Module, replaceTokens));
}
public string Get(string key, string defaultValue, SettingScope scope, bool replaceTokens)
{
string settingValue = GetSettingValue(key, scope);
string returnValue = "";
if (settingValue == null)
{
if (CreateMissing) Save(key, defaultValue, scope);
returnValue = defaultValue;
}
else
returnValue = settingValue;
// PortalTokens does a search and replace of embedded
// tokens in strings. Since it is not included with
// this code, I have commented it out.
// if (replaceTokens)
// return(PortalTokens.Replace(returnValue, "~Public", moduleInfo));
//else
return(returnValue);
}
public void Save(string key, string keyValue)
{
Save(key, keyValue, SettingScope.Module);
}
public void Save(string key, string keyValue, SettingScope scope)
{
SaveSettingValue(key, keyValue, scope);
}
public int GetNumeric(string key)
{
return(GetNumeric(key, 0, SettingScope.Module));
}
public int GetNumeric(string key, SettingScope scope)
{
return(GetNumeric(key, 0, scope));
}
public int GetNumeric(string key, int defaultValue)
{
return(GetNumeric(key, defaultValue, SettingScope.Module));
}
public int GetNumeric(string key, int defaultValue, SettingScope scope)
{
string tmpSettingValue = GetSettingValue(key, scope);
if (tmpSettingValue == null)
{
if (CreateMissing) SaveNumeric(key, defaultValue, scope);
return(defaultValue);
}
else
{
int settingValue = defaultValue;
try
{
settingValue = Convert.ToInt32(tmpSettingValue);
}
catch
{
settingValue = defaultValue;
}
return(settingValue);
}
}
public void SaveNumeric(string key, int keyValue)
{
SaveNumeric(key, keyValue, SettingScope.Module);
}
public void SaveNumeric(string key, int keyValue, SettingScope scope)
{
SaveSettingValue(key, keyValue.ToString(), scope);
}
public bool GetBoolean(string key)
{
return(GetBoolean(key, false, SettingScope.Module));
}
public bool GetBoolean(string key, SettingScope scope)
{
return(GetBoolean(key, false, scope));
}
public bool GetBoolean(string key, bool defaultValue)
{
return(GetBoolean(key, defaultValue, SettingScope.Module));
}
public bool GetBoolean(string key, bool defaultValue, SettingScope scope)
{
string settingValue = GetSettingValue(key, scope);
if (settingValue == null)
{
if (CreateMissing) SaveBoolean(key, defaultValue, scope);
return(defaultValue);
}
else
return(settingValue == "1" ? true : false);
}
public void SaveBoolean(string key, bool keyValue)
{
SaveBoolean(key, keyValue, SettingScope.Module);
}
public void SaveBoolean(string key, bool keyValue, SettingScope scope)
{
SaveSettingValue(key, (keyValue ? "1" : "0"), scope);
}
public bool GetEnumerated(string key)
{
return(GetEnumerated(key, "n", SettingScope.Module));
}
public bool GetEnumerated(string key, SettingScope scope)
{
return(GetEnumerated(key, "n", scope));
}
public bool GetEnumerated(string key, string defaultValue)
{
return(GetEnumerated(key, defaultValue, SettingScope.Module));
}
public bool GetEnumerated(string key, string defaultValue, SettingScope scope)
{
bool returnValue = false;
string settingValue = GetSettingValue(key, scope);
if (settingValue == null)
{
if (CreateMissing) SaveEnumerated(key, defaultValue, scope);
settingValue = defaultValue;
}
switch(settingValue)
{
case "*": returnValue = true; break;
case "r": if (PortalSecurity.IsInRole(portalSettings.RegisteredRoleName)) returnValue = true; break;
case "e": if (PortalSecurity.HasEditPermissions(ModuleId)) returnValue = true; break;
case "a": if (PortalSecurity.IsInRole(portalSettings.AdministratorRoleName)) returnValue = true; break;
case "n": returnValue = false; break;
}
return(returnValue);
}
public void SaveEnumerated(string key, string keyValue)
{
SaveEnumerated(key, keyValue, SettingScope.Module);
}
public void SaveEnumerated(string key, string keyValue, SettingScope scope)
{
SaveSettingValue(key, keyValue, scope);
}
private void SaveSettingValue(string key, string keyValue, SettingScope scope)
{
try
{
switch(scope)
{
case SettingScope.Module :
moduleController.UpdateModuleSetting(ModuleId, key, keyValue); break;
case SettingScope.Global :
moduleController.UpdateModuleSetting(GlobalId, key, keyValue); break;
case SettingScope.User :
PersonalizationInfo pInfo = personalizationController.LoadProfile(userInfo.UserID, portalSettings.PortalId);
Hashtable userProfile = pInfo.Profile;
if (pInfo.Profile.Contains(key))
pInfo.Profile[key] = keyValue;
else
pInfo.Profile.Add(key, keyValue);
personalizationController.SaveProfile(pInfo, userInfo.UserID, portalSettings.PortalId);
break;
}
}
catch
{
}
}
public bool SettingExists(string key)
{
return(SettingExists(key, SettingScope.Module));
}
public bool SettingExists(string key, SettingScope scope)
{
bool returnValue = false;
try
{
switch(scope)
{
case SettingScope.Global : if (GlobalSettings[key] != null) returnValue = true; break;
case SettingScope.Module : if (ModuleSettings[key] != null) returnValue = true; break;
case SettingScope.User : if (UserSettings[key] != null) returnValue = true; break;
}
}
catch
{
}
return(returnValue);
}
private string GetSettingValue(string key, SettingScope scope)
{
string settingValue = null;
try
{
switch(scope)
{
case SettingScope.Global : settingValue = (string) GlobalSettings[key]; break;
case SettingScope.Module : settingValue = (string) ModuleSettings[key]; break;
case SettingScope.User : settingValue = (string) UserSettings[key]; break;
}
}
catch
{
}
return(settingValue);
}
}
}