using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CMS.CMSHelper; using CMS.FormControls; using CMS.FormEngine; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.SiteProvider; using CMS.UIControls; using CMS.Forums; public partial class CMSModules_Forums_FormControls_ForumSelector : FormEngineUserControl { #region "Constants" const string ADHOCFORUM_VALUE = "ad_hoc_forum"; #endregion #region "Variables" private int siteId = 0; #endregion #region "Properties" /// /// Gets Value display name. /// public override string ValueDisplayName { get { return uniSelector.ValueDisplayName; } } /// /// Gets or sets the enabled state of the control. /// public override bool Enabled { get { return base.Enabled; } set { EnsureChildControls(); base.Enabled = value; uniSelector.Enabled = value; uniSelector.TextBoxSelect.Enabled = value; } } /// /// Gets or sets field value. /// public override object Value { get { EnsureChildControls(); // If field type is integer and selected item is "ad-hoc-forum" return int.MinValue if ((this.FieldInfo != null) && ((this.FieldInfo.DataType == FormFieldDataTypeEnum.Integer) || (this.FieldInfo.DataType == FormFieldDataTypeEnum.LongInteger))) { if (ValidationHelper.GetString(uniSelector.Value, null) == ADHOCFORUM_VALUE) { return int.MinValue; } } return uniSelector.Value; } set { EnsureChildControls(); // If field type is integer and incoming value is in.MinValue or "ad-hoc-forum" preselect "ad-hoc-forum" in selector if ((this.FieldInfo != null) && ((this.FieldInfo.DataType == FormFieldDataTypeEnum.Integer) || (this.FieldInfo.DataType == FormFieldDataTypeEnum.LongInteger))) { if ((ValidationHelper.GetInteger(value, 0) == int.MinValue) || (ValidationHelper.GetString(value, null) == ADHOCFORUM_VALUE)) { value = ADHOCFORUM_VALUE; } } uniSelector.Value = value; } } /// /// Gets or sets the value which determines whether to allow more than one user to select. /// public SelectionModeEnum SelectionMode { get { EnsureChildControls(); return uniSelector.SelectionMode; } set { EnsureChildControls(); uniSelector.SelectionMode = value; } } /// /// Gets or sets the value that indicates whether 'Create adHoc forum' option should be displayed. /// public bool DisplayAdHocOption { get { return ValidationHelper.GetBoolean(GetValue("DisplayAdHocOption"), true); } set { SetValue("DisplayAdHocOption", value); } } /// /// Gets or sets the sitename. If sitename is defined selector displays forums only from this site. /// If sitename is not defined, selector displays forums for current site /// public string SiteName { get { return ValidationHelper.GetString(GetValue("SiteName"), CMSContext.CurrentSiteName); } set { SetValue("SiteName", value); } } /// /// Indicates if control is used on live site. /// public override bool IsLiveSite { get { return base.IsLiveSite; } set { EnsureChildControls(); base.IsLiveSite = value; uniSelector.IsLiveSite = value; } } /// /// Gets or sets the value that indicates whether 'All forums' option should be displayed in dropdown list. /// public bool DisplayAllForumsOption { get { return ValidationHelper.GetBoolean(GetValue("DisplayAllForumsOption"), false); } set { SetValue("DisplayAllForumsOption", value); } } /// /// Gets or sets a value indicating whether to check the AliasPath variable in the URL. /// If set to true, use AliasPath variable from URL to decide whether to show or hide the "ad-hoc forum" item in the selector. /// public bool CheckAliasPath { get { return ValidationHelper.GetBoolean(GetValue("CheckAliasPath"), false); } set { SetValue("CheckAliasPath", value); } } /// /// Gets or sets value that indicates whether group forums is included in list. /// private bool ShowGroupForums { get; set; } /// /// Sets the property value of control, setting the value affects only local property value. /// /// Property name /// Value public override void SetValue(string propertyName, object value) { // Add special behavior for selection mode if (propertyName.ToLowerCSafe() == "selectionmode") { SelectionMode = (SelectionModeEnum)Enum.Parse(typeof(SelectionModeEnum), Convert.ToString(value)); } base.SetValue(propertyName, value); } #endregion #region "Methods" protected void Page_Load(object sender, EventArgs e) { // Set uniselector uniSelector.DisplayNameFormat = "{%ForumDisplayName%}"; uniSelector.ReturnColumnName = "ForumName"; uniSelector.AllowEmpty = false; uniSelector.AllowAll = false; // Return forum name or ID according to type of field (if no field specified forum name is returned) if ((FieldInfo != null) && ((FieldInfo.DataType == FormFieldDataTypeEnum.Integer) || (FieldInfo.DataType == FormFieldDataTypeEnum.LongInteger))) { uniSelector.ReturnColumnName = "ForumID"; uniSelector.AllowEmpty = true; ShowGroupForums = true; } if (DependsOnAnotherField) { CheckAliasPath = true; } if (DisplayAllForumsOption) { if (ContainsAdHocForum()) { uniSelector.SpecialFields = new string[2, 2] { { GetString("general.selectall"), "" }, { GetString("ForumSelector.AdHocForum"), ADHOCFORUM_VALUE } }; } else { uniSelector.SpecialFields = new string[1, 2] { { GetString("general.selectall"), "" } }; } } else { if (ContainsAdHocForum()) { uniSelector.SpecialFields = new string[1, 2] { { GetString("ForumSelector.AdHocForum"), ADHOCFORUM_VALUE } }; } } // Set resource prefix based on mode if ((SelectionMode == SelectionModeEnum.Multiple) || (SelectionMode == SelectionModeEnum.MultipleButton) || (SelectionMode == SelectionModeEnum.MultipleTextBox)) { uniSelector.ResourcePrefix = "forumsselector"; uniSelector.FilterControl = "~/CMSModules/Forums/Filters/ForumGroupFilter.ascx"; } else { uniSelector.ResourcePrefix = "forumselector"; } SetupWhereCondition(); } protected void Page_PreRender(object sender, EventArgs e) { if (URLHelper.IsPostback() && DependsOnAnotherField) { SetupWhereCondition(); uniSelector.Reload(true); pnlUpdate.Update(); } } /// /// 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 (uniSelector == null) { pnlUpdate.LoadContainer(); } // Call base method base.CreateChildControls(); } /// /// Generates a where condition for the uniselector. /// private void SetupWhereCondition() { SetFormSiteName(); SiteInfo si = SiteInfoProvider.GetSiteInfo(SiteName); if (si != null) { siteId = si.SiteID; } else { siteId = CMSContext.CurrentSiteID; } // Select non group forum of current site uniSelector.WhereCondition = "ForumDocumentID IS NULL AND ForumGroupID IN (SELECT GroupID FROM Forums_ForumGroup WHERE " + (!ShowGroupForums ? "GroupGroupID IS NULL AND " : "") + "GroupSiteID = " + siteId + ")"; uniSelector.SetValue("SiteID", siteId); } /// /// Sets the site name if the SiteName field is available in the form. /// private void SetFormSiteName() { if (DependsOnAnotherField && (Form != null) && Form.IsFieldAvailable("SiteName")) { SiteName = ValidationHelper.GetString(Form.GetFieldValue("SiteName"), ""); } } /// /// Returns true when the "Ah-hoc forum" item should be displayed. /// private bool ContainsAdHocForum() { string aliasPath = QueryHelper.GetString("AliasPath", null); return (!CheckAliasPath) || (!String.IsNullOrEmpty(aliasPath)); } /// /// 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(uniSelector.Value, 0); if (id > 0) { return base.GetWhereCondition(); } // Check if 'ad-hoc-forum" has been selected and modify condition accordingly else if (ValidationHelper.GetString(uniSelector.Value, null) == ADHOCFORUM_VALUE) { return String.Format("[{1}] IN (SELECT ForumID FROM Forums_Forum WHERE ForumSiteID={0} AND ForumName LIKE 'AdHoc-%')", CMSContext.CurrentSiteID, FieldInfo.Name); } } return null; } #endregion }