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:
Nathan Heskew 2010-07-07 10:16:30 -07:00
parent e9a0e09732
commit 75d7f6bc68
20 changed files with 371 additions and 314 deletions

View File

@ -86,6 +86,7 @@ namespace Orchard.Core.Settings.Metadata {
} }
private void Apply(ContentTypeDefinition model, ContentTypeDefinitionRecord record) { private void Apply(ContentTypeDefinition model, ContentTypeDefinitionRecord record) {
record.DisplayName = model.DisplayName;
record.Settings = _settingsWriter.Map(model.Settings).ToString(); record.Settings = _settingsWriter.Map(model.Settings).ToString();
var toRemove = record.ContentTypePartDefinitionRecords var toRemove = record.ContentTypePartDefinitionRecords

View File

@ -1,8 +1,6 @@
using System; using System.Linq;
using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Models; using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentTypes.Services; using Orchard.ContentTypes.Services;
using Orchard.ContentTypes.ViewModels; using Orchard.ContentTypes.ViewModels;
@ -11,34 +9,25 @@ using Orchard.Mvc.Results;
using Orchard.UI.Notify; using Orchard.UI.Notify;
namespace Orchard.ContentTypes.Controllers { namespace Orchard.ContentTypes.Controllers {
public class AdminController : Controller { public class AdminController : Controller, IUpdateModel {
private readonly IContentDefinitionService _contentDefinitionService; private readonly IContentDefinitionService _contentDefinitionService;
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly IContentDefinitionEditorEvents _extendViewModels;
public AdminController( public AdminController(IOrchardServices orchardServices, IContentDefinitionService contentDefinitionService) {
IOrchardServices orchardServices,
IContentDefinitionService contentDefinitionService,
IContentDefinitionManager contentDefinitionManager,
IContentDefinitionEditorEvents extendViewModels) {
Services = orchardServices; Services = orchardServices;
_contentDefinitionService = contentDefinitionService; _contentDefinitionService = contentDefinitionService;
_contentDefinitionManager = contentDefinitionManager;
_extendViewModels = extendViewModels;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
} }
public IOrchardServices Services { get; private set; } public IOrchardServices Services { get; private set; }
public Localizer T { get; set; } public Localizer T { get; set; }
public ActionResult Index() {
return List(); public ActionResult Index() { return List(); }
}
#region Types #region Types
public ActionResult List() { public ActionResult List() {
return View("List", new ListContentTypesViewModel { 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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to create a content type.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
if (!ModelState.IsValid) var typeViewModel = _contentDefinitionService.AddType(viewModel);
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);
});
}
});
}
});
if (!ModelState.IsValid) { if (!ModelState.IsValid) {
Services.TransactionManager.Cancel(); Services.TransactionManager.Cancel();
return View(viewModel); 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"); 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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id); var typeViewModel = _contentDefinitionService.GetType(id);
if (contentTypeDefinition == null) if (typeViewModel == null)
return new NotFoundResult(); return new NotFoundResult();
var viewModel = new AddPartsViewModel { var viewModel = new AddPartsViewModel {
Type = new EditTypeViewModel(contentTypeDefinition), Type = typeViewModel,
PartSelections = _contentDefinitionService.GetPartDefinitions() PartSelections = _contentDefinitionService.GetParts()
.Where(cpd => !contentTypeDefinition.Parts.Any(p => p.PartDefinition.Name == cpd.Name)) .Where(cpd => !typeViewModel.Parts.Any(p => p.PartDefinition.Name == cpd.Name))
.Select(cpd => new PartSelectionViewModel {PartName = cpd.Name}) .Select(cpd => new PartSelectionViewModel { PartName = cpd.Name })
}; };
return View(viewModel); 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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id); var typeViewModel = _contentDefinitionService.GetType(id);
if (contentTypeDefinition == null) if (typeViewModel == null)
return new NotFoundResult(); return new NotFoundResult();
var viewModel = new AddPartsViewModel(); var viewModel = new AddPartsViewModel();
TryUpdateModel(viewModel); if (!TryUpdateModel(viewModel)) {
viewModel.Type = typeViewModel;
if (!ModelState.IsValid) {
viewModel.Type = new EditTypeViewModel(contentTypeDefinition);
return View(viewModel); return View(viewModel);
} }
_contentDefinitionManager.AlterTypeDefinition(contentTypeDefinition.Name, typeBuilder => { var partsToAdd = viewModel.PartSelections.Where(ps => ps.IsSelected).Select(ps => ps.PartName);
var partsToAdd = viewModel.PartSelections.Where(ps => ps.IsSelected).Select(ps => ps.PartName); foreach (var partToAdd in partsToAdd) {
foreach (var partToAdd in partsToAdd) _contentDefinitionService.AddPartToType(partToAdd, typeViewModel.Name);
typeBuilder.WithPart(partToAdd); 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}); 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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id); var typeViewModel = _contentDefinitionService.GetType(id);
var viewModel = new RemovePartViewModel(); var viewModel = new RemovePartViewModel();
if (contentTypeDefinition == null if (typeViewModel == null
|| !TryUpdateModel(viewModel) || !TryUpdateModel(viewModel)
|| !contentTypeDefinition.Parts.Any(p => p.PartDefinition.Name == viewModel.Name)) || !typeViewModel.Parts.Any(p => p.PartDefinition.Name == viewModel.Name))
return new NotFoundResult(); return new NotFoundResult();
viewModel.Type = new EditTypeViewModel { Name = contentTypeDefinition.Name, DisplayName = contentTypeDefinition.DisplayName }; viewModel.Type = typeViewModel;
return View(viewModel); 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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content type.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id); var typeViewModel = _contentDefinitionService.GetType(id);
var viewModel = new RemovePartViewModel(); var viewModel = new RemovePartViewModel();
if (contentTypeDefinition == null if (typeViewModel == null
|| !TryUpdateModel(viewModel) || !TryUpdateModel(viewModel)
|| !contentTypeDefinition.Parts.Any(p => p.PartDefinition.Name == viewModel.Name)) || !typeViewModel.Parts.Any(p => p.PartDefinition.Name == viewModel.Name))
return new NotFoundResult(); return new NotFoundResult();
_contentDefinitionService.RemovePartFromType(viewModel.Name, typeViewModel.Name);
if (!ModelState.IsValid) { if (!ModelState.IsValid) {
viewModel.Type = new EditTypeViewModel { Name = contentTypeDefinition.Name, DisplayName = contentTypeDefinition.DisplayName }; Services.TransactionManager.Cancel();
viewModel.Type = typeViewModel;
return View(viewModel); return View(viewModel);
} }
_contentDefinitionManager.AlterTypeDefinition(id, typeBuilder => typeBuilder.RemovePart(viewModel.Name));
Services.Notifier.Information(T("The \"{0}\" part has been removed.", viewModel.Name)); Services.Notifier.Information(T("The \"{0}\" part has been removed.", viewModel.Name));
return RedirectToAction("Edit", new {id}); return RedirectToAction("Edit", new {id});
@ -261,7 +190,7 @@ namespace Orchard.ContentTypes.Controllers {
public ActionResult ListParts() { public ActionResult ListParts() {
return View(new ListContentPartsViewModel { 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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to create a content part.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
var partViewModel = _contentDefinitionService.AddPart(viewModel);
if (!ModelState.IsValid) if (!ModelState.IsValid)
return View(viewModel); 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) { public ActionResult EditPart(string id) {
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id); var partViewModel = _contentDefinitionService.GetPart(id);
if (contentPartDefinition == null) if (partViewModel == null)
return new NotFoundResult(); return new NotFoundResult();
var viewModel = new EditPartViewModel(contentPartDefinition) { return View(partViewModel);
Templates = _extendViewModels.PartEditor(contentPartDefinition)
};
return View(viewModel);
} }
[HttpPost, ActionName("EditPart")] [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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(viewModel.Name); var partViewModel = _contentDefinitionService.GetPart(id);
if (contentPartDefinition == null) if (partViewModel == null)
return new NotFoundResult(); return new NotFoundResult();
var updater = new Updater(this); if (!TryUpdateModel(partViewModel))
_contentDefinitionManager.AlterPartDefinition(viewModel.Name, partBuilder => { return View(partViewModel);
// allow extensions to alter part configuration
viewModel.Templates = _extendViewModels.PartEditorUpdate(partBuilder, updater); _contentDefinitionService.AlterPart(partViewModel, this);
});
if (!ModelState.IsValid) { if (!ModelState.IsValid) {
Services.TransactionManager.Cancel(); Services.TransactionManager.Cancel();
return View(viewModel); return View(partViewModel);
} }
Services.Notifier.Information(T("\"{0}\" settings have been saved.", partViewModel.Name));
return RedirectToAction("ListParts"); 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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
return new HttpUnauthorizedResult(); 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 //id passed in might be that of a type w/ no implicit field
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id); var typeViewModel = _contentDefinitionService.GetType(id);
if (contentTypeDefinition != null) if (typeViewModel != null)
contentPartDefinition = new ContentPartDefinition(id); partViewModel = new EditPartViewModel(new ContentPartDefinition(id));
else else
return new NotFoundResult(); return new NotFoundResult();
} }
var viewModel = new AddFieldViewModel { var viewModel = new AddFieldViewModel {
Part = new EditPartViewModel(contentPartDefinition), Part = partViewModel,
Fields = _contentDefinitionService.GetFieldDefinitions() Fields = _contentDefinitionService.GetFields()
}; };
return View(viewModel); 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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
var viewModel = new AddFieldViewModel(); var partViewModel = _contentDefinitionService.GetPart(id);
TryUpdateModel(viewModel); var typeViewModel = _contentDefinitionService.GetType(id);
if (partViewModel == null) {
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id);
var contentTypeDefinition = _contentDefinitionService.GetTypeDefinition(id);
if (!ModelState.IsValid)
return AddFieldTo(id);
if (contentPartDefinition == null) {
//id passed in might be that of a type w/ no implicit field //id passed in might be that of a type w/ no implicit field
if (contentTypeDefinition != null) { if (typeViewModel != null) {
contentPartDefinition = new ContentPartDefinition(id); partViewModel = new EditPartViewModel { Name = typeViewModel.Name };
var contentTypeDefinitionParts = contentTypeDefinition.Parts.ToList(); _contentDefinitionService.AddPart(new CreatePartViewModel { Name = partViewModel.Name });
contentTypeDefinitionParts.Add(new ContentTypeDefinition.Part(contentPartDefinition, null)); _contentDefinitionService.AddPartToType(partViewModel.Name, typeViewModel.Name);
_contentDefinitionService.AlterTypeDefinition(
new ContentTypeDefinition(
contentTypeDefinition.Name,
contentTypeDefinition.DisplayName,
contentTypeDefinitionParts,
contentTypeDefinition.Settings
)
);
} }
else { else {
return new NotFoundResult(); return new NotFoundResult();
} }
} }
var contentPartFields = contentPartDefinition.Fields.ToList(); var viewModel = new AddFieldViewModel();
contentPartFields.Add(new ContentPartDefinition.Field(new ContentFieldDefinition(viewModel.FieldTypeName), viewModel.DisplayName, null)); if (!TryUpdateModel(viewModel)) {
_contentDefinitionService.AlterPartDefinition(new ContentPartDefinition(contentPartDefinition.Name, contentPartFields, contentPartDefinition.Settings)); 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)); 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("Edit", new { id });
return RedirectToAction("EditPart", 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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id); var partViewModel = _contentDefinitionService.GetPart(id);
var viewModel = new RemoveFieldViewModel(); var viewModel = new RemoveFieldViewModel();
if (contentPartDefinition == null if (partViewModel == null
|| !TryUpdateModel(viewModel) || !TryUpdateModel(viewModel)
|| !contentPartDefinition.Fields.Any(p => p.Name == viewModel.Name)) || !partViewModel.Fields.Any(p => p.Name == viewModel.Name))
return new NotFoundResult(); return new NotFoundResult();
viewModel.Part = new EditPartViewModel { Name = contentPartDefinition.Name }; viewModel.Part = partViewModel;
return View(viewModel); 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."))) if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part.")))
return new HttpUnauthorizedResult(); return new HttpUnauthorizedResult();
var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id); var partViewModel = _contentDefinitionService.GetPart(id);
var viewModel = new RemoveFieldViewModel(); var viewModel = new RemoveFieldViewModel();
if (contentPartDefinition == null if (partViewModel == null
|| !TryUpdateModel(viewModel) || !TryUpdateModel(viewModel)
|| !contentPartDefinition.Fields.Any(p => p.Name == viewModel.Name)) || !partViewModel.Fields.Any(p => p.Name == viewModel.Name))
return new NotFoundResult(); return new NotFoundResult();
_contentDefinitionService.RemoveFieldFromPart(viewModel.Name, partViewModel.Name);
if (!ModelState.IsValid) { if (!ModelState.IsValid) {
viewModel.Part = new EditPartViewModel { Name = contentPartDefinition.Name }; Services.TransactionManager.Cancel();
viewModel.Part = partViewModel;
return View(viewModel); return View(viewModel);
} }
_contentDefinitionManager.AlterPartDefinition(id, typeBuilder => typeBuilder.RemoveField(viewModel.Name));
Services.Notifier.Information(T("The \"{0}\" field has been removed.", 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("Edit", new { id });
return RedirectToAction("EditPart", new { id }); return RedirectToAction("EditPart", new { id });
@ -440,23 +364,12 @@ namespace Orchard.ContentTypes.Controllers {
#endregion #endregion
class Updater : IUpdateModel { public new bool TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) where TModel : class {
private readonly AdminController _thunk; return base.TryUpdateModel(model, prefix, includeProperties, excludeProperties);
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 void AddModelError(string key, LocalizedString errorMessage) {
ModelState.AddModelError(key, errorMessage.ToString());
}
} }
} }

