PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Web/Controls/PostViewBase.cs

#
C# | 356 lines | 222 code | 40 blank | 94 comment | 28 complexity | 056410e8938958c4d97b53b4f98cdc3b MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core.Web.Controls
  2. {
  3. using System;
  4. using System.Globalization;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. /// <summary>
  11. /// The PostView.ascx that is located in the themes folder
  12. /// has to inherit from this class.
  13. /// <remarks>
  14. /// It provides the basic functionaly needed to display a post.
  15. /// </remarks>
  16. /// </summary>
  17. public class PostViewBase : UserControl
  18. {
  19. #region Constants and Fields
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="PostViewBase"/> class.
  22. /// </summary>
  23. public PostViewBase()
  24. {
  25. this.Location = ServingLocation.None;
  26. this.ContentBy = ServingContentBy.Unspecified;
  27. }
  28. #endregion
  29. #region Properties
  30. /// <summary>
  31. /// Gets the body of the post. Important: use this instead of Post.Content.
  32. /// </summary>
  33. public string Body
  34. {
  35. get
  36. {
  37. var post = this.Post;
  38. var body = post.Content;
  39. if (this.ShowExcerpt)
  40. {
  41. var link = string.Format(" <a href=\"{0}\">[{1}]</a>", post.RelativeLink, Utils.Translate("more"));
  42. if (!string.IsNullOrEmpty(post.Description))
  43. {
  44. body = post.Description.Replace(Environment.NewLine, "<br />") + link;
  45. }
  46. else
  47. {
  48. body = Utils.StripHtml(body);
  49. if (body.Length > this.DescriptionCharacters && this.DescriptionCharacters > 0)
  50. {
  51. body = string.Format("{0}...{1}", body.Substring(0, this.DescriptionCharacters), link);
  52. }
  53. }
  54. }
  55. var arg = new ServingEventArgs(body, this.Location, this.ContentBy);
  56. Post.OnServing(post, arg);
  57. if (arg.Cancel)
  58. {
  59. if (arg.Location == ServingLocation.SinglePost)
  60. {
  61. this.Response.Redirect("~/error404.aspx", true);
  62. }
  63. else
  64. {
  65. this.Visible = false;
  66. }
  67. }
  68. return arg.Body ?? string.Empty;
  69. }
  70. }
  71. /// <summary>
  72. /// Gets the comment feed link.
  73. /// </summary>
  74. /// <value>The comment feed.</value>
  75. public string CommentFeed
  76. {
  77. get
  78. {
  79. return this.Post.RelativeLink.Replace("/post/", "/post/feed/");
  80. }
  81. }
  82. /// <summary>
  83. /// Gets or sets the criteria by which the content is being served (by tag, category, author, etc).
  84. /// </summary>
  85. public ServingContentBy ContentBy { get; set; }
  86. /// <summary>
  87. /// Gets or sets a value indicating how many characters should be shown of the description.
  88. /// </summary>
  89. public int DescriptionCharacters
  90. {
  91. get
  92. {
  93. int chars = 0;
  94. string url = HttpContext.Current.Request.RawUrl.ToUpperInvariant();
  95. if (url.Contains("/CATEGORY/") || url.Contains("?TAG=/"))
  96. {
  97. if (BlogSettings.Instance.ShowDescriptionInPostListForPostsByTagOrCategory)
  98. {
  99. return BlogSettings.Instance.DescriptionCharactersForPostsByTagOrCategory;
  100. }
  101. }
  102. else
  103. {
  104. if (BlogSettings.Instance.ShowDescriptionInPostList)
  105. {
  106. return BlogSettings.Instance.DescriptionCharacters;
  107. }
  108. }
  109. return chars;
  110. }
  111. }
  112. /// <summary>
  113. /// Gets or sets the index of the post in a list of posts displayed
  114. /// </summary>
  115. public int Index { get; set; }
  116. /// <summary>
  117. /// Gets or sets the location where the serving takes place.
  118. /// </summary>
  119. public ServingLocation Location { get; set; }
  120. /// <summary>
  121. /// Gets or sets the Post object that is displayed through the PostView.ascx control.
  122. /// </summary>
  123. /// <value>The Post object that has to be displayed.</value>
  124. /// <remarks>
  125. ///
  126. /// This was being stored to ViewState, though I can't see any reason why. Storing this
  127. /// locally should improve performance.
  128. ///
  129. /// </remarks>
  130. public Post Post { get; set; }
  131. //public virtual Post Post
  132. //{
  133. // get
  134. // {
  135. // return (Post)this.ViewState["Post"];
  136. // }
  137. // set
  138. // {
  139. // this.ViewState["Post"] = value;
  140. // }
  141. //}
  142. /// <summary>
  143. /// Gets or sets a value indicating whether or not to show the entire post or just the excerpt/description.
  144. /// </summary>
  145. public bool ShowExcerpt { get; set; }
  146. /// <summary>
  147. /// Gets an Edit and Delete link to any authenticated user.
  148. /// </summary>
  149. public virtual string AdminLinks
  150. {
  151. get
  152. {
  153. if (!Security.IsAuthenticated)
  154. {
  155. return string.Empty;
  156. }
  157. else
  158. {
  159. var postRelativeLink = this.Post.RelativeLink;
  160. var sb = new StringBuilder();
  161. if (Security.IsAuthorizedTo(Rights.ModerateComments))
  162. {
  163. if (this.Post.NotApprovedComments.Count > 0 &&
  164. BlogSettings.Instance.ModerationType != BlogSettings.Moderation.Disqus)
  165. {
  166. sb.AppendFormat(
  167. CultureInfo.InvariantCulture,
  168. "<a href=\"{0}\">{1} ({2})</a> | ",
  169. postRelativeLink,
  170. Utils.Translate("unapprovedcomments"),
  171. this.Post.NotApprovedComments.Count);
  172. sb.AppendFormat(
  173. CultureInfo.InvariantCulture,
  174. "<a href=\"{0}\">{1}</a> | ",
  175. postRelativeLink + "?approveallcomments=true",
  176. Utils.Translate("approveallcomments"));
  177. }
  178. }
  179. if (this.Post.CanUserEdit)
  180. {
  181. sb.AppendFormat(
  182. CultureInfo.InvariantCulture,
  183. "<a href=\"{0}\">{1}</a> | ",
  184. Utils.AbsoluteWebRoot + "admin/Posts/Add_entry.aspx?id=" + this.Post.Id,
  185. Utils.Translate("edit"));
  186. }
  187. if (this.Post.CanUserDelete)
  188. {
  189. var confirmDelete = string.Format(
  190. CultureInfo.InvariantCulture,
  191. Utils.Translate("areYouSure"),
  192. Utils.Translate("delete").ToLowerInvariant(),
  193. Utils.Translate("thePost"));
  194. sb.AppendFormat(
  195. CultureInfo.InvariantCulture,
  196. "<a href=\"#\" onclick=\"if (confirm('{2}')) location.href='{0}?deletepost={1}'\">{3}</a> | ",
  197. postRelativeLink,
  198. this.Post.Id,
  199. confirmDelete,
  200. Utils.Translate("delete"));
  201. }
  202. return sb.ToString();
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// Gets the rating.
  208. /// Enable visitors to rate the post.
  209. /// </summary>
  210. public virtual string Rating
  211. {
  212. get
  213. {
  214. if (!BlogSettings.Instance.EnableRating || !Security.IsAuthorizedTo(AuthorizationCheck.HasAll, Rights.ViewRatingsOnPosts, Rights.SubmitRatingsOnPosts))
  215. {
  216. return string.Empty;
  217. }
  218. const string Script = "<div class=\"ratingcontainer\" style=\"visibility:hidden\">{0}|{1}|{2}</div>";
  219. return string.Format(
  220. Script,
  221. this.Post.Id,
  222. this.Post.Raters,
  223. this.Post.Rating.ToString("#.0", CultureInfo.InvariantCulture));
  224. }
  225. }
  226. #endregion
  227. #region Methods
  228. /// <summary>
  229. /// Displays the Post's categories seperated by the specified string.
  230. /// </summary>
  231. /// <param name="separator">
  232. /// The separator.
  233. /// </param>
  234. /// <returns>
  235. /// The category links.
  236. /// </returns>
  237. public virtual string CategoryLinks(string separator)
  238. {
  239. var keywords = new string[this.Post.Categories.Count];
  240. const string Link = "<a href=\"{0}\">{1}</a>";
  241. for (var i = 0; i < this.Post.Categories.Count; i++)
  242. {
  243. var c = Category.GetCategory(this.Post.Categories[i].Id);
  244. if (c != null)
  245. {
  246. keywords[i] = string.Format(CultureInfo.InvariantCulture, Link, c.RelativeLink, c.Title);
  247. }
  248. }
  249. return string.Join(separator, keywords);
  250. }
  251. /// <summary>
  252. /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
  253. /// </summary>
  254. /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
  255. protected override void OnInit(EventArgs e)
  256. {
  257. base.OnInit(e);
  258. if (!this.Post.IsVisible)
  259. {
  260. this.Visible = false;
  261. }
  262. }
  263. /// <summary>
  264. /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
  265. /// </summary>
  266. /// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
  267. /// <remarks>
  268. /// Lets process our .Body content and build up our controls collection
  269. /// inside the 'BodyContent' placeholder.
  270. /// User controls are insterted into the blog in the following format..
  271. /// [UserControl:~/path/usercontrol.ascx]
  272. /// TODO : Expose user control parameters.
  273. /// </remarks>
  274. protected override void OnLoad(EventArgs e)
  275. {
  276. base.OnLoad(e);
  277. var bodyContent = (PlaceHolder)this.FindControl("BodyContent");
  278. if (bodyContent == null)
  279. {
  280. // We have no placeholder so we assume this is an old style <% =Body %> theme and do nothing.
  281. }
  282. else
  283. {
  284. Utils.InjectUserControls(bodyContent, this.Body);
  285. }
  286. }
  287. /// <summary>
  288. /// Displays the Post's tags seperated by the specified string.
  289. /// </summary>
  290. /// <param name="separator">
  291. /// The separator.
  292. /// </param>
  293. /// <returns>
  294. /// The tag links.
  295. /// </returns>
  296. public virtual string TagLinks(string separator)
  297. {
  298. var tags = this.Post.Tags;
  299. if (tags.Count == 0)
  300. {
  301. return null;
  302. }
  303. var tagStrings = new string[tags.Count];
  304. const string Link = "<a href=\"{0}/{1}\" rel=\"tag\">{2}</a>";
  305. var path = Utils.RelativeWebRoot + "?tag=";
  306. for (var i = 0; i < tags.Count; i++)
  307. {
  308. var tag = tags[i];
  309. tagStrings[i] = string.Format(
  310. CultureInfo.InvariantCulture, Link, path, HttpUtility.UrlEncode(tag), HttpUtility.HtmlEncode(tag));
  311. }
  312. return string.Join(separator, tagStrings);
  313. }
  314. #endregion
  315. }
  316. }