2010-06-08 07:02:54 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using System.Web.Routing;
|
|
|
|
|
using Orchard.ContentManagement;
|
2010-06-22 18:36:04 +08:00
|
|
|
|
using Orchard.ContentManagement.MetaData.Models;
|
2010-06-16 19:22:51 +08:00
|
|
|
|
using Orchard.Core.Contents.Services;
|
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;
|
2010-06-16 19:22:51 +08:00
|
|
|
|
private readonly IContentDefinitionService _contentDefinitionService;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
private readonly IContentManager _contentManager;
|
|
|
|
|
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,
|
2010-06-16 19:22:51 +08:00
|
|
|
|
IContentDefinitionService contentDefinitionService,
|
2010-06-08 07:02:54 +08:00
|
|
|
|
IContentManager contentManager,
|
|
|
|
|
ITransactionManager transactionManager) {
|
2010-06-16 19:22:51 +08:00
|
|
|
|
Services = orchardServices;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
_notifier = notifier;
|
2010-06-16 19:22:51 +08:00
|
|
|
|
_contentDefinitionService = contentDefinitionService;
|
2010-06-08 07:02:54 +08:00
|
|
|
|
_contentManager = contentManager;
|
|
|
|
|
_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 Types
|
|
|
|
|
|
2010-06-08 07:02:54 +08:00
|
|
|
|
public ActionResult Index() {
|
|
|
|
|
return Types();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Types() {
|
2010-06-16 19:22:51 +08:00
|
|
|
|
return View("Types", new ListContentTypesViewModel {
|
|
|
|
|
Types = _contentDefinitionService.GetTypeDefinitions()
|
2010-06-08 07:02:54 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-22 18:36:04 +08:00
|
|
|
|
public ActionResult CreateType() {
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to create a content type.")))
|
2010-06-16 19:22:51 +08:00
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-06-22 18:36:04 +08:00
|
|
|
|
return View(new CreateTypeViewModel());
|
2010-06-16 19:22:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("CreateType")]
|
|
|
|
|
public ActionResult CreateTypePOST(CreateTypeViewModel viewModel) {
|
2010-06-22 18:36:04 +08:00
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to create a content type.")))
|
2010-06-16 19:22:51 +08:00
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
2010-06-22 18:36:04 +08:00
|
|
|
|
var model = new ContentTypeDefinition("");
|
2010-06-16 19:22:51 +08:00
|
|
|
|
TryUpdateModel(model);
|
|
|
|
|
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
Services.TransactionManager.Cancel();
|
|
|
|
|
return View(viewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_contentDefinitionService.AddTypeDefinition(model);
|
|
|
|
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-22 18:36:04 +08:00
|
|
|
|
public ActionResult EditType(string id) {
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id);
|
|
|
|
|
|
|
|
|
|
if (contentTypeDefinition == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
|
|
return View(new EditTypeViewModel(contentTypeDefinition));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("EditType")]
|
|
|
|
|
public ActionResult EditTypePOST(string id) {
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id);
|
|
|
|
|
|
|
|
|
|
if (contentTypeDefinition == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
|
|
var viewModel = new EditTypeViewModel();
|
|
|
|
|
TryUpdateModel(viewModel);
|
|
|
|
|
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return EditType(id);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-23 06:30:15 +08:00
|
|
|
|
var contentTypeDefinitionParts = viewModel.Parts.Select(
|
|
|
|
|
p => new ContentTypeDefinition.Part(
|
|
|
|
|
new ContentPartDefinition(
|
|
|
|
|
p.PartDefinition.Name,
|
|
|
|
|
p.PartDefinition.Fields.Select(
|
|
|
|
|
f => new ContentPartDefinition.Field(
|
|
|
|
|
new ContentFieldDefinition(f.FieldDefinition.Name),
|
|
|
|
|
f.Name,
|
|
|
|
|
f.Settings
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
p.PartDefinition.Settings
|
|
|
|
|
),
|
|
|
|
|
p.Settings
|
|
|
|
|
)
|
|
|
|
|
).ToList();
|
|
|
|
|
|
|
|
|
|
if (viewModel.Fields.Any()) {
|
|
|
|
|
var implicitContentTypeDefinitionPart = new ContentTypeDefinition.Part(
|
|
|
|
|
new ContentPartDefinition(
|
|
|
|
|
viewModel.Name,
|
|
|
|
|
viewModel.Fields.Select(
|
|
|
|
|
f => new ContentPartDefinition.Field(
|
|
|
|
|
new ContentFieldDefinition(f.FieldDefinition.Name),
|
|
|
|
|
f.Name,
|
|
|
|
|
f.Settings
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
null
|
|
|
|
|
),
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
contentTypeDefinitionParts.Add(implicitContentTypeDefinitionPart);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-22 18:36:04 +08:00
|
|
|
|
//todo: apply the changes along the lines of but definately not resembling
|
|
|
|
|
// for now this _might_ just get a little messy ->
|
|
|
|
|
_contentDefinitionService.AlterTypeDefinition(
|
|
|
|
|
new ContentTypeDefinition(
|
|
|
|
|
viewModel.Name,
|
|
|
|
|
viewModel.DisplayName,
|
2010-06-23 06:30:15 +08:00
|
|
|
|
contentTypeDefinitionParts,
|
2010-06-22 18:36:04 +08:00
|
|
|
|
viewModel.Settings
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
// little == lot
|
|
|
|
|
|
2010-06-23 03:56:14 +08:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult EditPart(string id) {
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a part.")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id);
|
|
|
|
|
|
|
|
|
|
if (contentPartDefinition == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
|
|
return View(new EditPartViewModel(contentPartDefinition));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, ActionName("EditPart")]
|
|
|
|
|
public ActionResult EditPartPOST(string id) {
|
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a part.")))
|
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
|
|
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id);
|
|
|
|
|
|
|
|
|
|
if (contentPartDefinition == null)
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
|
|
|
|
|
|
var viewModel = new EditPartViewModel();
|
|
|
|
|
TryUpdateModel(viewModel);
|
|
|
|
|
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return EditPart(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//todo: apply the changes along the lines of but definately not resembling
|
|
|
|
|
// for now this _might_ just get a little messy ->
|
|
|
|
|
_contentDefinitionService.AlterPartDefinition(
|
|
|
|
|
new ContentPartDefinition(
|
|
|
|
|
viewModel.Name,
|
|
|
|
|
viewModel.Fields.Select(
|
|
|
|
|
f => new ContentPartDefinition.Field(
|
|
|
|
|
new ContentFieldDefinition(f.FieldDefinition.Name),
|
|
|
|
|
f.Name,
|
|
|
|
|
f.Settings
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
viewModel.Settings
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
// little == lot
|
|
|
|
|
|
2010-06-22 18:36:04 +08:00
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-16 19:22:51 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Content
|
2010-06-22 18:36:04 +08:00
|
|
|
|
|
2010-06-16 19:22:51 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
Types = _contentDefinitionService.GetTypeDefinitions()
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|