PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Controls/AuthorList.cs

#
C# | 174 lines | 107 code | 34 blank | 33 comment | 9 complexity | c3d5898940321ff9e5d1978a0b447c81 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <summary>
  3. // Builds an author list.
  4. // </summary>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace App_Code.Controls
  7. {
  8. using System;
  9. using System.Web.Security;
  10. using System.Web.UI;
  11. using System.Web.UI.HtmlControls;
  12. using System.Collections.Generic;
  13. using BlogEngine.Core;
  14. using Resources;
  15. /// <summary>
  16. /// Builds an author list.
  17. /// </summary>
  18. public class AuthorList : Control
  19. {
  20. #region Constants and Fields
  21. /// <summary>
  22. /// The html string.
  23. /// </summary>
  24. private static Dictionary<Guid, string> blogsHtml = new Dictionary<Guid, string>();
  25. /// <summary>
  26. /// The show rss icon.
  27. /// </summary>
  28. private static Dictionary<Guid, bool> blogsShowRssIcon = new Dictionary<Guid, bool>();
  29. #endregion
  30. #region Constructors and Destructors
  31. /// <summary>
  32. /// Initializes static members of the <see cref="AuthorList"/> class.
  33. /// </summary>
  34. static AuthorList()
  35. {
  36. Post.Saved += (sender, args) => blogsHtml.Remove(Blog.CurrentInstance.Id);
  37. }
  38. #endregion
  39. #region Properties
  40. /// <summary>
  41. /// Gets or sets a value indicating whether or not to show feed icons next to the category links.
  42. /// </summary>
  43. public bool ShowRssIcon
  44. {
  45. get
  46. {
  47. Guid blogId = Blog.CurrentInstance.Id;
  48. if (!blogsShowRssIcon.ContainsKey(blogId))
  49. blogsShowRssIcon[blogId] = true;
  50. return blogsShowRssIcon[blogId];
  51. }
  52. set
  53. {
  54. if (ShowRssIcon == value)
  55. {
  56. return;
  57. }
  58. blogsShowRssIcon[Blog.CurrentInstance.Id] = value;
  59. blogsHtml.Remove(Blog.CurrentInstance.Id);
  60. }
  61. }
  62. /// <summary>
  63. /// Gets the rendered HTML in the private field and first
  64. /// updates it when a post has been saved (new or updated).
  65. /// </summary>
  66. private string Html
  67. {
  68. get
  69. {
  70. Guid blogId = Blog.CurrentInstance.Id;
  71. if (!blogsHtml.ContainsKey(blogId))
  72. blogsHtml[blogId] = string.Empty;
  73. return blogsHtml[blogId];
  74. }
  75. }
  76. #endregion
  77. #region Public Methods
  78. /// <summary>
  79. /// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object and stores tracing information about the control if tracing is enabled.
  80. /// </summary>
  81. /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content.</param>
  82. public override void RenderControl(HtmlTextWriter writer)
  83. {
  84. writer.Write(Html);
  85. writer.Write(Environment.NewLine);
  86. }
  87. #endregion
  88. #region Methods
  89. /// <summary>
  90. /// Loops through all users and builds the HTML
  91. /// presentation.
  92. /// </summary>
  93. /// <returns>The authors.</returns>
  94. private HtmlGenericControl BindAuthors()
  95. {
  96. if (Post.Posts.Count == 0)
  97. {
  98. var p = new HtmlGenericControl("p") { InnerHtml = labels.none };
  99. return p;
  100. }
  101. var ul = new HtmlGenericControl("ul") { ID = "authorlist" };
  102. foreach (MembershipUser user in Membership.GetAllUsers())
  103. {
  104. var postCount = Post.GetPostsByAuthor(user.UserName).Count;
  105. if (postCount == 0)
  106. {
  107. continue;
  108. }
  109. var li = new HtmlGenericControl("li");
  110. if (ShowRssIcon)
  111. {
  112. var img = new HtmlImage
  113. {
  114. Src = string.Format("{0}pics/rssButton.png", Utils.RelativeWebRoot),
  115. Alt = string.Format("RSS feed for {0}", user.UserName)
  116. };
  117. img.Attributes["class"] = "rssButton";
  118. var feedAnchor = new HtmlAnchor
  119. {
  120. HRef =
  121. string.Format("{0}syndication.axd?author={1}", Utils.RelativeWebRoot, Utils.RemoveIllegalCharacters(user.UserName))
  122. };
  123. feedAnchor.Attributes["rel"] = "nofollow";
  124. feedAnchor.Controls.Add(img);
  125. li.Controls.Add(feedAnchor);
  126. }
  127. var anc = new HtmlAnchor
  128. {
  129. HRef = string.Format("{0}author/{1}{2}", Utils.RelativeWebRoot, user.UserName, BlogConfig.FileExtension),
  130. InnerHtml = string.Format("{0} ({1})", user.UserName, postCount),
  131. Title = string.Format("Author: {0}", user.UserName)
  132. };
  133. li.Controls.Add(anc);
  134. ul.Controls.Add(li);
  135. }
  136. return ul;
  137. }
  138. #endregion
  139. }
  140. }