View File

@ -99,8 +99,8 @@
<Content Include="Views\Admin\EditPart.ascx" /> <Content Include="Views\Admin\EditPart.ascx" />
<Content Include="Views\Admin\Edit.ascx" /> <Content Include="Views\Admin\Edit.ascx" />
<Content Include="Views\Admin\List.ascx" /> <Content Include="Views\Admin\List.ascx" />
<Content Include="Views\DisplayTemplates\ContentTypeDefinition.ascx" /> <Content Include="Views\DisplayTemplates\EditTypeViewModel.ascx" />
<Content Include="Views\DisplayTemplates\ContentPartDefinition.ascx" /> <Content Include="Views\DisplayTemplates\EditPartViewModel.ascx" />
<Content Include="Views\DisplayTemplates\Field.ascx" /> <Content Include="Views\DisplayTemplates\Field.ascx" />
<Content Include="Views\DisplayTemplates\Fields.ascx" /> <Content Include="Views\DisplayTemplates\Fields.ascx" />
<Content Include="Views\DisplayTemplates\Settings.ascx" /> <Content Include="Views\DisplayTemplates\Settings.ascx" />

View File

@ -2,42 +2,69 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers; using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData; using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Models; using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentTypes.ViewModels;
using Orchard.Localization; using Orchard.Localization;
using Orchard.UI.Notify;
namespace Orchard.ContentTypes.Services { namespace Orchard.ContentTypes.Services {
public class ContentDefinitionService : IContentDefinitionService { public class ContentDefinitionService : IContentDefinitionService {
private readonly IContentDefinitionManager _contentDefinitionManager; private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly IEnumerable<IContentFieldDriver> _contentFieldDrivers; 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; Services = services;
_contentDefinitionManager = contentDefinitionManager; _contentDefinitionManager = contentDefinitionManager;
_contentFieldDrivers = contentFieldDrivers; _contentFieldDrivers = contentFieldDrivers;
_contentDefinitionEditorEvents = contentDefinitionEditorEvents;
T = NullLocalizer.Instance; T = NullLocalizer.Instance;
} }
public IOrchardServices Services { get; set; } public IOrchardServices Services { get; set; }
public Localizer T { get; set; } public Localizer T { get; set; }
public IEnumerable<ContentTypeDefinition> GetTypeDefinitions() { public IEnumerable<EditTypeViewModel> GetTypes() {
return _contentDefinitionManager.ListTypeDefinitions(); return _contentDefinitionManager.ListTypeDefinitions().Select(ctd => new EditTypeViewModel(ctd));
} }
public ContentTypeDefinition GetTypeDefinition(string name) { public EditTypeViewModel GetType(string name) {
return _contentDefinitionManager.GetTypeDefinition(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) { public EditTypeViewModel AddType(CreateTypeViewModel typeViewModel) {
var name = GenerateName(displayName); var name = GenerateName(typeViewModel.DisplayName);
while (_contentDefinitionManager.GetTypeDefinition(name) != null) while (_contentDefinitionManager.GetTypeDefinition(name) != null)
name = VersionName(name); 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 //just giving the new type some default parts for now
_contentDefinitionManager.StoreTypeDefinition(contentTypeDefinition); _contentDefinitionManager.StoreTypeDefinition(contentTypeDefinition);
_contentDefinitionManager.AlterTypeDefinition( _contentDefinitionManager.AlterTypeDefinition(
@ -45,36 +72,99 @@ namespace Orchard.ContentTypes.Services {
cfg => cfg.WithPart("CommonAspect") cfg => cfg.WithPart("CommonAspect")
//.WithPart("RoutableAspect") //need to go the new routable route //.WithPart("RoutableAspect") //need to go the new routable route
.WithPart("BodyAspect")); .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) { public void AlterType(EditTypeViewModel typeViewModel, IUpdateModel updateModel) {
_contentDefinitionManager.StoreTypeDefinition(contentTypeDefinition); 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); // allow extensions to alter type configuration
if (implicitTypePart != null) { typeViewModel.Templates = _contentDefinitionEditorEvents.TypeEditorUpdate(typeBuilder, updater);
AlterPartDefinition(implicitTypePart.PartDefinition);
} 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(); throw new NotImplementedException();
} }
public IEnumerable<ContentPartDefinition> GetPartDefinitions() { public void AddPartToType(string partName, string typeName) {
var typeNames = GetTypeDefinitions().Select(ctd => ctd.Name); _contentDefinitionManager.AlterTypeDefinition(typeName, typeBuilder => typeBuilder.WithPart(partName));
return _contentDefinitionManager.ListPartDefinitions().Where(cpd => !typeNames.Contains(cpd.Name));
} }
public ContentPartDefinition GetPartDefinition(string name) { public void RemovePartFromType(string partName, string typeName) {
return _contentDefinitionManager.GetPartDefinition(name); _contentDefinitionManager.AlterTypeDefinition(typeName, typeBuilder => typeBuilder.RemovePart(partName));
} }
public ContentPartDefinition AddPartDefinition(string name) { public IEnumerable<EditPartViewModel> GetParts() {
name = GenerateName(name); 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) while (_contentDefinitionManager.GetPartDefinition(name) != null)
name = VersionName(name); name = VersionName(name);
@ -82,21 +172,34 @@ namespace Orchard.ContentTypes.Services {
var contentPartDefinition = new ContentPartDefinition(name); var contentPartDefinition = new ContentPartDefinition(name);
_contentDefinitionManager.StorePartDefinition(contentPartDefinition); _contentDefinitionManager.StorePartDefinition(contentPartDefinition);
return contentPartDefinition; return new EditPartViewModel(contentPartDefinition);
} }
public void AlterPartDefinition(ContentPartDefinition contentPartDefinition) { public void AlterPart(EditPartViewModel partViewModel, IUpdateModel updateModel) {
_contentDefinitionManager.StorePartDefinition(contentPartDefinition); 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(); throw new NotImplementedException();
} }
public IEnumerable<ContentFieldInfo> GetFieldDefinitions() { public IEnumerable<ContentFieldInfo> GetFields() {
return _contentFieldDrivers.SelectMany(d => d.GetFieldInfo()); 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 //gratuitously stolen from the RoutableService
private static string GenerateName(string displayName) { private static string GenerateName(string displayName) {
if (string.IsNullOrWhiteSpace(displayName)) if (string.IsNullOrWhiteSpace(displayName))
@ -130,5 +233,23 @@ namespace Orchard.ContentTypes.Services {
return string.Format("{0}-{1}", name, version); 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);
}
}
} }
} }

View File

@ -1,21 +1,26 @@
using System.Collections.Generic; using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData; using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Models; using Orchard.ContentTypes.ViewModels;
namespace Orchard.ContentTypes.Services { namespace Orchard.ContentTypes.Services {
public interface IContentDefinitionService : IDependency { public interface IContentDefinitionService : IDependency {
IEnumerable<ContentTypeDefinition> GetTypeDefinitions(); IEnumerable<EditTypeViewModel> GetTypes();
ContentTypeDefinition GetTypeDefinition(string name); EditTypeViewModel GetType(string name);
ContentTypeDefinition AddTypeDefinition(string displayName); EditTypeViewModel AddType(CreateTypeViewModel typeViewModel);
void AlterTypeDefinition(ContentTypeDefinition contentTypeDefinition); void AlterType(EditTypeViewModel typeViewModel, IUpdateModel updater);
void RemoveTypeDefinition(string name); void RemoveType(string name);
void AddPartToType(string partName, string typeName);
void RemovePartFromType(string partName, string typeName);
IEnumerable<ContentPartDefinition> GetPartDefinitions(); IEnumerable<EditPartViewModel> GetParts();
ContentPartDefinition GetPartDefinition(string name); EditPartViewModel GetPart(string name);
ContentPartDefinition AddPartDefinition(string name); EditPartViewModel AddPart(CreatePartViewModel partViewModel);
void AlterPartDefinition(ContentPartDefinition contentPartDefinition); void AlterPart(EditPartViewModel partViewModel, IUpdateModel updater);
void RemovePartDefinition(string name); void RemovePart(string name);
IEnumerable<ContentFieldInfo> GetFieldDefinitions(); IEnumerable<ContentFieldInfo> GetFields();
void AddFieldToPart(string fieldName, string fieldTypeName, string partName);
void RemoveFieldFromPart(string fieldName, string partName);
} }
} }

View File

@ -11,34 +11,36 @@ namespace Orchard.ContentTypes.ViewModels {
Fields = new List<EditPartFieldViewModel>(); Fields = new List<EditPartFieldViewModel>();
Parts = new List<EditTypePartViewModel>(); Parts = new List<EditTypePartViewModel>();
} }
public EditTypeViewModel(ContentTypeDefinition contentTypeDefinition) { public EditTypeViewModel(ContentTypeDefinition contentTypeDefinition) {
Name = contentTypeDefinition.Name; Name = contentTypeDefinition.Name;
DisplayName = contentTypeDefinition.DisplayName; DisplayName = contentTypeDefinition.DisplayName;
Settings = contentTypeDefinition.Settings; Settings = contentTypeDefinition.Settings;
Fields = GetTypeFields(contentTypeDefinition).ToList(); Fields = GetTypeFields(contentTypeDefinition).ToList();
Parts = GetTypeParts(contentTypeDefinition).ToList(); Parts = GetTypeParts(contentTypeDefinition).ToList();
Definition = contentTypeDefinition; _Definition = contentTypeDefinition;
} }
public string Name { get; set; } public string Name { get; set; }
public string DisplayName { 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 SettingsDictionary Settings { get; set; }
public IEnumerable<EditPartFieldViewModel> Fields { get; set; } public IEnumerable<EditPartFieldViewModel> Fields { get; set; }
public IEnumerable<EditTypePartViewModel> Parts { 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) { private IEnumerable<EditPartFieldViewModel> GetTypeFields(ContentTypeDefinition contentTypeDefinition) {
var implicitTypePart = contentTypeDefinition.Parts.SingleOrDefault(p => p.PartDefinition.Name == Name); var implicitTypePart = contentTypeDefinition.Parts.SingleOrDefault(p => p.PartDefinition.Name == Name);
return implicitTypePart == null return implicitTypePart == null
? Enumerable.Empty<EditPartFieldViewModel>() ? 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) { 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() { public EditTypePartViewModel() {
Settings = new SettingsDictionary(); Settings = new SettingsDictionary();
} }
public EditTypePartViewModel(ContentTypeDefinition.Part part) {
public EditTypePartViewModel(int index, ContentTypeDefinition.Part part) {
Index = index;
PartDefinition = new EditPartViewModel(part.PartDefinition); PartDefinition = new EditPartViewModel(part.PartDefinition);
Settings = part.Settings; 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 EditPartViewModel PartDefinition { get; set; }
public SettingsDictionary Settings { get; set; } public SettingsDictionary Settings { get; set; }
public EditTypeViewModel Type { get; set; }
public IEnumerable<TemplateViewModel> Templates { get; set; } public IEnumerable<TemplateViewModel> Templates { get; set; }
public ContentTypeDefinition.Part _Definition { get; private set; }
} }
public class EditPartViewModel : BaseViewModel { public class EditPartViewModel : BaseViewModel {
@ -62,45 +70,56 @@ namespace Orchard.ContentTypes.ViewModels {
Fields = new List<EditPartFieldViewModel>(); Fields = new List<EditPartFieldViewModel>();
Settings = new SettingsDictionary(); Settings = new SettingsDictionary();
} }
public EditPartViewModel(ContentPartDefinition contentPartDefinition) { public EditPartViewModel(ContentPartDefinition contentPartDefinition) {
Name = contentPartDefinition.Name; 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; Settings = contentPartDefinition.Settings;
Definition = contentPartDefinition; _Definition = contentPartDefinition;
} }
public string Prefix { get { return "PartDefinition"; } }
public string Name { get; set; } public string Name { get; set; }
public IEnumerable<TemplateViewModel> Templates { get; set; } public IEnumerable<TemplateViewModel> Templates { get; set; }
public IEnumerable<EditPartFieldViewModel> Fields { get; set; } public IEnumerable<EditPartFieldViewModel> Fields { get; set; }
public ContentPartDefinition Definition { get; private set; }
public SettingsDictionary Settings { get; set; } public SettingsDictionary Settings { get; set; }
public ContentPartDefinition _Definition { get; private set; }
} }
public class EditPartFieldViewModel { public class EditPartFieldViewModel {
public EditPartFieldViewModel() { public EditPartFieldViewModel() {
Settings = new SettingsDictionary(); Settings = new SettingsDictionary();
} }
public EditPartFieldViewModel(ContentPartDefinition.Field field) {
public EditPartFieldViewModel(int index, ContentPartDefinition.Field field) {
Index = index;
Name = field.Name; Name = field.Name;
FieldDefinition = new EditFieldViewModel(field.FieldDefinition); FieldDefinition = new EditFieldViewModel(field.FieldDefinition);
Settings = field.Settings; Settings = field.Settings;
_Definition = field;
} }
public int Index { get; set; }
public string Prefix { get { return "Fields[" + Index + "]"; } }
public EditPartViewModel Part { get; set; } public EditPartViewModel Part { get; set; }
public string Name { get; set; } public string Name { get; set; }
public IEnumerable<TemplateViewModel> Templates { get; set; } public IEnumerable<TemplateViewModel> Templates { get; set; }
public EditFieldViewModel FieldDefinition { get; set; } public EditFieldViewModel FieldDefinition { get; set; }
public SettingsDictionary Settings { get; set; } public SettingsDictionary Settings { get; set; }
public ContentPartDefinition.Field _Definition { get; private set; }
} }
public class EditFieldViewModel { public class EditFieldViewModel {
public EditFieldViewModel() { } public EditFieldViewModel() { }
public EditFieldViewModel(ContentFieldDefinition contentFieldDefinition) { public EditFieldViewModel(ContentFieldDefinition contentFieldDefinition) {
Name = contentFieldDefinition.Name; Name = contentFieldDefinition.Name;
Definition = contentFieldDefinition; _Definition = contentFieldDefinition;
} }
public string Name { get; set; } public string Name { get; set; }
public ContentFieldDefinition Definition { get; private set; } public ContentFieldDefinition _Definition { get; private set; }
} }
} }

View File

@ -1,9 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.Mvc.ViewModels; using Orchard.Mvc.ViewModels;
namespace Orchard.ContentTypes.ViewModels { namespace Orchard.ContentTypes.ViewModels {
public class ListContentPartsViewModel : BaseViewModel { public class ListContentPartsViewModel : BaseViewModel {
public IEnumerable<ContentPartDefinition> Parts { get; set; } public IEnumerable<EditPartViewModel> Parts { get; set; }
} }
} }

View File

@ -1,9 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.Mvc.ViewModels; using Orchard.Mvc.ViewModels;
namespace Orchard.ContentTypes.ViewModels { namespace Orchard.ContentTypes.ViewModels {
public class ListContentTypesViewModel : BaseViewModel { public class ListContentTypesViewModel : BaseViewModel {
public IEnumerable<ContentTypeDefinition> Types { get; set; } public IEnumerable<EditTypeViewModel> Types { get; set; }
} }
} }

View File

@ -17,12 +17,12 @@ using (Html.BeginFormAntiForgeryPost()) { %>
<%:Html.HiddenFor(m => m.Name) %> <%:Html.HiddenFor(m => m.Name) %>
</fieldset><% </fieldset><%
Html.RenderTemplates(Model.Templates); %> 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> <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><%: <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", "") %> 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"> <fieldset class="action">
<button class="primaryAction" type="submit"><%:T("Save") %></button> <button class="primaryAction" type="submit"><%:T("Save") %></button>
</fieldset> </fieldset>

View File

@ -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="summary">
<div class="properties"> <div class="properties">
<h3><%:Model.Name%></h3> <h3><%:Model.Name%></h3>

View File

@ -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="summary">
<div class="properties"> <div class="properties">
<h3><%:Model.DisplayName%></h3> <h3><%:Model.DisplayName%></h3>

View File

@ -4,6 +4,8 @@
<div class="manage"> <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--%> <%:Html.ActionLink(T("Remove").Text, "RemoveFieldFrom", new { area = "Orchard.ContentTypes", id = Model.Part.Name, Model.Name }, new { itemprop = "RemoveUrl UnsafeUrl" })%><%--// <- some experimentation--%>
</div><% </div><%
Html.RenderTemplates(Model.Templates); %> Html.RenderTemplates(Model.Templates);
<%: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> </fieldset>

View File

@ -2,11 +2,9 @@
<% <%
if (Model.Any()) { %> if (Model.Any()) { %>
<fieldset><% <fieldset><%
var fi = 0;
foreach (var field in Model) { foreach (var field in Model) {
var f = field; var f = field; %>
var htmlFieldName = string.Format("Fields[{0}]", fi++); %> <%:Html.EditorFor(m => f, "Field", field.Prefix) %><%
<%:Html.EditorFor(m => f, "Field", htmlFieldName) %><%
} %> } %>
</fieldset><% </fieldset><%
} %> } %>

View File

@ -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> <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.DisplayFor(m => m.PartDefinition.Settings, "Settings", "PartDefinition")
%><%:Html.EditorFor(m => m.PartDefinition.Fields, "TypePartFields", "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> </fieldset>

View File

@ -6,5 +6,7 @@
Html.RenderTemplates(Model.Templates); %> Html.RenderTemplates(Model.Templates); %>
</div><% </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> </fieldset>

View File

@ -1,10 +1,8 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<IEnumerable<Orchard.ContentTypes.ViewModels.EditPartFieldViewModel>>" %> <%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<IEnumerable<Orchard.ContentTypes.ViewModels.EditPartFieldViewModel>>" %>
<% <%
if (Model.Any()) { if (Model.Any()) {
var fi = 0;
foreach (var field in Model) { foreach (var field in Model) {
var f = field; var f = field; %>
var htmlFieldName = string.Format("Fields[{0}]", fi++); %> <%:Html.EditorFor(m => f, "TypePartField", f.Prefix) %><%
<%:Html.EditorFor(m => f, "TypePartField", htmlFieldName) %><%
} }
} %> } %>

View File

@ -2,11 +2,9 @@
<% <%
if (Model.Any()) { %> if (Model.Any()) { %>
<fieldset><% <fieldset><%
var pi = 0;
foreach (var part in Model) { foreach (var part in Model) {
var p = part; var p = part; %>
var htmlFieldName = string.Format("Parts[{0}]", pi++); %> <%:Html.EditorFor(m => p, "TypePart", p.Prefix) %><%
<%:Html.EditorFor(m => p, "TypePart", htmlFieldName) %><%
} %> } %>
</fieldset><% </fieldset><%
} %> } %>

View File

@ -11,7 +11,7 @@ namespace Orchard.ContentManagement.MetaData.Builders {
private readonly SettingsDictionary _settings; private readonly SettingsDictionary _settings;
public ContentTypeDefinitionBuilder() public ContentTypeDefinitionBuilder()
: this(new ContentTypeDefinition(null)) { : this(new ContentTypeDefinition(null, null)) {
} }
public ContentTypeDefinitionBuilder(ContentTypeDefinition existing) { public ContentTypeDefinitionBuilder(ContentTypeDefinition existing) {

View File

@ -18,7 +18,7 @@ namespace Orchard.ContentManagement.MetaData {
public static class ContentDefinitionManagerExtensions{ public static class ContentDefinitionManagerExtensions{
public static void AlterTypeDefinition(this IContentDefinitionManager manager, string name, Action<ContentTypeDefinitionBuilder> alteration) { 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); var builder = new ContentTypeDefinitionBuilder(typeDefinition);
alteration(builder); alteration(builder);
manager.StoreTypeDefinition(builder.Build()); manager.StoreTypeDefinition(builder.Build());

View File

@ -11,8 +11,9 @@ namespace Orchard.ContentManagement.MetaData.Models {
Settings = settings; Settings = settings;
} }
public ContentTypeDefinition(string name) { public ContentTypeDefinition(string name, string displayName) {
Name = name; Name = name;
DisplayName = displayName;
Parts = Enumerable.Empty<Part>(); Parts = Enumerable.Empty<Part>();
Settings = new SettingsDictionary(); Settings = new SettingsDictionary();
} }
@ -20,7 +21,7 @@ namespace Orchard.ContentManagement.MetaData.Models {
[StringLength(128)] [StringLength(128)]
public string Name { get; private set; } public string Name { get; private set; }
[Required, StringLength(1024)] [Required, StringLength(1024)]
public string DisplayName { get; set; } public string DisplayName { get; private set; }
public IEnumerable<Part> Parts { get; private set; } public IEnumerable<Part> Parts { get; private set; }
public SettingsDictionary Settings { get; private set; } public SettingsDictionary Settings { get; private set; }