2010-01-28 06:18:39 +08:00
|
|
|
|
using System;
|
2010-11-19 01:31:21 +08:00
|
|
|
|
using System.Linq;
|
2010-01-28 06:18:39 +08:00
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using Orchard.Comments.Models;
|
|
|
|
|
using Orchard.Comments.Services;
|
|
|
|
|
using Orchard.Comments.ViewModels;
|
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
using Orchard.Localization;
|
2010-12-11 06:20:08 +08:00
|
|
|
|
using Orchard.Mvc.Extensions;
|
2010-01-28 06:18:39 +08:00
|
|
|
|
using Orchard.UI.Notify;
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Comments.Controllers {
|
|
|
|
|
public class CommentController : Controller {
|
2010-02-27 08:03:50 +08:00
|
|
|
|
public IOrchardServices Services { get; set; }
|
2010-01-28 06:18:39 +08:00
|
|
|
|
private readonly ICommentService _commentService;
|
|
|
|
|
private readonly INotifier _notifier;
|
|
|
|
|
|
2010-11-05 11:24:48 +08:00
|
|
|
|
public CommentController(IOrchardServices services, ICommentService commentService, INotifier notifier) {
|
2010-02-27 08:03:50 +08:00
|
|
|
|
Services = services;
|
2010-01-28 06:18:39 +08:00
|
|
|
|
_commentService = commentService;
|
|
|
|
|
_notifier = notifier;
|
|
|
|
|
T = NullLocalizer.Instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Localizer T { get; set; }
|
|
|
|
|
|
2010-12-03 07:55:25 +08:00
|
|
|
|
[HttpPost, ValidateInput(false)]
|
2010-01-28 06:18:39 +08:00
|
|
|
|
public ActionResult Create(string returnUrl) {
|
2010-03-02 23:01:33 +08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.AddComment, T("Couldn't add comment")))
|
2010-12-11 06:20:08 +08:00
|
|
|
|
return this.RedirectLocal(returnUrl, "~/");
|
2010-03-02 23:01:33 +08:00
|
|
|
|
|
2010-01-28 06:18:39 +08:00
|
|
|
|
var viewModel = new CommentsCreateViewModel();
|
2010-11-23 07:26:29 +08:00
|
|
|
|
|
|
|
|
|
TryUpdateModel(viewModel);
|
2010-11-19 01:31:21 +08:00
|
|
|
|
|
2010-11-23 07:26:29 +08:00
|
|
|
|
var context = new CreateCommentContext {
|
|
|
|
|
Author = viewModel.Name,
|
|
|
|
|
CommentText = viewModel.CommentText,
|
|
|
|
|
Email = viewModel.Email,
|
|
|
|
|
SiteName = viewModel.SiteName,
|
|
|
|
|
CommentedOn = viewModel.CommentedOn
|
|
|
|
|
};
|
|
|
|
|
|
2010-04-22 06:12:23 +08:00
|
|
|
|
|
2010-11-23 07:26:29 +08:00
|
|
|
|
if (ModelState.IsValid) {
|
2010-11-19 22:03:22 +08:00
|
|
|
|
if (!String.IsNullOrEmpty(context.SiteName) && !context.SiteName.StartsWith("http://") && !context.SiteName.StartsWith("https://")) {
|
2010-11-19 01:31:21 +08:00
|
|
|
|
context.SiteName = "http://" + context.SiteName;
|
2010-04-22 06:12:23 +08:00
|
|
|
|
}
|
2010-01-28 06:18:39 +08:00
|
|
|
|
|
2010-11-05 11:24:48 +08:00
|
|
|
|
CommentPart commentPart = _commentService.CreateComment(context, Services.WorkContext.CurrentSite.As<CommentSettingsPart>().Record.ModerateComments);
|
2010-01-28 06:18:39 +08:00
|
|
|
|
|
2010-12-03 00:20:03 +08:00
|
|
|
|
if (commentPart.Record.Status == CommentStatus.Pending) {
|
|
|
|
|
// if the user who submitted the comment has the right to moderate, don't make this comment moderated
|
|
|
|
|
if (Services.Authorizer.Authorize(Permissions.ManageComments)) {
|
|
|
|
|
commentPart.Record.Status = CommentStatus.Approved;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Services.Notifier.Information(T("Your comment will appear after the site administrator approves it."));
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-28 06:18:39 +08:00
|
|
|
|
}
|
2010-11-19 01:31:21 +08:00
|
|
|
|
else {
|
|
|
|
|
foreach (var error in ModelState.Values.SelectMany(m => m.Errors).Select( e=> e.ErrorMessage)) {
|
|
|
|
|
_notifier.Error(T(error));
|
|
|
|
|
}
|
2010-01-28 06:18:39 +08:00
|
|
|
|
}
|
2010-11-19 01:31:21 +08:00
|
|
|
|
|
2010-11-23 07:26:29 +08:00
|
|
|
|
if(!ModelState.IsValid) {
|
|
|
|
|
TempData["CreateCommentContext.Name"] = context.Author;
|
|
|
|
|
TempData["CreateCommentContext.CommentText"] = context.CommentText;
|
|
|
|
|
TempData["CreateCommentContext.Email"] = context.Email;
|
|
|
|
|
TempData["CreateCommentContext.SiteName"] = context.SiteName;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-11 06:20:08 +08:00
|
|
|
|
return this.RedirectLocal(returnUrl, "~/");
|
2010-01-28 06:18:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|