using System; using System.Web.UI.WebControls; using CMS.CMSHelper; using CMS.Community; using CMS.FormControls; using CMS.FormEngine; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.UIControls; public partial class CMSModules_Groups_FormControls_CommunityGroupSelector : FormEngineUserControl, IDataUserControl { #region "Private variables" private string mGroupCodeName = String.Empty; private bool mDisplayCurrentGroup = true; private bool mDisplayNoneWhenEmpty = false; private bool mDisplayNoneValue = false; private bool mUseDisplayNames = false; private string mWhereCondition = ""; private string mNoneValueText = ""; private bool specialFieldsAdded = false; #endregion #region "Public properties" /// /// ID of the current site. /// public int SiteID { get { return ValidationHelper.GetInteger(ViewState["SiteID"], CMSContext.CurrentSiteID); } set { ViewState["SiteID"] = value; } } /// /// ID of the currently selected group. /// public int GroupID { get { return ValidationHelper.GetInteger(usGroups.Value, 0); } set { usGroups.Value = value.ToString(); } } /// /// Indicates if '(global)' value should be displayed. /// public bool DisplayGlobalValue { get { return ValidationHelper.GetBoolean(ViewState["DisplayGlobalValue"], false); } set { ViewState["DisplayGlobalValue"] = value; } } /// /// Gets or sets text of 'none' item in the selector. /// public string NoneValueText { get { if (mNoneValueText == "") { mNoneValueText = GetString("general.empty"); } return mNoneValueText; } set { mNoneValueText = value; } } /// /// Gets or sets text of global item in the selector. /// public string GlobalValueText { get { if (string.IsNullOrEmpty(ValidationHelper.GetString(GetValue("GlobalValueText"), String.Empty))) { SetValue("GlobalValueText", GetString("general.global")); } return ValidationHelper.GetString(GetValue("GlobalValueText"), String.Empty); } set { SetValue("GlobalValueText", value); } } /// /// Indicates whether show display or code names of groups. /// public bool UseDisplayNames { get { return mUseDisplayNames; } set { mUseDisplayNames = value; } } /// /// Gets or sets if groups drop-down list should be enabled. /// public bool EnabledGroups { get { return usGroups.Enabled; } set { usGroups.Enabled = value; } } /// /// Indicates whether the '(none)' value should be displayed. /// public bool DisplayNoneValue { get { return mDisplayNoneValue; } set { mDisplayNoneValue = value; } } /// /// Indicates whether 'none' item should be added automatically when no groups exist for selector. /// public bool DisplayNoneWhenEmpty { get { return mDisplayNoneWhenEmpty; } set { mDisplayNoneWhenEmpty = value; } } /// /// Gets or sets WHERE condition to filter data. /// public string WhereCondition { get { return mWhereCondition; } set { mWhereCondition = value; } } /// /// Gets or sets the enabled state of the control. /// public override bool Enabled { get { return base.Enabled; } set { EnsureChildControls(); base.Enabled = value; usGroups.Enabled = value; } } /// /// Gets the currene UniSelector instance. /// public UniSelector CurrentSelector { get { EnsureChildControls(); return usGroups; } } /// /// Group code name. /// public string GroupCodeName { get { EnsureChildControls(); return Convert.ToString(usGroups.Value); } set { EnsureChildControls(); usGroups.Value = value; } } /// /// Gets the current drop down control. /// public DropDownList CurrentDropDown { get { EnsureChildControls(); return usGroups.DropDownSingleSelect; } } /// /// Gets or sets value name - default groupname. /// public override object Value { get { EnsureChildControls(); return Convert.ToString(usGroups.Value); } set { EnsureChildControls(); usGroups.Value = value; } } /// /// Gets ClientID of the dropdownlist with stylesheets. /// public override string ValueElementID { get { EnsureChildControls(); return usGroups.ClientID; } } /// /// Gets or sets if live iste property. /// public override bool IsLiveSite { get { EnsureChildControls(); return ValidationHelper.GetBoolean(ViewState["IsLiveSite"], true); } set { EnsureChildControls(); ViewState["IsLiveSite"] = value; usGroups.IsLiveSite = value; } } /// /// Indicates whether the current group option should be added to the list. /// public bool DisplayCurrentGroup { get { return mDisplayCurrentGroup; } set { mDisplayCurrentGroup = value; } } #endregion #region "SetValue/GetValue overrides" /// /// Sets the property value of the control, setting the value affects only local property value. /// /// Property name to set /// New property value public override void SetValue(string propertyName, object value) { switch (propertyName.ToLowerCSafe()) { case "groupid": EnsureChildControls(); usGroups.Value = value; break; case "reloaddata": ReloadData(); break; case "enabledgroups": EnabledGroups = ValidationHelper.GetBoolean(value, false); break; case "usedisplaynames": UseDisplayNames = ValidationHelper.GetBoolean(value, false); break; case "wherecondition": WhereCondition = ValidationHelper.GetString(value, ""); break; case "displaynonevalue": DisplayNoneValue = ValidationHelper.GetBoolean(value, false); break; case "nonevaluetext": NoneValueText = ValidationHelper.GetString(value, ""); break; case "displaynonewhenempty": DisplayNoneWhenEmpty = ValidationHelper.GetBoolean(value, false); break; case "displayglobalvalue": DisplayGlobalValue = ValidationHelper.GetBoolean(value, false); break; case "displaycurrentgroup": DisplayCurrentGroup = ValidationHelper.GetBoolean(value, false); break; case "siteid": SiteID = ValidationHelper.GetInteger(value, 0); break; } base.SetValue(propertyName, value); } /// /// Returns the value of the given webpart property property. /// /// Property name public override object GetValue(string propertyName) { switch (propertyName.ToLowerCSafe()) { case "groupid": return ValidationHelper.GetInteger(usGroups.Value, 0); case "enabledgroups": return EnabledGroups; case "usedisplaynames": return UseDisplayNames; case "wherecondition": return WhereCondition; case "displaynonevalue": return DisplayNoneValue; case "nonevaluetext": return NoneValueText; case "displaynonewhenempty": return DisplayNoneWhenEmpty; case "displayglobalvalue": return DisplayGlobalValue; case "currentselector": return CurrentSelector; case "currentdropdown": return CurrentDropDown; case "displaycurrentgroup": return DisplayCurrentGroup; case "siteid": return SiteID; } return base.GetValue(propertyName); } #endregion #region "Other methods/events" /// /// Page_Init event. /// /// Sender /// Arguments protected override void OnInit(EventArgs e) { SetValue("CurrentSelector", CurrentSelector); } /// /// Page_load event. /// /// Sender /// Arguments protected void Page_Load(object sender, EventArgs e) { // Return GroupID if field type is integer if ((FieldInfo != null) && ((FieldInfo.DataType == FormFieldDataTypeEnum.Integer) || (FieldInfo.DataType == FormFieldDataTypeEnum.LongInteger))) { usGroups.ReturnColumnName = "GroupID"; usGroups.AllowEmpty = true; DisplayCurrentGroup = DisplayNoneValue = DisplayNoneWhenEmpty = false; } if (StopProcessing) { usGroups.StopProcessing = true; } else { EnsureChildControls(); AddSpecialFields(); // Ensure correct livesite setting for uniselector IsLiveSite = IsLiveSite; usGroups.WhereCondition = BuildWhereCondition(); usGroups.OnSelectionChanged += new EventHandler(usGroups_OnSelectionChanged); } } /// /// Selection changed. /// /// Sender /// Arguments private void usGroups_OnSelectionChanged(object sender, EventArgs e) { RaiseOnChanged(); } /// /// Creates child controls and loads update panel container if it is required. /// protected override void CreateChildControls() { // If selector is not defined load updat panel container if (usGroups == null) { pnlUpdate.LoadContainer(); } // Call base method base.CreateChildControls(); } /// /// Reloads groups for the specified site. /// /// Indicates whether the load should be forced public void ReloadData(bool forceLoad) { ReloadData(); } /// /// Reloads groups for the specified site. /// public void ReloadData() { string[,] specialFields = new string[3,2]; int index = 0; // Add "current group" option if (DisplayCurrentGroup && usGroups.SpecialFields == null) { specialFields[index, 0] = GetString("group.current"); specialFields[index, 1] = GroupInfoProvider.CURRENT_GROUP; index++; } // Add default item if (DisplayGlobalValue) { specialFields[index, 0] = GlobalValueText; specialFields[index, 1] = "0"; index++; } // Add none item if required if (DisplayNoneValue || (DisplayNoneWhenEmpty && !usGroups.HasData)) { specialFields[index, 0] = NoneValueText; specialFields[index, 1] = "-1"; } // Handle on selection changed usGroups.SpecialFields = specialFields; usGroups.DropDownSingleSelect.ClearSelection(); usGroups.DropDownSingleSelect.Items.Clear(); AddSpecialFields(); usGroups.WhereCondition = BuildWhereCondition(); usGroups.Reload(true); if (!RequestHelper.IsPostBack()) { RaiseOnChanged(); } } /// /// Builds wehre condition. /// public string BuildWhereCondition() { string where = string.Empty; if (SiteID > 0) { where = "GroupSiteID=" + SiteID + " AND GroupApproved = 1"; } // If the user is global admin display all the site groups otherwise display just user's groups if (!CMSContext.CurrentUser.IsGlobalAdministrator && !CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.Groups", "read")) { if (where != null) { if (!string.IsNullOrEmpty(where)) { where += " AND "; } where += "(GroupID IN (SELECT MemberGroupID FROM Community_GroupMember WHERE MemberUserID=" + CMSContext.CurrentUser.UserID + "))"; } } // Include custom WHERE condition if specified if (!string.IsNullOrEmpty(WhereCondition)) { if (!string.IsNullOrEmpty(where)) { where += " AND "; } where += "(" + WhereCondition + ")"; } return where; } /// /// Adds special fields to selector. /// private void AddSpecialFields() { if (!specialFieldsAdded) { string[,] specialFields = new string[3,2]; int index = 0; // Add "current group" option if (DisplayCurrentGroup && usGroups.SpecialFields == null) { specialFields[index, 0] = GetString("group.current"); specialFields[index, 1] = GroupInfoProvider.CURRENT_GROUP; index++; } // Add default item if (DisplayGlobalValue) { specialFields[index, 0] = GlobalValueText; specialFields[index, 1] = "0"; index++; } // Add none item if required if (DisplayNoneValue || (DisplayNoneWhenEmpty && !usGroups.HasData)) { specialFields[index, 0] = NoneValueText; specialFields[index, 1] = "-1"; } usGroups.SpecialFields = specialFields; specialFieldsAdded = true; } } /// /// Returns WHERE condition for selected form. /// public override string GetWhereCondition() { // Return correct WHERE condition for integer if none value is selected if ((FieldInfo != null) && ((FieldInfo.DataType == FormFieldDataTypeEnum.Integer) || (FieldInfo.DataType == FormFieldDataTypeEnum.LongInteger))) { int id = ValidationHelper.GetInteger(usGroups.Value, 0); if (id > 0) { return base.GetWhereCondition(); } } return null; } #endregion }