PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Json/JsonPosts.cs

#
C# | 233 lines | 159 code | 35 blank | 39 comment | 34 complexity | b7963725a88a3116ab46c9f5e7bbf8d6 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core.Json
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. /// <summary>
  8. /// Post type
  9. /// </summary>
  10. public enum PostType
  11. {
  12. /// <summary>
  13. /// All posts
  14. /// </summary>
  15. All,
  16. /// <summary>
  17. /// Drafts
  18. /// </summary>
  19. Draft,
  20. /// <summary>
  21. /// Published posts
  22. /// </summary>
  23. Published
  24. }
  25. /// <summary>
  26. /// List of posts
  27. /// </summary>
  28. public static class JsonPosts
  29. {
  30. /// <summary>
  31. /// The current page.
  32. /// </summary>
  33. private static int currentPage = 1;
  34. /// <summary>
  35. /// The comm cnt.
  36. /// </summary>
  37. private static int postCnt;
  38. /// <summary>
  39. /// Gets post list based on selection for current page
  40. /// </summary>
  41. /// <param name="page">Current page</param>
  42. /// <param name="pageSize">Page Size</param>
  43. /// <param name="postType">Selected post type: draft, published or all</param>
  44. /// <param name="filter">Secondary filter: category, tag, author or all</param>
  45. /// <param name="title">Value selected in secondary filter</param>
  46. /// <returns>List of posts</returns>
  47. public static List<JsonPost> GetPosts(int page, int pageSize, string postType, string filter, string title)
  48. {
  49. pageSize = Math.Max(pageSize, 1);
  50. var cntTo = page * pageSize;
  51. var cntFrom = cntTo - pageSize;
  52. var cnt = 0;
  53. var allPosts = new List<Post>();
  54. var filteredPosts = new List<Post>();
  55. var pagePosts = new List<JsonPost>();
  56. // first filter on selected post type
  57. switch (postType)
  58. {
  59. case "Published":
  60. allPosts = (from p in Post.Posts.ToList() where p.IsPublished == true select p).ToList();
  61. break;
  62. case "Draft":
  63. allPosts = (from p in Post.Posts where p.IsPublished == false select p).ToList();
  64. break;
  65. default:
  66. allPosts = (from p in Post.Posts select p).ToList();
  67. break;
  68. }
  69. // now filter first results on secondary filter
  70. switch (filter)
  71. {
  72. case "Category":
  73. filteredPosts = (from x in allPosts where x.Categories.Contains(Category.GetCategory(new Guid(title))) orderby x.DateCreated descending select x).ToList();
  74. break;
  75. case "Tag":
  76. filteredPosts = (from x in allPosts where x.Tags.Contains(title) orderby x.DateCreated descending select x).ToList();
  77. break;
  78. case "Author":
  79. filteredPosts = (from x in allPosts where x.Author.Equals(title) orderby x.DateCreated descending select x).ToList();
  80. break;
  81. default:
  82. filteredPosts = (from x in allPosts orderby x.DateCreated descending select x).ToList();
  83. break;
  84. }
  85. // convert each post into smaller Json friendly object
  86. foreach (var x in filteredPosts)
  87. {
  88. cnt++;
  89. if (cnt <= cntFrom || cnt > cntTo)
  90. {
  91. continue;
  92. }
  93. string tags = x.Tags.Aggregate("", (current, tag) => current + (tag + ","));
  94. var jp = new JsonPost
  95. {
  96. Id = x.Id,
  97. Author = GetAuthor(x.Author),
  98. Title = string.Format("<a href=\"{0}\">{1}</a>", x.RelativeLink, System.Web.HttpContext.Current.Server.HtmlEncode(x.Title)),
  99. Date = x.DateCreated.ToString("dd MMM yyyy"),
  100. Time = x.DateCreated.ToString("t"),
  101. Categories = GetCategories(x.Categories),
  102. Tags = GetTags(x.Tags),
  103. Comments = GetComments(x.Comments, x.RelativeLink),
  104. IsPublished = x.IsPublished,
  105. CanUserEdit = x.CanUserEdit,
  106. CanUserDelete = x.CanUserDelete
  107. };
  108. pagePosts.Add(jp);
  109. }
  110. currentPage = page;
  111. postCnt = cnt;
  112. return pagePosts;
  113. }
  114. /// <summary>
  115. /// Builds pager control for posts page
  116. /// </summary>
  117. /// <param name="page">Current Page Number</param>
  118. /// <param name="pageSize">Page Size</param>
  119. /// <returns></returns>
  120. public static string GetPager(int page, int pageSize)
  121. {
  122. if (postCnt == 0)
  123. {
  124. return string.Empty;
  125. }
  126. if (page < 1) page = 1;
  127. var prvLnk = string.Empty;
  128. var nxtLnk = string.Empty;
  129. var firstLnk = string.Empty;
  130. var lastLnk = string.Empty;
  131. const string linkFormat = "<a href=\"#\" id=\"{0}\" onclick=\"return LoadPostsForPage('{1}');\" class=\"{0}\"></a>";
  132. pageSize = Math.Max(pageSize, 1);
  133. var pgs = Convert.ToDecimal(postCnt) / Convert.ToDecimal(pageSize);
  134. var p = pgs - (int)pgs;
  135. var lastPage = p > 0 ? (int)pgs + 1 : (int)pgs;
  136. var postTo = page * pageSize;
  137. if (postTo > postCnt) postTo = postCnt;
  138. var currentScope = ((page * pageSize) - (pageSize - 1)).ToString() + " - " + postTo.ToString();
  139. var pageLink = string.Format("Showing <span id=\"PagerCurrentPage\">{0}</span> of {1}", currentScope, postCnt);
  140. if (currentPage > 1)
  141. {
  142. prvLnk = string.Format(linkFormat, "prevLink", page - 1);
  143. firstLnk = string.Format(linkFormat, "firstLink", 1);
  144. }
  145. if (page < lastPage)
  146. {
  147. nxtLnk = string.Format(linkFormat, "nextLink", page + 1);
  148. lastLnk = string.Format(linkFormat, "lastLink", lastPage);
  149. }
  150. return "<div id=\"ListPager\">" + firstLnk + prvLnk + pageLink + nxtLnk + lastLnk + "</div>";
  151. }
  152. #region Private Methods
  153. static string GetCategories(ICollection<Category> categories)
  154. {
  155. if (categories == null || categories.Count == 0)
  156. return string.Empty;
  157. var html = categories.Aggregate("", (current, cat) => current + string.Format("<a href='#' onclick=\"ChangePostFilter('Category','{0}','{1}')\">{1}</a>, ", cat.Id, cat.Title));
  158. return html.Trim().Substring(0, html.Trim().Length - 1);
  159. }
  160. static string GetTags(ICollection<string> tags)
  161. {
  162. if (tags == null || tags.Count == 0)
  163. return string.Empty;
  164. var html = tags.Aggregate("", (current, tag) => current + string.Format("<a href='#' onclick=\"ChangePostFilter('Tag','{0}','')\">{0}</a>, ", tag));
  165. return html.Trim().Substring(0, html.Trim().Length - 1);
  166. }
  167. static string GetAuthor(string author)
  168. {
  169. if (string.IsNullOrEmpty(author))
  170. return string.Empty;
  171. return string.Format("<a href='#' onclick=\"ChangePostFilter('Author','{0}','')\">{0}</a>", author);
  172. }
  173. static string GetComments(ICollection<Comment> comments, string postUrl)
  174. {
  175. int pending, approved;
  176. int spam = comments.Count(c => c.IsSpam == true && c.IsDeleted == false);
  177. string pLink = "<a href=\"{0}\" class=\"comCountPending tipsyhelp\" original-title=\"Pending comments\">({1})</a>";
  178. string aLink = "<a href=\"{0}\" class=\"comCountApproved tipsyhelp\" original-title=\"Approved comments, visible to public\">{1}</a>";
  179. string sLink = "<a href=\"{0}\" class=\"comCountSpam tipsyhelp\" original-title=\"Spam comments\">({1})</a>";
  180. sLink = spam > 0 ? string.Format(sLink, postUrl + "#comments", spam) : "";
  181. if (BlogSettings.Instance.EnableCommentsModeration)
  182. {
  183. pending = comments.Count(c => (c.IsApproved == false && c.IsSpam == false && c.IsDeleted == false));
  184. approved = comments.Count(c => c.IsApproved == true && c.IsDeleted == false);
  185. pLink = pending > 0 ? string.Format(pLink, postUrl + "#comment", pending) : "";
  186. aLink = approved > 0 ? string.Format(aLink, postUrl + "#comment", approved) : "<span class=\"comNone tipsyhelp\" original-title=\"Approved comments, visible to public\">0</span>";
  187. return aLink + pLink + sLink;
  188. }
  189. else
  190. {
  191. approved = comments.Count(c => c.IsSpam == false);
  192. aLink = approved > 0 ? string.Format(aLink, postUrl + "#comment", approved) : "<span class=\"comNone tipsyhelp\" original-title=\"Approved comments, visible to public\">0</span>";
  193. return aLink + sLink;
  194. }
  195. }
  196. #endregion
  197. }
  198. }