PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Json/JsonComments.cs

#
C# | 245 lines | 125 code | 33 blank | 87 comment | 19 complexity | b6fd63938c971d02559aa00004960a0f 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. /// <summary>
  7. /// Comment type.
  8. /// </summary>
  9. public enum CommentType
  10. {
  11. /// <summary>
  12. /// Pending Comment Type
  13. /// </summary>
  14. Pending,
  15. /// <summary>
  16. /// Approved Comment Type
  17. /// </summary>
  18. Approved,
  19. /// <summary>
  20. /// Pingbacks and trackbacks Comment Type
  21. /// </summary>
  22. Pingback,
  23. /// <summary>
  24. /// Spam Comment Type
  25. /// </summary>
  26. Spam
  27. }
  28. /// <summary>
  29. /// List of comments
  30. /// </summary>
  31. public static class JsonComments
  32. {
  33. /// <summary>
  34. /// The current page.
  35. /// </summary>
  36. private static int currentPage = 1;
  37. /// <summary>
  38. /// The comm cnt.
  39. /// </summary>
  40. private static int commCnt;
  41. /// <summary>
  42. /// List of comments based on type for a single page.
  43. /// </summary>
  44. /// <param name="commentType">
  45. /// The comment type.
  46. /// </param>
  47. /// <param name="page">
  48. /// The current page.
  49. /// </param>
  50. /// <returns>
  51. /// A list of JSON comments.
  52. /// </returns>
  53. public static List<JsonComment> GetComments(CommentType commentType, int page)
  54. {
  55. return GetComments(commentType, BlogSettings.Instance.CommentsPerPage, page);
  56. }
  57. /// <summary>
  58. /// List of comments based on type for a single page.
  59. /// </summary>
  60. /// <param name="commentType">
  61. /// The comment type.
  62. /// </param>
  63. /// <param name="pageSize">
  64. /// The number of comments per page.
  65. /// </param>
  66. /// <param name="page">
  67. /// The current page.
  68. /// </param>
  69. /// <returns>
  70. /// A list of JSON comments.
  71. /// </returns>
  72. public static List<JsonComment> GetComments(CommentType commentType, int pageSize, int page)
  73. {
  74. var cntTo = page * pageSize;
  75. var cntFrom = cntTo - pageSize;
  76. var cnt = 0;
  77. var allComments = new List<Comment>();
  78. var pageComments = new List<JsonComment>();
  79. foreach (var p in Post.Posts)
  80. {
  81. switch (commentType)
  82. {
  83. case CommentType.Pending:
  84. allComments.AddRange(p.NotApprovedComments);
  85. break;
  86. case CommentType.Pingback:
  87. allComments.AddRange(p.Pingbacks);
  88. break;
  89. case CommentType.Spam:
  90. allComments.AddRange(p.SpamComments);
  91. break;
  92. default:
  93. allComments.AddRange(p.ApprovedComments);
  94. break;
  95. }
  96. }
  97. allComments.Sort((x, y) => DateTime.Compare(y.DateCreated, x.DateCreated));
  98. // TODO: find a way to better handle deleting last comment on the last page
  99. // below is not working properly; after moving to the next page and deleting
  100. // another comment there, things get nasty
  101. if (allComments.Count > 0 && allComments.Count == cntFrom && page > 1)
  102. {
  103. // removed last comment(s) on the page
  104. // need to shift one page back
  105. //var context = System.Web.HttpContext.Current;
  106. //if (context.Request.Cookies["CommentPagerCurrentPage"] != null)
  107. // context.Request.Cookies["CommentPagerCurrentPage"].Value = (page - 1).ToString();
  108. //return GetComments(commentType, page - 1);
  109. }
  110. foreach (var c in allComments)
  111. {
  112. cnt++;
  113. if (cnt <= cntFrom || cnt > cntTo)
  114. {
  115. continue;
  116. }
  117. pageComments.Add(CreateJsonCommentFromComment(c));
  118. }
  119. currentPage = page;
  120. commCnt = cnt;
  121. return pageComments;
  122. }
  123. /// <summary>
  124. /// Single commnet by ID
  125. /// </summary>
  126. /// <param name="id">
  127. /// Comment id
  128. /// </param>
  129. /// <returns>
  130. /// A JSON Comment
  131. /// </returns>
  132. public static JsonComment GetComment(Guid id)
  133. {
  134. return (from p in Post.Posts
  135. from c in p.AllComments
  136. where c.Id == id
  137. select CreateJsonCommentFromComment(c)).FirstOrDefault();
  138. }
  139. private static JsonComment CreateJsonCommentFromComment(Comment c)
  140. {
  141. var jc = new JsonComment
  142. {
  143. Id = c.Id,
  144. Email = c.Email,
  145. Author = c.Author,
  146. Title = c.Title,
  147. Teaser = c.Teaser,
  148. Website = c.Website == null ? "" : c.Website.ToString(),
  149. AuthorAvatar = c.Avatar,
  150. Content = c.Content,
  151. Ip = c.IP,
  152. Date = c.DateCreated.ToString("dd MMM yyyy"),
  153. Time = c.DateCreated.ToString("t")
  154. };
  155. return jc;
  156. }
  157. /// <summary>
  158. /// Builds pager control for comments page
  159. /// </summary>
  160. /// <param name="page">
  161. /// Current page
  162. /// </param>
  163. /// <param name="srvs">
  164. /// The Srvs..
  165. /// </param>
  166. /// <returns>
  167. /// HTML with next and previous buttons
  168. /// </returns>
  169. public static string GetPager(int page)
  170. {
  171. if (commCnt == 0)
  172. {
  173. return string.Empty;
  174. }
  175. var prvLnk = string.Empty;
  176. var nxtLnk = string.Empty;
  177. var firstLnk = string.Empty;
  178. var lastLnk = string.Empty;
  179. const string linkFormat = "<a href=\"#\" id=\"{0}\" onclick=\"return LoadComments({1});\" class=\"{0}\"></a>";
  180. var PageSize = BlogSettings.Instance.CommentsPerPage;
  181. var pgs = Convert.ToDecimal(commCnt) / Convert.ToDecimal(PageSize);
  182. var p = pgs - (int)pgs;
  183. var lastPage = p > 0 ? (int)pgs + 1 : (int)pgs;
  184. var cntFrom = ((page * PageSize) - (PageSize - 1));
  185. var cntTo = (page * PageSize);
  186. // adjust for the last (or single) page
  187. if (commCnt < cntTo) cntTo = commCnt;
  188. // when last comment on the last page deleted
  189. // this will reset "from" counter
  190. if (cntFrom > cntTo) cntFrom = cntFrom - PageSize;
  191. if (commCnt > 0 && commCnt == cntFrom && page > 1)
  192. {
  193. // removed last comment(s) on the page
  194. // need to shift one page back
  195. //return GetPager(page - 1);
  196. }
  197. var currentScope = cntFrom + " - " + cntTo;
  198. var pageLink = string.Format("<span>Showing {0} of {1}</span>", currentScope, commCnt);
  199. if (currentPage > 1)
  200. {
  201. prvLnk = string.Format(linkFormat, "prevLink", currentPage - 1);
  202. firstLnk = string.Format(linkFormat, "firstLink", 1);
  203. }
  204. if (page < lastPage)
  205. {
  206. nxtLnk = string.Format(linkFormat, "nextLink", currentPage + 1);
  207. lastLnk = string.Format(linkFormat, "lastLink", lastPage);
  208. }
  209. return firstLnk + prvLnk + pageLink + nxtLnk + lastLnk;
  210. }
  211. }
  212. }