using System;
using System.Data;
using System.Web.UI.WebControls;
using CMS.CMSHelper;
using CMS.GlobalHelper;
using CMS.Messaging;
using CMS.UIControls;
using CMS.ExtendedControls;
using CMS.SiteProvider;
using CMS.SettingsProvider;
public partial class CMSModules_Messaging_Controls_SelectFromContactList : CMSUserControl
{
#region "Public properties"
///
/// Messages placeholder
///
public override MessagesPlaceHolder MessagesPlaceHolder
{
get
{
return plcMess;
}
}
///
/// Indicates if control is used on live site.
///
public override bool IsLiveSite
{
get
{
return base.IsLiveSite;
}
set
{
plcMess.IsLiveSite = value;
base.IsLiveSite = value;
}
}
///
/// Zero rows text.
///
public string ZeroRowsText
{
get
{
EnsureChildControls();
return gridContactList.ZeroRowsText;
}
set
{
EnsureChildControls();
gridContactList.ZeroRowsText = value;
}
}
///
/// Page size values separated with comma.
///
public string PageSize
{
get
{
EnsureChildControls();
return gridContactList.PageSize;
}
set
{
EnsureChildControls();
gridContactList.PageSize = value;
}
}
#endregion
#region "Page events"
protected override void EnsureChildControls()
{
base.EnsureChildControls();
if (gridContactList == null)
{
pnlContactList.LoadContainer();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (StopProcessing)
{
// Stop processing
gridContactList.StopProcessing = true;
}
else
{
// Content is visible only for authenticated users
if (CMSContext.CurrentUser.IsAuthenticated())
{
if (string.IsNullOrEmpty(ZeroRowsText))
{
ZeroRowsText = GetString("messaging.contactlist.nodatafound");
}
// Register modal dialog JS function
ScriptHelper.RegisterDialogScript(Page);
// Setup unigrid
gridContactList.IsLiveSite = IsLiveSite;
gridContactList.OnDataReload += gridContactList_OnDataReload;
gridContactList.OnAction += gridContactList_OnAction;
gridContactList.OnExternalDataBound += gridContactList_OnExternalDataBound;
gridContactList.WhereCondition = GetWhereCondition();
}
else
{
Visible = false;
}
}
}
#endregion
#region "Grid methods"
protected object gridContactList_OnExternalDataBound(object sender, string sourceName, object parameter)
{
switch (sourceName)
{
case "formattedusername":
DataRowView drv = parameter as DataRowView;
int userId = ValidationHelper.GetInteger(drv["ContactListContactUserID"], 0);
return GetItemText(userId, drv["UserName"], drv["FullName"], drv["UserNickName"]);
}
return parameter;
}
protected void gridContactList_OnAction(string actionName, object actionArgument)
{
switch (actionName)
{
case "delete":
int deletedUserId = ValidationHelper.GetInteger(actionArgument, 0);
// If something is wrong return
if (CMSContext.CurrentUser == null)
{
return;
}
try
{
// Deletes from contact list
ContactListInfoProvider.RemoveFromContactList(CMSContext.CurrentUser.UserID, deletedUserId);
ShowConfirmation(GetString("Messaging.ContactList.DeleteSuccessful"));
}
catch (Exception ex)
{
ShowError(ex.Message);
}
break;
}
}
protected DataSet gridContactList_OnDataReload(string completeWhere, string currentOrder, int currentTopN, string columns, int currentOffset, int currentPageSize, ref int totalRecords)
{
return ContactListInfoProvider.GetContactList(CMSContext.CurrentUser.UserID, completeWhere, currentOrder, currentTopN, "UserName, UserNickname, FullName, ContactListContactUserID", currentOffset, currentPageSize, ref totalRecords);
}
///
/// Renders row item according to control settings.
///
protected string GetItemText(int userId, object username, object fullname, object usernickname)
{
string usrName = ValidationHelper.GetString(username, string.Empty);
string nick = HTMLHelper.HTMLEncode(Functions.GetFormattedUserName(usrName, fullname.ToString(), usernickname.ToString(), IsLiveSite));
return "" + nick + "";
}
///
/// Generate where condition
///
private string GetWhereCondition()
{
if (IsLiveSite && !CMSContext.CurrentUser.IsGlobalAdministrator)
{
// Hide hidden users
string where = "((UserIsHidden IS NULL) OR (UserIsHidden=0))";
// Hide disabled users
where = SqlHelperClass.AddWhereCondition(where, UserInfoProvider.USER_ENABLED_WHERE_CONDITION);
// Hide non-approved users
where = SqlHelperClass.AddWhereCondition(where, "ContactListUserID IN (SELECT UserSettingsUserID FROM CMS_UserSettings WHERE ((UserWaitingForApproval IS NULL) OR (UserWaitingForApproval = 0)))");
return where;
}
return null;
}
#endregion
}