From 4ada0e0996751d7820b7ebf41b4eb7ce4b11eaf8 Mon Sep 17 00:00:00 2001 From: suhacan Date: Sat, 21 Nov 2009 03:53:55 +0000 Subject: [PATCH] - Comments: Allow comments to be added to wikipages. --HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4041618 --- .../Controllers/AdminController.cs | 11 +++-- .../Orchard.Comments/Models/Comment.cs | 1 + .../Models/CommentSettings.cs | 1 - .../Models/CommentsHandler.cs | 41 +++++++++++++++++ .../Orchard.Comments/Orchard.Comments.csproj | 2 + .../Services/CommentService.cs | 14 +++++- .../ViewModels/CommentsCreateViewModel.cs | 1 + .../ViewModels/CommentsIndexViewModel.cs | 1 + .../Orchard.Comments/Views/Admin/Index.aspx | 3 +- .../Models/DisplayTemplates/HasComments.ascx | 45 +++++++++++++++++++ 10 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 src/Orchard.Web/Packages/Orchard.Comments/Models/CommentsHandler.cs create mode 100644 src/Orchard.Web/Packages/Orchard.Comments/Views/Models/DisplayTemplates/HasComments.ascx diff --git a/src/Orchard.Web/Packages/Orchard.Comments/Controllers/AdminController.cs b/src/Orchard.Web/Packages/Orchard.Comments/Controllers/AdminController.cs index 5c2cac737..9be1577e3 100644 --- a/src/Orchard.Web/Packages/Orchard.Comments/Controllers/AdminController.cs +++ b/src/Orchard.Web/Packages/Orchard.Comments/Controllers/AdminController.cs @@ -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, }; } diff --git a/src/Orchard.Web/Packages/Orchard.Comments/Models/Comment.cs b/src/Orchard.Web/Packages/Orchard.Comments/Models/Comment.cs index 658e06547..e4100864d 100644 --- a/src/Orchard.Web/Packages/Orchard.Comments/Models/Comment.cs +++ b/src/Orchard.Web/Packages/Orchard.Comments/Models/Comment.cs @@ -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 { diff --git a/src/Orchard.Web/Packages/Orchard.Comments/Models/CommentSettings.cs b/src/Orchard.Web/Packages/Orchard.Comments/Models/CommentSettings.cs index 48967bfc0..de73d99f1 100644 --- a/src/Orchard.Web/Packages/Orchard.Comments/Models/CommentSettings.cs +++ b/src/Orchard.Web/Packages/Orchard.Comments/Models/CommentSettings.cs @@ -23,5 +23,4 @@ namespace Orchard.Comments.Models { Filters.Add(new TemplateFilterForRecord("CommentSettings")); } } - } diff --git a/src/Orchard.Web/Packages/Orchard.Comments/Models/CommentsHandler.cs b/src/Orchard.Web/Packages/Orchard.Comments/Models/CommentsHandler.cs new file mode 100644 index 000000000..48ed959f7 --- /dev/null +++ b/src/Orchard.Web/Packages/Orchard.Comments/Models/CommentsHandler.cs @@ -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(); + } + + public IEnumerable Comments { get; set; } + } + + public class HasCommentsHandler : ContentHandler { + private readonly IRepository _commentsRepository; + + public HasCommentsHandler(IRepository commentsRepository) { + _commentsRepository = commentsRepository; + Filters.Add(new ActivatingFilter("wikipage")); + } + + protected override void GetDisplays(GetDisplaysContext context) { + if (context.ContentItem.Has() == false) { + return; + } + context.Displays.Add(new ModelTemplate { Model = context.ContentItem.Get(), Prefix = String.Empty }); + } + + protected override void Loading(LoadContentContext context) { + if (context.ContentItem.Has() == false) { + return; + } + + HasComments comments = context.ContentItem.Get(); + comments.Comments = _commentsRepository.Fetch(x => x.CommentedOn == context.ContentItem.Id); + } + } +} diff --git a/src/Orchard.Web/Packages/Orchard.Comments/Orchard.Comments.csproj b/src/Orchard.Web/Packages/Orchard.Comments/Orchard.Comments.csproj index d048bb0ae..3abd4db21 100644 --- a/src/Orchard.Web/Packages/Orchard.Comments/Orchard.Comments.csproj +++ b/src/Orchard.Web/Packages/Orchard.Comments/Orchard.Comments.csproj @@ -69,6 +69,7 @@ + @@ -80,6 +81,7 @@ + diff --git a/src/Orchard.Web/Packages/Orchard.Comments/Services/CommentService.cs b/src/Orchard.Web/Packages/Orchard.Comments/Services/CommentService.cs index ac78ed539..702fef4e5 100644 --- a/src/Orchard.Web/Packages/Orchard.Comments/Services/CommentService.cs +++ b/src/Orchard.Web/Packages/Orchard.Comments/Services/CommentService.cs @@ -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 GetComments(); IEnumerable 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 _commentRepository; private readonly ICommentValidator _commentValidator; + private readonly IContentManager _contentManager; private readonly IAuthorizer _authorizer; private readonly INotifier _notifier; - public CommentService(IRepository commentRepository, ICommentValidator commentValidator, IAuthorizer authorizer, INotifier notifier) { + public CommentService(IRepository 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(); + } + public void CreateComment(Comment comment) { comment.Status = _commentValidator.ValidateComment(comment) ? CommentStatus.Approved : CommentStatus.Spam; _commentRepository.Create(comment); diff --git a/src/Orchard.Web/Packages/Orchard.Comments/ViewModels/CommentsCreateViewModel.cs b/src/Orchard.Web/Packages/Orchard.Comments/ViewModels/CommentsCreateViewModel.cs index 21fd1464b..9ec57bd49 100644 --- a/src/Orchard.Web/Packages/Orchard.Comments/ViewModels/CommentsCreateViewModel.cs +++ b/src/Orchard.Web/Packages/Orchard.Comments/ViewModels/CommentsCreateViewModel.cs @@ -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; } } } diff --git a/src/Orchard.Web/Packages/Orchard.Comments/ViewModels/CommentsIndexViewModel.cs b/src/Orchard.Web/Packages/Orchard.Comments/ViewModels/CommentsIndexViewModel.cs index 420607e43..ff9a11dcf 100644 --- a/src/Orchard.Web/Packages/Orchard.Comments/ViewModels/CommentsIndexViewModel.cs +++ b/src/Orchard.Web/Packages/Orchard.Comments/ViewModels/CommentsIndexViewModel.cs @@ -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; } } diff --git a/src/Orchard.Web/Packages/Orchard.Comments/Views/Admin/Index.aspx b/src/Orchard.Web/Packages/Orchard.Comments/Views/Admin/Index.aspx index 703fe93a8..ab93c86bc 100644 --- a/src/Orchard.Web/Packages/Orchard.Comments/Views/Admin/Index.aspx +++ b/src/Orchard.Web/Packages/Orchard.Comments/Views/Admin/Index.aspx @@ -7,7 +7,6 @@

