From d892e88d09f7ddab90d65a07eed4c785332a8ffd Mon Sep 17 00:00:00 2001 From: Nathan Heskew Date: Fri, 2 Jul 2010 16:07:43 -0700 Subject: [PATCH] Added part creation and tidied up the content type management flow a little - could make use of a redirectUrl for a little better experience --HG-- branch : dev --- .../Controllers/AdminController.cs | 26 ++++++++++++++-- .../Orchard.ContentTypes.csproj | 2 ++ .../Services/ContentDefinitionService.cs | 31 +++++++++++++------ .../Services/IContentDefinitionService.cs | 4 +-- .../ViewModels/CreatePartViewModel.cs | 7 +++++ .../Views/Admin/Create.ascx | 1 - .../Views/Admin/CreatePart.ascx | 12 +++++++ .../Views/Admin/EditPart.ascx | 8 ++--- .../Views/Admin/List.ascx | 3 +- 9 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/CreatePartViewModel.cs create mode 100644 src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/CreatePart.ascx diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs index 11f04f725..10e125ccb 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs @@ -57,9 +57,9 @@ namespace Orchard.ContentTypes.Controllers { if (!ModelState.IsValid) return View(viewModel); - _contentDefinitionService.AddTypeDefinition(viewModel.DisplayName); + var definition = _contentDefinitionService.AddTypeDefinition(viewModel.DisplayName); - return RedirectToAction("Index"); + return RedirectToAction("Edit", new { id = definition.Name }); } public ActionResult Edit(string id) { @@ -288,6 +288,26 @@ namespace Orchard.ContentTypes.Controllers { }); } + public ActionResult CreatePart() { + if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to create a content part."))) + return new HttpUnauthorizedResult(); + + return View(new CreatePartViewModel()); + } + + [HttpPost, ActionName("CreatePart")] + public ActionResult CreatePartPOST(CreatePartViewModel viewModel) { + if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to create a content part."))) + return new HttpUnauthorizedResult(); + + if (!ModelState.IsValid) + return View(viewModel); + + var definition = _contentDefinitionService.AddPartDefinition(viewModel.Name); + + return RedirectToAction("EditPart", new { id = definition.Name }); + } + public ActionResult EditPart(string id) { if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part."))) return new HttpUnauthorizedResult(); @@ -325,7 +345,7 @@ namespace Orchard.ContentTypes.Controllers { return View(viewModel); } - return RedirectToAction("Index"); + return RedirectToAction("ListParts"); } public ActionResult AddFieldTo(string id) { diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj b/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj index b58355bd1..b079e5db4 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj @@ -77,6 +77,7 @@ + @@ -90,6 +91,7 @@ + diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/ContentDefinitionService.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/ContentDefinitionService.cs index 374bc0a6e..a39cfdb92 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/ContentDefinitionService.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/ContentDefinitionService.cs @@ -31,21 +31,24 @@ namespace Orchard.ContentTypes.Services { return _contentDefinitionManager.GetTypeDefinition(name); } - public void AddTypeDefinition(string displayName) { - var name = GenerateTypeName(displayName); + public ContentTypeDefinition AddTypeDefinition(string displayName) { + var name = GenerateName(displayName); while (_contentDefinitionManager.GetTypeDefinition(name) != null) - name = VersionTypeName(name); + name = VersionName(name); + var contentTypeDefinition = new ContentTypeDefinition(name) { DisplayName = displayName }; //just giving the new type some default parts for now - _contentDefinitionManager.StoreTypeDefinition(new ContentTypeDefinition(name) {DisplayName = displayName}); + _contentDefinitionManager.StoreTypeDefinition(contentTypeDefinition); _contentDefinitionManager.AlterTypeDefinition( - name, + contentTypeDefinition.Name, 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.", displayName)); + Services.Notifier.Information(T("The \"{0}\" content type has created.", contentTypeDefinition.DisplayName)); + + return contentTypeDefinition; } public void AlterTypeDefinition(ContentTypeDefinition contentTypeDefinition) { @@ -70,8 +73,16 @@ namespace Orchard.ContentTypes.Services { return _contentDefinitionManager.GetPartDefinition(name); } - public void AddPartDefinition(ContentPartDefinition contentPartDefinition) { - throw new NotImplementedException(); + public ContentPartDefinition AddPartDefinition(string name) { + name = GenerateName(name); + + while (_contentDefinitionManager.GetPartDefinition(name) != null) + name = VersionName(name); + + var contentPartDefinition = new ContentPartDefinition(name); + _contentDefinitionManager.StorePartDefinition(contentPartDefinition); + + return contentPartDefinition; } public void AlterPartDefinition(ContentPartDefinition contentPartDefinition) { @@ -87,7 +98,7 @@ namespace Orchard.ContentTypes.Services { } //gratuitously stolen from the RoutableService - private static string GenerateTypeName(string displayName) { + private static string GenerateName(string displayName) { if (string.IsNullOrWhiteSpace(displayName)) return ""; @@ -104,7 +115,7 @@ namespace Orchard.ContentTypes.Services { return name.ToLowerInvariant(); } - private static string VersionTypeName(string name) { + private static string VersionName(string name) { int version; var nameParts = name.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries); diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/IContentDefinitionService.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/IContentDefinitionService.cs index 13e6e56da..62be29138 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/IContentDefinitionService.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/IContentDefinitionService.cs @@ -6,13 +6,13 @@ namespace Orchard.ContentTypes.Services { public interface IContentDefinitionService : IDependency { IEnumerable GetTypeDefinitions(); ContentTypeDefinition GetTypeDefinition(string name); - void AddTypeDefinition(string displayName); + ContentTypeDefinition AddTypeDefinition(string displayName); void AlterTypeDefinition(ContentTypeDefinition contentTypeDefinition); void RemoveTypeDefinition(string name); IEnumerable GetPartDefinitions(); ContentPartDefinition GetPartDefinition(string name); - void AddPartDefinition(ContentPartDefinition contentPartDefinition); + ContentPartDefinition AddPartDefinition(string name); void AlterPartDefinition(ContentPartDefinition contentPartDefinition); void RemovePartDefinition(string name); diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/CreatePartViewModel.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/CreatePartViewModel.cs new file mode 100644 index 000000000..a9de89208 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/CreatePartViewModel.cs @@ -0,0 +1,7 @@ +using Orchard.Mvc.ViewModels; + +namespace Orchard.ContentTypes.ViewModels { + public class CreatePartViewModel : BaseViewModel { + public string Name { get; set; } + } +} diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/Create.ascx b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/Create.ascx index 01c72136a..ed94de70a 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/Create.ascx +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/Create.ascx @@ -1,5 +1,4 @@ <%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl" %> -

<%:Html.TitleForPage(T("New Content Type").ToString())%>

<% using (Html.BeginFormAntiForgeryPost()) { %> <%:Html.ValidationSummary() %> diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/CreatePart.ascx b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/CreatePart.ascx new file mode 100644 index 000000000..3d6a65626 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/CreatePart.ascx @@ -0,0 +1,12 @@ +<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl" %> +

<%:Html.TitleForPage(T("New Content Part").ToString())%>

<% +using (Html.BeginFormAntiForgeryPost()) { %> + <%:Html.ValidationSummary() %> +
+ + <%:Html.TextBoxFor(m => m.Name, new {@class = "textMedium", autofocus = "autofocus"}) %> +
+
+ +
<% +} %> \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/EditPart.ascx b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/EditPart.ascx index 070df8709..f468cd1b4 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/EditPart.ascx +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/EditPart.ascx @@ -1,14 +1,14 @@ <%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl" %> -<% -Html.RegisterStyle("admin.css"); %> +<% Html.RegisterStyle("admin.css"); %>

<%:Html.TitleForPage(T("Edit Part").ToString()) %>

-<% +<% using (Html.BeginFormAntiForgeryPost()) { %> <%:Html.ValidationSummary() %>
<%--// has unintended consequences (renamging the part) - changing the name creates a new part of that name--%> - <%:Html.TextBoxFor(m => m.Name, new {@class = "textMedium"}) %> + <%:Html.TextBoxFor(m => m.Name, new {@class = "textMedium", disabled = "disabled"}) %> + <%:Html.HiddenFor(m => m.Name) %>
<% Html.RenderTemplates(Model.Templates); %>

<%:T("Fields") %>

diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/List.ascx b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/List.ascx index 090b286c7..850bbaf2d 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/List.ascx +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/List.ascx @@ -1,6 +1,5 @@ <%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl" %> -<% - Html.RegisterStyle("admin.css"); %> +<% Html.RegisterStyle("admin.css"); %>

<%:Html.TitleForPage(T("Content Types").ToString())%>

<%:Html.ActionLink(T("Create new type").ToString(), "Create", new{Controller="Admin",Area="Orchard.ContentTypes"}, new { @class = "button primaryAction" }) %>