mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-18 17:47:54 +08:00
Hooked up the ability to add parts to content types (in the edit type UI)
--HG-- branch : dev
This commit is contained in:
@@ -192,6 +192,52 @@ namespace Orchard.ContentTypes.Controllers {
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
public ActionResult AddPartsTo(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 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})
|
||||
};
|
||||
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
[HttpPost, ActionName("AddPartsTo")]
|
||||
public ActionResult AddPartsToPOST(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 AddPartsViewModel();
|
||||
TryUpdateModel(viewModel);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
viewModel.Type = new EditTypeViewModel(contentTypeDefinition);
|
||||
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);
|
||||
});
|
||||
|
||||
return RedirectToAction("Edit", new {id});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Parts
|
||||
|
@@ -70,6 +70,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AdminMenu.cs" />
|
||||
<Compile Include="ViewModels\AddPartsViewModel.cs" />
|
||||
<Compile Include="Controllers\AdminController.cs" />
|
||||
<Compile Include="Permissions.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
@@ -85,6 +86,7 @@
|
||||
<Content Include="Module.txt" />
|
||||
<Content Include="Styles\admin.css" />
|
||||
<Content Include="Views\Admin\AddFieldTo.ascx" />
|
||||
<Content Include="Views\Admin\AddPartsTo.ascx" />
|
||||
<Content Include="Views\Admin\Create.ascx" />
|
||||
<Content Include="Views\Admin\EditPart.ascx" />
|
||||
<Content Include="Views\Admin\Edit.ascx" />
|
||||
|
@@ -61,6 +61,11 @@ namespace Orchard.ContentTypes.Services {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerable<ContentPartDefinition> GetPartDefinitions() {
|
||||
var typeNames = GetTypeDefinitions().Select(ctd => ctd.Name);
|
||||
return _contentDefinitionManager.ListPartDefinitions().Where(cpd => !typeNames.Contains(cpd.Name));
|
||||
}
|
||||
|
||||
public ContentPartDefinition GetPartDefinition(string name) {
|
||||
return _contentDefinitionManager.GetPartDefinition(name);
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ namespace Orchard.ContentTypes.Services {
|
||||
void AlterTypeDefinition(ContentTypeDefinition contentTypeDefinition);
|
||||
void RemoveTypeDefinition(string name);
|
||||
|
||||
IEnumerable<ContentPartDefinition> GetPartDefinitions();
|
||||
ContentPartDefinition GetPartDefinition(string name);
|
||||
void AddPartDefinition(ContentPartDefinition contentPartDefinition);
|
||||
void AlterPartDefinition(ContentPartDefinition contentPartDefinition);
|
||||
|
@@ -0,0 +1,18 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
|
||||
namespace Orchard.ContentTypes.ViewModels {
|
||||
public class AddPartsViewModel : BaseViewModel {
|
||||
public AddPartsViewModel() {
|
||||
PartSelections = new List<PartSelectionViewModel>();
|
||||
}
|
||||
|
||||
public EditTypeViewModel Type { get; set; }
|
||||
public IEnumerable<PartSelectionViewModel> PartSelections { get; set; }
|
||||
}
|
||||
|
||||
public class PartSelectionViewModel {
|
||||
public string PartName { get; set; }
|
||||
public bool IsSelected { get; set; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<Orchard.ContentTypes.ViewModels.AddPartsViewModel>" %>
|
||||
<%
|
||||
Html.RegisterStyle("admin.css"); %>
|
||||
<h1><%:Html.TitleForPage(T("Add parts to \"{0}\"", Model.Type.DisplayName).ToString())%></h1><%
|
||||
using (Html.BeginFormAntiForgeryPost()) { %>
|
||||
<%:Html.ValidationSummary() %>
|
||||
<fieldset>
|
||||
<%:Html.UnorderedList(
|
||||
Model.PartSelections,
|
||||
(partSelection, i) => {
|
||||
var fieldNameStart = "PartSelections[" + i + "].";
|
||||
return MvcHtmlString.Create(
|
||||
string.Format(
|
||||
"{0} <label for=\"{1}\" class=\"forcheckbox\">{2}</label>{3}",
|
||||
Html.CheckBox(fieldNameStart + "IsSelected"),
|
||||
fieldNameStart + "IsSelected",
|
||||
partSelection.PartName,
|
||||
Html.Hidden(fieldNameStart + "PartName", partSelection.PartName)));
|
||||
},
|
||||
"available-parts")%>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<button class="primaryAction" type="submit"><%:T("Save") %></button>
|
||||
</fieldset><%
|
||||
} %>
|
@@ -14,7 +14,7 @@ using (Html.BeginFormAntiForgeryPost()) { %>
|
||||
</fieldset>
|
||||
<% Html.RenderTemplates(Model.Templates); %>
|
||||
<h2><%:T("Parts") %></h2>
|
||||
<div class="manage add-to-type"><%: Html.ActionLink(T("Add").Text, "AddPart", new { }, new { @class = "button" }) %></div>
|
||||
<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, "Parts", "") %>
|
||||
<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>
|
||||
|
Reference in New Issue
Block a user