Manage Comments

<%=Html.ValidationSummary() %> - <%=Html.ActionLink("Add a new comment", "Create", new {}, new {@class="floatRight topSpacer"}) %>
  1. @@ -72,7 +71,7 @@ <% } %> <%= commentEntry.Comment.CommentDate %> - Link to commented item + <%=Html.ActionLink(commentEntry.CommentedOn, "Details", new {}, new {@class="floatRight topSpacer"}) %> <% commentIndex++; diff --git a/src/Orchard.Web/Packages/Orchard.Comments/Views/Models/DisplayTemplates/HasComments.ascx b/src/Orchard.Web/Packages/Orchard.Comments/Views/Models/DisplayTemplates/HasComments.ascx new file mode 100644 index 000000000..ff1b1d4dd --- /dev/null +++ b/src/Orchard.Web/Packages/Orchard.Comments/Views/Models/DisplayTemplates/HasComments.ascx @@ -0,0 +1,45 @@ +<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> +<%@ Import Namespace="Orchard.Comments.Models"%> +

    <%= Model.Comments.Count() %> Comments

    +
      + <% foreach (var comment in Model.Comments) {%> +
    1. + <%= comment.CommentText %> +
    2. +
    3. + Posted by <%= comment.UserName %> on <%= comment.CommentDate %> +
    4. +
      + <% } %> +
    +<% Html.BeginForm("Create", "Admin", new { area = "Orchard.Comments" }); %> + <%= Html.ValidationSummary() %> +
    +

    Add a Comment

    +

    Information

    +
      +
    1. + <%= Html.Hidden("CommentedOn", Model.ContentItem.Id) %> + <%= Html.Hidden("ReturnUrl", Context.Request.Url) %> + + +
    2. +
    3. + + +
    4. +
    5. + + +
    6. +
    7. + + +
    8. +
    9. + +
    10. +
    +
    + <% Html.EndForm(); %>