PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/widgets/RecentPosts/widget.ascx.cs

#
C# | 200 lines | 120 code | 41 blank | 39 comment | 17 complexity | 542f682b71eceddbb81679e764b746db 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.RecentPosts
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.Specialized;
  11. using System.Globalization;
  12. using System.Text;
  13. using System.Web;
  14. using System.Web.UI;
  15. using App_Code.Controls;
  16. using BlogEngine.Core;
  17. using Resources;
  18. /// <summary>
  19. /// The widget.
  20. /// </summary>
  21. public partial class Widget : WidgetBase
  22. {
  23. #region Constants and Fields
  24. /// <summary>
  25. /// The default number of posts.
  26. /// </summary>
  27. private const int DefaultNumberOfPosts = 10;
  28. /// <summary>
  29. /// The default show comments.
  30. /// </summary>
  31. private const bool DefaultShowComments = true;
  32. /// <summary>
  33. /// The default show rating.
  34. /// </summary>
  35. private const bool DefaultShowRating = true;
  36. #endregion
  37. #region Constructors and Destructors
  38. /// <summary>
  39. /// Initializes static members of the <see cref="Widget"/> class.
  40. /// </summary>
  41. static Widget()
  42. {
  43. Post.Saved += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_recentposts");
  44. Post.CommentAdded += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_recentposts");
  45. Post.CommentRemoved += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_recentposts");
  46. Post.Rated += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_recentposts");
  47. BlogSettings.Changed += (sender, args) => Blog.CurrentInstance.Cache.Remove("widget_recentposts");
  48. }
  49. #endregion
  50. #region Properties
  51. /// <summary>
  52. /// Gets a value indicating whether IsEditable.
  53. /// </summary>
  54. public override bool IsEditable
  55. {
  56. get
  57. {
  58. return true;
  59. }
  60. }
  61. /// <summary>
  62. /// Gets Name.
  63. /// </summary>
  64. public override string Name
  65. {
  66. get
  67. {
  68. return "RecentPosts";
  69. }
  70. }
  71. #endregion
  72. #region Public Methods
  73. /// <summary>
  74. /// This method works as a substitute for Page_Load. You should use this method for
  75. /// data binding etc. instead of Page_Load.
  76. /// </summary>
  77. public override void LoadWidget()
  78. {
  79. var settings = this.GetSettings();
  80. var numberOfPosts = DefaultNumberOfPosts;
  81. if (settings.ContainsKey("numberofposts"))
  82. {
  83. numberOfPosts = int.Parse(settings["numberofposts"]);
  84. }
  85. if (Blog.CurrentInstance.Cache["widget_recentposts"] == null)
  86. {
  87. var visiblePosts = Post.Posts.FindAll(p => p.IsVisibleToPublic);
  88. var max = Math.Min(visiblePosts.Count, numberOfPosts);
  89. var list = visiblePosts.GetRange(0, max);
  90. Blog.CurrentInstance.Cache.Insert("widget_recentposts", list);
  91. }
  92. var content = RenderPosts((List<Post>)Blog.CurrentInstance.Cache["widget_recentposts"], settings);
  93. var html = new LiteralControl(content);
  94. // new LiteralControl((string)Blog.CurrentInstance.Cache["widget_recentposts"]);
  95. this.phPosts.Controls.Add(html);
  96. }
  97. #endregion
  98. #region Methods
  99. /// <summary>
  100. /// Renders the posts.
  101. /// </summary>
  102. /// <param name="posts">The posts.</param>
  103. /// <param name="settings">The settings.</param>
  104. /// <returns>The rendered html.</returns>
  105. private static string RenderPosts(List<Post> posts, StringDictionary settings)
  106. {
  107. if (posts.Count == 0)
  108. {
  109. // Blog.CurrentInstance.Cache.Insert("widget_recentposts", "<p>" + Resources.labels.none + "</p>");
  110. return string.Format("<p>{0}</p>", labels.none);
  111. }
  112. var sb = new StringBuilder();
  113. sb.Append("<ul class=\"recentPosts\" id=\"recentPosts\">");
  114. var showComments = DefaultShowComments;
  115. var showRating = DefaultShowRating;
  116. if (settings.ContainsKey("showcomments"))
  117. {
  118. bool.TryParse(settings["showcomments"], out showComments);
  119. }
  120. if (settings.ContainsKey("showrating"))
  121. {
  122. bool.TryParse(settings["showrating"], out showRating);
  123. }
  124. foreach (var post in posts)
  125. {
  126. if (!post.IsVisibleToPublic)
  127. {
  128. continue;
  129. }
  130. var rating = Math.Round(post.Rating, 1).ToString(CultureInfo.InvariantCulture);
  131. const string LinkFormat = "<li><a href=\"{0}\">{1}</a>{2}{3}</li>";
  132. var comments = string.Format("<span>{0}: {1}</span>", labels.comments, post.ApprovedComments.Count);
  133. if (BlogSettings.Instance.ModerationType == BlogSettings.Moderation.Disqus)
  134. {
  135. comments = string.Format(
  136. "<span><a href=\"{0}#disqus_thread\">{1}</a></span>", post.PermaLink, labels.comments);
  137. }
  138. var rate = string.Format("<span>{0}: {1} / {2}</span>", labels.rating, rating, post.Raters);
  139. if (!showComments || !BlogSettings.Instance.IsCommentsEnabled)
  140. {
  141. comments = null;
  142. }
  143. if (!showRating || !BlogSettings.Instance.EnableRating)
  144. {
  145. rate = null;
  146. }
  147. else if (post.Raters == 0)
  148. {
  149. rate = string.Format("<span>{0}</span>", labels.notRatedYet);
  150. }
  151. sb.AppendFormat(LinkFormat, post.RelativeLink, HttpUtility.HtmlEncode(post.Title), comments, rate);
  152. }
  153. sb.Append("</ul>");
  154. // Blog.CurrentInstance.Cache.Insert("widget_recentposts", sb.ToString());
  155. return sb.ToString();
  156. }
  157. #endregion
  158. }
  159. }