2010-06-08 07:02:54 +08:00
|
|
|
|
using System;
|
2010-07-17 05:27:27 +08:00
|
|
|
|
using System.Collections.Generic;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
using System.Linq;
|
2010-07-17 05:27:27 +08:00
|
|
|
|
using System.Reflection;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using System.Web.Routing;
|
|
|
|
|
using Orchard.ContentManagement;
|
2010-07-15 06:31:43 +08:00
|
|
|
|
using Orchard.ContentManagement.Aspects;
|
2010-06-25 07:11:10 +08:00
|
|
|
|
using Orchard.ContentManagement.MetaData;
|
2010-07-17 07:37:12 +08:00
|
|
|
|
using Orchard.ContentManagement.Records;
|
|
|
|
|
using Orchard.Core.Common.Models;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
using Orchard.Core.Contents.ViewModels;
|
|
|
|
|
using Orchard.Data;
|
|
|
|
|
using Orchard.Localization;
|
|
|
|
|
using Orchard.Logging;
|
2010-06-22 18:36:04 +08:00
|
|
|
|
using Orchard.Mvc.Results;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
using Orchard.Mvc.ViewModels;
|
|
|
|
|
using Orchard.UI.Notify;
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Core.Contents.Controllers {
|
|
|
|
|
[ValidateInput(false)]
|
|
|
|
|
public class AdminController : Controller, IUpdateModel {
|
|
|
|
|
private readonly INotifier _notifier;
|
|
|
|
|
private readonly IContentManager _contentManager;
|
2010-06-25 07:11:10 +08:00
|
|
|
|
private readonly IContentDefinitionManager _contentDefinitionManager;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
private readonly ITransactionManager _transactionManager;
|
|
|
|
|
|
|
|
|
|
public AdminController(
|
2010-06-16 19:22:51 +08:00
|
|
|
|
IOrchardServices orchardServices,
|
2010-06-08 07:02:54 +08:00
|
|
|
|
INotifier notifier,
|
|
|
|
|
IContentManager contentManager,
|
2010-06-25 07:11:10 +08:00
|
|
|
|
IContentDefinitionManager contentDefinitionManager,
|
2010-07-14 01:14:46 +08:00
|
|
|
|
ITransactionManager transactionManager) {
|
2010-06-16 19:22:51 +08:00
|
|
|
|
Services = orchardServices;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
_notifier = notifier;
|
|
|
|
|
_contentManager = contentManager;
|
2010-06-25 07:11:10 +08:00
|
|
|
|
_contentDefinitionManager = contentDefinitionManager;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
_transactionManager = transactionManager;
|
|
|
|
|
T = NullLocalizer.Instance;
|
|
|
|
|
Logger = NullLogger.Instance;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-16 19:22:51 +08:00
|
|
|
|
public IOrchardServices Services { get; private set; }
|
2010-06-08 07:02:54 +08:00
|
|
|
|
public Localizer T { get; set; }
|
|
|
|
|
public ILogger Logger { get; set; }
|
|
|
|
|
|
2010-06-16 19:22:51 +08:00
|
|
|
|
public ActionResult List(ListContentsViewModel model) {
|
2010-07-17 07:37:12 +08:00
|
|
|
|
if (model.ContainerId != null && _contentManager.GetLatest((int)model.ContainerId) == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
2010-06-08 07:02:54 +08:00
|
|
|
|
const int pageSize = 20;
|
|
|
|
|
var skip = (Math.Max(model.Page ?? 0, 1) - 1) * pageSize;
|
|
|
|
|
|
2010-07-09 01:12:34 +08:00
|
|
|
|
var query = _contentManager.Query(VersionOptions.Latest, _contentDefinitionManager.ListTypeDefinitions().Select(ctd => ctd.Name).ToArray());
|
2010-06-08 07:02:54 +08:00
|
|
|
|
|
2010-07-09 01:12:34 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(model.TypeName)) {
|
|
|
|
|
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(model.TypeName);
|
|
|
|
|
if (contentTypeDefinition == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
|
|
model.TypeDisplayName = !string.IsNullOrWhiteSpace(contentTypeDefinition.DisplayName)
|
|
|
|
|
? contentTypeDefinition.DisplayName
|
|
|
|
|
: contentTypeDefinition.Name;
|
|
|
|
|
query = query.ForType(model.TypeName);
|
2010-06-08 07:02:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-17 07:37:12 +08:00
|
|
|
|
if (model.ContainerId != null)
|
|
|
|
|
query = query.Join<CommonRecord>().Where(cr => cr.Container.Id == model.ContainerId);
|
|
|
|
|
|
2010-06-08 07:02:54 +08:00
|
|
|
|
var contentItems = query.Slice(skip, pageSize);
|
|
|
|
|
|
|
|
|
|
model.Entries = contentItems.Select(BuildEntry).ToList();
|
|
|
|
|
|
|
|
|
|
return View("List", model);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-17 05:27:27 +08:00
|
|
|
|
[HttpPost, ActionName("List")]
|
|
|
|
|
[FormValueRequired("submit.BulkEdit")]
|
|
|
|
|
public ActionResult ListPOST(ContentOptions options, IEnumerable<int> itemIds) {
|
|
|
|
|
switch (options.BulkAction) {
|
|
|
|
|
case ContentsBulkAction.None:
|
|
|
|
|
break;
|
|
|
|
|
case ContentsBulkAction.PublishNow:
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Couldn't publish selected content.")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
foreach (var item in itemIds.Select(itemId => _contentManager.GetLatest(itemId))) {
|
|
|
|
|
_contentManager.Publish(item);
|
|
|
|
|
Services.ContentManager.Flush();
|
|
|
|
|
}
|
|
|
|
|
Services.Notifier.Information(T("Content successfully published."));
|
|
|
|
|
break;
|
|
|
|
|
case ContentsBulkAction.Unpublish:
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Couldn't unpublish selected content.")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
foreach (var item in itemIds.Select(itemId => _contentManager.GetLatest(itemId))) {
|
|
|
|
|
_contentManager.Unpublish(item);
|
|
|
|
|
Services.ContentManager.Flush();
|
|
|
|
|
}
|
|
|
|
|
Services.Notifier.Information(T("Content successfully unpublished."));
|
|
|
|
|
break;
|
|
|
|
|
case ContentsBulkAction.Remove:
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Couldn't delete selected content.")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
foreach (var item in itemIds.Select(itemId => _contentManager.GetLatest(itemId))) {
|
|
|
|
|
_contentManager.Remove(item);
|
|
|
|
|
Services.ContentManager.Flush();
|
|
|
|
|
}
|
|
|
|
|
Services.Notifier.Information(T("Content successfully removed."));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo: persist filter & order
|
|
|
|
|
return RedirectToAction("List");
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-16 19:22:51 +08:00
|
|
|
|
private ListContentsViewModel.Entry BuildEntry(ContentItem contentItem) {
|
|
|
|
|
var entry = new ListContentsViewModel.Entry {
|
2010-06-08 07:02:54 +08:00
|
|
|
|
ContentItem = contentItem,
|
|
|
|
|
ContentItemMetadata = _contentManager.GetItemMetadata(contentItem),
|
2010-07-09 06:47:20 +08:00
|
|
|
|
ViewModel = _contentManager.BuildDisplayModel(contentItem, "SummaryAdmin"),
|
2010-06-08 07:02:54 +08:00
|
|
|
|
};
|
|
|
|
|
if (string.IsNullOrEmpty(entry.ContentItemMetadata.DisplayText)) {
|
|
|
|
|
entry.ContentItemMetadata.DisplayText = string.Format("[{0}#{1}]", contentItem.ContentType, contentItem.Id);
|
|
|
|
|
}
|
|
|
|
|
if (entry.ContentItemMetadata.EditorRouteValues == null) {
|
|
|
|
|
entry.ContentItemMetadata.EditorRouteValues = new RouteValueDictionary {
|
|
|
|
|
{"Area", "Contents"},
|
|
|
|
|
{"Controller", "Admin"},
|
|
|
|
|
{"Action", "Edit"},
|
|
|
|
|
{"Id", contentItem.Id}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ActionResult CreatableTypeList() {
|
2010-06-16 19:22:51 +08:00
|
|
|
|
var model = new ListContentTypesViewModel {
|
2010-06-25 07:11:10 +08:00
|
|
|
|
Types = _contentDefinitionManager.ListTypeDefinitions()
|
2010-06-08 07:02:54 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View("CreatableTypeList", model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Create(string id) {
|
|
|
|
|
if (string.IsNullOrEmpty(id))
|
|
|
|
|
return CreatableTypeList();
|
|
|
|
|
|
|
|
|
|
var contentItem = _contentManager.New(id);
|
|
|
|
|
var model = new CreateItemViewModel {
|
|
|
|
|
Id = id,
|
|
|
|
|
Content = _contentManager.BuildEditorModel(contentItem)
|
|
|
|
|
};
|
|
|
|
|
PrepareEditorViewModel(model.Content);
|
|
|
|
|
return View("Create", model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2010-07-14 01:14:46 +08:00
|
|
|
|
public ActionResult Create(CreateItemViewModel model) {
|
2010-06-16 19:22:51 +08:00
|
|
|
|
//todo: need to integrate permissions into generic content management
|
2010-06-08 07:02:54 +08:00
|
|
|
|
var contentItem = _contentManager.New(model.Id);
|
2010-07-17 02:40:22 +08:00
|
|
|
|
_contentManager.Create(contentItem, VersionOptions.Draft);
|
2010-06-08 07:02:54 +08:00
|
|
|
|
model.Content = _contentManager.UpdateEditorModel(contentItem, this);
|
2010-07-14 23:54:19 +08:00
|
|
|
|
|
2010-06-08 07:02:54 +08:00
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
_transactionManager.Cancel();
|
|
|
|
|
PrepareEditorViewModel(model.Content);
|
|
|
|
|
return View("Create", model);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-17 02:40:22 +08:00
|
|
|
|
if (!contentItem.Has<IPublishingControlAspect>()) {
|
2010-07-14 23:54:19 +08:00
|
|
|
|
_contentManager.Publish(contentItem);
|
2010-07-17 02:40:22 +08:00
|
|
|
|
_notifier.Information(T("Created content item"));
|
|
|
|
|
}
|
2010-07-14 23:54:19 +08:00
|
|
|
|
|
2010-06-08 07:02:54 +08:00
|
|
|
|
return RedirectToAction("Edit", new RouteValueDictionary { { "Id", contentItem.Id } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Edit(int id) {
|
|
|
|
|
var contentItem = _contentManager.Get(id, VersionOptions.Latest);
|
2010-07-14 02:30:39 +08:00
|
|
|
|
|
|
|
|
|
if (contentItem == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
2010-06-08 07:02:54 +08:00
|
|
|
|
var model = new EditItemViewModel {
|
|
|
|
|
Id = id,
|
|
|
|
|
Content = _contentManager.BuildEditorModel(contentItem)
|
|
|
|
|
};
|
2010-07-14 02:30:39 +08:00
|
|
|
|
|
2010-06-08 07:02:54 +08:00
|
|
|
|
PrepareEditorViewModel(model.Content);
|
2010-07-14 02:30:39 +08:00
|
|
|
|
|
2010-06-08 07:02:54 +08:00
|
|
|
|
return View("Edit", model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2010-07-14 01:14:46 +08:00
|
|
|
|
public ActionResult Edit(EditItemViewModel model) {
|
2010-06-08 07:02:54 +08:00
|
|
|
|
var contentItem = _contentManager.Get(model.Id, VersionOptions.DraftRequired);
|
2010-07-14 02:30:39 +08:00
|
|
|
|
|
|
|
|
|
if (contentItem == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
2010-06-08 07:02:54 +08:00
|
|
|
|
model.Content = _contentManager.UpdateEditorModel(contentItem, this);
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
_transactionManager.Cancel();
|
|
|
|
|
PrepareEditorViewModel(model.Content);
|
|
|
|
|
return View("Edit", model);
|
|
|
|
|
}
|
2010-07-13 17:52:02 +08:00
|
|
|
|
|
2010-07-15 06:31:43 +08:00
|
|
|
|
//need to go about this differently - to know when to publish (IPlublishableAspect ?)
|
|
|
|
|
if (!contentItem.Has<IPublishingControlAspect>())
|
|
|
|
|
_contentManager.Publish(contentItem);
|
|
|
|
|
|
2010-06-08 07:02:54 +08:00
|
|
|
|
return RedirectToAction("Edit", new RouteValueDictionary { { "Id", contentItem.Id } });
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-09 01:12:34 +08:00
|
|
|
|
[HttpPost, ActionName("Remove")]
|
|
|
|
|
public ActionResult RemovePOST(int id, string returnUrl) {
|
2010-07-13 17:52:02 +08:00
|
|
|
|
var contentItem = _contentManager.Get(id, VersionOptions.Latest);
|
2010-07-09 01:12:34 +08:00
|
|
|
|
if (contentItem != null)
|
|
|
|
|
_contentManager.Remove(contentItem);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(returnUrl))
|
|
|
|
|
return Redirect(returnUrl);
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("List");
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-16 15:49:24 +08:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public ActionResult Publish(int id) {
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Couldn't publish content")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
var contentItem = _contentManager.GetLatest(id);
|
|
|
|
|
if (contentItem == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
|
|
_contentManager.Publish(contentItem);
|
|
|
|
|
Services.ContentManager.Flush();
|
|
|
|
|
Services.Notifier.Information(T("{0} successfully published.", contentItem.TypeDefinition.DisplayName));
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("List");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public ActionResult Unpublish(int id) {
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Couldn't unpublish content")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
var contentItem = _contentManager.GetLatest(id);
|
|
|
|
|
if (contentItem == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
|
|
_contentManager.Unpublish(contentItem);
|
|
|
|
|
Services.ContentManager.Flush();
|
|
|
|
|
Services.Notifier.Information(T("{0} successfully unpublished.", contentItem.TypeDefinition.DisplayName));
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("List");
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-10 05:14:17 +08:00
|
|
|
|
private static void PrepareEditorViewModel(ContentItemViewModel itemViewModel) {
|
2010-06-08 07:02:54 +08:00
|
|
|
|
if (string.IsNullOrEmpty(itemViewModel.TemplateName)) {
|
|
|
|
|
itemViewModel.TemplateName = "Items/Contents.Item";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
|
|
|
|
|
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
|
2010-06-08 07:07:57 +08:00
|
|
|
|
ModelState.AddModelError(key, errorMessage.ToString());
|
2010-06-08 07:02:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-17 05:27:27 +08:00
|
|
|
|
|
|
|
|
|
public class FormValueRequiredAttribute : ActionMethodSelectorAttribute {
|
|
|
|
|
private readonly string _submitButtonName;
|
|
|
|
|
|
|
|
|
|
public FormValueRequiredAttribute(string submitButtonName) {
|
|
|
|
|
_submitButtonName = submitButtonName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) {
|
|
|
|
|
var value = controllerContext.HttpContext.Request.Form[_submitButtonName];
|
|
|
|
|
return !string.IsNullOrEmpty(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-06-08 07:02:54 +08:00
|
|
|
|
}
|