using System;
using System.Data;
using System.Web.UI.WebControls;
using CMS.CMSHelper;
using CMS.GlobalHelper;
using CMS.Newsletter;
using CMS.PortalEngine;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.UIControls;
using CMS.WebAnalytics;
using CMS.ExtendedControls;
public partial class CMSModules_Newsletters_Controls_MySubscriptions : CMSAdminControl
{
#region "Variables"
private SubscriberInfo sb = null;
private bool mExternalUse = false;
private int mCacheMinutes = 0;
private string subscriberEmail = string.Empty;
private bool userIsIdentified = false;
private int mUserId = 0;
private int mSiteId = 0;
private string selectorValue = string.Empty;
private string currentValues = string.Empty;
private bool mSendConfirmationEmail = true;
#endregion
#region "Properties"
///
/// Messages placeholder
///
public override MessagesPlaceHolder MessagesPlaceHolder
{
get
{
return plcMess;
}
}
///
/// Gets or sets the value that indicates whether send confirmation emails.
///
public bool SendConfirmationEmail
{
get
{
return mSendConfirmationEmail;
}
set
{
mSendConfirmationEmail = value;
}
}
///
/// Gets or sets the value that indicates whether this control is visible.
///
public bool ForcedVisible
{
get
{
return plcMain.Visible;
}
set
{
plcMain.Visible = value;
}
}
///
/// Gets or sets the value that indicates whether this control is used in other control.
///
public bool ExternalUse
{
get
{
return mExternalUse;
}
set
{
mExternalUse = value;
}
}
///
/// Gets or sets the WebPart cache minutes.
///
public int CacheMinutes
{
get
{
return mCacheMinutes;
}
set
{
mCacheMinutes = value;
}
}
///
/// Gets or sets current site ID.
///
public int SiteID
{
get
{
return mSiteId;
}
set
{
mSiteId = value;
}
}
///
/// Gets or sets current user ID.
///
public int UserID
{
get
{
return mUserId;
}
set
{
mUserId = value;
}
}
///
/// Indicates if selector control is on live site.
///
public override bool IsLiveSite
{
get
{
return ValidationHelper.GetBoolean(GetValue("IsLiveSite"), false);
}
set
{
SetValue("IsLiveSite", value);
plcMess.IsLiveSite = value;
}
}
///
/// Last selector value.
///
private string SelectorValue
{
get
{
if (string.IsNullOrEmpty(selectorValue))
{
// Try to get value from hidden field
selectorValue = ValidationHelper.GetString(hdnValue.Value, string.Empty);
}
return selectorValue;
}
set
{
selectorValue = value;
}
}
#endregion
#region "Methods"
///
/// PageLoad.
///
protected void Page_Load(object sender, EventArgs e)
{
if (ExternalUse)
{
LoadData();
}
}
///
/// Load data.
///
public void LoadData()
{
if (StopProcessing)
{
// Hide control
Visible = false;
}
else
{
SetContext();
// Get specified user if used instead of current user
UserInfo ui = null;
if (UserID > 0)
{
ui = UserInfoProvider.GetUserInfo(UserID);
}
else
{
ui = CMSContext.CurrentUser;
}
// Get specified site ID instead of current site ID
int siteId = 0;
if (SiteID > 0)
{
siteId = SiteID;
}
else
{
siteId = CMSContext.CurrentSiteID;
}
usNewsletters.WhereCondition = "NewsletterSiteID = " + siteId;
usNewsletters.ButtonRemoveSelected.CssClass = "XLongButton";
usNewsletters.ButtonAddItems.CssClass = "XLongButton";
usNewsletters.OnSelectionChanged += new EventHandler(usNewsletters_OnSelectionChanged);
usNewsletters.IsLiveSite = IsLiveSite;
userIsIdentified = (ui != null) && (!ui.IsPublic()) && (ValidationHelper.IsEmail(ui.Email) || ValidationHelper.IsEmail(ui.UserName));
if (userIsIdentified)
{
usNewsletters.Visible = true;
// Try to get subscriber info with specified e-mail
sb = SubscriberInfoProvider.GetSubscriberInfo(ui.Email, siteId);
if (sb == null)
{
// Try to get subscriber info according to user info
sb = SubscriberInfoProvider.GetSubscriberInfo(SiteObjectType.USER, ui.UserID, siteId);
}
// Get user e-mail address
if (sb != null)
{
subscriberEmail = sb.SubscriberEmail;
// Get selected newsletters
DataSet ds = SubscriberNewsletterInfoProvider.GetSubscriberNewsletters(sb.SubscriberID, null, -1, "NewsletterID");
if (!DataHelper.DataSourceIsEmpty(ds))
{
currentValues = TextHelper.Join(";", SystemDataHelper.GetStringValues(ds.Tables[0], "NewsletterID"));
}
// Load selected newsletters
if (!RequestHelper.IsPostBack() || !string.IsNullOrEmpty(DataHelper.GetNewItemsInList(SelectorValue, currentValues)))
{
usNewsletters.Value = currentValues;
}
}
// Try to get email address from user data
if (string.IsNullOrEmpty(subscriberEmail))
{
if (ValidationHelper.IsEmail(ui.Email))
{
subscriberEmail = ui.Email;
}
else if (ValidationHelper.IsEmail(ui.UserName))
{
subscriberEmail = ui.UserName;
}
}
}
else
{
usNewsletters.Visible = false;
if ((UserID > 0) && (CMSContext.CurrentUser.UserID == UserID))
{
ShowInformation(GetString("MySubscriptions.CannotIdentify"));
}
else
{
ShowInformation(GetString("MySubscriptions.CannotIdentifyUser"));
}
}
ReleaseContext();
}
}
///
/// Logs activity for subscribing/unsubscribing
///
/// User
/// Newsletter ID
/// Subscribing/unsubscribing flag
private void LogActivity(UserInfo ui, int newsletterId, bool subscribe)
{
if ((sb == null) || (ui == null))
{
return;
}
// Log activity only if subscriber is User
if ((sb.SubscriberType != null) && sb.SubscriberType.Equals(SiteObjectType.USER, StringComparison.InvariantCultureIgnoreCase))
{
NewsletterInfo news = NewsletterInfoProvider.GetNewsletterInfo(newsletterId);
Activity activity;
if (subscribe)
{
activity = new ActivityNewsletterSubscribing(sb, news, CMSContext.ActivityEnvironmentVariables);
}
else
{
activity = new ActivityNewsletterUnsubscribing(sb, news, CMSContext.ActivityEnvironmentVariables);
}
activity.Log();
}
}
private void usNewsletters_OnSelectionChanged(object sender, EventArgs e)
{
if (RaiseOnCheckPermissions("ManageSubscribers", this))
{
if (StopProcessing)
{
return;
}
}
// Get specified user if used instead of current user
UserInfo ui = null;
if (UserID > 0)
{
ui = UserInfoProvider.GetUserInfo(UserID);
}
else
{
ui = CMSContext.CurrentUser;
}
// Get specified site ID instead of current site ID
int siteId = 0;
if (SiteID > 0)
{
siteId = SiteID;
}
else
{
siteId = CMSContext.CurrentSiteID;
}
if ((sb == null) && (ui != null))
{
// Create new subsciber (bind to existing user account)
if ((!ui.IsPublic()) && (ValidationHelper.IsEmail(ui.Email) || ValidationHelper.IsEmail(ui.UserName)))
{
sb = new SubscriberInfo();
if (ui != null)
{
if (!string.IsNullOrEmpty(ui.FirstName) && !string.IsNullOrEmpty(ui.LastName))
{
sb.SubscriberFirstName = ui.FirstName;
sb.SubscriberLastName = ui.LastName;
}
else
{
sb.SubscriberFirstName = ui.FullName;
}
// Full name consists of "user " and user full name
sb.SubscriberFullName = "User '" + ui.FullName + "'";
}
sb.SubscriberSiteID = siteId;
sb.SubscriberType = SiteObjectType.USER;
sb.SubscriberRelatedID = ui.UserID;
// Save subscriber to DB
SubscriberInfoProvider.SetSubscriberInfo(sb);
}
}
if (sb == null)
{
return;
}
// Create membership between current contact and subscriber
ModuleCommands.OnlineMarketingCreateRelation(sb.SubscriberID, MembershipType.NEWSLETTER_SUBSCRIBER, ModuleCommands.OnlineMarketingGetCurrentContactID());
// Remove old items
int newsletterId = 0;
string newValues = ValidationHelper.GetString(usNewsletters.Value, null);
string items = DataHelper.GetNewItemsInList(newValues, currentValues);
if (!String.IsNullOrEmpty(items))
{
string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
if (newItems != null)
{
foreach (string item in newItems)
{
newsletterId = ValidationHelper.GetInteger(item, 0);
// If subscriber is subscribed, unsubscribe him
if (SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId))
{
SubscriberInfoProvider.Unsubscribe(sb.SubscriberID, newsletterId, SendConfirmationEmail);
// Log activity
LogActivity(ui, newsletterId, false);
}
}
}
}
// Add new items
items = DataHelper.GetNewItemsInList(currentValues, newValues);
if (!String.IsNullOrEmpty(items))
{
string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
if (newItems != null)
{
foreach (string item in newItems)
{
newsletterId = ValidationHelper.GetInteger(item, 0);
// If subscriber is not subscribed, subscribe him
if (!SubscriberInfoProvider.IsSubscribed(sb.SubscriberID, newsletterId))
{
try
{
SubscriberInfoProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, SendConfirmationEmail);
// Log activity
LogActivity(ui, newsletterId, true);
}
catch
{
}
}
}
}
}
// Display information about successful (un)subscription
ShowChangesSaved();
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
// Display appropriate message
if (userIsIdentified)
{
// There are some newsletters to display
if (CMSContext.CurrentUser.UserID == UserID)
{
lblText.Text = GetString("MySubscriptions.MainText").Replace("##EMAIL##", HTMLHelper.HTMLEncode(subscriberEmail));
}
else
{
lblText.Text = GetString("MySubscriptions.MainTextUser").Replace("##EMAIL##", HTMLHelper.HTMLEncode(subscriberEmail));
}
}
// Preserve selected values
hdnValue.Value = ValidationHelper.GetString(usNewsletters.Value, string.Empty);
}
///
/// Overridden SetValue - because of MyAccount webpart.
///
/// Name of the property to set
/// Value to set
public override void SetValue(string propertyName, object value)
{
base.SetValue(propertyName, value);
switch (propertyName.ToLowerCSafe())
{
case "forcedvisible":
ForcedVisible = ValidationHelper.GetBoolean(value, false);
break;
case "externaluse":
ExternalUse = ValidationHelper.GetBoolean(value, false);
break;
case "cacheminutes":
CacheMinutes = ValidationHelper.GetInteger(value, 0);
break;
case "reloaddata":
// Special property which enables to call LoadData from MyAccount webpart
LoadData();
break;
case "userid":
UserID = ValidationHelper.GetInteger(value, 0);
break;
case "siteid":
SiteID = ValidationHelper.GetInteger(value, 0);
break;
case "sendconfirmationemail":
mSendConfirmationEmail = ValidationHelper.GetBoolean(value, true);
break;
}
}
#endregion
}