using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.ExtendedControls; using CMS.SettingsProvider; using CMS.UIControls; public partial class CMSModules_ContactManagement_Controls_UI_Contact_ContactGroups : CMSAdminListControl { #region "Variables" private List mFilterByContacts = new List(); private List mFilterBySites = new List(); #endregion #region "Events" /// /// Remove button on group is clicked. /// public event EventHandler OnRemoveGroup; /// /// Remove button on group is drawed. /// Control by default disables delete button, for enabling the button /// you should set ButtonEnabled to true in sent DrawButtonEventArgs. /// public event OnDrawButtonEventHandler OnDrawRemoveButton; /// /// Delegate for OnDrawRemoveButton. /// public delegate void OnDrawButtonEventHandler(object sender, DrawButtonEventArgs args); /// /// Event arguments for OnDrawRemoveButton. /// public class DrawButtonEventArgs : CMSEventArgs { public object EditedObject; public bool ButtonEnabled = false; } #endregion #region "Properties" /// /// Sets or gets contact ID to filter unigrid. /// Leave empty for displaying groups from all contacts. /// public List FilterByContacts { get { return mFilterByContacts; } set { mFilterByContacts = value; } } /// /// Gets or Sets site list to display contact group from. /// Leave empty for displaying groups from all sites. /// Add null for 'global' items. /// public List FilterBySites { get { return mFilterBySites; } set { mFilterBySites = value; } } /// /// Returns inner unigrid. /// public UniGrid UniGrid { get { return gridElem; } } #endregion #region "Methods" protected void Page_Load(object sender, EventArgs e) { SetUniGridQuery(); gridElem.OnAction += new OnActionEventHandler(gridElem_OnAction); gridElem.OnExternalDataBound += new OnExternalDataBoundEventHandler(gridElem_OnExternalDataBound); } /// /// Sets unigrid query. /// private void SetUniGridQuery() { // Filter by contacts if ((mFilterByContacts != null) && (mFilterByContacts.Count > 0)) { // Get joined contacts in format "string1, string2, string3", // create SQL WHERE condition from that and set it to grid element. String joinedContacts = string.Join(",", mFilterByContacts.ToArray().Select(x => x.ToString()).ToArray()); String contactFilterCondition = "ContactID IN (" + joinedContacts + ")"; gridElem.WhereCondition = SqlHelperClass.AddWhereCondition(gridElem.WhereCondition, contactFilterCondition); } // And sites if ((mFilterBySites != null) && (mFilterBySites.Count > 0)) { // Get joined contacts in format "string1, string2, string3", // create SQL WHERE condition from that and set it to grid element. String joinedSites = string.Join("','", mFilterBySites.ToArray().Select(x => x.ToString()).ToArray()); String siteFilterCondition = "ContactGroupSiteID IN ('" + joinedSites + "')"; if (mFilterBySites.Contains(null)) { siteFilterCondition += " OR ContactGroupSiteID IS NULL"; } gridElem.WhereCondition = SqlHelperClass.AddWhereCondition(gridElem.WhereCondition, siteFilterCondition); } } private object gridElem_OnExternalDataBound(object sender, string sourceName, object parameter) { switch (sourceName) { case "remove": // Set the OnDrawRemoveButton event arguments, fire it // and set delete button's properties appropriately afterwards. DataRowView drv = (parameter as GridViewRow).DataItem as DataRowView; DrawButtonEventArgs drawButtonArgs = new DrawButtonEventArgs(); drawButtonArgs.EditedObject = drv["ContactGroupID"]; OnDrawRemoveButton(sender, drawButtonArgs); if (drawButtonArgs.ButtonEnabled) { ((CMSImageButton)sender).ImageUrl = GetImageUrl("Design/Controls/UniGrid/Actions/Delete.png"); ((CMSImageButton)sender).Enabled = true; } else { ((CMSImageButton)sender).ImageUrl = GetImageUrl("Design/Controls/UniGrid/Actions/DeleteDisabled.png"); ((CMSImageButton)sender).Enabled = false; } break; } return null; } private void gridElem_OnAction(string actionName, object actionArgument) { switch (actionName) { case "remove": OnRemoveGroup(actionArgument, EventArgs.Empty); break; } } /// /// Reloads unigrid. /// public override void ReloadData() { SetUniGridQuery(); gridElem.ReloadData(); } #endregion }