using System; using System.Collections; using System.Data; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CMS.CMSHelper; using CMS.ExtendedControls; using CMS.GlobalHelper; using CMS.OnlineMarketing; using CMS.SettingsProvider; using CMS.UIControls; public partial class CMSModules_ContactManagement_Controls_UI_Contact_Accounts : CMSAdminListControl, ICallbackEventHandler { #region "Variables" private ContactInfo ci = null; private int mSiteID = -1; private Hashtable mParameters; private bool modifyAccountContact = false; /// /// Available actions in mass action selector. /// protected enum Action { SelectAction = 0, Remove = 1, SelectRole = 2, } /// /// Selected objects in mass action selector. /// protected enum What { Selected = 0, All = 1 } /// /// Mass action selector parameters. /// protected enum Argument { Action = 0, AllSelected = 1, Items = 2 } /// /// URL of modal dialog window for contact role selection. /// public const string CONTACT_ROLE_DIALOG = "~/CMSModules/ContactManagement/FormControls/ContactRoleDialog.aspx"; /// /// URL of modal dialog window for account editing. /// public const string ACCOUNT_DETAIL_DIALOG = "~/CMSModules/ContactManagement/Pages/Tools/Contact/Account_Detail.aspx"; #endregion #region "Properties" /// /// Messages placeholder. /// public override MessagesPlaceHolder MessagesPlaceHolder { get { return plcMess; } } /// /// Inner grid. /// public UniGrid Grid { get { return gridElem; } } /// /// Indicates if the control should perform the operations. /// public override bool StopProcessing { get { return base.StopProcessing; } set { base.StopProcessing = value; gridElem.StopProcessing = value; accountSelector.StopProcessing = value; } } /// /// Gets current site ID. /// private int SiteID { get { return mSiteID; } set { mSiteID = value; accountSelector.SiteID = value; } } /// /// Dialog control identifier. /// private string Identifier { get { string identifier = hdnValue.Value; if (string.IsNullOrEmpty(identifier)) { identifier = Guid.NewGuid().ToString(); hdnValue.Value = identifier; } return identifier; } } /// /// Gets or sets the callback argument. /// private string CallbackArgument { get; set; } /// /// Indicates if control is used on live site. /// public override bool IsLiveSite { get { return base.IsLiveSite; } set { base.IsLiveSite = value; accountSelector.IsLiveSite = value; gridElem.IsLiveSite = value; } } #endregion #region "Page events" protected void Page_Load(object sender, EventArgs e) { // Get edited object if (CMSContext.EditedObject != null) { ci = (ContactInfo)CMSContext.EditedObject; SiteID = ci.ContactSiteID; // Setup unigrid gridElem.GridOptions.ShowSelection = (ci.ContactMergedWithContactID == 0); gridElem.WhereCondition = SqlHelperClass.AddWhereCondition(gridElem.WhereCondition, "(ContactID = " + ci.ContactID + ") AND ((AccountMergedWithAccountID IS NULL AND AccountSiteID > 0) OR (AccountGlobalAccountID IS NULL AND AccountSiteID IS NULL))"); gridElem.OnAction += new OnActionEventHandler(gridElem_OnAction); gridElem.OnExternalDataBound += new OnExternalDataBoundEventHandler(gridElem_OnExternalDataBound); gridElem.ZeroRowsText = GetString("om.contact.noaccounts"); // Initialize dropdown lists if (!RequestHelper.IsPostBack()) { drpAction.Items.Add(new ListItem(GetString("general." + Action.SelectAction), Convert.ToInt32(Action.SelectAction).ToString())); drpAction.Items.Add(new ListItem(GetString("general.remove"), Convert.ToInt32(Action.Remove).ToString())); drpAction.Items.Add(new ListItem(GetString("om.contactrole.select"), Convert.ToInt32(Action.SelectRole).ToString())); drpWhat.Items.Add(new ListItem(GetString("om.account." + What.Selected), Convert.ToInt32(What.Selected).ToString())); drpWhat.Items.Add(new ListItem(GetString("om.account." + What.All), Convert.ToInt32(What.All).ToString())); } else { if (RequestHelper.CausedPostback(btnOk)) { // Set delayed reload for unigrid if mass action is performed gridElem.DelayedReload = true; } } // Initialize contact selector accountSelector.UniSelector.ButtonImage = GetImageUrl("/Objects/OM_Account/add.png"); accountSelector.UniSelector.DialogLabel.ResourceString = "om.account.addaccount"; accountSelector.UniSelector.OnItemsSelected += new EventHandler(UniSelector_OnItemsSelected); accountSelector.UniSelector.SelectionMode = SelectionModeEnum.MultipleButton; accountSelector.UniSelector.DialogImage.CssClass = "NewItemImage"; accountSelector.UniSelector.DialogLink.CssClass = "MenuItemEdit"; accountSelector.UniSelector.SelectItemPageUrl = "~/CMSModules/ContactManagement/Pages/Tools/Contact/Add_Account_Dialog.aspx"; accountSelector.UniSelector.SetValue("SiteID", SiteID); accountSelector.UniSelector.SetValue("IsLiveSite", false); accountSelector.IsSiteManager = ContactHelper.IsSiteManager; modifyAccountContact = AccountHelper.AuthorizedModifyAccount(SiteID, false) || ContactHelper.AuthorizedModifyContact(SiteID, false); // Registr JS scripts RegisterScripts(); } else { StopProcessing = true; } } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); // Hide footer if grid is empty or if the contact is merged (is not active) pnlFooter.Visible = (!gridElem.IsEmpty) && (gridElem.GridOptions.ShowSelection); // Hide controls when contact is merged or user doesn't have permission if ((ci.ContactMergedWithContactID != 0) || !modifyAccountContact) { pnlFooter.Visible = false; pnlSelector.Visible = false; } } #endregion #region "Events" /// /// Unigrid external databoud event handler. /// protected object gridElem_OnExternalDataBound(object sender, string sourceName, object parameter) { ImageButton btn = null; switch (sourceName.ToLowerCSafe()) { case "edit": btn = ((ImageButton)sender); btn.Attributes.Add("onClick", "EditAccount(" + btn.CommandArgument + "); return false;"); break; case "selectrole": btn = (ImageButton)sender; if ((ci.ContactMergedWithContactID == 0) && modifyAccountContact) { btn.OnClientClick = string.Format("dialogParams_{0} = '{1}';{2};return false;", ClientID, btn.CommandArgument, Page.ClientScript.GetCallbackEventReference(this, "dialogParams_" + ClientID, "SelectRole", null)); } else { btn.Enabled = false; btn.Attributes.Add("src", GetImageUrl("Design/Controls/UniGrid/Actions/contactroledisabled.png")); } break; case "remove": if ((ci.ContactMergedWithContactID != 0) || !modifyAccountContact) { btn = (ImageButton)sender; btn.Enabled = false; btn.Attributes.Add("src", GetImageUrl("Design/Controls/UniGrid/Actions/DeleteDisabled.png")); } break; } return null; } /// /// Unigrid button clicked. /// protected void gridElem_OnAction(string actionName, object actionArgument) { if (actionName == "remove") { // User has permission modify if (modifyAccountContact) { int relationId = ValidationHelper.GetInteger(actionArgument, 0); AccountContactInfo relation = AccountContactInfoProvider.GetAccountContactInfo(relationId); if (relation != null) { // We need to invalidate the contact as we might have modified some of its relationships and data in the cache might not be valid AccountContactInfoProvider.DeleteAccountContactInfo(relation); } } // User doesn't have sufficient permissions else { if (SiteID > 0) { CMSPage.RedirectToCMSDeskAccessDenied(ModuleEntry.CONTACTMANAGEMENT, "ModifyContacts"); } else { CMSPage.RedirectToCMSDeskAccessDenied(ModuleEntry.CONTACTMANAGEMENT, "ModifyGlobalContacts"); } } } } /// /// Items changed event handler. /// protected void UniSelector_OnItemsSelected(object sender, EventArgs e) { if (modifyAccountContact) { // Get new items from selector string newValues = ValidationHelper.GetString(accountSelector.Value, null); if (!String.IsNullOrEmpty(newValues)) { string[] newItems = newValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (newItems != null) { int previousStop = 0; string where = FetchNextAccounts(ref previousStop, newItems, 1000); while (!String.IsNullOrEmpty(where)) { AccountContactInfoProvider.SetAccountsIntoContact(ci.ContactID, "AccountID IN (" + where + ")", ValidationHelper.GetInteger(hdnRoleID.Value, 0)); where = FetchNextAccounts(ref previousStop, newItems, 1000); } } gridElem.ReloadData(); pnlUpdate.Update(); accountSelector.Value = null; } } // No permission modify else { if (SiteID > 0) { CMSPage.RedirectToCMSDeskAccessDenied(ModuleEntry.CONTACTMANAGEMENT, "ModifyContacts"); } else { CMSPage.RedirectToCMSDeskAccessDenied(ModuleEntry.CONTACTMANAGEMENT, "ModifyGlobalContacts"); } } } protected void btnOk_Click(object sender, EventArgs e) { if (modifyAccountContact) { Action action = (Action)ValidationHelper.GetInteger(drpAction.SelectedItem.Value, 0); What what = (What)ValidationHelper.GetInteger(drpWhat.SelectedItem.Value, 0); string where = string.Empty; switch (what) { // All items case What.All: where = SqlHelperClass.AddWhereCondition(gridElem.WhereCondition, gridElem.WhereClause); where = string.Format("ContactID={0} AND AccountID IN (SELECT AccountID FROM View_OM_AccountContact_AccountJoined WHERE {1})", ci.ContactID, where); break; // Selected items case What.Selected: where = SqlHelperClass.GetWhereCondition("AccountContactID", gridElem.SelectedItems, false); break; default: return; } switch (action) { // Action 'Remove' case Action.Remove: // Reset accounts' main contact IDs if the contact was set as primary or secondary contact AccountContactInfoProvider.ResetAccountMainContacts(0, ci.ContactID, where); // Delete the relations between contact and accounts AccountContactInfoProvider.DeleteAllAccountContacts(where); // Show result message if (what == What.Selected) { ShowConfirmation(GetString("om.account.massaction.removed")); } else { ShowConfirmation(GetString("om.account.massaction.removedall")); } break; // Action 'Select role' case Action.SelectRole: // Get selected role ID from hidden field int roleId = ValidationHelper.GetInteger(hdnValue.Value, -1); if (roleId >= 0 && modifyAccountContact) { AccountContactInfoProvider.UpdateContactRole(roleId, where); ShowConfirmation(GetString("om.contact.massaction.roleassigned")); } break; default: return; } // Reload unigrid gridElem.ClearSelectedItems(); gridElem.ReloadData(); pnlUpdate.Update(); } // No permission modify else { if (SiteID > 0) { CMSPage.RedirectToCMSDeskAccessDenied(ModuleEntry.CONTACTMANAGEMENT, "ModifyContacts"); } else { CMSPage.RedirectToCMSDeskAccessDenied(ModuleEntry.CONTACTMANAGEMENT, "ModifyGlobalContacts"); } } } #endregion #region "Methods" /// /// Returns limited number of accounts to be added. /// /// Previous position /// Array of items to be added /// How much of records to fetch /// Returns items separated by colon. private string FetchNextAccounts(ref int previousStop, string[] newItems, int howMuch) { StringBuilder whereBuild = new StringBuilder(); // Get new where for (int i = previousStop; (i < (previousStop + howMuch)) && (i < newItems.Length); i++) { whereBuild.Append(ValidationHelper.GetInteger(newItems[i], 0) + ","); } // Update last position if (previousStop + howMuch > newItems.Length) { previousStop = newItems.Length; } else { previousStop += howMuch; } // Return WHERE String where = whereBuild.ToString(); if (!String.IsNullOrEmpty(where)) { return where.Remove(where.Length - 1, 1); } else { return null; } } /// /// Registers JS. /// private void RegisterScripts() { ScriptHelper.RegisterDialogScript(Page); StringBuilder script = new StringBuilder(); // Register script to open dialogs for role selection and for account editing script.Append(@" function SelectRole(queryParameters) { modalDialog('", ResolveUrl(CONTACT_ROLE_DIALOG), @"' + queryParameters, 'selectRole', '660', '590'); } function EditAccount(accountID) { modalDialog('", ResolveUrl(ACCOUNT_DETAIL_DIALOG), @"?accountid=' + accountID + '&isSiteManager=", ContactHelper.IsSiteManager, @"', 'AccountDetail', '1024', '700'); } function Refresh() { __doPostBack('", pnlUpdate.ClientID, @"', ''); } function setRole(roleID) { $j('#", hdnRoleID.ClientID, @"').val(roleID); } function PerformAction(selectionFunction, actionId, actionLabel, whatId) { var confirmation = null; var label = document.getElementById(actionLabel); var action = document.getElementById(actionId).value; var whatDrp = document.getElementById(whatId).value; label.innerHTML = ''; if (action == '", (int)Action.SelectAction, @"') { label.innerHTML = '", GetString("MassAction.SelectSomeAction"), @"' } else if (eval(selectionFunction) && (whatDrp == '", (int)What.Selected, @"')) { label.innerHTML = '", GetString("om.account.massaction.select"), @"'; } else { switch(action) { case '", (int)Action.Remove, @"': if (whatDrp == ", (int)What.Selected, @") { confirmation = ", ScriptHelper.GetString(GetString("General.ConfirmRemove")), @"; } else { confirmation = ", ScriptHelper.GetString(GetString("General.ConfirmRemoveAll")), @"; } break; case '", (int)Action.SelectRole, @"': dialogParams_", ClientID, @" = 'ismassaction';", Page.ClientScript.GetCallbackEventReference(this, "dialogParams_" + ClientID, "SelectRole", null), @"; break; default: confirmation = null; break; } if (confirmation != null) { return confirm(confirmation) } } return false; } function AssignContactRole_", ClientID, @"(roleId) { document.getElementById('", hdnValue.ClientID, @"').value = roleId;", ControlsHelper.GetPostBackEventReference(btnOk, null), @"; } var dialogParams_", ClientID, @" = '';"); ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "Actions", ScriptHelper.GetScript(script.ToString())); // Add action to button btnOk.OnClientClick = "return PerformAction('" + gridElem.GetCheckSelectionScript() + "','" + drpAction.ClientID + "','" + lblInfo.ClientID + "','" + drpWhat.ClientID + "');"; } #endregion #region "ICallbackEventHandler Members" /// /// Gets callback result. /// public string GetCallbackResult() { string queryString = string.Empty; if (!string.IsNullOrEmpty(CallbackArgument)) { // Prepare parameters... mParameters = new Hashtable(); if (CallbackArgument.EqualsCSafe("ismassaction", true)) { // for mass action mParameters["ismassaction"] = "1"; mParameters["siteid"] = SiteID; mParameters["clientid"] = ClientID; } else { // for unigrid action mParameters["accountcontactid"] = CallbackArgument; } mParameters["allownone"] = "1"; mParameters["issitemanager"] = ContactHelper.IsSiteManager; WindowHelper.Add(Identifier, mParameters); queryString = "?params=" + Identifier; queryString = URLHelper.AddParameterToUrl(queryString, "hash", QueryHelper.GetHash(queryString)); } return queryString; } /// /// Raise callback method. /// public void RaiseCallbackEvent(string eventArgument) { CallbackArgument = eventArgument; } #endregion }