using System;
using System.Web.UI.WebControls;
using CMS.CMSHelper;
using CMS.ExtendedControls;
using CMS.FormControls;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.UIControls;
public partial class CMSModules_Membership_FormControls_Roles_SelectRole : FormEngineUserControl
{
#region "Variables"
private int mSiteId = 0;
private int mGroupId = 0;
private bool mUseFriendlyMode = false;
private bool mUseCodeNameForSelection = true;
private bool mShowSiteFilter = true;
private bool mSimpleMode = false;
private bool mAllowEmpty = true;
private bool mGlobalRoles = true;
private bool mSiteRoles = true;
#endregion
#region "Public properties"
///
/// Indicates whether global objects have suffix "(global)" in the grid.
///
public bool AddGlobalObjectSuffix
{
get
{
EnsureChildControls();
return usRoles.AddGlobalObjectSuffix;
}
set
{
EnsureChildControls();
usRoles.AddGlobalObjectSuffix = value;
}
}
///
/// Indicates if role selector allow empty selection.
///
public bool AllowEmpty
{
get
{
return mAllowEmpty;
}
set
{
mAllowEmpty = value;
}
}
///
/// Gets or sets the ID of the group
/// (search will be only among group roles if this property is nonzero)
///
public int GroupID
{
get
{
return mGroupId;
}
set
{
mGroupId = value;
}
}
///
/// If true site roles are selected.
///
public bool SiteRoles
{
get
{
return mSiteRoles;
}
set
{
mSiteRoles = value;
}
}
///
/// Gets or sets the ID of the site to display:
/// (-1 means CurrentSite, 0 means you can choose, > 0 means specific site)
///
public int SiteID
{
get
{
return mSiteId;
}
set
{
mSiteId = value;
}
}
///
/// Gets or sets the enabled state of the control.
///
public override bool Enabled
{
get
{
return base.Enabled;
}
set
{
EnsureChildControls();
base.Enabled = value;
usRoles.Enabled = value;
}
}
///
/// Gets the current UniSelector instance.
///
public UniSelector CurrentSelector
{
get
{
EnsureChildControls();
return usRoles;
}
}
///
/// Gets or sets role name.
///
public override object Value
{
get
{
EnsureChildControls();
return Convert.ToString(usRoles.Value);
}
set
{
EnsureChildControls();
usRoles.Value = value;
}
}
public override string ValueDisplayName
{
get
{
return usRoles.ValueDisplayName;
}
}
///
/// Gets or sets the value that indicates whether role should be edited in friendly format.
///
public bool UseFriendlyMode
{
get
{
return mUseFriendlyMode;
}
set
{
EnsureChildControls();
mUseFriendlyMode = value;
// Change the selector settings for (un)friendly mode
if (value)
{
usRoles.AllowEditTextBox = false;
}
else
{
usRoles.AllowEditTextBox = true;
}
}
}
///
/// Gets or sets the value that indicates whether to use code name as return value.
///
public bool UseCodeNameForSelection
{
get
{
return mUseCodeNameForSelection;
}
set
{
mUseCodeNameForSelection = value;
}
}
///
/// Gets or sets if site filter should be shown or not.
///
public bool ShowSiteFilter
{
get
{
return mShowSiteFilter;
}
set
{
mShowSiteFilter = value;
}
}
///
/// Gets or sets if control works in simple mode (ignores SiteID, GroupID parameters).
///
public bool SimpleMode
{
get
{
return mSimpleMode;
}
set
{
mSimpleMode = value;
}
}
///
/// Gets or sets if live iste property.
///
public override bool IsLiveSite
{
get
{
EnsureChildControls();
return base.IsLiveSite;
}
set
{
EnsureChildControls();
base.IsLiveSite = value;
usRoles.IsLiveSite = value;
}
}
///
/// Gets the drop down control.
///
public DropDownList DropDownSingleSelect
{
get
{
EnsureChildControls();
return usRoles.DropDownSingleSelect;
}
}
///
/// Returns ClientID of the textboxselect.
///
public override string ValueElementID
{
get
{
return usRoles.TextBoxSelect.ClientID;
}
}
///
/// If true global roles are selected.
///
public bool GlobalRoles
{
get
{
return mGlobalRoles;
}
set
{
mGlobalRoles = value;
}
}
///
/// Indicates if generic role 'Everyone' should be displayed even if other generic roles are hidden.
///
public bool DisplayEveryone
{
get;
set;
}
#endregion
#region "Events"
///
/// Page_Load event.
///
protected void Page_Load(object sender, EventArgs e)
{
// If current control context is widget or livesite hide site selector
if (ControlsHelper.CheckControlContext(this, ControlContext.WIDGET_PROPERTIES) || ControlsHelper.CheckControlContext(this, ControlContext.LIVE_SITE))
{
ShowSiteFilter = false;
}
Reload(false);
}
///
/// Reloads the selector's data.
///
/// Indicates whether data should be forcibly reloaded
public void Reload(bool forceReload)
{
// Set allow empty
usRoles.AllowEmpty = AllowEmpty;
// Get siteID
if (SiteID == 0)
{
SiteID = CMSContext.CurrentSiteID;
}
// Add sites filter
if (ShowSiteFilter)
{
usRoles.FilterControl = "~/CMSFormControls/Filters/SiteFilter.ascx";
usRoles.SetValue("DefaultFilterValue", SiteID);
usRoles.SetValue("FilterMode", "role");
}
// Build where condition
string whereCondition = (GroupID > 0) ? "(RoleGroupID = " + GroupID.ToString() + ") " : "(RoleGroupID IS NULL)";
if (UseFriendlyMode)
{
if (DisplayEveryone)
{
whereCondition += " AND RoleName != '" + RoleInfoProvider.AUTHENTICATED + "' AND RoleName != '" + RoleInfoProvider.NOTAUTHENTICATED + "' ";
}
else
{
whereCondition += " AND RoleName != '" + RoleInfoProvider.EVERYONE + "' AND RoleName != '" + RoleInfoProvider.AUTHENTICATED + "' AND RoleName != '" + RoleInfoProvider.NOTAUTHENTICATED + "' ";
}
}
if (!ShowSiteFilter)
{
if (SiteRoles && GlobalRoles)
{
whereCondition = SqlHelperClass.AddWhereCondition(whereCondition, "SiteID IS NULL OR SiteID =" + SiteID.ToString());
}
else if (SiteRoles)
{
whereCondition = SqlHelperClass.AddWhereCondition(whereCondition, "SiteID =" + SiteID.ToString());
}
else if (GlobalRoles)
{
whereCondition = SqlHelperClass.AddWhereCondition(whereCondition, "SiteID IS NULL");
}
}
usRoles.ReturnColumnName = (UseCodeNameForSelection ? "RoleName" : "RoleID");
usRoles.WhereCondition = whereCondition;
if (SiteRoles && GlobalRoles && UseCodeNameForSelection)
{
usRoles.AddGlobalObjectNamePrefix = true;
}
if (forceReload)
{
usRoles.Reload(forceReload);
}
}
///
/// Creates child controls and loads update panle container if it is required.
///
protected override void CreateChildControls()
{
// If selector is not defined load updat panel container
if (usRoles == null)
{
pnlUpdate.LoadContainer();
}
// Call base method
base.CreateChildControls();
}
#endregion
}