PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/widgets/Visitor info/widget.ascx.cs

#
C# | 167 lines | 103 code | 30 blank | 34 comment | 16 complexity | 7003005cb02168d208362f5e838b720c MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <summary>
  3. // The widget.
  4. // </summary>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace Widgets.VisitorInfo
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using App_Code.Controls;
  11. using BlogEngine.Core;
  12. /// <summary>
  13. /// The widget.
  14. /// </summary>
  15. public partial class Widget : WidgetBase
  16. {
  17. #region Constants and Fields
  18. /// <summary>
  19. /// The number of comments.
  20. /// </summary>
  21. private int numberOfComments;
  22. #endregion
  23. #region Properties
  24. /// <summary>
  25. /// Gets a value indicating whether IsEditable.
  26. /// </summary>
  27. public override bool IsEditable
  28. {
  29. get
  30. {
  31. return false;
  32. }
  33. }
  34. /// <summary>
  35. /// Gets Name.
  36. /// </summary>
  37. public override string Name
  38. {
  39. get
  40. {
  41. return "Visitor info";
  42. }
  43. }
  44. #endregion
  45. #region Public Methods
  46. /// <summary>
  47. /// This method works as a substitute for Page_Load. You should use this method for
  48. /// data binding etc. instead of Page_Load.
  49. /// </summary>
  50. public override void LoadWidget()
  51. {
  52. Visible = false;
  53. var cookie = Request.Cookies["comment"];
  54. if (cookie == null)
  55. {
  56. return;
  57. }
  58. var name = cookie.Values["name"];
  59. var email = cookie.Values["email"];
  60. var website = cookie.Values["url"];
  61. if (name == null)
  62. {
  63. return;
  64. }
  65. name = name.Replace("+", " ");
  66. WriteHtml(name, email, website);
  67. Uri url;
  68. if (Request.QueryString["apml"] == null && Uri.TryCreate(website, UriKind.Absolute, out url))
  69. {
  70. phScript.Visible = true;
  71. // ltWebsite.Text = url.ToString();
  72. }
  73. Visible = true;
  74. }
  75. #endregion
  76. #region Methods
  77. /// <summary>
  78. /// Gets the commented posts.
  79. /// </summary>
  80. /// <param name="email">The email.</param>
  81. /// <param name="website">The website.</param>
  82. /// <returns>A list of Post</returns>
  83. private List<Post> GetCommentedPosts(string email, string website)
  84. {
  85. var list = new List<Post>();
  86. foreach (var post in Post.Posts)
  87. {
  88. var comments = post.Comments.FindAll(
  89. c => email.Equals(c.Email, StringComparison.OrdinalIgnoreCase) ||
  90. (c.Website != null &&
  91. c.Website.ToString().Equals(website, StringComparison.OrdinalIgnoreCase)));
  92. if (comments.Count <= 0)
  93. {
  94. continue;
  95. }
  96. numberOfComments += comments.Count;
  97. var index = post.Comments.IndexOf(comments[comments.Count - 1]);
  98. if (index < post.Comments.Count - 1 &&
  99. post.Comments[post.Comments.Count - 1].DateCreated > DateTime.Now.AddDays(-7))
  100. {
  101. list.Add(post);
  102. }
  103. }
  104. return list;
  105. }
  106. /// <summary>
  107. /// Writes the HTML.
  108. /// </summary>
  109. /// <param name="name">The name to write.</param>
  110. /// <param name="email">The email.</param>
  111. /// <param name="website">The website.</param>
  112. private void WriteHtml(string name, string email, string website)
  113. {
  114. if (name.Contains(" "))
  115. {
  116. name = name.Substring(0, name.IndexOf(" "));
  117. }
  118. var avatar = Avatar.GetAvatar(16, email, null, null, name);
  119. var avatarLink = avatar == null || avatar.Url == null ? string.Empty : avatar.Url.ToString();
  120. Title = string.Format(
  121. String.Concat("<img src=\"{0}\" alt=\"{1}\" align=\"top\" /> ", Resources.labels.visitorHi, " {1}"), avatarLink, Server.HtmlEncode(name));
  122. pName.InnerHtml = "<strong>" + Resources.labels.welcomeBack + "</strong>";
  123. var list = GetCommentedPosts(email, website);
  124. if (list.Count > 0)
  125. {
  126. var link = string.Format(
  127. "<a href=\"{0}\">{1}</a>", list[0].RelativeLink, Server.HtmlEncode(list[0].Title));
  128. pComment.InnerHtml = string.Format(Resources.labels.commentsAddedSince, link);
  129. }
  130. if (numberOfComments > 0)
  131. {
  132. pComment.InnerHtml += string.Format(Resources.labels.writtenCommentsTotal, numberOfComments);
  133. }
  134. }
  135. #endregion
  136. }
  137. }