PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 259 lines | 143 code | 28 blank | 88 comment | 7 complexity | 60fc0cf725c71e78e8c48dfca8302c04 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.Linq;
  6. using System.Text;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. /// <summary>
  10. /// Inherit from this class when you are building the
  11. /// commentview.ascx user control in your custom theme.
  12. /// </summary>
  13. /// <remarks>
  14. /// The class exposes a lot of functionality to the custom
  15. /// comment control in the theme folder.
  16. /// </remarks>
  17. public class CommentViewBase : UserControl
  18. {
  19. #region Constants and Fields
  20. /// <summary>
  21. /// The flag image.
  22. /// </summary>
  23. private const string FlagImage =
  24. "<span class=\"adr\"><img src=\"{0}pics/flags/{1}.png\" class=\"country-name flag\" title=\"{2}\" alt=\"{2}\" /></span>";
  25. /*
  26. /// <summary>
  27. /// The link.
  28. /// </summary>
  29. private const string Link = "<a href=\"{0}{1}\" rel=\"nofollow\">{2}</a>";
  30. */
  31. /*
  32. /// <summary>
  33. /// The link regex.
  34. /// </summary>
  35. private static readonly Regex LinkRegex =
  36. new Regex(
  37. "((http://|www\\.)([A-Z0-9.-]{1,})\\.[0-9A-Z?;~&#=\\-_\\./]{2,})",
  38. RegexOptions.Compiled | RegexOptions.IgnoreCase);
  39. */
  40. #endregion
  41. #region Properties
  42. /// <summary>
  43. /// Gets or sets the Comment.
  44. /// </summary>
  45. /// <value>The comment.</value>
  46. public Comment Comment { get; set; }
  47. /// <summary>
  48. /// Gets or sets the Post from which the comment belongs.
  49. /// </summary>
  50. /// <value>The Post object.</value>
  51. public Post Post { get; set; }
  52. /// <summary>
  53. /// Gets the text of the comment.
  54. /// </summary>
  55. /// <value>The comment text.</value>
  56. public string Text
  57. {
  58. get
  59. {
  60. var arg = new ServingEventArgs(this.Comment.Content, ServingLocation.SinglePost);
  61. Comment.OnServing(this.Comment, arg);
  62. if (arg.Cancel)
  63. {
  64. this.Visible = false;
  65. }
  66. var body = arg.Body.Replace("\n", "<br />");
  67. body = body.Replace("\t", "&nbsp;&nbsp;");
  68. body = body.Replace(" ", "&nbsp;&nbsp;");
  69. return body;
  70. }
  71. }
  72. /// <summary>
  73. /// Gets a delete link to visitors that are authenticated
  74. /// using the default membership provider.
  75. /// </summary>
  76. public string AdminLinks
  77. {
  78. get
  79. {
  80. if (Security.IsAuthorizedTo(Rights.ModerateComments))
  81. {
  82. var sb = new StringBuilder();
  83. sb.AppendFormat(" | <a class=\"email\" href=\"mailto:{0}\">{0}</a>", this.Comment.Email);
  84. sb.AppendFormat(
  85. " | <a href=\"http://www.domaintools.com/go/?service=whois&amp;q={0}/\">{0}</a>",
  86. this.Comment.IP);
  87. if (this.Comment.Comments.Count > 0)
  88. {
  89. var confirmDelete = string.Format(
  90. CultureInfo.InvariantCulture,
  91. Utils.Translate("areYouSure"),
  92. Utils.Translate("delete").ToLowerInvariant(),
  93. Utils.Translate("theComment"));
  94. sb.AppendFormat(
  95. " | <a href=\"javascript:void(0);\" onclick=\"if (confirm('{1}?')) location.href='?deletecomment={0}'\">{2}</a>",
  96. this.Comment.Id,
  97. confirmDelete,
  98. Utils.Translate("deleteKeepReplies"));
  99. var confirmRepliesDelete = string.Format(
  100. CultureInfo.InvariantCulture,
  101. Utils.Translate("areYouSure"),
  102. Utils.Translate("delete").ToLowerInvariant(),
  103. Utils.Translate("theComment"));
  104. sb.AppendFormat(
  105. " | <a href=\"javascript:void(0);\" onclick=\"if (confirm('{1}?')) location.href='?deletecommentandchildren={0}'\">{2}</a>",
  106. this.Comment.Id,
  107. confirmRepliesDelete,
  108. Utils.Translate("deletePlusReplies"));
  109. }
  110. else
  111. {
  112. var confirmDelete = string.Format(
  113. CultureInfo.InvariantCulture,
  114. Utils.Translate("areYouSure"),
  115. Utils.Translate("delete").ToLowerInvariant(),
  116. Utils.Translate("theComment"));
  117. sb.AppendFormat(
  118. " | <a href=\"javascript:void(0);\" onclick=\"if (confirm('{1}?')) location.href='?deletecomment={0}'\">{2}</a>",
  119. this.Comment.Id,
  120. confirmDelete,
  121. Utils.Translate("delete"));
  122. }
  123. if (!this.Comment.IsApproved)
  124. {
  125. sb.AppendFormat(
  126. " | <a href=\"?approvecomment={0}\">{1}</a>", this.Comment.Id, Utils.Translate("approve"));
  127. }
  128. return sb.ToString();
  129. }
  130. return string.Empty;
  131. }
  132. }
  133. /// <summary>
  134. /// Gets the flag of the country from which the comment was written.
  135. /// <remarks>
  136. /// If the country hasn't been resolved from the authors IP address or
  137. /// the flag does not exist for that country, nothing is displayed.
  138. /// </remarks>
  139. /// </summary>
  140. public string Flag
  141. {
  142. get
  143. {
  144. if (!string.IsNullOrEmpty(this.Comment.Country))
  145. {
  146. // return "<img src=\"" + Utils.RelativeWebRoot + "pics/flags/" + Comment.Country + ".png\" class=\"country-name flag\" title=\"" + Comment.Country + "\" alt=\"" + Comment.Country + "\" />";
  147. return string.Format(
  148. FlagImage, Utils.RelativeWebRoot, this.Comment.Country, FindCountry(this.Comment.Country));
  149. }
  150. return null;
  151. }
  152. }
  153. /// <summary>
  154. /// Gets a link that lets a user reply to a specific comment
  155. /// </summary>
  156. public string ReplyToLink
  157. {
  158. get
  159. {
  160. return (((BlogSettings.Instance.IsCommentsEnabled && BlogSettings.Instance.IsCommentNestingEnabled) &&
  161. this.Post.HasCommentsEnabled) && this.Comment.IsApproved) &&
  162. (BlogSettings.Instance.DaysCommentsAreEnabled <= 0 ||
  163. this.Post.DateCreated.AddDays(BlogSettings.Instance.DaysCommentsAreEnabled) >= DateTime.Now.Date)
  164. ? string.Format(
  165. "<a href=\"javascript:void(0);\" class=\"reply-to-comment\" onclick=\"BlogEngine.replyToComment('{0}');\">{1}</a>",
  166. this.Comment.Id,
  167. Utils.Translate("replyToThis"))
  168. : string.Empty;
  169. }
  170. }
  171. #endregion
  172. #region Methods
  173. /// <summary>
  174. /// The comment will be processed when invoked, rather than waiting
  175. /// for the Load event to occur.
  176. /// </summary>
  177. public virtual void RenderComment() { }
  178. /// <summary>
  179. /// For styling, indicates if the comment is odd or even.
  180. /// </summary>
  181. public bool IsOdd { get; set; }
  182. /// <summary>
  183. /// Displays the Gravatar image that matches the specified email.
  184. /// </summary>
  185. /// <param name="size">
  186. /// The image size.
  187. /// </param>
  188. /// <returns>
  189. /// The gravatar.
  190. /// </returns>
  191. public string Gravatar(int size)
  192. {
  193. return Avatar.GetAvatarImageTag(size, this.Comment.Email, this.Comment.Website, this.Comment.Avatar, this.Comment.Author);
  194. }
  195. /// <summary>
  196. /// Examines the comment body for any links and turns them
  197. /// automatically into one that can be clicked.
  198. /// </summary>
  199. /// <param name="body">
  200. /// The body string.
  201. /// </param>
  202. /// <returns>
  203. /// The resolve links.
  204. /// </returns>
  205. [Obsolete("Use the Text property instead. This method will be removed in a future version.")]
  206. public string ResolveLinks(string body)
  207. {
  208. return this.Text;
  209. }
  210. /// <summary>
  211. /// Finds country.
  212. /// </summary>
  213. /// <param name="isoCode">
  214. /// The iso code.
  215. /// </param>
  216. /// <returns>
  217. /// The find country.
  218. /// </returns>
  219. private static string FindCountry(string isoCode)
  220. {
  221. foreach (var ri in
  222. CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(ci => new RegionInfo(ci.Name)).Where(
  223. ri => ri.TwoLetterISORegionName.Equals(isoCode, StringComparison.OrdinalIgnoreCase)))
  224. {
  225. return ri.DisplayName;
  226. }
  227. return isoCode;
  228. }
  229. #endregion
  230. }
  231. }