2010-01-28 06:18:39 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using Orchard.Comments.Models;
|
|
|
|
|
using Orchard.Comments.Services;
|
|
|
|
|
using Orchard.Comments.ViewModels;
|
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
using Orchard.Localization;
|
|
|
|
|
using Orchard.UI.Notify;
|
2010-04-22 06:12:23 +08:00
|
|
|
|
using Orchard.Utility.Extensions;
|
2010-01-28 06:18:39 +08:00
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public ActionResult Create(string returnUrl) {
|
2010-03-02 23:01:33 +08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.AddComment, T("Couldn't add comment")))
|
|
|
|
|
return !String.IsNullOrEmpty(returnUrl)
|
|
|
|
|
? Redirect(returnUrl)
|
|
|
|
|
: Redirect("~/");
|
|
|
|
|
|
2010-01-28 06:18:39 +08:00
|
|
|
|
var viewModel = new CommentsCreateViewModel();
|
|
|
|
|
try {
|
2010-04-22 06:12:23 +08:00
|
|
|
|
|
|
|
|
|
// UpdateModel(viewModel);
|
|
|
|
|
|
|
|
|
|
if(!TryUpdateModel(viewModel)) {
|
|
|
|
|
if (Request.Form["Name"].IsNullOrEmptyTrimmed()) {
|
2010-06-13 04:33:35 +08:00
|
|
|
|
_notifier.Error(T("You must provide a Name in order to comment"));
|
2010-04-22 06:12:23 +08:00
|
|
|
|
}
|
|
|
|
|
return Redirect(returnUrl);
|
|
|
|
|
}
|
2010-01-28 06:18:39 +08:00
|
|
|
|
|
|
|
|
|
var context = new CreateCommentContext {
|
|
|
|
|
Author = viewModel.Name,
|
|
|
|
|
CommentText = viewModel.CommentText,
|
|
|
|
|
Email = viewModel.Email,
|
|
|
|
|
SiteName = viewModel.SiteName,
|
2010-02-27 07:29:31 +08:00
|
|
|
|
CommentedOn = viewModel.CommentedOn
|
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-07-23 05:08:31 +08:00
|
|
|
|
if (commentPart.Record.Status == CommentStatus.Pending)
|
2010-03-02 23:01:33 +08:00
|
|
|
|
Services.Notifier.Information(T("Your comment will appear after the site administrator approves it."));
|
2010-02-27 08:03:50 +08:00
|
|
|
|
|
2010-03-02 23:01:33 +08:00
|
|
|
|
return !String.IsNullOrEmpty(returnUrl)
|
|
|
|
|
? Redirect(returnUrl)
|
|
|
|
|
: Redirect("~/");
|
2010-01-28 06:18:39 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception exception) {
|
|
|
|
|
_notifier.Error(T("Creating Comment failed: " + exception.Message));
|
2010-04-22 06:12:23 +08:00
|
|
|
|
// return View(viewModel);
|
|
|
|
|
return Redirect(returnUrl);
|
2010-01-28 06:18:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|