PageRenderTime 61ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/App_Code/CMSModules/Blogs/BlogFunctions.cs

https://bitbucket.org/kudutest2/kenticogit
C# | 215 lines | 140 code | 29 blank | 46 comment | 22 complexity | de9674ed22374b6c0b577096d41b7fa2 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using CMS.SiteProvider;
  11. using CMS.GlobalHelper;
  12. using CMS.Blogs;
  13. using CMS.CMSHelper;
  14. using CMS.PortalEngine;
  15. using TreeNode = CMS.TreeEngine.TreeNode;
  16. /// <summary>
  17. /// Blog functions.
  18. /// </summary>
  19. public class BlogFunctions
  20. {
  21. /// <summary>
  22. /// Returns user name.
  23. /// </summary>
  24. /// <param name="userId">User id</param>
  25. public static string GetUserName(object userId)
  26. {
  27. int id = ValidationHelper.GetInteger(userId, 0);
  28. if (id > 0)
  29. {
  30. string key = "blogPostUserName" + id.ToString();
  31. // Most of the post will be from the same user, store fullname to the request to minimize the DB access
  32. if (RequestStockHelper.Contains(key))
  33. {
  34. return ValidationHelper.GetString(RequestStockHelper.GetItem(key), "");
  35. }
  36. else
  37. {
  38. DataSet ds = UserInfoProvider.GetUsers("UserID = " + id, null, 1, "UserName");
  39. if (!DataHelper.DataSourceIsEmpty(ds))
  40. {
  41. string result = HTMLHelper.HTMLEncode(UserInfoProvider.TrimSitePrefix(ValidationHelper.GetString(ds.Tables[0].Rows[0]["UserName"], "")));
  42. RequestStockHelper.Add(key, result);
  43. return result;
  44. }
  45. }
  46. }
  47. return "";
  48. }
  49. /// <summary>
  50. /// Returns user full name.
  51. /// </summary>
  52. /// <param name="userId">User id</param>
  53. public static string GetUserFullName(object userId)
  54. {
  55. int id = ValidationHelper.GetInteger(userId, 0);
  56. if (id > 0)
  57. {
  58. string key = "TransfUserFullName_" + id.ToString();
  59. // Most of the post will be from the same user, store fullname to the request to minimize the DB access
  60. if (RequestStockHelper.Contains(key))
  61. {
  62. return ValidationHelper.GetString(RequestStockHelper.GetItem(key), "");
  63. }
  64. else
  65. {
  66. DataSet ds = UserInfoProvider.GetUsers("UserID = " + id, null, 1, "FullName");
  67. if (!DataHelper.DataSourceIsEmpty(ds))
  68. {
  69. string result = HTMLHelper.HTMLEncode(ValidationHelper.GetString(ds.Tables[0].Rows[0]["FullName"], ""));
  70. RequestStockHelper.Add(key, result);
  71. return result;
  72. }
  73. }
  74. }
  75. return "";
  76. }
  77. /// <summary>
  78. /// Returns number of comments of given blog.
  79. /// </summary>
  80. /// <param name="postId">Post document id</param>
  81. /// <param name="postAliasPath">Post alias path</param>
  82. public static int GetBlogCommentsCount(object postId, object postAliasPath)
  83. {
  84. return GetBlogCommentsCount(postId, postAliasPath, true);
  85. }
  86. /// <summary>
  87. /// Returns number of comments of given blog.
  88. /// </summary>
  89. /// <param name="postId">Post document id</param>
  90. /// <param name="postAliasPath">Post alias path</param>
  91. /// <param name="includingTrackbacks">Indicates if trackback comments should be included</param>
  92. public static int GetBlogCommentsCount(object postId, object postAliasPath, bool includingTrackbacks)
  93. {
  94. int docId = ValidationHelper.GetInteger(postId, 0);
  95. string aliasPath = ValidationHelper.GetString(postAliasPath, "");
  96. CurrentUserInfo currentUser = CMSContext.CurrentUser;
  97. // There has to be the current site
  98. if (CMSContext.CurrentSite == null)
  99. {
  100. throw new Exception("[BlogFunctions.GetBlogCommentsCount]: There is no current site!");
  101. }
  102. bool isOwner = false;
  103. // Is user authorized to manage comments?
  104. bool selectOnlyPublished = (CMSContext.ViewMode == ViewModeEnum.LiveSite);
  105. TreeNode blogNode = BlogHelper.GetParentBlog(aliasPath, CMSContext.CurrentSiteName, selectOnlyPublished);
  106. if (blogNode != null)
  107. {
  108. isOwner = (currentUser.UserID == ValidationHelper.GetInteger(blogNode.GetValue("NodeOwner"), 0));
  109. }
  110. bool isUserAuthorized = (currentUser.IsAuthorizedPerResource("cms.blog", "Manage") || isOwner || BlogHelper.IsUserBlogModerator(currentUser.UserName, blogNode));
  111. // Get post comments
  112. return BlogCommentInfoProvider.GetPostCommentsCount(docId, !isUserAuthorized, isUserAuthorized, includingTrackbacks);
  113. }
  114. /// <summary>
  115. /// Gets a list of links of tags assigned for the specific document pointing to the page with URL specified
  116. /// </summary>
  117. /// <param name="documentGroupId">ID of the group document tags belong to</param>
  118. /// <param name="documentTags">String containing all the tags related to the document</param>
  119. /// <param name="documentListPage">URL of the page displaying other documents of the specified tag</param>
  120. public static string GetDocumentTags(object documentGroupId, object documentTags, string documentListPage)
  121. {
  122. return GetDocumentTags(documentGroupId, documentTags, null, documentListPage);
  123. }
  124. /// <summary>
  125. /// Gets a list of links of tags assigned for the specific document pointing to the page with URL specified.
  126. /// </summary>
  127. /// <param name="documentGroupId">ID of the group document tags belong to</param>
  128. /// <param name="documentTags">String containing all the tags related to the document</param>
  129. /// <param name="nodeAliasPath">Node alias path</param>
  130. /// <param name="documentListPage">Path or URL of the page displaying other documents of the specified tag</param>
  131. public static string GetDocumentTags(object documentGroupId, object documentTags, object nodeAliasPath, string documentListPage)
  132. {
  133. string result = "";
  134. string tags = ValidationHelper.GetString(documentTags, "");
  135. if (tags.Trim() != "")
  136. {
  137. // If list page was specified make a list of links, otherwise return just list of tags
  138. bool renderLink = !string.IsNullOrEmpty(documentListPage);
  139. string listPageUrl = "";
  140. int groupId = ValidationHelper.GetInteger(documentGroupId, 0);
  141. if (renderLink)
  142. {
  143. // If page specified by URL
  144. if (ValidationHelper.IsURL(documentListPage))
  145. {
  146. // Resolve URL
  147. listPageUrl = URLHelper.ResolveUrl(documentListPage);
  148. }
  149. else
  150. {
  151. // Resolve path
  152. listPageUrl = CMSContext.CurrentResolver.ResolvePath(documentListPage);
  153. }
  154. // Look for group ID of document parent if not supplied
  155. if (groupId == 0)
  156. {
  157. string aliasPath = ValidationHelper.GetString(nodeAliasPath, CMSContext.CurrentPageInfo.NodeAliasPath);
  158. string strGroupId = PageInfoProvider.GetParentProperty(CMSContext.CurrentSiteID,
  159. (String.IsNullOrEmpty(aliasPath) ? CMSContext.CurrentPageInfo.NodeAliasPath : aliasPath),
  160. "DocumentTagGroupID IS NOT NULL", "DocumentTagGroupID");
  161. groupId = ValidationHelper.GetInteger(strGroupId, 0);
  162. }
  163. }
  164. // Go through the specified tags and make a list of them
  165. string[] tagsArr = tags.Split(',');
  166. for (int i = 0; i < tagsArr.Length; i++)
  167. {
  168. tagsArr[i] = tagsArr[i].Replace("\"", "").Trim();
  169. }
  170. Array.Sort(tagsArr);
  171. foreach (string tag in tagsArr)
  172. {
  173. if (renderLink)
  174. {
  175. result += "<a href=\"" + listPageUrl + "?tagname=" + HttpUtility.UrlPathEncode(tag) + "&amp;groupid=" + groupId + "\">" + HTMLHelper.HTMLEncode(tag) + "</a>, ";
  176. }
  177. else
  178. {
  179. result += HTMLHelper.HTMLEncode(tag) + ",";
  180. }
  181. }
  182. result = result.Trim().TrimEnd(',');
  183. }
  184. return result;
  185. }
  186. }