- Comments: Allow comments to be added to wikipages.

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4041618
This commit is contained in:
suhacan
2009-11-21 03:53:55 +00:00
parent 6010a9b915
commit 4ada0e0996
10 changed files with 113 additions and 7 deletions

View File

@@ -109,7 +109,7 @@ namespace Orchard.Comments.Controllers {
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection input) {
public ActionResult Create(FormCollection input, string returnUrl) {
var viewModel = new CommentsCreateViewModel();
try {
UpdateModel(viewModel, input.ToValueProvider());
@@ -121,9 +121,13 @@ namespace Orchard.Comments.Controllers {
CommentText = viewModel.CommentText,
Email = viewModel.Email,
SiteName = viewModel.SiteName,
UserName = CurrentUser.UserName ?? "Anonymous"
UserName = CurrentUser.UserName ?? "Anonymous",
CommentedOn = viewModel.CommentedOn
};
_commentService.CreateComment(comment);
if (!String.IsNullOrEmpty(returnUrl)) {
return Redirect(returnUrl);
}
return RedirectToAction("Index");
}
catch (Exception exception) {
@@ -132,9 +136,10 @@ namespace Orchard.Comments.Controllers {
}
}
private static CommentEntry CreateCommentEntry(Comment comment) {
private CommentEntry CreateCommentEntry(Comment comment) {
return new CommentEntry {
Comment = comment,
CommentedOn = _commentService.GetDisplayForCommentedContent(comment.CommentedOn).DisplayText,
IsChecked = false,
};
}

View File

@@ -10,6 +10,7 @@ namespace Orchard.Comments.Models {
public virtual CommentStatus Status { get; set; }
public virtual DateTime CommentDate { get; set; }
public virtual string CommentText { get; set; }
public virtual int CommentedOn { get; set; }
}
public enum CommentStatus {

View File

@@ -23,5 +23,4 @@ namespace Orchard.Comments.Models {
Filters.Add(new TemplateFilterForRecord<CommentSettingsRecord>("CommentSettings"));
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using Orchard.Data;
using Orchard.Models;
using Orchard.Models.Driver;
using Orchard.UI.Models;
namespace Orchard.Comments.Models {
public class HasComments : ContentItemPart {
public HasComments() {
Comments = new List<Comment>();
}
public IEnumerable<Comment> Comments { get; set; }
}
public class HasCommentsHandler : ContentHandler {
private readonly IRepository<Comment> _commentsRepository;
public HasCommentsHandler(IRepository<Comment> commentsRepository) {
_commentsRepository = commentsRepository;
Filters.Add(new ActivatingFilter<HasComments>("wikipage"));
}
protected override void GetDisplays(GetDisplaysContext context) {
if (context.ContentItem.Has<HasComments>() == false) {
return;
}
context.Displays.Add(new ModelTemplate { Model = context.ContentItem.Get<HasComments>(), Prefix = String.Empty });
}
protected override void Loading(LoadContentContext context) {
if (context.ContentItem.Has<HasComments>() == false) {
return;
}
HasComments comments = context.ContentItem.Get<HasComments>();
comments.Comments = _commentsRepository.Fetch(x => x.CommentedOn == context.ContentItem.Id);
}
}
}

View File

@@ -69,6 +69,7 @@
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Models\Comment.cs" />
<Compile Include="Models\CommentSettings.cs" />
<Compile Include="Models\CommentsHandler.cs" />
<Compile Include="Permissions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\CommentService.cs" />
@@ -80,6 +81,7 @@
<Content Include="Package.txt" />
<Content Include="Views\Admin\Index.aspx" />
<Content Include="Views\Admin\Create.aspx" />
<Content Include="Views\Models\DisplayTemplates\HasComments.ascx" />
<Content Include="Views\Models\EditorTemplates\CommentSettingsRecord.ascx" />
<Content Include="Web.config" />
<Content Include="Views\Web.config" />

View File

@@ -3,6 +3,7 @@ using System.Linq;
using Orchard.Comments.Models;
using Orchard.Data;
using Orchard.Logging;
using Orchard.Models;
using Orchard.Security;
using Orchard.Settings;
using Orchard.UI.Notify;
@@ -12,6 +13,7 @@ namespace Orchard.Comments.Services {
IEnumerable<Comment> GetComments();
IEnumerable<Comment> GetComments(CommentStatus status);
Comment GetComment(int id);
IContentItemDisplay GetDisplayForCommentedContent(int id);
void CreateComment(Comment comment);
void MarkCommentAsSpam(int commentId);
void DeleteComment(int commentId);
@@ -20,12 +22,18 @@ namespace Orchard.Comments.Services {
public class CommentService : ICommentService {
private readonly IRepository<Comment> _commentRepository;
private readonly ICommentValidator _commentValidator;
private readonly IContentManager _contentManager;
private readonly IAuthorizer _authorizer;
private readonly INotifier _notifier;
public CommentService(IRepository<Comment> commentRepository, ICommentValidator commentValidator, IAuthorizer authorizer, INotifier notifier) {
public CommentService(IRepository<Comment> commentRepository,
ICommentValidator commentValidator,
IContentManager contentManager,
IAuthorizer authorizer,
INotifier notifier) {
_commentRepository = commentRepository;
_commentValidator = commentValidator;
_contentManager = contentManager;
_authorizer = authorizer;
_notifier = notifier;
Logger = NullLogger.Instance;
@@ -48,6 +56,10 @@ namespace Orchard.Comments.Services {
return _commentRepository.Get(id);
}
public IContentItemDisplay GetDisplayForCommentedContent(int id) {
return _contentManager.Get(id).As<IContentItemDisplay>();
}
public void CreateComment(Comment comment) {
comment.Status = _commentValidator.ValidateComment(comment) ? CommentStatus.Approved : CommentStatus.Spam;
_commentRepository.Create(comment);

View File

@@ -9,5 +9,6 @@ namespace Orchard.Comments.ViewModels {
public string Email { get; set; }
public string SiteName { get; set; }
public string CommentText { get; set; }
public int CommentedOn { get; set; }
}
}

View File

@@ -10,6 +10,7 @@ namespace Orchard.Comments.ViewModels {
public class CommentEntry {
public Comment Comment { get; set; }
public string CommentedOn { get; set; }
public bool IsChecked { get; set; }
}

View File

@@ -7,7 +7,6 @@
<div class="yui-g">
<h2 class="separator">Manage Comments</h2>
<%=Html.ValidationSummary() %>
<%=Html.ActionLink("Add a new comment", "Create", new {}, new {@class="floatRight topSpacer"}) %>
<ol class="horizontal actions floatLeft">
<li>
<label class="floatLeft" for="publishActions"> Actions:</label>
@@ -72,7 +71,7 @@
<% } %>
</td>
<td><%= commentEntry.Comment.CommentDate %></td>
<td>Link to commented item</td>
<td><%=Html.ActionLink(commentEntry.CommentedOn, "Details", new {}, new {@class="floatRight topSpacer"}) %></td>
</tr>
<%
commentIndex++;

View File

@@ -0,0 +1,45 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<HasComments>" %>
<%@ Import Namespace="Orchard.Comments.Models"%>
<h3><%= Model.Comments.Count() %> Comments</h3>
<ol>
<% foreach (var comment in Model.Comments) {%>
<li>
<%= comment.CommentText %>
</li>
<li>
Posted by <%= comment.UserName %> on <%= comment.CommentDate %>
</li>
<hr />
<% } %>
</ol>
<% Html.BeginForm("Create", "Admin", new { area = "Orchard.Comments" }); %>
<%= Html.ValidationSummary() %>
<div class="yui-g">
<h2 class="separator">Add a Comment</h2>
<h3>Information</h3>
<ol>
<li>
<%= Html.Hidden("CommentedOn", Model.ContentItem.Id) %>
<%= Html.Hidden("ReturnUrl", Context.Request.Url) %>
<label for="Name">Name:</label>
<input id="Text1" class="inputText inputTextLarge" name="Name" type="text" />
</li>
<li>
<label for="Email">Email:</label>
<input id="Email" class="inputText inputTextLarge" name="Email" type="text" />
</li>
<li>
<label for="SiteName">SiteName:</label>
<input id="SiteName" class="inputText inputTextLarge" name="SiteName" type="text" />
</li>
<li>
<label for="CommentText">Leave a comment</label>
<textarea id="CommentText" rows="10" cols="30" name="CommentText">
</textarea>
</li>
<li>
<input type="submit" class="button" value="Submit Comment" />
</li>
</ol>
</div>
<% Html.EndForm(); %>