protected virtual void LoadComments(CommentItem addedComment) { // Comments enabled and exist? if (CurrentEntry.DisableComments.Checked || ManagerFactory.CommentManagerInstance.GetCommentsCount() == 0) { if (CommentList != null) { CommentList.Visible = false; } } else { if (ListViewComments != null) { CommentItem[] comments = ManagerFactory.CommentManagerInstance.GetEntryComments(); //if a comment has been added but is not coming back yet (i.e. being indexed), fake it if (addedComment != null && comments.Count(comment => comment.ID == addedComment.ID) == 0) { List<CommentItem> newList = new List<CommentItem>(); newList.Add(addedComment); newList.AddRange(comments); comments = newList.ToArray(); } ListViewComments.DataSource = comments; ListViewComments.DataBind(); } } }
/// <summary> /// Get the name of the blog entry a comment was made against /// </summary> /// <param name="comment">The comment to find the blog entry title for</param> /// <returns>The title if found, otherwise an empty string</returns> protected virtual string GetEntryTitleForComment(CommentItem comment) { if (comment != null) return ManagerFactory.EntryManagerInstance.GetBlogEntryByComment(comment).Title.Text; else return string.Empty; }
/// <summary> /// Get the URL of the blog entry a comment was made against /// </summary> /// <param name="comment">The comment to find the blog entry URL for</param> /// <returns>The URL if found, otherwise an empty string</returns> protected virtual string GetEntryUrlForComment(CommentItem comment) { if (comment != null) return LinkManager.GetItemUrl(ManagerFactory.EntryManagerInstance.GetBlogEntryByComment(comment).InnerItem); else return string.Empty; }
public void Process(CreateCommentArgs args) { Assert.IsNotNull(args.Database, "Database cannot be null"); Assert.IsNotNull(args.Comment, "Comment cannot be null"); Assert.IsNotNull(args.EntryID, "Entry ID cannot be null"); var entryItem = args.Database.GetItem(args.EntryID, args.Language); if (entryItem != null) { var blogItem = ManagerFactory.BlogManagerInstance.GetCurrentBlog(entryItem); if (blogItem != null) { var template = args.Database.GetTemplate(blogItem.BlogSettings.CommentTemplateID); string itemName = ItemUtil.ProposeValidItemName("Comment by " + args.Comment.AuthorName + " at " + DateTime.Now.ToString("d")); //need to emulate creation within shell site to ensure workflow is applied to comment using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext("shell"))) { using (new SecurityDisabler()) { var newItem = entryItem.Add(itemName, template); var newComment = new CommentItem(newItem); newComment.BeginEdit(); newComment.Name.Field.Value = args.Comment.AuthorName; newComment.Email.Field.Value = args.Comment.AuthorEmail; newComment.Comment.Field.Value = args.Comment.Text; foreach (var entry in args.Comment.Fields) { newComment.InnerItem[entry.Key] = entry.Value; } newComment.EndEdit(); args.CommentItem = newComment; } } } else { string message = "Failed to find blog for entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'"; Log.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem)); } } else { string message = "Failed to find blog entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'"; Log.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem)); } }
public void AddCommentToEntry() { // Perform this test in master DB as comments get created there var db = Sitecore.Configuration.Factory.GetDatabase("master"); var blogSettings = new BlogHomeItem(m_blog1).BlogSettings; var originalCount = m_entry12.Axes.GetDescendants().Count(i => i.TemplateID == blogSettings.CommentTemplateID); ID commentId = null; try { var comment = new Sitecore.Modules.WeBlog.Model.Comment() { AuthorEmail = "*****@*****.**", AuthorName = "commentor", Text = "My Comment" }; comment.Fields[Sitecore.Modules.WeBlog.Constants.Fields.IpAddress] = "127.0.0.1"; comment.Fields[Sitecore.Modules.WeBlog.Constants.Fields.Website] = "website"; commentId = new Mod.CommentManager().AddCommentToEntry(m_entry12.ID, comment); var childCount = m_entry12.Axes.GetDescendants().Count(i => i.TemplateID == blogSettings.CommentTemplateID); Assert.IsTrue(commentId != ID.Null); Assert.AreEqual(originalCount + 1, childCount); var commentItem = db.GetItem(commentId); Assert.IsNotNull(commentItem); var commentAsComment = new Sitecore.Modules.WeBlog.Items.WeBlog.CommentItem(commentItem); Assert.AreEqual("*****@*****.**", commentAsComment.Email.Text); Assert.AreEqual("commentor", commentAsComment.Name.Text); Assert.AreEqual("127.0.0.1", commentAsComment.IPAddress.Text); Assert.AreEqual("website", commentAsComment.Website.Text); Assert.AreEqual("My Comment", StringUtil.RemoveTags(commentAsComment.Comment.Text)); } finally { var webDb = Sitecore.Configuration.Factory.GetDatabase("web"); if (commentId != (ID)null) { var commentItem = db.GetItem(commentId); if (commentItem != null) { using (new SecurityDisabler()) { commentItem.Delete(); } } commentItem = webDb.GetItem(commentId); if (commentItem != null) { using (new SecurityDisabler()) { commentItem.Delete(); } } } } }
/// <summary> /// Gets the entry item for the current comment. /// </summary> /// <param name="commentItem">The comment item.</param> /// <returns></returns> public EntryItem GetBlogEntryByComment(CommentItem commentItem) { Item[] blogEntry = commentItem.InnerItem.Axes.GetAncestors().Where(item => item.TemplateIsOrBasedOn(Settings.EntryTemplateID)).ToArray(); if (blogEntry.Length > 0) { return new EntryItem(blogEntry.FirstOrDefault()); } return null; }
public void Process(CreateCommentArgs args) { Assert.IsNotNull(args.Database, "Database cannot be null"); Assert.IsNotNull(args.Comment, "Comment cannot be null"); Assert.IsNotNull(args.EntryID, "Entry ID cannot be null"); var entryItem = args.Database.GetItem(args.EntryID, args.Language); if (entryItem != null) { var blogItem = ManagerFactory.BlogManagerInstance.GetCurrentBlog(entryItem); if (blogItem != null) { var template = args.Database.GetTemplate(blogItem.BlogSettings.CommentTemplateID); var itemName = ItemUtil.ProposeValidItemName(string.Format("Comment at {0} by {1}", DateTime.Now.ToString("yyyyMMdd HHmmss"), args.Comment.AuthorName)); // verify the comment item name is unique for this entry var query = "{0}//{1}".FormatWith(ContentHelper.EscapePath(entryItem.Paths.FullPath), itemName); #if FEATURE_FAST_QUERY query = "fast:" + query; #endif var num = 1; var nondupItemName = itemName; while (entryItem.Database.SelectSingleItem(query) != null) { nondupItemName = itemName + " " + num; num++; query = "fast:{0}//{1}".FormatWith(entryItem.Paths.FullPath, nondupItemName); } //need to emulate creation within shell site to ensure workflow is applied to comment using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext(Sitecore.Constants.ShellSiteName))) { using (new SecurityDisabler()) { var newItem = entryItem.Add(nondupItemName, template); var newComment = new CommentItem(newItem); newComment.BeginEdit(); newComment.Name.Field.Value = args.Comment.AuthorName; newComment.Email.Field.Value = args.Comment.AuthorEmail; newComment.Comment.Field.Value = args.Comment.Text; foreach (var entry in args.Comment.Fields) { newComment.InnerItem[entry.Key] = entry.Value; } newComment.EndEdit(); args.CommentItem = newComment; } } } else { var message = "Failed to find blog for entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'"; Log.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem)); } } else { var message = "Failed to find blog entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'"; Log.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem)); } }