PageRenderTime 69ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/CMSModules/Newsletters/Controls/MySubscriptions.ascx.cs

https://bitbucket.org/kudutest/kenticogit
C# | 494 lines | 366 code | 65 blank | 63 comment | 66 complexity | bbf61dbed45eaeb6da0374e4cb1409f4 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Web.UI.WebControls;
  4. using CMS.GlobalHelper;
  5. using CMS.Newsletter;
  6. using CMS.CMSHelper;
  7. using CMS.PortalEngine;
  8. using CMS.SiteProvider;
  9. using CMS.UIControls;
  10. using CMS.SettingsProvider;
  11. using CMS.WebAnalytics;
  12. public partial class CMSModules_Newsletters_Controls_MySubscriptions : CMSAdminControl
  13. {
  14. #region "Variables"
  15. private Subscriber sb = null;
  16. private bool mExternalUse = false;
  17. private int mCacheMinutes = 0;
  18. private string subscriberEmail = string.Empty;
  19. private bool userIsIdentified = false;
  20. private int mUserId = 0;
  21. private int mSiteId = 0;
  22. private string currentValues = string.Empty;
  23. private bool mSendConfirmationEmail = true;
  24. #endregion
  25. #region "Properties"
  26. /// <summary>
  27. /// Gets or sets the value that indicates whether send confirmation emails.
  28. /// </summary>
  29. public bool SendConfirmationEmail
  30. {
  31. get
  32. {
  33. return mSendConfirmationEmail;
  34. }
  35. set
  36. {
  37. mSendConfirmationEmail = value;
  38. }
  39. }
  40. /// <summary>
  41. /// Gets or sets the value that indicates whether this control is visible.
  42. /// </summary>
  43. public bool ForcedVisible
  44. {
  45. get
  46. {
  47. return plcMain.Visible;
  48. }
  49. set
  50. {
  51. plcMain.Visible = value;
  52. lblInfo.Visible = value;
  53. }
  54. }
  55. /// <summary>
  56. /// Gets or sets the value that indicates whether this control is used in other control.
  57. /// </summary>
  58. public bool ExternalUse
  59. {
  60. get
  61. {
  62. return mExternalUse;
  63. }
  64. set
  65. {
  66. mExternalUse = value;
  67. }
  68. }
  69. /// <summary>
  70. /// Gets or sets the WebPart cache minutes.
  71. /// </summary>
  72. public int CacheMinutes
  73. {
  74. get
  75. {
  76. return mCacheMinutes;
  77. }
  78. set
  79. {
  80. mCacheMinutes = value;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets or sets current site ID.
  85. /// </summary>
  86. public int SiteID
  87. {
  88. get
  89. {
  90. return mSiteId;
  91. }
  92. set
  93. {
  94. mSiteId = value;
  95. }
  96. }
  97. /// <summary>
  98. /// Gets or sets current user ID.
  99. /// </summary>
  100. public int UserID
  101. {
  102. get
  103. {
  104. return mUserId;
  105. }
  106. set
  107. {
  108. mUserId = value;
  109. }
  110. }
  111. /// <summary>
  112. /// Indicatec if selector control is on live site.
  113. /// </summary>
  114. public override bool IsLiveSite
  115. {
  116. get
  117. {
  118. return ValidationHelper.GetBoolean(this.GetValue("IsLiveSite"), false);
  119. }
  120. set
  121. {
  122. this.SetValue("IsLiveSite", value);
  123. }
  124. }
  125. #endregion
  126. #region "Methods"
  127. /// <summary>
  128. /// PageLoad.
  129. /// </summary>
  130. protected void Page_Load(object sender, EventArgs e)
  131. {
  132. if (ExternalUse)
  133. {
  134. LoadData();
  135. }
  136. }
  137. /// <summary>
  138. /// Load data.
  139. /// </summary>
  140. public void LoadData()
  141. {
  142. if (this.StopProcessing)
  143. {
  144. // Hide control
  145. this.Visible = false;
  146. }
  147. else
  148. {
  149. this.SetContext();
  150. // Get specified user if used instead of current user
  151. UserInfo ui = null;
  152. if (this.UserID > 0)
  153. {
  154. ui = UserInfoProvider.GetUserInfo(this.UserID);
  155. }
  156. else
  157. {
  158. ui = CMSContext.CurrentUser;
  159. }
  160. // Get specified site ID instead of current site ID
  161. int siteId = 0;
  162. if (this.SiteID > 0)
  163. {
  164. siteId = this.SiteID;
  165. }
  166. else
  167. {
  168. siteId = CMSContext.CurrentSiteID;
  169. }
  170. usNewsletters.WhereCondition = "NewsletterSiteID = " + siteId;
  171. usNewsletters.ButtonRemoveSelected.CssClass = "XLongButton";
  172. usNewsletters.ButtonAddItems.CssClass = "XLongButton";
  173. usNewsletters.OnSelectionChanged += new EventHandler(usNewsletters_OnSelectionChanged);
  174. usNewsletters.IsLiveSite = this.IsLiveSite;
  175. this.userIsIdentified = (ui != null) && (!ui.IsPublic()) && (ValidationHelper.IsEmail(ui.Email) || ValidationHelper.IsEmail(ui.UserName));
  176. if (this.userIsIdentified)
  177. {
  178. usNewsletters.Visible = true;
  179. // Try to get subsriber info with specified e-mail
  180. sb = SubscriberProvider.GetSubscriber(ui.Email, siteId);
  181. if (sb == null)
  182. {
  183. // Try to get subscriber info according to user info
  184. sb = SubscriberProvider.GetSubscriber(SiteObjectType.USER, ui.UserID, siteId);
  185. }
  186. // Get user e-mail address
  187. if (sb != null)
  188. {
  189. subscriberEmail = sb.SubscriberEmail;
  190. // Get selected newsletters
  191. DataSet ds = SubscriberNewsletterInfoProvider.GetSubscriberNewsletters(sb.SubscriberID, null, -1, "NewsletterID");
  192. if (!DataHelper.DataSourceIsEmpty(ds))
  193. {
  194. currentValues = TextHelper.Join(";", SqlHelperClass.GetStringValues(ds.Tables[0], "NewsletterID"));
  195. }
  196. // Load selected newsletters
  197. if (!string.IsNullOrEmpty(currentValues))
  198. {
  199. usNewsletters.Value = currentValues;
  200. }
  201. }
  202. // Try to get email address from user data
  203. if (string.IsNullOrEmpty(subscriberEmail))
  204. {
  205. if (ValidationHelper.IsEmail(ui.Email))
  206. {
  207. subscriberEmail = ui.Email;
  208. }
  209. else if (ValidationHelper.IsEmail(ui.UserName))
  210. {
  211. subscriberEmail = ui.UserName;
  212. }
  213. }
  214. }
  215. else
  216. {
  217. usNewsletters.Visible = false;
  218. lblInfo.Visible = true;
  219. if ((this.UserID > 0) && (CMSContext.CurrentUser.UserID == this.UserID))
  220. {
  221. lblInfo.Text = GetString("MySubscriptions.CannotIdentify");
  222. }
  223. else
  224. {
  225. lblInfo.Text = GetString("MySubscriptions.CannotIdentifyUser");
  226. }
  227. }
  228. this.ReleaseContext();
  229. }
  230. }
  231. /// <summary>
  232. /// Logs activity for subscribing/unsubscribing
  233. /// </summary>
  234. /// <param name="ui">User</param>
  235. /// <param name="newsletterId">Newsletter ID</param>
  236. /// <param name="subscribe">Subscribing/unsubscribing flag</param>
  237. /// <param name="siteId">Site ID</param>
  238. private void LogActivity(UserInfo ui, int newsletterId, bool subscribe, int siteId)
  239. {
  240. if ((sb == null) || (ui == null) || (CMSContext.ViewMode != ViewModeEnum.LiveSite) ||
  241. !ActivitySettingsHelper.ActivitiesEnabledAndModuleLoaded(siteId) || !ActivitySettingsHelper.ActivitiesEnabledForThisUser(ui))
  242. {
  243. return;
  244. }
  245. if (sb.SubscriberType == SiteObjectType.USER)
  246. {
  247. if (subscribe && ActivitySettingsHelper.NewsletterSubscribeEnabled(siteId) ||
  248. !subscribe && ActivitySettingsHelper.NewsletterUnsubscribeEnabled(siteId))
  249. {
  250. Newsletter news = NewsletterProvider.GetNewsletter(newsletterId);
  251. if ((news != null) && news.NewsletterLogActivity)
  252. {
  253. var data = new ActivityData()
  254. {
  255. ContactID = ModuleCommands.OnlineMarketingGetCurrentContactID(),
  256. SiteID = sb.SubscriberSiteID,
  257. Type = PredefinedActivityType.NEWSLETTER_UNSUBSCRIBING,
  258. TitleData = news.NewsletterName,
  259. ItemID = newsletterId,
  260. URL = URLHelper.CurrentRelativePath,
  261. Campaign = CMSContext.Campaign
  262. };
  263. ActivityLogProvider.LogActivity(data);
  264. }
  265. }
  266. }
  267. }
  268. private void usNewsletters_OnSelectionChanged(object sender, EventArgs e)
  269. {
  270. if (RaiseOnCheckPermissions("ManageSubscribers", this))
  271. {
  272. if (this.StopProcessing)
  273. {
  274. return;
  275. }
  276. }
  277. // Get specified user if used instead of current user
  278. UserInfo ui = null;
  279. if (this.UserID > 0)
  280. {
  281. ui = UserInfoProvider.GetUserInfo(this.UserID);
  282. }
  283. else
  284. {
  285. ui = CMSContext.CurrentUser;
  286. }
  287. // Get specified site ID instead of current site ID
  288. int siteId = 0;
  289. if (this.SiteID > 0)
  290. {
  291. siteId = this.SiteID;
  292. }
  293. else
  294. {
  295. siteId = CMSContext.CurrentSiteID;
  296. }
  297. if ((sb == null) && (ui != null))
  298. {
  299. // Create new subsciber (bind to existing user account)
  300. if ((!ui.IsPublic()) && (ValidationHelper.IsEmail(ui.Email) || ValidationHelper.IsEmail(ui.UserName)))
  301. {
  302. sb = new Subscriber();
  303. if (ui != null)
  304. {
  305. if (!string.IsNullOrEmpty(ui.FirstName) && !string.IsNullOrEmpty(ui.LastName))
  306. {
  307. sb.SubscriberFirstName = ui.FirstName;
  308. sb.SubscriberLastName = ui.LastName;
  309. }
  310. else
  311. {
  312. sb.SubscriberFirstName = ui.FullName;
  313. }
  314. // Full name consists of "user " and user full name
  315. sb.SubscriberFullName = "User '" + ui.FullName + "'";
  316. }
  317. sb.SubscriberSiteID = siteId;
  318. sb.SubscriberType = SiteObjectType.USER;
  319. sb.SubscriberRelatedID = ui.UserID;
  320. // Save subscriber to DB
  321. SubscriberProvider.SetSubscriber(sb);
  322. }
  323. }
  324. if (sb == null)
  325. {
  326. return;
  327. }
  328. // Remove old items
  329. int newsletterId = 0;
  330. string newValues = ValidationHelper.GetString(usNewsletters.Value, null);
  331. string items = DataHelper.GetNewItemsInList(newValues, currentValues);
  332. if (!String.IsNullOrEmpty(items))
  333. {
  334. string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  335. if (newItems != null)
  336. {
  337. foreach (string item in newItems)
  338. {
  339. newsletterId = ValidationHelper.GetInteger(item, 0);
  340. // If subscriber is subscribed, unsubscribe him
  341. if (SubscriberProvider.IsSubscribed(sb.SubscriberID, newsletterId))
  342. {
  343. SubscriberProvider.Unsubscribe(sb.SubscriberID, newsletterId, SendConfirmationEmail);
  344. // Log activity
  345. LogActivity(ui, newsletterId, false, siteId);
  346. }
  347. }
  348. }
  349. }
  350. // Add new items
  351. items = DataHelper.GetNewItemsInList(currentValues, newValues);
  352. if (!String.IsNullOrEmpty(items))
  353. {
  354. string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  355. if (newItems != null)
  356. {
  357. foreach (string item in newItems)
  358. {
  359. newsletterId = ValidationHelper.GetInteger(item, 0);
  360. // If subscriber is not subscribed, subscribe him
  361. if (!SubscriberProvider.IsSubscribed(sb.SubscriberID, newsletterId))
  362. {
  363. try
  364. {
  365. SubscriberProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, SendConfirmationEmail);
  366. // Log activity
  367. LogActivity(ui, newsletterId, true, siteId);
  368. }
  369. catch { }
  370. }
  371. }
  372. }
  373. }
  374. // Display information about successful (un)subscription
  375. this.lblInfoMsg.Visible = true;
  376. }
  377. protected override void OnPreRender(EventArgs e)
  378. {
  379. base.OnPreRender(e);
  380. // Display appropriate message
  381. if (this.userIsIdentified)
  382. {
  383. // There are some newsletters to display
  384. if (CMSContext.CurrentUser.UserID == this.UserID)
  385. {
  386. lblText.Text = GetString("MySubscriptions.MainText").Replace("##EMAIL##", HTMLHelper.HTMLEncode(subscriberEmail));
  387. }
  388. else
  389. {
  390. lblText.Text = GetString("MySubscriptions.MainTextUser").Replace("##EMAIL##", HTMLHelper.HTMLEncode(subscriberEmail));
  391. }
  392. }
  393. }
  394. /// <summary>
  395. /// Overriden SetValue - because of MyAccount webpart.
  396. /// </summary>
  397. /// <param name="propertyName">Name of the property to set</param>
  398. /// <param name="value">Value to set</param>
  399. public override void SetValue(string propertyName, object value)
  400. {
  401. base.SetValue(propertyName, value);
  402. switch (propertyName.ToLower())
  403. {
  404. case "forcedvisible":
  405. this.ForcedVisible = ValidationHelper.GetBoolean(value, false);
  406. break;
  407. case "externaluse":
  408. this.ExternalUse = ValidationHelper.GetBoolean(value, false);
  409. break;
  410. case "cacheminutes":
  411. this.CacheMinutes = ValidationHelper.GetInteger(value, 0);
  412. break;
  413. case "reloaddata":
  414. // Special property which enables to call LoadData from MyAccount webpart
  415. LoadData();
  416. break;
  417. case "userid":
  418. this.UserID = ValidationHelper.GetInteger(value, 0);
  419. break;
  420. case "siteid":
  421. this.SiteID = ValidationHelper.GetInteger(value, 0);
  422. break;
  423. case "sendconfirmationemail":
  424. mSendConfirmationEmail = ValidationHelper.GetBoolean (value,true);
  425. break;
  426. }
  427. }
  428. #endregion
  429. }