diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs index e9ccb7dee..00c33ec29 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs @@ -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 diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj b/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj index 77e7c5269..5cbef0319 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj @@ -70,6 +70,7 @@ + @@ -85,6 +86,7 @@ + diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/ContentDefinitionService.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/ContentDefinitionService.cs index 91ee04914..374bc0a6e 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/ContentDefinitionService.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/ContentDefinitionService.cs @@ -61,6 +61,11 @@ namespace Orchard.ContentTypes.Services { throw new NotImplementedException(); } + public IEnumerable 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); } diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/IContentDefinitionService.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/IContentDefinitionService.cs index 4f4da54ec..13e6e56da 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/IContentDefinitionService.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/IContentDefinitionService.cs @@ -10,6 +10,7 @@ namespace Orchard.ContentTypes.Services { void AlterTypeDefinition(ContentTypeDefinition contentTypeDefinition); void RemoveTypeDefinition(string name); + IEnumerable GetPartDefinitions(); ContentPartDefinition GetPartDefinition(string name); void AddPartDefinition(ContentPartDefinition contentPartDefinition); void AlterPartDefinition(ContentPartDefinition contentPartDefinition); diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/AddPartsViewModel.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/AddPartsViewModel.cs new file mode 100644 index 000000000..cf4e4e945 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/AddPartsViewModel.cs @@ -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(); + } + + public EditTypeViewModel Type { get; set; } + public IEnumerable PartSelections { get; set; } + } + + public class PartSelectionViewModel { + public string PartName { get; set; } + public bool IsSelected { get; set; } + } +} diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/AddPartsTo.ascx b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/AddPartsTo.ascx new file mode 100644 index 000000000..7f70491e7 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/AddPartsTo.ascx @@ -0,0 +1,25 @@ +<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl" %> +<% +Html.RegisterStyle("admin.css"); %> +

<%:Html.TitleForPage(T("Add parts to \"{0}\"", Model.Type.DisplayName).ToString())%>

<% +using (Html.BeginFormAntiForgeryPost()) { %> + <%:Html.ValidationSummary() %> +
+ <%:Html.UnorderedList( + Model.PartSelections, + (partSelection, i) => { + var fieldNameStart = "PartSelections[" + i + "]."; + return MvcHtmlString.Create( + string.Format( + "{0} {3}", + Html.CheckBox(fieldNameStart + "IsSelected"), + fieldNameStart + "IsSelected", + partSelection.PartName, + Html.Hidden(fieldNameStart + "PartName", partSelection.PartName))); + }, + "available-parts")%> +
+
+ +
<% +} %> diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/Edit.ascx b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/Edit.ascx index 13f50be98..e438597b9 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/Edit.ascx +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/Edit.ascx @@ -14,7 +14,7 @@ using (Html.BeginFormAntiForgeryPost()) { %> <% Html.RenderTemplates(Model.Templates); %>

<%:T("Parts") %>

-
<%: Html.ActionLink(T("Add").Text, "AddPart", new { }, new { @class = "button" }) %>
+
<%: Html.ActionLink(T("Add").Text, "AddPartsTo", new { area = "Orchard.ContentTypes", id = Model.Name }, new { @class = "button" })%>
<%:Html.EditorFor(m => m.Parts, "Parts", "") %>

<%:T("Fields") %>

<%: Html.ActionLink(T("Add").Text, "AddFieldTo", new { area = "Orchard.ContentTypes", id = Model.Name }, new { @class = "button" }) %>