2010-06-08 07:02:54 +08:00
|
|
|
|
using System;
|
2010-06-25 04:30:57 +08:00
|
|
|
|
using System.Collections.Generic;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using System.Web.Routing;
|
|
|
|
|
using Orchard.ContentManagement;
|
2010-06-25 07:11:10 +08:00
|
|
|
|
using Orchard.ContentManagement.MetaData;
|
2010-06-22 18:36:04 +08:00
|
|
|
|
using Orchard.ContentManagement.MetaData.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-06-08 07:02:54 +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
|
|
|
|
|
|
|
|
|
#region Content
|
2010-06-22 18:36:04 +08:00
|
|
|
|
|
2010-06-16 19:22:51 +08:00
|
|
|
|
public ActionResult List(ListContentsViewModel model) {
|
2010-06-08 07:02:54 +08:00
|
|
|
|
const int pageSize = 20;
|
|
|
|
|
var skip = (Math.Max(model.Page ?? 0, 1) - 1) * pageSize;
|
|
|
|
|
|
|
|
|
|
var query = _contentManager.Query(VersionOptions.Latest);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.Id)) {
|
|
|
|
|
query = query.ForType(model.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var contentItems = query.Slice(skip, pageSize);
|
|
|
|
|
|
|
|
|
|
model.Entries = contentItems.Select(BuildEntry).ToList();
|
|
|
|
|
|
|
|
|
|
return View("List", model);
|
|
|
|
|
}
|
|
|
|
|
|
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),
|
|
|
|
|
ViewModel = _contentManager.BuildDisplayModel(contentItem, "List"),
|
|
|
|
|
};
|
|
|
|
|
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]
|
|
|
|
|
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);
|
|
|
|
|
model.Content = _contentManager.UpdateEditorModel(contentItem, this);
|
|
|
|
|
if (ModelState.IsValid) {
|
|
|
|
|
_contentManager.Create(contentItem, VersionOptions.Draft);
|
|
|
|
|
model.Content = _contentManager.UpdateEditorModel(contentItem, this);
|
|
|
|
|
}
|
|
|
|
|
if (ModelState.IsValid) {
|
|
|
|
|
_contentManager.Publish(contentItem);
|
|
|
|
|
}
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
_transactionManager.Cancel();
|
|
|
|
|
PrepareEditorViewModel(model.Content);
|
|
|
|
|
return View("Create", model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_notifier.Information(T("Created content item"));
|
|
|
|
|
return RedirectToAction("Edit", new RouteValueDictionary { { "Id", contentItem.Id } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Edit(int id) {
|
|
|
|
|
var contentItem = _contentManager.Get(id, VersionOptions.Latest);
|
|
|
|
|
var model = new EditItemViewModel {
|
|
|
|
|
Id = id,
|
|
|
|
|
Content = _contentManager.BuildEditorModel(contentItem)
|
|
|
|
|
};
|
|
|
|
|
PrepareEditorViewModel(model.Content);
|
|
|
|
|
return View("Edit", model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public ActionResult Edit(EditItemViewModel model) {
|
|
|
|
|
var contentItem = _contentManager.Get(model.Id, VersionOptions.DraftRequired);
|
|
|
|
|
model.Content = _contentManager.UpdateEditorModel(contentItem, this);
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
_transactionManager.Cancel();
|
|
|
|
|
PrepareEditorViewModel(model.Content);
|
|
|
|
|
return View("Edit", model);
|
|
|
|
|
}
|
|
|
|
|
_contentManager.Publish(contentItem);
|
|
|
|
|
return RedirectToAction("Edit", new RouteValueDictionary { { "Id", contentItem.Id } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PrepareEditorViewModel(ContentItemViewModel itemViewModel) {
|
|
|
|
|
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-06-24 05:53:41 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
2010-06-08 07:02:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|