PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/User controls/PostList.ascx.cs

#
C# | 222 lines | 141 code | 38 blank | 43 comment | 19 complexity | 295226c46e45f9904712f3ceba49514f MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace UserControls
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Web.UI;
  6. using BlogEngine.Core;
  7. using BlogEngine.Core.Web.Controls;
  8. /// <summary>
  9. /// The post list user control.
  10. /// </summary>
  11. public partial class PostList : UserControl
  12. {
  13. #region Constants and Fields
  14. /// <summary>
  15. /// The posts.
  16. /// </summary>
  17. private List<IPublishable> publishables;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref = "PostList" /> class.
  20. /// </summary>
  21. public PostList()
  22. {
  23. this.ContentBy = ServingContentBy.AllContent;
  24. }
  25. #endregion
  26. #region Properties
  27. /// <summary>
  28. /// Gets or sets the criteria by which the content is being served (by tag, category, author, etc).
  29. /// </summary>
  30. public ServingContentBy ContentBy { get; set; }
  31. /// <summary>
  32. /// Gets or sets the list of posts to display.
  33. /// </summary>
  34. public List<IPublishable> Posts
  35. {
  36. get
  37. {
  38. return this.publishables;
  39. }
  40. set
  41. {
  42. this.publishables = value;
  43. this.pager1.Posts = value;
  44. }
  45. }
  46. #endregion
  47. #region Methods
  48. protected override void OnInit(EventArgs e)
  49. {
  50. base.OnInit(e);
  51. if (!Security.IsAuthorizedTo(Rights.ViewPublicPosts))
  52. {
  53. this.Visible = false;
  54. }
  55. }
  56. /// <summary>
  57. /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
  58. /// </summary>
  59. /// <param name="e">
  60. /// The <see cref="T:System.EventArgs"/> object that contains the event data.
  61. /// </param>
  62. protected override void OnLoad(EventArgs e)
  63. {
  64. base.OnLoad(e);
  65. if (this.Page.IsCallback)
  66. {
  67. return;
  68. }
  69. this.BindPosts();
  70. this.InitPaging();
  71. }
  72. /// <summary>
  73. /// Binds the list of posts to individual postview.ascx controls
  74. /// from the current theme.
  75. /// </summary>
  76. private void BindPosts()
  77. {
  78. if (this.Posts == null) {
  79. // no posts provided, load all posts by default
  80. Posts = Post.Posts.ConvertAll(new Converter<Post, IPublishable>(delegate(Post p) { return p as IPublishable; }));
  81. }
  82. if (this.Posts.Count == 0)
  83. {
  84. this.hlPrev.Visible = false;
  85. return;
  86. }
  87. var visiblePosts = this.Posts.FindAll(p => p.IsVisible);
  88. var count = Math.Min(BlogSettings.Instance.PostsPerPage, visiblePosts.Count);
  89. var page = this.GetPageIndex();
  90. var index = page * count;
  91. var stop = count;
  92. if (index + count > visiblePosts.Count)
  93. {
  94. stop = visiblePosts.Count - index;
  95. }
  96. if (stop < 0 || stop + index > visiblePosts.Count)
  97. {
  98. this.hlPrev.Visible = false;
  99. this.hlNext.Visible = false;
  100. return;
  101. }
  102. var path = string.Format("{0}themes/{1}/PostView.ascx", Utils.ApplicationRelativeWebRoot, BlogSettings.Instance.GetThemeWithAdjustments(this.Request.QueryString["theme"]));
  103. var counter = 0;
  104. foreach (Post post in visiblePosts.GetRange(index, stop))
  105. {
  106. if (counter == stop)
  107. {
  108. break;
  109. }
  110. var postView = (PostViewBase)this.LoadControl(path);
  111. postView.ShowExcerpt = ShowExcerpt();
  112. postView.Post = post;
  113. postView.ID = post.Id.ToString().Replace("-", string.Empty);
  114. postView.Location = ServingLocation.PostList;
  115. postView.Index = counter;
  116. this.posts.Controls.Add(postView);
  117. counter++;
  118. }
  119. if (index + stop == this.Posts.Count)
  120. {
  121. this.hlPrev.Visible = false;
  122. }
  123. }
  124. /// <summary>
  125. /// Retrieves the current page index based on the QueryString.
  126. /// </summary>
  127. /// <returns>
  128. /// The get page index.
  129. /// </returns>
  130. private int GetPageIndex()
  131. {
  132. int index;
  133. if (int.TryParse(this.Request.QueryString["page"], out index))
  134. {
  135. index--;
  136. }
  137. return index;
  138. }
  139. /// <summary>
  140. /// Initializes the Next and Previous links
  141. /// </summary>
  142. private void InitPaging()
  143. {
  144. var path = this.Request.RawUrl.Replace("Default.aspx", string.Empty);
  145. path = path.Contains("?")
  146. ? (path.Contains("page=")
  147. ? path.Substring(0, path.IndexOf("page="))
  148. : string.Format("{0}&", path))
  149. : string.Format("{0}?", path);
  150. var page = this.GetPageIndex();
  151. var url = string.Format("{0}page={{0}}", path);
  152. // if (page != 1)
  153. this.hlNext.HRef = string.Format(url, page);
  154. // else
  155. // hlNext.HRef = path.Replace("?", string.Empty);
  156. this.hlPrev.HRef = string.Format(url, page + 2);
  157. if (page == 0)
  158. {
  159. this.hlNext.Visible = false;
  160. }
  161. else
  162. {
  163. ((BlogBasePage)this.Page).AddGenericLink("next", "Next page", this.hlNext.HRef);
  164. }
  165. if (this.hlPrev.Visible)
  166. {
  167. ((BlogBasePage)this.Page).AddGenericLink("prev", "Previous page", string.Format(url, page + 2));
  168. }
  169. }
  170. /// <summary>
  171. /// Whether or not to show the entire post or just the excerpt/description
  172. /// in the post list
  173. /// </summary>
  174. /// <returns></returns>
  175. private bool ShowExcerpt()
  176. {
  177. string url = this.Request.RawUrl.ToUpperInvariant();
  178. bool tagOrCategory = url.Contains("/CATEGORY/") || url.Contains("?TAG=/");
  179. return BlogSettings.Instance.ShowDescriptionInPostList ||
  180. (BlogSettings.Instance.ShowDescriptionInPostListForPostsByTagOrCategory && tagOrCategory);
  181. }
  182. #endregion
  183. }
  184. }