using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using ScrewTurn.Wiki.PluginFramework; using System.Text; namespace ScrewTurn.Wiki { public partial class PageDiscussion : System.Web.UI.UserControl { private PageInfo currentPage; private bool canPostMessages = false; private bool canManageDiscussion = false; protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { RenderMessages(); } } /// /// Gets or sets the current page. /// public PageInfo CurrentPage { get { return currentPage; } set { currentPage = value; } } /// /// Gets or sets a value indicating whether the current user can post messages. /// public bool CanPostMessages { get { return canPostMessages; } set { canPostMessages = value; } } /// /// Gets or sets a value indicating whether the current user can manage the discussion. /// public bool CanManageDiscussion { get { return canManageDiscussion; } set { canManageDiscussion = value; } } /// /// Renders the messages. /// private void RenderMessages() { if(currentPage == null) return; Message[] messages = Pages.GetPageMessages(currentPage); if(messages.Length == 0) { lblMessages.Text = "" + Properties.Messages.NoMessages + ""; return; } else { lblMessages.Text = PrintDiscussion(); } } /// /// Prints the discussion tree. /// /// The formatted page discussion. private string PrintDiscussion() { List messages = new List(Pages.GetPageMessages(currentPage)); if(messages.Count == 0) { return "" + Properties.Messages.NoMessages + ""; } else { StringBuilder sb = new StringBuilder(10000); PrintSubtree(messages, null, sb); return sb.ToString(); } } /// /// Prints a subtree of Messages depth-first. /// /// The Messages. /// The parent message, or null. /// The output . private void PrintSubtree(IEnumerable messages, Message parent, StringBuilder sb) { foreach(Message msg in messages) { sb.Append(@""); PrintMessage(msg, parent, sb); PrintSubtree(msg.Replies, msg, sb); sb.Append(""); } } /// /// Prints a message. /// /// The message to print. /// The parent message, or null. /// The output . private void PrintMessage(Message message, Message parent, StringBuilder sb) { // Print header sb.Append(@"
"); //sb.AppendFormat(@"", message.ID); sb.AppendFormat(@" ", Tools.GetMessageIdForAnchor(message.DateTime)); if(!currentPage.Provider.ReadOnly) { // Print reply/edit/delete buttons only if provider is not read-only sb.Append(@"
"); if(canPostMessages) { sb.Append(@""); sb.Append(Properties.Messages.Reply); sb.Append(""); } // If current user is the author of the message or is an admin, print the edit hyperLink // A message can be edited only if the user is authenticated - anonymous users cannot edit their messages if(SessionFacade.LoginKey != null && ((message.Username == SessionFacade.CurrentUsername && canPostMessages) || canManageDiscussion)) { sb.Append(@" "); sb.Append(Properties.Messages.Edit); sb.Append(""); } // If the current user is an admin, print the delete hyperLink if(SessionFacade.LoginKey != null && canManageDiscussion) { sb.Append(@" "); sb.Append(Properties.Messages.Delete); sb.Append(""); } sb.Append("
"); } // Print subject if(message.Subject.Length > 0) { sb.Append(@""); sb.Append(FormattingPipeline.PrepareTitle(message.Subject, false, FormattingContext.MessageBody, currentPage)); sb.Append(""); } // Print message date/time sb.Append(@""); sb.Append(Preferences.AlignWithTimezone(message.DateTime).ToString(Settings.DateTimeFormat)); sb.Append(" "); sb.Append(Properties.Messages.By); sb.Append(" "); sb.Append(Users.UserLink(message.Username)); sb.Append(""); sb.Append("
"); // Print body sb.Append(@"
"); sb.Append(FormattingPipeline.FormatWithPhase3(FormattingPipeline.FormatWithPhase1And2(message.Body, false, FormattingContext.MessageBody, currentPage), FormattingContext.MessageBody, currentPage)); sb.Append("
"); } } }