using System; using CMS.CMSHelper; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.UIControls; using CMS.Controls; using TextSimpleFilter = CMSAdminControls_UI_UniGrid_Filters_TextSimpleFilter; public partial class CMSModules_ContactManagement_Controls_UI_Account_Filter : CMSAbstractBaseFilterControl { #region "Variables" private int mSiteId = -1; private int mSelectedSiteID = -1; #endregion #region "Public properties" /// /// Gets or sets the site ID for which the events should be filtered. /// public int SiteID { get { return mSiteId; } set { mSiteId = value; fltAccountStatus.SiteID = value; if (value == UniSelector.US_ALL_RECORDS) { fltAccountStatus.DisplayAll = true; } else if (value == UniSelector.US_GLOBAL_OR_SITE_RECORD) { fltAccountStatus.DisplaySiteOrGlobal = true; fltAccountStatus.SiteID = CMSContext.CurrentSiteID; } } } /// /// Indicates whether to show global statuses or not. /// public bool ShowGlobalStatuses { get { return fltAccountStatus.DisplaySiteOrGlobal; } set { fltAccountStatus.DisplaySiteOrGlobal = value; } } /// /// Selected site ID. /// public int SelectedSiteID { get { return mSelectedSiteID; } } /// /// Gets the where condition created using filtered parameters. /// public override string WhereCondition { get { return GenerateWhereCondition(); } } /// /// Indicates if AccountStatus filter should be displayed even if site is not specified. /// public bool DisplayAccountStatus { get; set; } /// /// Indicates if filter is in advanced mode. /// protected bool IsAdvancedMode { get { return ValidationHelper.GetBoolean(ViewState["IsAdvancedMode"], false); } set { ViewState["IsAdvancedMode"] = value; } } /// /// Indicates if filter is used on live site or in UI. /// public override bool IsLiveSite { get { return base.IsLiveSite; } set { base.IsLiveSite = value; fltAccountStatus.IsLiveSite = value; } } /// /// Returns TRUE if displaying both merged and not merged accounts. /// public bool DisplayingAll { get { return chkMerged.Checked; } } /// /// Indicates if merging filter should be hidden. /// public bool HideMergedFilter { get { return !plcMerged.Visible; } set { plcMerged.Visible = !value; } } /// /// Indicates if filter should return only not merged accounts. Otherwise filter returns only merged accounts. /// Applies only when filter is hidden. /// public bool NotMerged { get; set; } /// /// Indicates if siteselector should be displayed. /// public bool DisplaySiteSelector { get { return siteSelector.Visible; } set { if (value) { plcSite.Visible = true; } siteSelector.Visible = value; } } /// /// Indicates if 'Site or global selector' should be displayed. /// public bool DisplayGlobalOrSiteSelector { get { return siteOrGlobalSelector.Visible; } set { if (value) { plcSite.Visible = true; } siteOrGlobalSelector.Visible = value; } } /// /// Gets or sets if all accounts merged into global accounts should be hidde. /// public bool HideMergedIntoGlobal { get; set; } /// /// Gets or sets value that indicates whether "show children" checkbox should be visible. /// public bool ShowChildren { get; set; } /// /// Returns true if all child contacts will be shown. /// public bool ChildrenSelected { get { return chkChildren.Checked; } } /// /// When true, site id is not added to where clause generated by filter. /// public bool DisableGeneratingSiteClause { get; set; } #endregion #region "Page methods" protected override void OnInit(EventArgs e) { base.OnInit(e); fltPhone.Columns = new[] { "AccountPhone", "AccountFax" }; fltContactName.Columns = new[] { "PrimaryContactFullName", "SecondaryContactFullName" }; btnReset.Text = GetString("general.reset"); btnReset.Click += btnReset_Click; btnSearch.Click += btnSearch_Click; } /// /// Resets the associated UniGrid control. /// protected void btnReset_Click(object sender, EventArgs e) { UniGrid grid = FilteredControl as UniGrid; if (grid != null) { grid.FilterIsSet = true; grid.Reset(); } } /// /// Applies filter on associated UniGrid control. /// protected void btnSearch_Click(object sender, EventArgs e) { UniGrid grid = FilteredControl as UniGrid; if (grid != null) { grid.ApplyFilter(sender, e); } } protected void Page_Load(object sender, EventArgs e) { InitializeForm(); plcAdvancedSearch.Visible = IsAdvancedMode; plcChildren.Visible = ShowChildren; } #endregion #region "UI methods" /// /// Shows/hides all elements for advanced or simple mode. /// private void ShowFilterElements(bool showAdvanced) { plcAdvancedSearch.Visible = showAdvanced; pnlAdvanced.Visible = showAdvanced; pnlSimple.Visible = !showAdvanced; } /// /// Initializes the layout of the form. /// private void InitializeForm() { // General UI fltAccountStatus.DropDownList.CssClass = "DropDownFieldFilter"; lnkShowAdvancedFilter.Text = GetString("user.filter.showadvanced"); imgShowAdvancedFilter.ImageUrl = GetImageUrl("Design/Controls/UniGrid/Actions/SortDown.png"); lnkShowSimpleFilter.Text = GetString("user.filter.showsimple"); imgShowSimpleFilter.ImageUrl = GetImageUrl("Design/Controls/UniGrid/Actions/SortUp.png"); plcAdvancedSearch.Visible = IsAdvancedMode; pnlAdvanced.Visible = IsAdvancedMode; pnlSimple.Visible = !IsAdvancedMode; } /// /// Sets the advanced mode. /// protected void lnkShowAdvancedFilter_Click(object sender, EventArgs e) { IsAdvancedMode = true; ShowFilterElements(true); } /// /// Sets the simple mode. /// protected void lnkShowSimpleFilter_Click(object sender, EventArgs e) { IsAdvancedMode = false; ShowFilterElements(false); } #endregion #region "Search methods - where condition" /// /// Generates complete filter where condition. /// private string GenerateWhereCondition() { string whereCond = string.Empty; fltAccountStatus.ReloadData(); // Create WHERE condition for basic filter int contactStatus = ValidationHelper.GetInteger(fltAccountStatus.Value, -1); if (fltAccountStatus.Value == null) { whereCond = "AccountStatusID IS NULL"; } else if (contactStatus > 0) { whereCond = "AccountStatusID = " + contactStatus; } whereCond = SqlHelperClass.AddWhereCondition(whereCond, fltName.GetCondition()); whereCond = SqlHelperClass.AddWhereCondition(whereCond, fltEmail.GetCondition()); whereCond = SqlHelperClass.AddWhereCondition(whereCond, fltContactName.GetCondition()); if (IsAdvancedMode) { whereCond = SqlHelperClass.AddWhereCondition(whereCond, GetOwnerCondition(fltOwner)); whereCond = SqlHelperClass.AddWhereCondition(whereCond, GetCountryCondition(fltCountry)); whereCond = SqlHelperClass.AddWhereCondition(whereCond, GetStateCondition(fltState)); whereCond = SqlHelperClass.AddWhereCondition(whereCond, fltCity.GetCondition()); whereCond = SqlHelperClass.AddWhereCondition(whereCond, fltPhone.GetCondition()); whereCond = SqlHelperClass.AddWhereCondition(whereCond, fltCreated.GetCondition()); } // When "merged/not merged" filter is hidden if ((HideMergedFilter && NotMerged) || (IsAdvancedMode && !HideMergedFilter && !chkMerged.Checked) || (!IsAdvancedMode && !HideMergedFilter && !NotMerged)) { whereCond = SqlHelperClass.AddWhereCondition(whereCond, "(AccountMergedWithAccountID IS NULL AND AccountSiteID > 0) OR (AccountGlobalAccountID IS NULL AND AccountSiteID IS NULL)"); } // Hide accounts merged into global account when displying list of available accounts for global account if (HideMergedIntoGlobal) { whereCond = SqlHelperClass.AddWhereCondition(whereCond, "AccountGlobalAccountID IS NULL"); } if (!DisableGeneratingSiteClause) { // Filter current account's site if (!plcSite.Visible) { // Filter site objects if (SiteID > 0) { whereCond = SqlHelperClass.AddWhereCondition(whereCond, "(AccountSiteID = " + SiteID.ToString() + ")"); } // Filter only global objects else if (SiteID == UniSelector.US_GLOBAL_RECORD) { whereCond = SqlHelperClass.AddWhereCondition(whereCond, "(AccountSiteID IS NULL)"); } } // Filter by site filter else { mSelectedSiteID = UniSelector.US_ALL_RECORDS; if (siteSelector.Visible) { mSelectedSiteID = siteSelector.SiteID; } else if (siteOrGlobalSelector.Visible) { mSelectedSiteID = siteOrGlobalSelector.SiteID; } if (mSelectedSiteID == 0) { mSelectedSiteID = UniSelector.US_ALL_RECORDS; } // Only global objects if (mSelectedSiteID == UniSelector.US_GLOBAL_RECORD) { whereCond = SqlHelperClass.AddWhereCondition(whereCond, "AccountSiteID IS NULL"); } // Global and site objects else if (mSelectedSiteID == UniSelector.US_GLOBAL_OR_SITE_RECORD) { whereCond = SqlHelperClass.AddWhereCondition(whereCond, "AccountSiteID IS NULL OR AccountSiteID = " + CMSContext.CurrentSiteID); } // Site objects else if (mSelectedSiteID != UniSelector.US_ALL_RECORDS) { whereCond = SqlHelperClass.AddWhereCondition(whereCond, "AccountSiteID = " + mSelectedSiteID); } } } return whereCond; } #endregion #region "Additional filter conditions" /// /// Gets SQL Where condition for a country. /// private string GetCountryCondition(TextSimpleFilter filter) { string originalQuery = filter.WhereCondition; if (!String.IsNullOrEmpty(originalQuery)) { originalQuery = String.Format("AccountCountryID IN (SELECT CountryID FROM CMS_Country WHERE {0})", originalQuery); if (filter.FilterOperator == "NOT LIKE" || filter.FilterOperator == "<>") { originalQuery = SqlHelperClass.AddWhereCondition("AccountCountryID IS NULL", originalQuery, "OR"); } } return originalQuery; } /// /// Gets SQL Where condition user. /// private string GetOwnerCondition(TextSimpleFilter filter) { string originalQuery = filter.WhereCondition; if (!String.IsNullOrEmpty(originalQuery)) { originalQuery = String.Format("AccountOwnerUserID IN (SELECT UserID FROM CMS_User WHERE {0})", originalQuery); if (filter.FilterOperator == "NOT LIKE" || filter.FilterOperator == "<>") { originalQuery = SqlHelperClass.AddWhereCondition("AccountOwnerUserID IS NULL", originalQuery, "OR"); } } return originalQuery; } /// /// Gets SQL Where condition for a state. /// private string GetStateCondition(TextSimpleFilter filter) { string originalQuery = filter.WhereCondition; if (!String.IsNullOrEmpty(originalQuery)) { originalQuery = String.Format("AccountStateID IN (SELECT StateID FROM CMS_State WHERE {0})", originalQuery); if (filter.FilterOperator == "NOT LIKE" || filter.FilterOperator == "<>") { originalQuery = SqlHelperClass.AddWhereCondition("AccountStateID IS NULL", originalQuery, "OR"); } } return originalQuery; } #endregion #region "State management" /// /// Resets filter to the default state. /// public override void ResetFilter() { fltAccountStatus.Value = UniSelector.US_ALL_RECORDS; fltCity.ResetFilter(); fltContactName.ResetFilter(); fltCountry.ResetFilter(); fltCreated.Clear(); fltEmail.ResetFilter(); fltName.ResetFilter(); fltOwner.ResetFilter(); fltPhone.ResetFilter(); fltState.ResetFilter(); chkChildren.Checked = false; chkMerged.Checked = false; } /// /// Stores filter state to the specified object. /// /// The object that holds the filter state. public override void StoreFilterState(FilterState state) { state.AddValue("ShowAll",chkMerged.Checked); state.AddValue("AdvancedMode", IsAdvancedMode); state.AddValue("Status", fltAccountStatus.Value); state.AddValue("ToTime", fltCreated.ValueToTime); state.AddValue("FromTime", fltCreated.ValueFromTime); base.StoreFilterState(state); } /// /// Restores filter state from the specified object. /// /// The object that holds the filter state. public override void RestoreFilterState(FilterState state) { base.RestoreFilterState(state); chkMerged.Checked = state.GetBoolean("ShowAll"); IsAdvancedMode = state.GetBoolean("AdvancedMode"); fltAccountStatus.Value = state.GetString("Status"); fltCreated.ValueFromTime = state.GetDateTime("FromTime"); fltCreated.ValueToTime = state.GetDateTime("ToTime"); } #endregion }