mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-08-24 14:07:11 +08:00
A bit of content type management refactoring
- put the controller on a diet - made type display name changes take - pulled some hard-coded template prefix strings into the view model --HG-- branch : dev
This commit is contained in:
parent
e9a0e09732
commit
75d7f6bc68
@ -86,6 +86,7 @@ namespace Orchard.Core.Settings.Metadata {
|
||||
}
|
||||
|
||||
private void Apply(ContentTypeDefinition model, ContentTypeDefinitionRecord record) {
|
||||
record.DisplayName = model.DisplayName;
|
||||
record.Settings = _settingsWriter.Map(model.Settings).ToString();
|
||||
|
||||
var toRemove = record.ContentTypePartDefinitionRecords
|
||||
|
@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.ContentManagement.MetaData.Models;
|
||||
using Orchard.ContentTypes.Services;
|
||||
using Orchard.ContentTypes.ViewModels;
|
||||
@ -11,34 +9,25 @@ using Orchard.Mvc.Results;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.ContentTypes.Controllers {
|
||||
public class AdminController : Controller {
|
||||
public class AdminController : Controller, IUpdateModel {
|
||||
private readonly IContentDefinitionService _contentDefinitionService;
|
||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||
private readonly IContentDefinitionEditorEvents _extendViewModels;
|
||||
|
||||
public AdminController(
|
||||
IOrchardServices orchardServices,
|
||||
IContentDefinitionService contentDefinitionService,
|
||||
IContentDefinitionManager contentDefinitionManager,
|
||||
IContentDefinitionEditorEvents extendViewModels) {
|
||||
public AdminController(IOrchardServices orchardServices, IContentDefinitionService contentDefinitionService) {
|
||||
Services = orchardServices;
|
||||
_contentDefinitionService = contentDefinitionService;
|
||||
_contentDefinitionManager = contentDefinitionManager;
|
||||
_extendViewModels = extendViewModels;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public IOrchardServices Services { get; private set; }
|
||||
public Localizer T { get; set; }
|
||||
public ActionResult Index() {
|
||||
return List();
|
||||
}
|
||||
|
||||
public ActionResult Index() { return List(); }
|
||||
|
||||
#region Types
|
||||
|
||||
public ActionResult List() {
|
||||
return View("List", new ListContentTypesViewModel {
|
||||
Types = _contentDefinitionService.GetTypeDefinitions()
|
||||
Types = _contentDefinitionService.GetTypes()
|
||||
});
|
||||
}
|
||||
|
||||
@ -54,117 +43,51 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to create a content type.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return View(viewModel);
|
||||
|
||||
var definition = _contentDefinitionService.AddTypeDefinition(viewModel.DisplayName);
|
||||
|
||||
return RedirectToAction("Edit", new { id = definition.Name });
|
||||
}
|
||||
|
||||
public ActionResult Edit(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(contentTypeDefinition) {
|
||||
Templates = _extendViewModels.TypeEditor(contentTypeDefinition)
|
||||
};
|
||||
|
||||
foreach (var part in viewModel.Parts) {
|
||||
part.Templates = _extendViewModels.TypePartEditor(new ContentTypeDefinition.Part(part.PartDefinition.Definition, part.Settings));
|
||||
foreach (var field in part.PartDefinition.Fields)
|
||||
field.Templates = _extendViewModels.PartFieldEditor(new ContentPartDefinition.Field(field.FieldDefinition.Definition, field.Name, field.Settings));
|
||||
}
|
||||
|
||||
if (viewModel.Fields.Any()) {
|
||||
foreach (var field in viewModel.Fields)
|
||||
field.Templates = _extendViewModels.PartFieldEditor(new ContentPartDefinition.Field(field.FieldDefinition.Definition, field.Name, field.Settings));
|
||||
}
|
||||
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
[HttpPost, ActionName("Edit")]
|
||||
public ActionResult EditPOST(EditTypeViewModel viewModel) {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(viewModel.Name);
|
||||
|
||||
if (contentTypeDefinition == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
var updater = new Updater(this);
|
||||
_contentDefinitionManager.AlterTypeDefinition(viewModel.Name, typeBuilder => {
|
||||
|
||||
typeBuilder.DisplayedAs(viewModel.DisplayName);
|
||||
|
||||
// allow extensions to alter type configuration
|
||||
viewModel.Templates = _extendViewModels.TypeEditorUpdate(typeBuilder, updater);
|
||||
|
||||
foreach (var entry in viewModel.Parts.Select((part, index) => new { part, index })) {
|
||||
var partViewModel = entry.part;
|
||||
|
||||
// enable updater to be aware of changing part prefix
|
||||
// todo: stick this info on the view model so the strings don't need to be in code & view
|
||||
var firstHalf = "Parts[" + entry.index + "].";
|
||||
updater._prefix = secondHalf => firstHalf + secondHalf;
|
||||
|
||||
// allow extensions to alter typePart configuration
|
||||
typeBuilder.WithPart(entry.part.PartDefinition.Name, typePartBuilder => {
|
||||
partViewModel.Templates = _extendViewModels.TypePartEditorUpdate(typePartBuilder, updater);
|
||||
});
|
||||
|
||||
if (!partViewModel.PartDefinition.Fields.Any())
|
||||
continue;
|
||||
|
||||
_contentDefinitionManager.AlterPartDefinition(partViewModel.PartDefinition.Name, partBuilder => {
|
||||
foreach (var fieldEntry in partViewModel.PartDefinition.Fields.Select((field, index) => new { field, index })) {
|
||||
partViewModel.PartDefinition.Fields = partViewModel.PartDefinition.Fields.ToArray();
|
||||
var fieldViewModel = fieldEntry.field;
|
||||
|
||||
// enable updater to be aware of changing field prefix
|
||||
var firstHalfFieldName = firstHalf + "PartDefinition.Fields[" + fieldEntry.index + "].";
|
||||
updater._prefix = secondHalf => firstHalfFieldName + secondHalf;
|
||||
|
||||
// allow extensions to alter partField configuration
|
||||
partBuilder.WithField(fieldViewModel.Name, partFieldBuilder => {
|
||||
fieldViewModel.Templates = _extendViewModels.PartFieldEditorUpdate(partFieldBuilder, updater);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (viewModel.Fields.Any()) {
|
||||
_contentDefinitionManager.AlterPartDefinition(viewModel.Name, partBuilder => {
|
||||
viewModel.Fields = viewModel.Fields.ToArray();
|
||||
foreach (var fieldEntry in viewModel.Fields.Select((field, index) => new { field, index })) {
|
||||
var fieldViewModel = fieldEntry.field;
|
||||
|
||||
// enable updater to be aware of changing field prefix
|
||||
var firstHalfFieldName = "Fields[" + fieldEntry.index + "].";
|
||||
updater._prefix = secondHalf => firstHalfFieldName + secondHalf;
|
||||
|
||||
// allow extensions to alter partField configuration
|
||||
partBuilder.WithField(fieldViewModel.Name, partFieldBuilder => {
|
||||
fieldViewModel.Templates = _extendViewModels.PartFieldEditorUpdate(partFieldBuilder, updater);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
var typeViewModel = _contentDefinitionService.AddType(viewModel);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
Services.TransactionManager.Cancel();
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
Services.Notifier.Information(T("\"{0}\" settings have been saved.", viewModel.DisplayName));
|
||||
Services.Notifier.Information(T("The \"{0}\" content type has been created.", typeViewModel.DisplayName));
|
||||
|
||||
return RedirectToAction("Edit", new { id = typeViewModel.Name });
|
||||
}
|
||||
|
||||
public ActionResult Edit(string id) {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var typeViewModel = _contentDefinitionService.GetType(id);
|
||||
|
||||
if (typeViewModel == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
return View(typeViewModel);
|
||||
}
|
||||
|
||||
[HttpPost, ActionName("Edit")]
|
||||
public ActionResult EditPOST(string id) {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var typeViewModel = _contentDefinitionService.GetType(id);
|
||||
|
||||
if (typeViewModel == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
if (!TryUpdateModel(typeViewModel))
|
||||
return View(typeViewModel);
|
||||
|
||||
_contentDefinitionService.AlterType(typeViewModel, this);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
Services.TransactionManager.Cancel();
|
||||
return View(typeViewModel);
|
||||
}
|
||||
|
||||
Services.Notifier.Information(T("\"{0}\" settings have been saved.", typeViewModel.DisplayName));
|
||||
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
@ -173,16 +96,16 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id);
|
||||
var typeViewModel = _contentDefinitionService.GetType(id);
|
||||
|
||||
if (contentTypeDefinition == null)
|
||||
if (typeViewModel == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
var viewModel = new AddPartsViewModel {
|
||||
Type = new EditTypeViewModel(contentTypeDefinition),
|
||||
PartSelections = _contentDefinitionService.GetPartDefinitions()
|
||||
.Where(cpd => !contentTypeDefinition.Parts.Any(p => p.PartDefinition.Name == cpd.Name))
|
||||
.Select(cpd => new PartSelectionViewModel {PartName = cpd.Name})
|
||||
Type = typeViewModel,
|
||||
PartSelections = _contentDefinitionService.GetParts()
|
||||
.Where(cpd => !typeViewModel.Parts.Any(p => p.PartDefinition.Name == cpd.Name))
|
||||
.Select(cpd => new PartSelectionViewModel { PartName = cpd.Name })
|
||||
};
|
||||
|
||||
return View(viewModel);
|
||||
@ -193,24 +116,28 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id);
|
||||
var typeViewModel = _contentDefinitionService.GetType(id);
|
||||
|
||||
if (contentTypeDefinition == null)
|
||||
if (typeViewModel == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
var viewModel = new AddPartsViewModel();
|
||||
TryUpdateModel(viewModel);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
viewModel.Type = new EditTypeViewModel(contentTypeDefinition);
|
||||
if (!TryUpdateModel(viewModel)) {
|
||||
viewModel.Type = typeViewModel;
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
_contentDefinitionManager.AlterTypeDefinition(contentTypeDefinition.Name, typeBuilder => {
|
||||
var partsToAdd = viewModel.PartSelections.Where(ps => ps.IsSelected).Select(ps => ps.PartName);
|
||||
foreach (var partToAdd in partsToAdd)
|
||||
typeBuilder.WithPart(partToAdd);
|
||||
});
|
||||
var partsToAdd = viewModel.PartSelections.Where(ps => ps.IsSelected).Select(ps => ps.PartName);
|
||||
foreach (var partToAdd in partsToAdd) {
|
||||
_contentDefinitionService.AddPartToType(partToAdd, typeViewModel.Name);
|
||||
Services.Notifier.Information(T("The \"{0}\" part has been added.", partToAdd));
|
||||
}
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
Services.TransactionManager.Cancel(); ;
|
||||
viewModel.Type = typeViewModel;
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
return RedirectToAction("Edit", new {id});
|
||||
}
|
||||
@ -219,15 +146,15 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id);
|
||||
var typeViewModel = _contentDefinitionService.GetType(id);
|
||||
|
||||
var viewModel = new RemovePartViewModel();
|
||||
if (contentTypeDefinition == null
|
||||
if (typeViewModel == null
|
||||
|| !TryUpdateModel(viewModel)
|
||||
|| !contentTypeDefinition.Parts.Any(p => p.PartDefinition.Name == viewModel.Name))
|
||||
|| !typeViewModel.Parts.Any(p => p.PartDefinition.Name == viewModel.Name))
|
||||
return new NotFoundResult();
|
||||
|
||||
viewModel.Type = new EditTypeViewModel { Name = contentTypeDefinition.Name, DisplayName = contentTypeDefinition.DisplayName };
|
||||
viewModel.Type = typeViewModel;
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
@ -236,20 +163,22 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id);
|
||||
var typeViewModel = _contentDefinitionService.GetType(id);
|
||||
|
||||
var viewModel = new RemovePartViewModel();
|
||||
if (contentTypeDefinition == null
|
||||
if (typeViewModel == null
|
||||
|| !TryUpdateModel(viewModel)
|
||||
|| !contentTypeDefinition.Parts.Any(p => p.PartDefinition.Name == viewModel.Name))
|
||||
|| !typeViewModel.Parts.Any(p => p.PartDefinition.Name == viewModel.Name))
|
||||
return new NotFoundResult();
|
||||
|
||||
_contentDefinitionService.RemovePartFromType(viewModel.Name, typeViewModel.Name);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
viewModel.Type = new EditTypeViewModel { Name = contentTypeDefinition.Name, DisplayName = contentTypeDefinition.DisplayName };
|
||||
Services.TransactionManager.Cancel();
|
||||
viewModel.Type = typeViewModel;
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
_contentDefinitionManager.AlterTypeDefinition(id, typeBuilder => typeBuilder.RemovePart(viewModel.Name));
|
||||
Services.Notifier.Information(T("The \"{0}\" part has been removed.", viewModel.Name));
|
||||
|
||||
return RedirectToAction("Edit", new {id});
|
||||
@ -261,7 +190,7 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
|
||||
public ActionResult ListParts() {
|
||||
return View(new ListContentPartsViewModel {
|
||||
Parts = _contentDefinitionService.GetPartDefinitions()
|
||||
Parts = _contentDefinitionService.GetParts()
|
||||
});
|
||||
}
|
||||
|
||||
@ -277,51 +206,50 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to create a content part.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var partViewModel = _contentDefinitionService.AddPart(viewModel);
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return View(viewModel);
|
||||
|
||||
var definition = _contentDefinitionService.AddPartDefinition(viewModel.Name);
|
||||
Services.Notifier.Information(T("The \"{0}\" content part has been created.", partViewModel.Name));
|
||||
|
||||
return RedirectToAction("EditPart", new { id = definition.Name });
|
||||
return RedirectToAction("EditPart", new { id = partViewModel.Name });
|
||||
}
|
||||
|
||||
public ActionResult EditPart(string id) {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id);
|
||||
var partViewModel = _contentDefinitionService.GetPart(id);
|
||||
|
||||
if (contentPartDefinition == null)
|
||||
if (partViewModel == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
var viewModel = new EditPartViewModel(contentPartDefinition) {
|
||||
Templates = _extendViewModels.PartEditor(contentPartDefinition)
|
||||
};
|
||||
|
||||
return View(viewModel);
|
||||
return View(partViewModel);
|
||||
}
|
||||
|
||||
[HttpPost, ActionName("EditPart")]
|
||||
public ActionResult EditPartPOST(EditPartViewModel viewModel) {
|
||||
public ActionResult EditPartPOST(string id) {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(viewModel.Name);
|
||||
var partViewModel = _contentDefinitionService.GetPart(id);
|
||||
|
||||
if (contentPartDefinition == null)
|
||||
if (partViewModel == null)
|
||||
return new NotFoundResult();
|
||||
|
||||
var updater = new Updater(this);
|
||||
_contentDefinitionManager.AlterPartDefinition(viewModel.Name, partBuilder => {
|
||||
// allow extensions to alter part configuration
|
||||
viewModel.Templates = _extendViewModels.PartEditorUpdate(partBuilder, updater);
|
||||
});
|
||||
if (!TryUpdateModel(partViewModel))
|
||||
return View(partViewModel);
|
||||
|
||||
_contentDefinitionService.AlterPart(partViewModel, this);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
Services.TransactionManager.Cancel();
|
||||
return View(viewModel);
|
||||
return View(partViewModel);
|
||||
}
|
||||
|
||||
Services.Notifier.Information(T("\"{0}\" settings have been saved.", partViewModel.Name));
|
||||
|
||||
return RedirectToAction("ListParts");
|
||||
}
|
||||
|
||||
@ -329,20 +257,20 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id);
|
||||
var partViewModel = _contentDefinitionService.GetPart(id);
|
||||
|
||||
if (contentPartDefinition == null) {
|
||||
if (partViewModel == null) {
|
||||
//id passed in might be that of a type w/ no implicit field
|
||||
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id);
|
||||
if (contentTypeDefinition != null)
|
||||
contentPartDefinition = new ContentPartDefinition(id);
|
||||
var typeViewModel = _contentDefinitionService.GetType(id);
|
||||
if (typeViewModel != null)
|
||||
partViewModel = new EditPartViewModel(new ContentPartDefinition(id));
|
||||
else
|
||||
return new NotFoundResult();
|
||||
}
|
||||
|
||||
var viewModel = new AddFieldViewModel {
|
||||
Part = new EditPartViewModel(contentPartDefinition),
|
||||
Fields = _contentDefinitionService.GetFieldDefinitions()
|
||||
Part = partViewModel,
|
||||
Fields = _contentDefinitionService.GetFields()
|
||||
};
|
||||
|
||||
return View(viewModel);
|
||||
@ -353,42 +281,36 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var viewModel = new AddFieldViewModel();
|
||||
TryUpdateModel(viewModel);
|
||||
|
||||
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id);
|
||||
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id);
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return AddFieldTo(id);
|
||||
|
||||
if (contentPartDefinition == null) {
|
||||
var partViewModel = _contentDefinitionService.GetPart(id);
|
||||
var typeViewModel = _contentDefinitionService.GetType(id);
|
||||
if (partViewModel == null) {
|
||||
//id passed in might be that of a type w/ no implicit field
|
||||
if (contentTypeDefinition != null) {
|
||||
contentPartDefinition = new ContentPartDefinition(id);
|
||||
var contentTypeDefinitionParts = contentTypeDefinition.Parts.ToList();
|
||||
contentTypeDefinitionParts.Add(new ContentTypeDefinition.Part(contentPartDefinition, null));
|
||||
_contentDefinitionService.AlterTypeDefinition(
|
||||
new ContentTypeDefinition(
|
||||
contentTypeDefinition.Name,
|
||||
contentTypeDefinition.DisplayName,
|
||||
contentTypeDefinitionParts,
|
||||
contentTypeDefinition.Settings
|
||||
)
|
||||
);
|
||||
if (typeViewModel != null) {
|
||||
partViewModel = new EditPartViewModel { Name = typeViewModel.Name };
|
||||
_contentDefinitionService.AddPart(new CreatePartViewModel { Name = partViewModel.Name });
|
||||
_contentDefinitionService.AddPartToType(partViewModel.Name, typeViewModel.Name);
|
||||
}
|
||||
else {
|
||||
return new NotFoundResult();
|
||||
}
|
||||
}
|
||||
|
||||
var contentPartFields = contentPartDefinition.Fields.ToList();
|
||||
contentPartFields.Add(new ContentPartDefinition.Field(new ContentFieldDefinition(viewModel.FieldTypeName), viewModel.DisplayName, null));
|
||||
_contentDefinitionService.AlterPartDefinition(new ContentPartDefinition(contentPartDefinition.Name, contentPartFields, contentPartDefinition.Settings));
|
||||
var viewModel = new AddFieldViewModel();
|
||||
if (!TryUpdateModel(viewModel)) {
|
||||
Services.TransactionManager.Cancel();
|
||||
return AddFieldTo(id);
|
||||
}
|
||||
|
||||
_contentDefinitionService.AddFieldToPart(viewModel.DisplayName, viewModel.FieldTypeName, partViewModel.Name);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
Services.TransactionManager.Cancel();
|
||||
return AddFieldTo(id);
|
||||
}
|
||||
|
||||
Services.Notifier.Information(T("The \"{0}\" field has been added.", viewModel.DisplayName));
|
||||
|
||||
if (contentTypeDefinition != null)
|
||||
if (typeViewModel != null)
|
||||
return RedirectToAction("Edit", new { id });
|
||||
|
||||
return RedirectToAction("EditPart", new { id });
|
||||
@ -399,15 +321,15 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id);
|
||||
var partViewModel = _contentDefinitionService.GetPart(id);
|
||||
|
||||
var viewModel = new RemoveFieldViewModel();
|
||||
if (contentPartDefinition == null
|
||||
if (partViewModel == null
|
||||
|| !TryUpdateModel(viewModel)
|
||||
|| !contentPartDefinition.Fields.Any(p => p.Name == viewModel.Name))
|
||||
|| !partViewModel.Fields.Any(p => p.Name == viewModel.Name))
|
||||
return new NotFoundResult();
|
||||
|
||||
viewModel.Part = new EditPartViewModel { Name = contentPartDefinition.Name };
|
||||
viewModel.Part = partViewModel;
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
@ -416,23 +338,25 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id);
|
||||
var partViewModel = _contentDefinitionService.GetPart(id);
|
||||
|
||||
var viewModel = new RemoveFieldViewModel();
|
||||
if (contentPartDefinition == null
|
||||
if (partViewModel == null
|
||||
|| !TryUpdateModel(viewModel)
|
||||
|| !contentPartDefinition.Fields.Any(p => p.Name == viewModel.Name))
|
||||
|| !partViewModel.Fields.Any(p => p.Name == viewModel.Name))
|
||||
return new NotFoundResult();
|
||||
|
||||
_contentDefinitionService.RemoveFieldFromPart(viewModel.Name, partViewModel.Name);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
viewModel.Part = new EditPartViewModel { Name = contentPartDefinition.Name };
|
||||
Services.TransactionManager.Cancel();
|
||||
viewModel.Part = partViewModel;
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
_contentDefinitionManager.AlterPartDefinition(id, typeBuilder => typeBuilder.RemoveField(viewModel.Name));
|
||||
Services.Notifier.Information(T("The \"{0}\" field has been removed.", viewModel.Name));
|
||||
|
||||
if (_contentDefinitionService.GetTypeDefinition(id) != null)
|
||||
if (_contentDefinitionService.GetType(id) != null)
|
||||
return RedirectToAction("Edit", new { id });
|
||||
|
||||
return RedirectToAction("EditPart", new { id });
|
||||
@ -440,23 +364,12 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
|
||||
#endregion
|
||||
|
||||
class Updater : IUpdateModel {
|
||||
private readonly AdminController _thunk;
|
||||
|
||||
public Updater(AdminController thunk) {
|
||||
_thunk = thunk;
|
||||
}
|
||||
|
||||
public Func<string, string> _prefix = x => x;
|
||||
|
||||
public bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) where TModel : class {
|
||||
return _thunk.TryUpdateModel(model, _prefix(prefix), includeProperties, excludeProperties);
|
||||
}
|
||||
|
||||
public void AddModelError(string key, LocalizedString errorMessage) {
|
||||
_thunk.ModelState.AddModelError(_prefix(key), errorMessage.ToString());
|
||||
}
|
||||
public new bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) where TModel : class {
|
||||
return base.TryUpdateModel(model, prefix, includeProperties, excludeProperties);
|
||||
}
|
||||
|
||||
public void AddModelError(string key, LocalizedString errorMessage) {
|
||||
ModelState.AddModelError(key, errorMessage.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -99,8 +99,8 @@
|
||||
<Content Include="Views\Admin\EditPart.ascx" />
|
||||
<Content Include="Views\Admin\Edit.ascx" />
|
||||
<Content Include="Views\Admin\List.ascx" />
|
||||
<Content Include="Views\DisplayTemplates\ContentTypeDefinition.ascx" />
|
||||
<Content Include="Views\DisplayTemplates\ContentPartDefinition.ascx" />
|
||||
<Content Include="Views\DisplayTemplates\EditTypeViewModel.ascx" />
|
||||
<Content Include="Views\DisplayTemplates\EditPartViewModel.ascx" />
|
||||
<Content Include="Views\DisplayTemplates\Field.ascx" />
|
||||
<Content Include="Views\DisplayTemplates\Fields.ascx" />
|
||||
<Content Include="Views\DisplayTemplates\Settings.ascx" />
|
||||
|
@ -2,42 +2,69 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.ContentManagement.MetaData.Models;
|
||||
using Orchard.ContentTypes.ViewModels;
|
||||
using Orchard.Localization;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.ContentTypes.Services {
|
||||
public class ContentDefinitionService : IContentDefinitionService {
|
||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||
private readonly IEnumerable<IContentFieldDriver> _contentFieldDrivers;
|
||||
private readonly IContentDefinitionEditorEvents _contentDefinitionEditorEvents;
|
||||
|
||||
public ContentDefinitionService(IOrchardServices services, IContentDefinitionManager contentDefinitionManager, IEnumerable<IContentFieldDriver> contentFieldDrivers) {
|
||||
public ContentDefinitionService(
|
||||
IOrchardServices services,
|
||||
IContentDefinitionManager contentDefinitionManager,
|
||||
IEnumerable<IContentFieldDriver> contentFieldDrivers,
|
||||
IContentDefinitionEditorEvents contentDefinitionEditorEvents) {
|
||||
Services = services;
|
||||
_contentDefinitionManager = contentDefinitionManager;
|
||||
_contentFieldDrivers = contentFieldDrivers;
|
||||
_contentDefinitionEditorEvents = contentDefinitionEditorEvents;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public IOrchardServices Services { get; set; }
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public IEnumerable<ContentTypeDefinition> GetTypeDefinitions() {
|
||||
return _contentDefinitionManager.ListTypeDefinitions();
|
||||
public IEnumerable<EditTypeViewModel> GetTypes() {
|
||||
return _contentDefinitionManager.ListTypeDefinitions().Select(ctd => new EditTypeViewModel(ctd));
|
||||
}
|
||||
|
||||
public ContentTypeDefinition GetTypeDefinition(string name) {
|
||||
return _contentDefinitionManager.GetTypeDefinition(name);
|
||||
public EditTypeViewModel GetType(string name) {
|
||||
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(name);
|
||||
|
||||
if (contentTypeDefinition == null)
|
||||
return null;
|
||||
|
||||
var viewModel = new EditTypeViewModel(contentTypeDefinition) {
|
||||
Templates = _contentDefinitionEditorEvents.TypeEditor(contentTypeDefinition)
|
||||
};
|
||||
|
||||
foreach (var part in viewModel.Parts) {
|
||||
part.Templates = _contentDefinitionEditorEvents.TypePartEditor(part._Definition);
|
||||
foreach (var field in part.PartDefinition.Fields)
|
||||
field.Templates = _contentDefinitionEditorEvents.PartFieldEditor(field._Definition);
|
||||
}
|
||||
|
||||
if (viewModel.Fields.Any()) {
|
||||
foreach (var field in viewModel.Fields)
|
||||
field.Templates = _contentDefinitionEditorEvents.PartFieldEditor(field._Definition);
|
||||
}
|
||||
|
||||
return viewModel;
|
||||
}
|
||||
|
||||
public ContentTypeDefinition AddTypeDefinition(string displayName) {
|
||||
var name = GenerateName(displayName);
|
||||
public EditTypeViewModel AddType(CreateTypeViewModel typeViewModel) {
|
||||
var name = GenerateName(typeViewModel.DisplayName);
|
||||
|
||||
while (_contentDefinitionManager.GetTypeDefinition(name) != null)
|
||||
name = VersionName(name);
|
||||
|
||||
var contentTypeDefinition = new ContentTypeDefinition(name) { DisplayName = displayName };
|
||||
var contentTypeDefinition = new ContentTypeDefinition(name, typeViewModel.DisplayName);
|
||||
//just giving the new type some default parts for now
|
||||
_contentDefinitionManager.StoreTypeDefinition(contentTypeDefinition);
|
||||
_contentDefinitionManager.AlterTypeDefinition(
|
||||
@ -45,36 +72,99 @@ namespace Orchard.ContentTypes.Services {
|
||||
cfg => cfg.WithPart("CommonAspect")
|
||||
//.WithPart("RoutableAspect") //need to go the new routable route
|
||||
.WithPart("BodyAspect"));
|
||||
|
||||
Services.Notifier.Information(T("The \"{0}\" content type has created.", contentTypeDefinition.DisplayName));
|
||||
|
||||
return contentTypeDefinition;
|
||||
return new EditTypeViewModel(contentTypeDefinition);
|
||||
}
|
||||
|
||||
public void AlterTypeDefinition(ContentTypeDefinition contentTypeDefinition) {
|
||||
_contentDefinitionManager.StoreTypeDefinition(contentTypeDefinition);
|
||||
public void AlterType(EditTypeViewModel typeViewModel, IUpdateModel updateModel) {
|
||||
var updater = new Updater(updateModel);
|
||||
_contentDefinitionManager.AlterTypeDefinition(typeViewModel.Name, typeBuilder => {
|
||||
typeBuilder.DisplayedAs(typeViewModel.DisplayName);
|
||||
|
||||
var implicitTypePart = contentTypeDefinition.Parts.SingleOrDefault(p => p.PartDefinition.Name == contentTypeDefinition.Name);
|
||||
if (implicitTypePart != null) {
|
||||
AlterPartDefinition(implicitTypePart.PartDefinition);
|
||||
}
|
||||
// allow extensions to alter type configuration
|
||||
typeViewModel.Templates = _contentDefinitionEditorEvents.TypeEditorUpdate(typeBuilder, updater);
|
||||
|
||||
foreach (var part in typeViewModel.Parts) {
|
||||
var partViewModel = part;
|
||||
|
||||
// enable updater to be aware of changing part prefix
|
||||
updater._prefix = secondHalf => string.Format("{0}.{1}", partViewModel.Prefix, secondHalf);
|
||||
|
||||
// allow extensions to alter typePart configuration
|
||||
typeBuilder.WithPart(partViewModel.PartDefinition.Name, typePartBuilder => {
|
||||
partViewModel.Templates = _contentDefinitionEditorEvents.TypePartEditorUpdate(typePartBuilder, updater);
|
||||
});
|
||||
|
||||
if (!partViewModel.PartDefinition.Fields.Any())
|
||||
continue;
|
||||
|
||||
_contentDefinitionManager.AlterPartDefinition(partViewModel.PartDefinition.Name, partBuilder => {
|
||||
var fieldFirstHalf = string.Format("{0}.{1}", partViewModel.Prefix, partViewModel.PartDefinition.Prefix);
|
||||
foreach (var field in partViewModel.PartDefinition.Fields) {
|
||||
var fieldViewModel = field;
|
||||
|
||||
// enable updater to be aware of changing field prefix
|
||||
updater._prefix = secondHalf =>
|
||||
string.Format("{0}.{1}.{2}", fieldFirstHalf, fieldViewModel.Prefix, secondHalf);
|
||||
// allow extensions to alter partField configuration
|
||||
partBuilder.WithField(fieldViewModel.Name, partFieldBuilder => {
|
||||
fieldViewModel.Templates = _contentDefinitionEditorEvents.PartFieldEditorUpdate(partFieldBuilder, updater);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (typeViewModel.Fields.Any()) {
|
||||
_contentDefinitionManager.AlterPartDefinition(typeViewModel.Name, partBuilder => {
|
||||
foreach (var field in typeViewModel.Fields) {
|
||||
var fieldViewModel = field;
|
||||
|
||||
// enable updater to be aware of changing field prefix
|
||||
updater._prefix = secondHalf =>
|
||||
string.Format("{0}.{1}", fieldViewModel.Prefix, secondHalf);
|
||||
|
||||
// allow extensions to alter partField configuration
|
||||
partBuilder.WithField(fieldViewModel.Name, partFieldBuilder => {
|
||||
fieldViewModel.Templates = _contentDefinitionEditorEvents.PartFieldEditorUpdate(partFieldBuilder, updater);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void RemoveTypeDefinition(string name) {
|
||||
public void RemoveType(string name) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerable<ContentPartDefinition> GetPartDefinitions() {
|
||||
var typeNames = GetTypeDefinitions().Select(ctd => ctd.Name);
|
||||
return _contentDefinitionManager.ListPartDefinitions().Where(cpd => !typeNames.Contains(cpd.Name));
|
||||
public void AddPartToType(string partName, string typeName) {
|
||||
_contentDefinitionManager.AlterTypeDefinition(typeName, typeBuilder => typeBuilder.WithPart(partName));
|
||||
}
|
||||
|
||||
public ContentPartDefinition GetPartDefinition(string name) {
|
||||
return _contentDefinitionManager.GetPartDefinition(name);
|
||||
public void RemovePartFromType(string partName, string typeName) {
|
||||
_contentDefinitionManager.AlterTypeDefinition(typeName, typeBuilder => typeBuilder.RemovePart(partName));
|
||||
}
|
||||
|
||||
public ContentPartDefinition AddPartDefinition(string name) {
|
||||
name = GenerateName(name);
|
||||
public IEnumerable<EditPartViewModel> GetParts() {
|
||||
var typeNames = GetTypes().Select(ctd => ctd.Name);
|
||||
return _contentDefinitionManager.ListPartDefinitions().Where(cpd => !typeNames.Contains(cpd.Name)).Select(cpd => new EditPartViewModel(cpd));
|
||||
}
|
||||
|
||||
public EditPartViewModel GetPart(string name) {
|
||||
var contentPartDefinition = _contentDefinitionManager.GetPartDefinition(name);
|
||||
|
||||
if (contentPartDefinition == null)
|
||||
return null;
|
||||
|
||||
var viewModel = new EditPartViewModel(contentPartDefinition) {
|
||||
Templates = _contentDefinitionEditorEvents.PartEditor(contentPartDefinition)
|
||||
};
|
||||
|
||||
return viewModel;
|
||||
}
|
||||
|
||||
public EditPartViewModel AddPart(CreatePartViewModel partViewModel) {
|
||||
var name = GenerateName(partViewModel.Name);
|
||||
|
||||
while (_contentDefinitionManager.GetPartDefinition(name) != null)
|
||||
name = VersionName(name);
|
||||
@ -82,21 +172,34 @@ namespace Orchard.ContentTypes.Services {
|
||||
var contentPartDefinition = new ContentPartDefinition(name);
|
||||
_contentDefinitionManager.StorePartDefinition(contentPartDefinition);
|
||||
|
||||
return contentPartDefinition;
|
||||
return new EditPartViewModel(contentPartDefinition);
|
||||
}
|
||||
|
||||
public void AlterPartDefinition(ContentPartDefinition contentPartDefinition) {
|
||||
_contentDefinitionManager.StorePartDefinition(contentPartDefinition);
|
||||
public void AlterPart(EditPartViewModel partViewModel, IUpdateModel updateModel) {
|
||||
var updater = new Updater(updateModel);
|
||||
_contentDefinitionManager.AlterPartDefinition(partViewModel.Name, partBuilder => {
|
||||
partViewModel.Templates = _contentDefinitionEditorEvents.PartEditorUpdate(partBuilder, updater);
|
||||
});
|
||||
}
|
||||
|
||||
public void RemovePartDefinition(string name) {
|
||||
public void RemovePart(string name) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerable<ContentFieldInfo> GetFieldDefinitions() {
|
||||
public IEnumerable<ContentFieldInfo> GetFields() {
|
||||
return _contentFieldDrivers.SelectMany(d => d.GetFieldInfo());
|
||||
}
|
||||
|
||||
public void AddFieldToPart(string fieldName, string fieldTypeName, string partName) {
|
||||
_contentDefinitionManager.AlterPartDefinition(partName, partBuilder =>
|
||||
partBuilder.WithField(fieldName, fieldBuilder => fieldBuilder.OfType(fieldTypeName))
|
||||
);
|
||||
}
|
||||
|
||||
public void RemoveFieldFromPart(string fieldName, string partName) {
|
||||
_contentDefinitionManager.AlterPartDefinition(partName, typeBuilder => typeBuilder.RemoveField(fieldName));
|
||||
}
|
||||
|
||||
//gratuitously stolen from the RoutableService
|
||||
private static string GenerateName(string displayName) {
|
||||
if (string.IsNullOrWhiteSpace(displayName))
|
||||
@ -130,5 +233,23 @@ namespace Orchard.ContentTypes.Services {
|
||||
|
||||
return string.Format("{0}-{1}", name, version);
|
||||
}
|
||||
|
||||
class Updater : IUpdateModel {
|
||||
private readonly IUpdateModel _thunk;
|
||||
|
||||
public Updater(IUpdateModel thunk) {
|
||||
_thunk = thunk;
|
||||
}
|
||||
|
||||
public Func<string, string> _prefix = x => x;
|
||||
|
||||
public bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) where TModel : class {
|
||||
return _thunk.TryUpdateModel(model, _prefix(prefix), includeProperties, excludeProperties);
|
||||
}
|
||||
|
||||
public void AddModelError(string key, LocalizedString errorMessage) {
|
||||
_thunk.AddModelError(_prefix(key), errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.ContentManagement.MetaData.Models;
|
||||
using Orchard.ContentTypes.ViewModels;
|
||||
|
||||
namespace Orchard.ContentTypes.Services {
|
||||
public interface IContentDefinitionService : IDependency {
|
||||
IEnumerable<ContentTypeDefinition> GetTypeDefinitions();
|
||||
ContentTypeDefinition GetTypeDefinition(string name);
|
||||
ContentTypeDefinition AddTypeDefinition(string displayName);
|
||||
void AlterTypeDefinition(ContentTypeDefinition contentTypeDefinition);
|
||||
void RemoveTypeDefinition(string name);
|
||||
IEnumerable<EditTypeViewModel> GetTypes();
|
||||
EditTypeViewModel GetType(string name);
|
||||
EditTypeViewModel AddType(CreateTypeViewModel typeViewModel);
|
||||
void AlterType(EditTypeViewModel typeViewModel, IUpdateModel updater);
|
||||
void RemoveType(string name);
|
||||
void AddPartToType(string partName, string typeName);
|
||||
void RemovePartFromType(string partName, string typeName);
|
||||
|
||||
IEnumerable<ContentPartDefinition> GetPartDefinitions();
|
||||
ContentPartDefinition GetPartDefinition(string name);
|
||||
ContentPartDefinition AddPartDefinition(string name);
|
||||
void AlterPartDefinition(ContentPartDefinition contentPartDefinition);
|
||||
void RemovePartDefinition(string name);
|
||||
IEnumerable<EditPartViewModel> GetParts();
|
||||
EditPartViewModel GetPart(string name);
|
||||
EditPartViewModel AddPart(CreatePartViewModel partViewModel);
|
||||
void AlterPart(EditPartViewModel partViewModel, IUpdateModel updater);
|
||||
void RemovePart(string name);
|
||||
|
||||
IEnumerable<ContentFieldInfo> GetFieldDefinitions();
|
||||
IEnumerable<ContentFieldInfo> GetFields();
|
||||
void AddFieldToPart(string fieldName, string fieldTypeName, string partName);
|
||||
void RemoveFieldFromPart(string fieldName, string partName);
|
||||
}
|
||||
}
|
@ -11,34 +11,36 @@ namespace Orchard.ContentTypes.ViewModels {
|
||||
Fields = new List<EditPartFieldViewModel>();
|
||||
Parts = new List<EditTypePartViewModel>();
|
||||
}
|
||||
|
||||
public EditTypeViewModel(ContentTypeDefinition contentTypeDefinition) {
|
||||
Name = contentTypeDefinition.Name;
|
||||
DisplayName = contentTypeDefinition.DisplayName;
|
||||
Settings = contentTypeDefinition.Settings;
|
||||
Fields = GetTypeFields(contentTypeDefinition).ToList();
|
||||
Parts = GetTypeParts(contentTypeDefinition).ToList();
|
||||
Definition = contentTypeDefinition;
|
||||
_Definition = contentTypeDefinition;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public string DisplayName { get; set; }
|
||||
public ContentTypeDefinition Definition { get; private set; }
|
||||
public IEnumerable<TemplateViewModel> Templates { get; set; }
|
||||
|
||||
public SettingsDictionary Settings { get; set; }
|
||||
public IEnumerable<EditPartFieldViewModel> Fields { get; set; }
|
||||
public IEnumerable<EditTypePartViewModel> Parts { get; set; }
|
||||
public IEnumerable<TemplateViewModel> Templates { get; set; }
|
||||
public ContentTypeDefinition _Definition { get; private set; }
|
||||
|
||||
private IEnumerable<EditPartFieldViewModel> GetTypeFields(ContentTypeDefinition contentTypeDefinition) {
|
||||
var implicitTypePart = contentTypeDefinition.Parts.SingleOrDefault(p => p.PartDefinition.Name == Name);
|
||||
|
||||
return implicitTypePart == null
|
||||
? Enumerable.Empty<EditPartFieldViewModel>()
|
||||
: implicitTypePart.PartDefinition.Fields.Select(f => new EditPartFieldViewModel(f) { Part = new EditPartViewModel(implicitTypePart.PartDefinition) });
|
||||
: implicitTypePart.PartDefinition.Fields.Select((f, i) => new EditPartFieldViewModel(i, f) { Part = new EditPartViewModel(implicitTypePart.PartDefinition) });
|
||||
}
|
||||
|
||||
private IEnumerable<EditTypePartViewModel> GetTypeParts(ContentTypeDefinition contentTypeDefinition) {
|
||||
return contentTypeDefinition.Parts.Where(p => p.PartDefinition.Name != Name).Select(p => new EditTypePartViewModel(p) { Type = this });
|
||||
return contentTypeDefinition.Parts
|
||||
.Where(p => p.PartDefinition.Name != Name)
|
||||
.Select((p, i) => new EditTypePartViewModel(i, p) { Type = this });
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,15 +48,21 @@ namespace Orchard.ContentTypes.ViewModels {
|
||||
public EditTypePartViewModel() {
|
||||
Settings = new SettingsDictionary();
|
||||
}
|
||||
public EditTypePartViewModel(ContentTypeDefinition.Part part) {
|
||||
|
||||
public EditTypePartViewModel(int index, ContentTypeDefinition.Part part) {
|
||||
Index = index;
|
||||
PartDefinition = new EditPartViewModel(part.PartDefinition);
|
||||
Settings = part.Settings;
|
||||
_Definition = part;
|
||||
}
|
||||
|
||||
public EditTypeViewModel Type { get; set; }
|
||||
public int Index { get; set; }
|
||||
public string Prefix { get { return "Parts[" + Index + "]"; } }
|
||||
public EditPartViewModel PartDefinition { get; set; }
|
||||
public SettingsDictionary Settings { get; set; }
|
||||
public EditTypeViewModel Type { get; set; }
|
||||
public IEnumerable<TemplateViewModel> Templates { get; set; }
|
||||
public ContentTypeDefinition.Part _Definition { get; private set; }
|
||||
}
|
||||
|
||||
public class EditPartViewModel : BaseViewModel {
|
||||
@ -62,45 +70,56 @@ namespace Orchard.ContentTypes.ViewModels {
|
||||
Fields = new List<EditPartFieldViewModel>();
|
||||
Settings = new SettingsDictionary();
|
||||
}
|
||||
|
||||
public EditPartViewModel(ContentPartDefinition contentPartDefinition) {
|
||||
Name = contentPartDefinition.Name;
|
||||
Fields = contentPartDefinition.Fields.Select(f => new EditPartFieldViewModel(f) { Part = this }).ToList();
|
||||
Fields = contentPartDefinition.Fields.Select((f, i) => new EditPartFieldViewModel(i, f) { Part = this }).ToList();
|
||||
Settings = contentPartDefinition.Settings;
|
||||
Definition = contentPartDefinition;
|
||||
_Definition = contentPartDefinition;
|
||||
}
|
||||
|
||||
public string Prefix { get { return "PartDefinition"; } }
|
||||
public string Name { get; set; }
|
||||
public IEnumerable<TemplateViewModel> Templates { get; set; }
|
||||
public IEnumerable<EditPartFieldViewModel> Fields { get; set; }
|
||||
public ContentPartDefinition Definition { get; private set; }
|
||||
public SettingsDictionary Settings { get; set; }
|
||||
public ContentPartDefinition _Definition { get; private set; }
|
||||
}
|
||||
|
||||
public class EditPartFieldViewModel {
|
||||
|
||||
public EditPartFieldViewModel() {
|
||||
Settings = new SettingsDictionary();
|
||||
}
|
||||
public EditPartFieldViewModel(ContentPartDefinition.Field field) {
|
||||
|
||||
public EditPartFieldViewModel(int index, ContentPartDefinition.Field field) {
|
||||
Index = index;
|
||||
Name = field.Name;
|
||||
FieldDefinition = new EditFieldViewModel(field.FieldDefinition);
|
||||
Settings = field.Settings;
|
||||
_Definition = field;
|
||||
}
|
||||
|
||||
public int Index { get; set; }
|
||||
public string Prefix { get { return "Fields[" + Index + "]"; } }
|
||||
public EditPartViewModel Part { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public IEnumerable<TemplateViewModel> Templates { get; set; }
|
||||
public EditFieldViewModel FieldDefinition { get; set; }
|
||||
public SettingsDictionary Settings { get; set; }
|
||||
public ContentPartDefinition.Field _Definition { get; private set; }
|
||||
}
|
||||
|
||||
public class EditFieldViewModel {
|
||||
public EditFieldViewModel() { }
|
||||
|
||||
public EditFieldViewModel(ContentFieldDefinition contentFieldDefinition) {
|
||||
Name = contentFieldDefinition.Name;
|
||||
Definition = contentFieldDefinition;
|
||||
_Definition = contentFieldDefinition;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public ContentFieldDefinition Definition { get; private set; }
|
||||
public ContentFieldDefinition _Definition { get; private set; }
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.ContentManagement.MetaData.Models;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.ContentTypes.ViewModels {
|
||||
public class ListContentPartsViewModel : BaseViewModel {
|
||||
public IEnumerable<ContentPartDefinition> Parts { get; set; }
|
||||
public IEnumerable<EditPartViewModel> Parts { get; set; }
|
||||
}
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.ContentManagement.MetaData.Models;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.ContentTypes.ViewModels {
|
||||
public class ListContentTypesViewModel : BaseViewModel {
|
||||
public IEnumerable<ContentTypeDefinition> Types { get; set; }
|
||||
public IEnumerable<EditTypeViewModel> Types { get; set; }
|
||||
}
|
||||
}
|
@ -17,12 +17,12 @@ using (Html.BeginFormAntiForgeryPost()) { %>
|
||||
<%:Html.HiddenFor(m => m.Name) %>
|
||||
</fieldset><%
|
||||
Html.RenderTemplates(Model.Templates); %>
|
||||
<h2><%:T("Parts") %></h2>
|
||||
<div class="manage add-to-type"><%: Html.ActionLink(T("Add").Text, "AddPartsTo", new { area = "Orchard.ContentTypes", id = Model.Name }, new { @class = "button" })%></div><%:
|
||||
Html.EditorFor(m => m.Parts, "TypeParts", "") %>
|
||||
<h2><%:T("Fields") %></h2>
|
||||
<div class="manage add-to-type"><%: Html.ActionLink(T("Add").Text, "AddFieldTo", new { area = "Orchard.ContentTypes", id = Model.Name }, new { @class = "button" }) %></div><%:
|
||||
Html.EditorFor(m => m.Fields, "Fields", "") %>
|
||||
<h2><%:T("Parts") %></h2>
|
||||
<div class="manage add-to-type"><%: Html.ActionLink(T("Add").Text, "AddPartsTo", new { area = "Orchard.ContentTypes", id = Model.Name }, new { @class = "button" })%></div><%:
|
||||
Html.EditorFor(m => m.Parts, "TypeParts", "") %>
|
||||
<fieldset class="action">
|
||||
<button class="primaryAction" type="submit"><%:T("Save") %></button>
|
||||
</fieldset>
|
||||
|
@ -1,4 +1,4 @@
|
||||
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<Orchard.ContentManagement.MetaData.Models.ContentPartDefinition>" %>
|
||||
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<Orchard.ContentTypes.ViewModels.EditPartViewModel>" %>
|
||||
<div class="summary">
|
||||
<div class="properties">
|
||||
<h3><%:Model.Name%></h3>
|
@ -1,4 +1,4 @@
|
||||
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<Orchard.ContentManagement.MetaData.Models.ContentTypeDefinition>" %>
|
||||
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<Orchard.ContentTypes.ViewModels.EditTypeViewModel>" %>
|
||||
<div class="summary">
|
||||
<div class="properties">
|
||||
<h3><%:Model.DisplayName%></h3>
|
@ -4,6 +4,8 @@
|
||||
<div class="manage">
|
||||
<%:Html.ActionLink(T("Remove").Text, "RemoveFieldFrom", new { area = "Orchard.ContentTypes", id = Model.Part.Name, Model.Name }, new { itemprop = "RemoveUrl UnsafeUrl" })%><%--// <- some experimentation--%>
|
||||
</div><%
|
||||
Html.RenderTemplates(Model.Templates); %>
|
||||
<%:Html.HiddenFor(m => m.Name) %><%:Html.HiddenFor(m => m.FieldDefinition.Name) %>
|
||||
Html.RenderTemplates(Model.Templates);
|
||||
%><%:Html.HiddenFor(m => m.Name)
|
||||
%><%:Html.HiddenFor(m => m.FieldDefinition.Name)
|
||||
%><%:Html.HiddenFor(m => m.Index) %>
|
||||
</fieldset>
|
@ -2,11 +2,9 @@
|
||||
<%
|
||||
if (Model.Any()) { %>
|
||||
<fieldset><%
|
||||
var fi = 0;
|
||||
foreach (var field in Model) {
|
||||
var f = field;
|
||||
var htmlFieldName = string.Format("Fields[{0}]", fi++); %>
|
||||
<%:Html.EditorFor(m => f, "Field", htmlFieldName) %><%
|
||||
var f = field; %>
|
||||
<%:Html.EditorFor(m => f, "Field", field.Prefix) %><%
|
||||
} %>
|
||||
</fieldset><%
|
||||
} %>
|
@ -12,5 +12,6 @@
|
||||
<div class="manage minor"><%:Html.ActionLink(T("Edit global part config").Text, "EditPart", new { area = "Orchard.ContentTypes", id = Model.PartDefinition.Name })%></div>
|
||||
<%:Html.DisplayFor(m => m.PartDefinition.Settings, "Settings", "PartDefinition")
|
||||
%><%:Html.EditorFor(m => m.PartDefinition.Fields, "TypePartFields", "PartDefinition")
|
||||
%><%:Html.Hidden("PartDefinition.Name", Model.PartDefinition.Name) %>
|
||||
%><%:Html.HiddenFor(m => m.PartDefinition.Name)
|
||||
%><%:Html.HiddenFor(m => m.Index) %>
|
||||
</fieldset>
|
@ -6,5 +6,7 @@
|
||||
Html.RenderTemplates(Model.Templates); %>
|
||||
</div><%
|
||||
} %>
|
||||
<%:Html.HiddenFor(m => m.Name) %><%:Html.HiddenFor(m => m.FieldDefinition.Name) %>
|
||||
<%:Html.HiddenFor(m => m.Name)
|
||||
%><%:Html.HiddenFor(m => m.FieldDefinition.Name)
|
||||
%><%:Html.HiddenFor(m => m.Index) %>
|
||||
</fieldset>
|
@ -1,10 +1,8 @@
|
||||
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<IEnumerable<Orchard.ContentTypes.ViewModels.EditPartFieldViewModel>>" %>
|
||||
<%
|
||||
if (Model.Any()) {
|
||||
var fi = 0;
|
||||
foreach (var field in Model) {
|
||||
var f = field;
|
||||
var htmlFieldName = string.Format("Fields[{0}]", fi++); %>
|
||||
<%:Html.EditorFor(m => f, "TypePartField", htmlFieldName) %><%
|
||||
var f = field; %>
|
||||
<%:Html.EditorFor(m => f, "TypePartField", f.Prefix) %><%
|
||||
}
|
||||
} %>
|
@ -2,11 +2,9 @@
|
||||
<%
|
||||
if (Model.Any()) { %>
|
||||
<fieldset><%
|
||||
var pi = 0;
|
||||
foreach (var part in Model) {
|
||||
var p = part;
|
||||
var htmlFieldName = string.Format("Parts[{0}]", pi++); %>
|
||||
<%:Html.EditorFor(m => p, "TypePart", htmlFieldName) %><%
|
||||
var p = part; %>
|
||||
<%:Html.EditorFor(m => p, "TypePart", p.Prefix) %><%
|
||||
} %>
|
||||
</fieldset><%
|
||||
} %>
|
@ -11,7 +11,7 @@ namespace Orchard.ContentManagement.MetaData.Builders {
|
||||
private readonly SettingsDictionary _settings;
|
||||
|
||||
public ContentTypeDefinitionBuilder()
|
||||
: this(new ContentTypeDefinition(null)) {
|
||||
: this(new ContentTypeDefinition(null, null)) {
|
||||
}
|
||||
|
||||
public ContentTypeDefinitionBuilder(ContentTypeDefinition existing) {
|
||||
|
@ -18,7 +18,7 @@ namespace Orchard.ContentManagement.MetaData {
|
||||
|
||||
public static class ContentDefinitionManagerExtensions{
|
||||
public static void AlterTypeDefinition(this IContentDefinitionManager manager, string name, Action<ContentTypeDefinitionBuilder> alteration) {
|
||||
var typeDefinition = manager.GetTypeDefinition(name) ?? new ContentTypeDefinition(name);
|
||||
var typeDefinition = manager.GetTypeDefinition(name) ?? new ContentTypeDefinition(name, name);
|
||||
var builder = new ContentTypeDefinitionBuilder(typeDefinition);
|
||||
alteration(builder);
|
||||
manager.StoreTypeDefinition(builder.Build());
|
||||
|
@ -11,8 +11,9 @@ namespace Orchard.ContentManagement.MetaData.Models {
|
||||
Settings = settings;
|
||||
}
|
||||
|
||||
public ContentTypeDefinition(string name) {
|
||||
public ContentTypeDefinition(string name, string displayName) {
|
||||
Name = name;
|
||||
DisplayName = displayName;
|
||||
Parts = Enumerable.Empty<Part>();
|
||||
Settings = new SettingsDictionary();
|
||||
}
|
||||
@ -20,7 +21,7 @@ namespace Orchard.ContentManagement.MetaData.Models {
|
||||
[StringLength(128)]
|
||||
public string Name { get; private set; }
|
||||
[Required, StringLength(1024)]
|
||||
public string DisplayName { get; set; }
|
||||
public string DisplayName { get; private set; }
|
||||
public IEnumerable<Part> Parts { get; private set; }
|
||||
public SettingsDictionary Settings { get; private set; }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user