Some progress on adding fields to parts through the UI

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-06-24 13:30:57 -07:00
parent c191c3ea63
commit 360332fbb9
11 changed files with 72 additions and 15 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
@@ -186,9 +187,8 @@ namespace Orchard.Core.Contents.Controllers {
var viewModel = new EditPartViewModel();
TryUpdateModel(viewModel);
if (!ModelState.IsValid) {
if (!ModelState.IsValid)
return EditPart(id);
}
//todo: apply the changes along the lines of but definately not resembling
// for now this _might_ just get a little messy ->
@@ -212,10 +212,10 @@ namespace Orchard.Core.Contents.Controllers {
return new NotFoundResult();
}
//get all of the field types
var viewModel = new AddFieldViewModel(contentPartDefinition);
var viewModel = new AddFieldViewModel {
Part = new EditPartViewModel(contentPartDefinition),
Fields = _contentDefinitionService.GetFieldDefinitions()
};
return View(viewModel);
}
@@ -225,9 +225,39 @@ namespace Orchard.Core.Contents.Controllers {
if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a 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) {
//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
)
);
}
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));
if (contentTypeDefinition != null)
return RedirectToAction("EditType", new { id });

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.Localization;
@@ -10,10 +11,12 @@ using Orchard.UI.Notify;
namespace Orchard.Core.Contents.Services {
public class ContentDefinitionService : IContentDefinitionService {
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly IEnumerable<IContentFieldDriver> _contentFieldDrivers;
public ContentDefinitionService(IOrchardServices services, IContentDefinitionManager contentDefinitionManager) {
public ContentDefinitionService(IOrchardServices services, IContentDefinitionManager contentDefinitionManager, IEnumerable<IContentFieldDriver> contentFieldDrivers) {
Services = services;
_contentDefinitionManager = contentDefinitionManager;
_contentFieldDrivers = contentFieldDrivers;
T = NullLocalizer.Instance;
}
@@ -76,6 +79,10 @@ namespace Orchard.Core.Contents.Services {
throw new NotImplementedException();
}
public IEnumerable<ContentFieldInfo> GetFieldDefinitions() {
return _contentFieldDrivers.SelectMany(d => d.GetFieldInfo());
}
//gratuitously stolen from the RoutableService
private static string GenerateTypeName(string displayName) {
if (string.IsNullOrWhiteSpace(displayName))

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Models;
namespace Orchard.Core.Contents.Services {
@@ -13,5 +14,7 @@ namespace Orchard.Core.Contents.Services {
void AddPartDefinition(ContentPartDefinition contentPartDefinition);
void AlterPartDefinition(ContentPartDefinition contentPartDefinition);
void RemovePartDefinition(string name);
IEnumerable<ContentFieldInfo> GetFieldDefinitions();
}
}

View File

@@ -1,12 +1,16 @@
using Orchard.ContentManagement.MetaData.Models;
using System.Collections.Generic;
using Orchard.ContentManagement.MetaData;
using Orchard.Mvc.ViewModels;
namespace Orchard.Core.Contents.ViewModels {
public class AddFieldViewModel : BaseViewModel {
public AddFieldViewModel(ContentPartDefinition part) {
Part = part;
public AddFieldViewModel() {
Fields = new List<ContentFieldInfo>();
}
public ContentPartDefinition Part { get; private set; }
public string DisplayName { get; set; }
public string FieldTypeName { get; set; }
public EditPartViewModel Part { get; set; }
public IEnumerable<ContentFieldInfo> Fields { get; set; }
}
}

View File

@@ -4,6 +4,14 @@ Html.RegisterStyle("admin.css"); %>
<h1><%:Html.TitleForPage(T("Add a new field to {0}", Model.Part.Name).ToString())%></h1><%
using (Html.BeginFormAntiForgeryPost()) { %>
<%:Html.ValidationSummary() %>
<fieldset>
<label for="DisplayName"><%:T("Display Name") %></label>
<%:Html.TextBoxFor(m => m.DisplayName, new {@class = "textMedium", autofocus = "autofocus"}) %>
</fieldset>
<fieldset>
<label for="FieldTypeName"><%:T("Field Type") %></label>
<%:Html.DropDownListFor(m => m.FieldTypeName, new SelectList(Model.Fields, "FieldTypeName", "FieldTypeName"))%>
</fieldset>
<fieldset>
<button class="primaryAction" type="submit"><%:T("Save") %></button>
</fieldset><%

View File

@@ -4,7 +4,7 @@ if (Model.Any()) { %>
<dl><%
foreach (var field in Model) {
var f = field; %>
<%:Html.EditorFor(m => f, "Part.Field") %><%
<%:Html.DisplayFor(m => f, "Field") %><%
} %>
</dl><%
} %>

View File

@@ -15,6 +15,6 @@
<h4><%:T("Global configuration") %></h4>
<div class="manage minor"><%:Html.ActionLink(T("Edit").Text, "EditPart", new { area = "Contents", id = Model.PartDefinition.Name }) %></div>
<%:Html.DisplayFor(m => m.PartDefinition.Settings, "Settings", "PartDefinition") %>
<%:Html.EditorFor(m => m.PartDefinition.Fields, "Part.Fields") %>
<%:Html.DisplayFor(m => m.PartDefinition.Fields, "Fields") %>
<%:Html.Hidden("PartDefinition.Name", Model.PartDefinition.Name) %>
</fieldset>

View File

@@ -224,8 +224,8 @@
<Content Include="Contents\Views\EditorTemplates\Field.ascx" />
<Content Include="Contents\Views\EditorTemplates\Fields.ascx" />
<Content Include="Contents\Views\EditorTemplates\Settings.ascx" />
<Content Include="Contents\Views\EditorTemplates\Part.Fields.ascx" />
<Content Include="Contents\Views\EditorTemplates\Part.Field.ascx" />
<Content Include="Contents\Views\DisplayTemplates\Fields.ascx" />
<Content Include="Contents\Views\DisplayTemplates\Field.ascx" />
<Content Include="Contents\Views\EditorTemplates\Parts.ascx" />
<Content Include="Contents\Views\EditorTemplates\Part.ascx" />
<Content Include="Contents\Views\EditorTemplates\Items\Contents.Item.ascx" />

View File

@@ -46,6 +46,10 @@ namespace Orchard.Core.Settings.Metadata {
return _partDefinitionRepository.Fetch(x => !x.Hidden).Select(Build).ToReadOnlyCollection();
}
public IEnumerable<ContentFieldDefinition> ListFieldDefinitions() {
return _fieldDefinitionRepository.Fetch(x => true, cfdr => cfdr.Asc(fdr => fdr.Name)).Select(Build).ToReadOnlyCollection();
}
public void StoreTypeDefinition(ContentTypeDefinition contentTypeDefinition) {
Apply(contentTypeDefinition, Acquire(contentTypeDefinition));
}

View File

@@ -7,6 +7,7 @@ namespace Orchard.ContentManagement.MetaData {
public interface IContentDefinitionManager : IDependency {
IEnumerable<ContentTypeDefinition> ListTypeDefinitions();
IEnumerable<ContentPartDefinition> ListPartDefinitions();
IEnumerable<ContentFieldDefinition> ListFieldDefinitions();
ContentTypeDefinition GetTypeDefinition(string name);
ContentPartDefinition GetPartDefinition(string name);