PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/archive.aspx.cs

#
C# | 247 lines | 192 code | 43 blank | 12 comment | 23 complexity | c3647367ec8a4ce7636684e024479616 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. #region Using
  2. using System;
  3. using System.Web;
  4. using System.Collections.Generic;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Web.UI.HtmlControls;
  8. using BlogEngine.Core;
  9. #endregion
  10. public partial class archive : BlogEngine.Core.Web.Controls.BlogBasePage
  11. {
  12. /// <summary>
  13. /// Handles the Load event of the Page control.
  14. /// </summary>
  15. /// <param name="sender">The source of the event.</param>
  16. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. if (!IsPostBack && !IsCallback)
  20. {
  21. CreateMenu();
  22. CreateArchive();
  23. AddTotals();
  24. }
  25. Page.Title = Server.HtmlEncode(Resources.labels.archive);
  26. base.AddMetaTag("description", Resources.labels.archive + " | " + BlogSettings.Instance.Name);
  27. }
  28. /// <summary>
  29. /// Creates the category top menu.
  30. /// </summary>
  31. private void CreateMenu()
  32. {
  33. var categories = new List<string>();
  34. foreach (Category cat in Category.ApplicableCategories)
  35. {
  36. if (cat.Posts.Count > 0)
  37. {
  38. if (!categories.Contains(cat.Title))
  39. {
  40. AddCategoryToMenu(cat.Title);
  41. categories.Add(cat.Title);
  42. }
  43. }
  44. }
  45. }
  46. private void AddCategoryToMenu(string title)
  47. {
  48. HtmlAnchor a = new HtmlAnchor();
  49. a.InnerHtml = Server.HtmlEncode(title);
  50. a.HRef = string.Format("{0}archive{1}#cat-{2}", Blog.CurrentInstance.RelativeWebRoot, BlogConfig.FileExtension, Utils.RemoveIllegalCharacters(title));
  51. a.Attributes.Add("rel", "directory");
  52. HtmlGenericControl li = new HtmlGenericControl("li");
  53. li.Controls.Add(a);
  54. ulMenu.Controls.Add(li);
  55. }
  56. /// <summary>
  57. /// Sorts the categories.
  58. /// </summary>
  59. /// <param name="categories">The categories.</param>
  60. private SortedDictionary<string, Guid> SortCategories(Dictionary<Guid, string> categories)
  61. {
  62. SortedDictionary<string, Guid> dic = new SortedDictionary<string, Guid>();
  63. foreach (Category cat in Category.Categories)
  64. {
  65. bool postsExist = cat.Posts.FindAll(delegate(Post post)
  66. {
  67. return post.IsVisible;
  68. }).Count > 0;
  69. if (postsExist)
  70. dic.Add(cat.Title, cat.Id);
  71. }
  72. return dic;
  73. }
  74. private void CreateArchive()
  75. {
  76. var categories = new List<string>();
  77. foreach (Category cat in Category.ApplicableCategories)
  78. {
  79. if (cat.Posts.Count > 0)
  80. {
  81. if (!categories.Contains(cat.Title))
  82. {
  83. string name = cat.Title;
  84. List<Post> list = cat.Posts.FindAll(delegate(Post p) { return p.IsVisible; });
  85. HtmlGenericControl h2 = CreateRowHeader(cat, name, list.Count);
  86. phArchive.Controls.Add(h2);
  87. HtmlTable table = CreateTable(name);
  88. foreach (Post post in list)
  89. {
  90. CreateTableRow(table, post);
  91. }
  92. phArchive.Controls.Add(table);
  93. categories.Add(cat.Title);
  94. }
  95. }
  96. }
  97. List<Post> noCatList = Post.ApplicablePosts.FindAll(delegate(Post p) { return p.Categories.Count == 0 && p.IsVisible; });
  98. if (noCatList.Count > 0)
  99. {
  100. string name = Resources.labels.uncategorized;
  101. HtmlGenericControl h2 = CreateRowHeader(null, name, noCatList.Count);
  102. phArchive.Controls.Add(h2);
  103. HtmlTable table = CreateTable(name);
  104. foreach (Post post in noCatList)
  105. {
  106. CreateTableRow(table, post);
  107. }
  108. phArchive.Controls.Add(table);
  109. AddCategoryToMenu(name);
  110. }
  111. }
  112. private static HtmlGenericControl CreateRowHeader(Category cat, string name, int count)
  113. {
  114. HtmlGenericControl h2 = new HtmlGenericControl("h2");
  115. h2.Attributes["id"] = "cat-" + Utils.RemoveIllegalCharacters(name);
  116. if (cat != null)
  117. {
  118. HtmlAnchor feed = new HtmlAnchor();
  119. feed.HRef = cat.FeedRelativeLink;
  120. HtmlImage img = new HtmlImage();
  121. img.Src = Utils.RelativeWebRoot + "Content/images/blog/rssButton.png";
  122. img.Alt = "RSS";
  123. feed.Controls.Add(img);
  124. h2.Controls.Add(feed);
  125. }
  126. Control header = new LiteralControl(name + " (" + count + ")");
  127. h2.Controls.Add(header);
  128. return h2;
  129. }
  130. private static void CreateTableRow(HtmlTable table, Post post)
  131. {
  132. HtmlTableRow row = new HtmlTableRow();
  133. HtmlTableCell date = new HtmlTableCell();
  134. date.InnerHtml = post.DateCreated.ToString("yyyy-MM-dd");
  135. date.Attributes.Add("class", "date");
  136. row.Cells.Add(date);
  137. HtmlTableCell title = new HtmlTableCell();
  138. title.InnerHtml = string.Format("<a href=\"{0}\">{1}</a>", post.RelativeLink, post.Title);
  139. title.Attributes.Add("class", "title");
  140. row.Cells.Add(title);
  141. if (BlogSettings.Instance.IsCommentsEnabled)
  142. {
  143. HtmlTableCell comments = new HtmlTableCell();
  144. if(BlogSettings.Instance.ModerationType == BlogSettings.Moderation.Disqus)
  145. comments.InnerHtml = string.Format("<span><a href=\"{0}#disqus_thread\">{1}</a></span>", post.PermaLink, Resources.labels.comments);
  146. else
  147. comments.InnerHtml = post.ApprovedComments.Count.ToString();
  148. comments.Attributes.Add("class", "comments");
  149. row.Cells.Add(comments);
  150. }
  151. if (BlogSettings.Instance.EnableRating)
  152. {
  153. HtmlTableCell rating = new HtmlTableCell();
  154. rating.InnerHtml = post.Raters == 0 ? "None" : Math.Round(post.Rating, 1).ToString();
  155. rating.Attributes.Add("class", "rating");
  156. row.Cells.Add(rating);
  157. }
  158. table.Rows.Add(row);
  159. }
  160. private HtmlTable CreateTable(string name)
  161. {
  162. HtmlTable table = new HtmlTable();
  163. table.Attributes.Add("summary", name);
  164. HtmlTableRow header = new HtmlTableRow();
  165. HtmlTableCell date = new HtmlTableCell("th");
  166. date.InnerHtml = Utils.Translate("date");
  167. header.Cells.Add(date);
  168. HtmlTableCell title = new HtmlTableCell("th");
  169. title.InnerHtml = Utils.Translate("title");
  170. header.Cells.Add(title);
  171. if (BlogSettings.Instance.IsCommentsEnabled)
  172. {
  173. HtmlTableCell comments = new HtmlTableCell("th");
  174. comments.InnerHtml = Utils.Translate("comments");
  175. comments.Attributes.Add("class", "comments");
  176. header.Cells.Add(comments);
  177. }
  178. if (BlogSettings.Instance.EnableRating)
  179. {
  180. HtmlTableCell rating = new HtmlTableCell("th");
  181. rating.InnerHtml = Utils.Translate("rating");
  182. rating.Attributes.Add("class", "rating");
  183. header.Cells.Add(rating);
  184. }
  185. table.Rows.Add(header);
  186. return table;
  187. }
  188. private void AddTotals()
  189. {
  190. int comments = 0;
  191. int raters = 0;
  192. List<Post> posts = Post.ApplicablePosts.FindAll(delegate(Post p) { return p.IsVisible; });
  193. foreach (Post post in posts)
  194. {
  195. comments += post.ApprovedComments.Count;
  196. raters += post.Raters;
  197. }
  198. ltPosts.Text = posts.Count + " " + Resources.labels.posts.ToLowerInvariant();
  199. if (BlogSettings.Instance.IsCommentsEnabled && BlogSettings.Instance.ModerationType != BlogSettings.Moderation.Disqus)
  200. ltComments.Text = "<span>" + comments + " " + Resources.labels.comments.ToLowerInvariant() + "</span><br />";
  201. if (BlogSettings.Instance.EnableRating)
  202. ltRaters.Text = raters + " " + Resources.labels.raters.ToLowerInvariant();
  203. }
  204. }