PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Web/Controls/SubscriberPreferencesControl.ascx.cs

#
C# | 306 lines | 194 code | 77 blank | 35 comment | 33 complexity | fc10a617d6d41bed9d1335bd1f327cb7 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. // Author: Joe Audette
  2. // Created: 2007-12-21
  3. // Last Modified: 2009-10-31
  4. //
  5. // The use and distribution terms for this software are covered by the
  6. // Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. // which can be found in the file CPL.TXT at the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by
  9. // the terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Web.UI;
  15. using mojoPortal.Business;
  16. using mojoPortal.Business.WebHelpers;
  17. using mojoPortal.Web.Framework;
  18. using Resources;
  19. namespace mojoPortal.Web.ELetterUI
  20. {
  21. public partial class SubscriberPreferencesControl : UserControl
  22. {
  23. //private ScriptManager scriptController;
  24. private SiteSettings siteSettings = null;
  25. private SiteUser siteUser = null;
  26. private List<LetterSubscriber> userSubscriptions = null;
  27. private SubscriberRepository subscriptions = new SubscriberRepository();
  28. private List<LetterInfo> siteAvailableSubscriptions = null;
  29. private Guid userGuid = Guid.Empty;
  30. protected string siteRoot = string.Empty;
  31. private bool hasUnverifiedSubscriptions = false;
  32. public Guid UserGuid
  33. {
  34. get { return userGuid; }
  35. set { userGuid = value; }
  36. }
  37. protected void Page_Load(object sender, EventArgs e)
  38. {
  39. LoadSettings();
  40. PopulateLabels();
  41. PopulateControls();
  42. }
  43. private void PopulateControls()
  44. {
  45. if (!Request.IsAuthenticated) return;
  46. if (siteUser == null) return;
  47. if (siteAvailableSubscriptions == null) return;
  48. if (Page.IsPostBack) return;
  49. BindPreferences();
  50. if (hasUnverifiedSubscriptions)
  51. {
  52. lblUnverifiedWarning.Visible = true;
  53. }
  54. }
  55. private void BindPreferences()
  56. {
  57. rbHtmlFormat.Checked = UseHtml();
  58. rbPlainText.Checked = !rbHtmlFormat.Checked;
  59. rptLetterPrefs.DataSource = siteAvailableSubscriptions;
  60. rptLetterPrefs.DataBind();
  61. }
  62. void btnSavePreferences_Click(object sender, EventArgs e)
  63. {
  64. foreach (LetterInfo availableSubscription in siteAvailableSubscriptions)
  65. {
  66. string controlID = "chk" + availableSubscription.LetterInfoGuid.ToString();
  67. if (Request.Params.Get(controlID) != null)
  68. {
  69. if (!IsSubscribed(availableSubscription.LetterInfoGuid))
  70. {
  71. //subscribe
  72. LetterSubscriber subscriber = new LetterSubscriber();
  73. subscriber.SiteGuid = siteSettings.SiteGuid;
  74. subscriber.LetterInfoGuid = availableSubscription.LetterInfoGuid;
  75. subscriber.UserGuid = siteUser.UserGuid;
  76. subscriber.EmailAddress = siteUser.Email.ToLower();
  77. subscriber.IsVerified = true;
  78. subscriber.UseHtml = rbHtmlFormat.Checked;
  79. subscriber.IpAddress = SiteUtils.GetIP4Address();
  80. subscriptions.Save(subscriber);
  81. LetterInfo.UpdateSubscriberCount(availableSubscription.LetterInfoGuid);
  82. }
  83. else
  84. {
  85. // user is subscribed already
  86. foreach (LetterSubscriber s in userSubscriptions)
  87. {
  88. if (s.LetterInfoGuid == availableSubscription.LetterInfoGuid)
  89. {
  90. if (s.UseHtml != rbHtmlFormat.Checked)
  91. {
  92. s.UseHtml = rbHtmlFormat.Checked;
  93. subscriptions.Save(s);
  94. }
  95. if (!s.IsVerified)
  96. {
  97. subscriptions.Verify(s.SubscribeGuid, true, Guid.Empty);
  98. LetterInfo.UpdateSubscriberCount(availableSubscription.LetterInfoGuid);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. else
  105. {
  106. if (IsSubscribed(availableSubscription.LetterInfoGuid))
  107. {
  108. // unsubscribe
  109. foreach (LetterSubscriber s in userSubscriptions)
  110. {
  111. if (s.LetterInfoGuid == availableSubscription.LetterInfoGuid)
  112. {
  113. subscriptions.Delete(s);
  114. }
  115. }
  116. LetterInfo.UpdateSubscriberCount(availableSubscription.LetterInfoGuid);
  117. }
  118. }
  119. }
  120. GetSubscriptions();
  121. BindPreferences();
  122. lblUnverifiedWarning.Visible = false;
  123. //btnSavePreferences.Text = Resource.NewsletterSavePreferencesButton;
  124. System.Threading.Thread.Sleep(1000);
  125. UpdatePanel1.Update();
  126. }
  127. private bool UseHtml()
  128. {
  129. foreach (LetterSubscriber subscription in userSubscriptions)
  130. {
  131. return subscription.UseHtml;
  132. }
  133. return true;
  134. }
  135. protected string GetChecked(string strLetterInfoGuid)
  136. {
  137. if(IsSubscribed(strLetterInfoGuid))
  138. return " checked ";
  139. return string.Empty;
  140. }
  141. protected bool IsSubscribed(string strLetterInfoGuid)
  142. {
  143. if (strLetterInfoGuid.Length == 36)
  144. {
  145. Guid letterInfoGuid = new Guid(strLetterInfoGuid);
  146. return IsSubscribed(letterInfoGuid);
  147. }
  148. return false;
  149. }
  150. protected bool IsSubscribed(Guid letterInfoGuid)
  151. {
  152. if (siteUser == null) { return false; }
  153. foreach (LetterSubscriber subscription in userSubscriptions)
  154. {
  155. if (subscription.LetterInfoGuid == letterInfoGuid) return true;
  156. }
  157. return false;
  158. }
  159. private void PopulateLabels()
  160. {
  161. litHeader.Text = Resource.NewsletterPreferencesHeader;
  162. rbHtmlFormat.Text = Resource.NewsletterHtmlFormatLabel;
  163. rbPlainText.Text = Resource.NewsletterPlainTextFormatLabel;
  164. btnSavePreferences.Text = Resource.NewsletterSavePreferencesButton;
  165. btnSavePreferences.ToolTip = Resource.NewsletterSavePreferencesButton;
  166. //btnSavePreferences.Attributes.Add("style", "display:inline;");
  167. //if (!Page.IsPostBack)
  168. //{
  169. // UIHelper.DisableButtonAfterClick(
  170. // btnSavePreferences,
  171. // Resource.ButtonDisabledPleaseWait,
  172. // Page.ClientScript.GetPostBackEventReference(this.btnSavePreferences, string.Empty)
  173. // );
  174. //}
  175. }
  176. private void LoadSettings()
  177. {
  178. siteRoot = SiteUtils.GetNavigationSiteRoot();
  179. siteSettings = CacheHelper.GetCurrentSiteSettings();
  180. if (userGuid == Guid.Empty)
  181. {
  182. siteUser = SiteUtils.GetCurrentSiteUser();
  183. }
  184. else
  185. {
  186. siteUser = new SiteUser(siteSettings, userGuid);
  187. }
  188. //if (scriptController == null)
  189. //{
  190. // scriptController = (ScriptManager)Page.Master.FindControl("ScriptManager1");
  191. //}
  192. //if (scriptController != null)
  193. //{
  194. // scriptController.RegisterAsyncPostBackControl(this.btnSavePreferences);
  195. //}
  196. try
  197. {
  198. // this keeps the action from changing during ajax postback in folder based sites
  199. SiteUtils.SetFormAction(Page, Request.RawUrl);
  200. }
  201. catch (MissingMethodException)
  202. {
  203. //this method was introduced in .NET 3.5 SP1
  204. }
  205. GetSubscriptions();
  206. }
  207. private void GetSubscriptions()
  208. {
  209. if ((siteSettings != null) && (siteUser != null))
  210. {
  211. siteAvailableSubscriptions = NewsletterHelper.GetAvailableNewslettersForCurrentUser(siteSettings.SiteGuid);
  212. userSubscriptions = subscriptions.GetListByUser(siteSettings.SiteGuid, siteUser.UserGuid);
  213. foreach (LetterSubscriber s in userSubscriptions)
  214. {
  215. if (!s.IsVerified) { hasUnverifiedSubscriptions = true; }
  216. }
  217. }
  218. }
  219. protected override void OnInit(EventArgs e)
  220. {
  221. base.OnInit(e);
  222. this.Load += new EventHandler(Page_Load);
  223. this.btnSavePreferences.Click += new EventHandler(btnSavePreferences_Click);
  224. #if NET35
  225. if (WebConfigSettings.DisablePageViewStateByDefault) {Page.EnableViewState = true; }
  226. #endif
  227. }
  228. }
  229. }