mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 10:54:50 +08:00
- Comments: Moderation. Editing/Deleting/Marking as Spam/Closing comments per content item.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4041721
This commit is contained in:
@@ -136,6 +136,131 @@ namespace Orchard.Comments.Controllers {
|
||||
}
|
||||
}
|
||||
|
||||
public ActionResult Details(int id, CommentDetailsOptions options) {
|
||||
// Default options
|
||||
if (options == null)
|
||||
options = new CommentDetailsOptions();
|
||||
|
||||
// Filtering
|
||||
IEnumerable<Comment> comments;
|
||||
try {
|
||||
switch (options.Filter) {
|
||||
case CommentDetailsFilter.All:
|
||||
comments = _commentService.GetCommentsForCommentedContent(id);
|
||||
break;
|
||||
case CommentDetailsFilter.Approved:
|
||||
comments = _commentService.GetCommentsForCommentedContent(id, CommentStatus.Approved);
|
||||
break;
|
||||
case CommentDetailsFilter.Spam:
|
||||
comments = _commentService.GetCommentsForCommentedContent(id, CommentStatus.Spam);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
var entries = comments.Select(comment => CreateCommentEntry(comment)).ToList();
|
||||
var model = new CommentsDetailsViewModel {
|
||||
Comments = entries,
|
||||
Options = options,
|
||||
DisplayNameForCommentedItem = _commentService.GetDisplayForCommentedContent(id).DisplayText,
|
||||
CommentedItemId = id,
|
||||
};
|
||||
return View(model);
|
||||
}
|
||||
catch (Exception exception) {
|
||||
_notifier.Error(T("Listing comments failed: " + exception.Message));
|
||||
return Index(new CommentIndexOptions());
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[FormValueRequired("submit.BulkEdit")]
|
||||
public ActionResult Details(FormCollection input) {
|
||||
var viewModel = new CommentsDetailsViewModel { Comments = new List<CommentEntry>(), Options = new CommentDetailsOptions() };
|
||||
UpdateModel(viewModel, input.ToValueProvider());
|
||||
|
||||
try {
|
||||
IEnumerable<CommentEntry> checkedEntries = viewModel.Comments.Where(c => c.IsChecked);
|
||||
switch (viewModel.Options.BulkAction) {
|
||||
case CommentDetailsBulkAction.None:
|
||||
break;
|
||||
case CommentDetailsBulkAction.MarkAsSpam:
|
||||
if (!_authorizer.Authorize(Permissions.ModerateComment, T("Couldn't moderate comment")))
|
||||
return new HttpUnauthorizedResult();
|
||||
//TODO: Transaction
|
||||
foreach (CommentEntry entry in checkedEntries) {
|
||||
_commentService.MarkCommentAsSpam(entry.Comment.Id);
|
||||
}
|
||||
break;
|
||||
case CommentDetailsBulkAction.Delete:
|
||||
if (!_authorizer.Authorize(Permissions.ModerateComment, T("Couldn't delete comment")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
foreach (CommentEntry entry in checkedEntries) {
|
||||
_commentService.DeleteComment(entry.Comment.Id);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
catch (Exception exception) {
|
||||
_notifier.Error(T("Editing comments failed: " + exception.Message));
|
||||
return Details(viewModel.CommentedItemId, viewModel.Options);
|
||||
}
|
||||
|
||||
return RedirectToAction("Details", new { viewModel.CommentedItemId, viewModel.Options });
|
||||
}
|
||||
|
||||
public ActionResult Close(int commentedItemId) {
|
||||
try {
|
||||
if (!_authorizer.Authorize(Permissions.CloseComment, T("Couldn't close comments")))
|
||||
return new HttpUnauthorizedResult();
|
||||
_commentService.CloseCommentsForCommentedContent(commentedItemId);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
catch (Exception exception) {
|
||||
_notifier.Error(T("Closing Comments failed: " + exception.Message));
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
}
|
||||
|
||||
public ActionResult Edit(int id) {
|
||||
try {
|
||||
Comment comment = _commentService.GetComment(id);
|
||||
var viewModel = new CommentsEditViewModel {
|
||||
CommentText = comment.CommentText,
|
||||
Email = comment.Email,
|
||||
Id = comment.Id,
|
||||
Name = comment.Author,
|
||||
SiteName = comment.SiteName
|
||||
};
|
||||
return View(viewModel);
|
||||
|
||||
}
|
||||
catch (Exception exception) {
|
||||
_notifier.Error(T("Editing comment failed: " + exception.Message));
|
||||
return Index(new CommentIndexOptions());
|
||||
}
|
||||
}
|
||||
|
||||
[AcceptVerbs(HttpVerbs.Post)]
|
||||
public ActionResult Edit(FormCollection input) {
|
||||
var viewModel = new CommentsEditViewModel();
|
||||
try {
|
||||
UpdateModel(viewModel, input.ToValueProvider());
|
||||
if (!_authorizer.Authorize(Permissions.ModerateComment, T("Couldn't edit comment")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
_commentService.UpdateComment(viewModel.Id, viewModel.Name, viewModel.Email, viewModel.SiteName, viewModel.CommentText);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
catch (Exception exception) {
|
||||
_notifier.Error(T("Editing Comment failed: " + exception.Message));
|
||||
return View(viewModel);
|
||||
}
|
||||
}
|
||||
|
||||
private CommentEntry CreateCommentEntry(Comment comment) {
|
||||
return new CommentEntry {
|
||||
Comment = comment,
|
||||
|
@@ -13,6 +13,11 @@ namespace Orchard.Comments.Models {
|
||||
public virtual int CommentedOn { get; set; }
|
||||
}
|
||||
|
||||
public class ClosedComments {
|
||||
public virtual int Id { get; set; }
|
||||
public virtual int ContentItemId { get; set; }
|
||||
}
|
||||
|
||||
public enum CommentStatus {
|
||||
Approved,
|
||||
Spam
|
||||
|
@@ -12,13 +12,16 @@ namespace Orchard.Comments.Models {
|
||||
}
|
||||
|
||||
public IEnumerable<Comment> Comments { get; set; }
|
||||
public bool Closed { get; set; }
|
||||
}
|
||||
|
||||
public class HasCommentsProvider : ContentProvider {
|
||||
private readonly IRepository<Comment> _commentsRepository;
|
||||
private readonly IRepository<ClosedComments> _closedCommentsRepository;
|
||||
|
||||
public HasCommentsProvider(IRepository<Comment> commentsRepository) {
|
||||
public HasCommentsProvider(IRepository<Comment> commentsRepository, IRepository<ClosedComments> closedCommentsRepository) {
|
||||
_commentsRepository = commentsRepository;
|
||||
_closedCommentsRepository = closedCommentsRepository;
|
||||
Filters.Add(new ActivatingFilter<HasComments>("wikipage"));
|
||||
}
|
||||
|
||||
@@ -35,7 +38,10 @@ namespace Orchard.Comments.Models {
|
||||
}
|
||||
|
||||
HasComments comments = context.ContentItem.Get<HasComments>();
|
||||
comments.Comments = _commentsRepository.Fetch(x => x.CommentedOn == context.ContentItem.Id);
|
||||
comments.Comments = _commentsRepository.Fetch(x => x.CommentedOn == context.ContentItem.Id && x.Status == CommentStatus.Approved);
|
||||
if (_closedCommentsRepository.Get(x => x.ContentItemId == context.ContentItem.Id) != null) {
|
||||
comments.Closed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -75,10 +75,14 @@
|
||||
<Compile Include="Services\CommentService.cs" />
|
||||
<Compile Include="Services\CommentValidator.cs" />
|
||||
<Compile Include="ViewModels\CommentsCreateViewModel.cs" />
|
||||
<Compile Include="ViewModels\CommentsDetailsViewModel.cs" />
|
||||
<Compile Include="ViewModels\CommentsEditViewModel.cs" />
|
||||
<Compile Include="ViewModels\CommentsIndexViewModel.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Package.txt" />
|
||||
<Content Include="Views\Admin\Details.aspx" />
|
||||
<Content Include="Views\Admin\Edit.aspx" />
|
||||
<Content Include="Views\Admin\Index.aspx" />
|
||||
<Content Include="Views\Admin\Create.aspx" />
|
||||
<Content Include="Views\Models\DisplayTemplates\HasComments.ascx" />
|
||||
|
@@ -12,26 +12,33 @@ namespace Orchard.Comments.Services {
|
||||
public interface ICommentService : IDependency {
|
||||
IEnumerable<Comment> GetComments();
|
||||
IEnumerable<Comment> GetComments(CommentStatus status);
|
||||
IEnumerable<Comment> GetCommentsForCommentedContent(int id);
|
||||
IEnumerable<Comment> GetCommentsForCommentedContent(int id, CommentStatus status);
|
||||
Comment GetComment(int id);
|
||||
IContentDisplayInfo GetDisplayForCommentedContent(int id);
|
||||
void CreateComment(Comment comment);
|
||||
void UpdateComment(int id, string name, string email, string siteName, string commentText);
|
||||
void MarkCommentAsSpam(int commentId);
|
||||
void DeleteComment(int commentId);
|
||||
void CloseCommentsForCommentedContent(int id);
|
||||
}
|
||||
|
||||
public class CommentService : ICommentService {
|
||||
private readonly IRepository<Comment> _commentRepository;
|
||||
private readonly IRepository<ClosedComments> _closedCommentsRepository;
|
||||
private readonly ICommentValidator _commentValidator;
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly IAuthorizer _authorizer;
|
||||
private readonly INotifier _notifier;
|
||||
|
||||
public CommentService(IRepository<Comment> commentRepository,
|
||||
IRepository<ClosedComments> closedCommentsRepository,
|
||||
ICommentValidator commentValidator,
|
||||
IContentManager contentManager,
|
||||
IAuthorizer authorizer,
|
||||
INotifier notifier) {
|
||||
_commentRepository = commentRepository;
|
||||
_closedCommentsRepository = closedCommentsRepository;
|
||||
_commentValidator = commentValidator;
|
||||
_contentManager = contentManager;
|
||||
_authorizer = authorizer;
|
||||
@@ -52,6 +59,14 @@ namespace Orchard.Comments.Services {
|
||||
return from comment in _commentRepository.Table.ToList() where comment.Status == status select comment;
|
||||
}
|
||||
|
||||
public IEnumerable<Comment> GetCommentsForCommentedContent(int id) {
|
||||
return from comment in _commentRepository.Table.ToList() where comment.CommentedOn == id select comment;
|
||||
}
|
||||
|
||||
public IEnumerable<Comment> GetCommentsForCommentedContent(int id, CommentStatus status) {
|
||||
return from comment in _commentRepository.Table.ToList() where comment.CommentedOn == id && comment.Status == status select comment;
|
||||
}
|
||||
|
||||
public Comment GetComment(int id) {
|
||||
return _commentRepository.Get(id);
|
||||
}
|
||||
@@ -65,6 +80,14 @@ namespace Orchard.Comments.Services {
|
||||
_commentRepository.Create(comment);
|
||||
}
|
||||
|
||||
public void UpdateComment(int id, string name, string email, string siteName, string commentText) {
|
||||
Comment comment = GetComment(id);
|
||||
comment.Author = name;
|
||||
comment.Email = email;
|
||||
comment.SiteName = siteName;
|
||||
comment.CommentText = commentText;
|
||||
}
|
||||
|
||||
public void MarkCommentAsSpam(int commentId) {
|
||||
Comment comment = GetComment(commentId);
|
||||
comment.Status = CommentStatus.Spam;
|
||||
@@ -74,6 +97,10 @@ namespace Orchard.Comments.Services {
|
||||
_commentRepository.Delete(GetComment(commentId));
|
||||
}
|
||||
|
||||
public void CloseCommentsForCommentedContent(int id) {
|
||||
_closedCommentsRepository.Create(new ClosedComments { ContentItemId = id });
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,28 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.Comments.ViewModels {
|
||||
public class CommentsDetailsViewModel : AdminViewModel {
|
||||
public IList<CommentEntry> Comments { get; set; }
|
||||
public CommentDetailsOptions Options { get; set; }
|
||||
public string DisplayNameForCommentedItem { get; set; }
|
||||
public int CommentedItemId { get; set; }
|
||||
}
|
||||
|
||||
public class CommentDetailsOptions {
|
||||
public CommentDetailsFilter Filter { get; set; }
|
||||
public CommentDetailsBulkAction BulkAction { get; set; }
|
||||
}
|
||||
|
||||
public enum CommentDetailsBulkAction {
|
||||
None,
|
||||
Delete,
|
||||
MarkAsSpam,
|
||||
}
|
||||
|
||||
public enum CommentDetailsFilter {
|
||||
All,
|
||||
Approved,
|
||||
Spam,
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.Comments.ViewModels {
|
||||
public class CommentsEditViewModel : AdminViewModel {
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string SiteName { get; set; }
|
||||
public string CommentText { get; set; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CommentsDetailsViewModel>" %>
|
||||
<%@ Import Namespace="Orchard.Comments.Models"%>
|
||||
<%@ Import Namespace="Orchard.Comments.ViewModels"%>
|
||||
<%@ Import Namespace="Orchard.Mvc.Html"%>
|
||||
<% Html.Include("Header"); %>
|
||||
<% Html.BeginForm(); %>
|
||||
<div class="yui-g">
|
||||
<h2 class="separator">Comments for <%= Model.DisplayNameForCommentedItem %></h2>
|
||||
<%=Html.ValidationSummary() %>
|
||||
<ol class="horizontal actions floatLeft">
|
||||
<li>
|
||||
<label class="floatLeft" for="publishActions"> Actions:</label>
|
||||
<select id="publishActions" name="<%=Html.NameOf(m => m.Options.BulkAction)%>">
|
||||
<%=Html.SelectOption(Model.Options.BulkAction, CommentDetailsBulkAction.None, "Choose action...")%>
|
||||
<%=Html.SelectOption(Model.Options.BulkAction, CommentDetailsBulkAction.Delete, "Delete")%>
|
||||
<%=Html.SelectOption(Model.Options.BulkAction, CommentDetailsBulkAction.MarkAsSpam, "Mark as Spam")%>
|
||||
</select>
|
||||
</li>
|
||||
<li>
|
||||
<input class="button roundCorners" type="submit" name="submit.BulkEdit" value="Apply" />
|
||||
</li>
|
||||
</ol>
|
||||
<ol class="horizontal actions">
|
||||
<li>
|
||||
<label class="floatLeft" for="filterResults"></label>
|
||||
<select id="filterResults" name="<%=Html.NameOf(m => m.Options.Filter)%>">
|
||||
<%=Html.SelectOption(Model.Options.Filter, CommentDetailsFilter.All, "All Comments")%>
|
||||
<%=Html.SelectOption(Model.Options.Filter, CommentDetailsFilter.Approved, "Approved Comments")%>
|
||||
<%=Html.SelectOption(Model.Options.Filter, CommentDetailsFilter.Spam, "Spam")%>
|
||||
</select>
|
||||
</li>
|
||||
<li>
|
||||
<input class="button roundCorners" type="submit" name="submit.Filter" value="Filter"/>
|
||||
</li>
|
||||
</ol>
|
||||
<table id="Table1" cellspacing="0" class="roundCorners clearLayout" summary="This is a table of the comments in your application">
|
||||
<colgroup>
|
||||
<col id="Col1" />
|
||||
<col id="Col2" />
|
||||
<col id="Col3" />
|
||||
<col id="Col4" />
|
||||
<col id="Col5" />
|
||||
<col id="Col6" />
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col"><%--<input type="checkbox" value="" />--%></th>
|
||||
<th scope="col">Status</th>
|
||||
<th scope="col">Author</th>
|
||||
<th scope="col">Comment</th>
|
||||
<th scope="col">Date</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<%
|
||||
int commentIndex = 0;
|
||||
foreach (var commentEntry in Model.Comments) {
|
||||
%>
|
||||
<tr>
|
||||
<td>
|
||||
<input type="hidden" value="<%=Model.Comments[commentIndex].Comment.Id%>" name="<%=Html.NameOf(m => m.Comments[commentIndex].Comment.Id)%>"/>
|
||||
<input type="checkbox" value="true" name="<%=Html.NameOf(m => m.Comments[commentIndex].IsChecked)%>"/>
|
||||
<input type="hidden" value="<%= Model.DisplayNameForCommentedItem %>" name="DisplayNameForCommentedtem" />
|
||||
<input type="hidden" value="<%= Model.CommentedItemId %>" name="CommentedItemId" />
|
||||
</td>
|
||||
<td><% if (commentEntry.Comment.Status == CommentStatus.Spam) {%> Spam <% } %>
|
||||
<% else {%> Approved <% } %>
|
||||
</td>
|
||||
<td><%= commentEntry.Comment.UserName %></td>
|
||||
<td>
|
||||
<% if (commentEntry.Comment.CommentText != null) {%>
|
||||
<%= commentEntry.Comment.CommentText.Length > 23 ? commentEntry.Comment.CommentText.Substring(0, 24) : commentEntry.Comment.CommentText %> ...
|
||||
<% } %>
|
||||
</td>
|
||||
<td><%= commentEntry.Comment.CommentDate %></td>
|
||||
<td><%=Html.ActionLink("Edit", "Edit", new {commentEntry.Comment.Id}, new {@class="floatRight topSpacer"}) %>
|
||||
</td>
|
||||
</tr>
|
||||
<%
|
||||
commentIndex++;
|
||||
} %>
|
||||
</table>
|
||||
<li class="clearLayout">
|
||||
<%=Html.ActionLink("Close Comments", "Close", new {commentedItemId = Model.CommentedItemId}, new {@class="floatRight topSpacer"}) %>
|
||||
</li>
|
||||
</div>
|
||||
|
||||
<% Html.EndForm(); %>
|
||||
<% Html.Include("Footer"); %>
|
@@ -0,0 +1,36 @@
|
||||
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CommentsEditViewModel>" %>
|
||||
<%@ Import Namespace="Orchard.Comments.ViewModels"%>
|
||||
<%@ Import Namespace="Orchard.Mvc.Html" %>
|
||||
<% Html.Include("Header"); %>
|
||||
<% Html.BeginForm(); %>
|
||||
<%= Html.ValidationSummary() %>
|
||||
<div class="yui-g">
|
||||
<h2 class="separator">Add a Comment</h2>
|
||||
<h3>Information</h3>
|
||||
<ol>
|
||||
<li>
|
||||
<label for="Name">Name:</label>
|
||||
<input id="CommentId" name="Id" type="hidden" value="<%=Model.Id %>" />
|
||||
<input id="Text1" class="inputText inputTextLarge" name="Name" type="text" value="<%= Model.Name %>" />
|
||||
</li>
|
||||
<li>
|
||||
<label for="Email">Email:</label>
|
||||
<input id="Email" class="inputText inputTextLarge" name="Email" type="text" value="<%= Model.Email%>" />
|
||||
</li>
|
||||
<li>
|
||||
<label for="SiteName">SiteName:</label>
|
||||
<input id="SiteName" class="inputText inputTextLarge" name="SiteName" type="text" value="<%= Model.SiteName %>" />
|
||||
</li>
|
||||
<li>
|
||||
<label for="CommentText">Leave a comment</label>
|
||||
<textarea id="CommentText" rows="10" cols="30" name="CommentText">
|
||||
<%= Model.CommentText %>
|
||||
</textarea>
|
||||
</li>
|
||||
<li>
|
||||
<input type="submit" class="button" value="Save" />
|
||||
</li>
|
||||
</ol>
|
||||
</div>
|
||||
<% Html.EndForm(); %>
|
||||
<% Html.Include("Footer"); %>
|
@@ -67,11 +67,11 @@
|
||||
<td><%= commentEntry.Comment.UserName %></td>
|
||||
<td>
|
||||
<% if (commentEntry.Comment.CommentText != null) {%>
|
||||
<%= commentEntry.Comment.CommentText.Length > 23 ? commentEntry.Comment.CommentText.Substring(0, 24) : commentEntry.Comment.CommentText %>
|
||||
<%= commentEntry.Comment.CommentText.Length > 23 ? commentEntry.Comment.CommentText.Substring(0, 24) : commentEntry.Comment.CommentText %> ...
|
||||
<% } %>
|
||||
</td>
|
||||
<td><%= commentEntry.Comment.CommentDate %></td>
|
||||
<td><%=Html.ActionLink(commentEntry.CommentedOn, "Details", new {}, new {@class="floatRight topSpacer"}) %></td>
|
||||
<td><%=Html.ActionLink(commentEntry.CommentedOn, "Details", new {id = commentEntry.Comment.CommentedOn}, new {@class="floatRight topSpacer"}) %></td>
|
||||
</tr>
|
||||
<%
|
||||
commentIndex++;
|
||||
|
@@ -12,6 +12,10 @@
|
||||
<hr />
|
||||
<% } %>
|
||||
</ol>
|
||||
<% if (Model.Closed) { %>
|
||||
<p>Comments have been disabled for this content.</p>
|
||||
<% } %>
|
||||
<% else { %>
|
||||
<% Html.BeginForm("Create", "Admin", new { area = "Orchard.Comments" }); %>
|
||||
<%= Html.ValidationSummary() %>
|
||||
<div class="yui-g">
|
||||
@@ -43,3 +47,4 @@
|
||||
</ol>
|
||||
</div>
|
||||
<% Html.EndForm(); %>
|
||||
<% } %>
|
Reference in New Issue
Block a user