From c1f4474a8560557a6705c78fef88a9550e7468da Mon Sep 17 00:00:00 2001 From: Nathan Heskew Date: Fri, 2 Jul 2010 01:42:19 -0700 Subject: [PATCH] Hooked up field removal from a content part --HG-- branch : dev --- .../Controllers/AdminController.cs | 52 +++++++++++++++++-- .../Orchard.ContentTypes.csproj | 2 + .../ViewModels/RemoveFieldViewModel.cs | 8 +++ .../Views/Admin/EditPart.ascx | 2 +- .../Views/Admin/RemoveFieldFrom.ascx | 11 ++++ .../Views/EditorTemplates/Field.ascx | 2 +- 6 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/RemoveFieldViewModel.cs create mode 100644 src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/RemoveFieldFrom.ascx diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs index 087d62988..29c7cb4b0 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Controllers/AdminController.cs @@ -283,7 +283,7 @@ namespace Orchard.ContentTypes.Controllers { #region Parts public ActionResult EditPart(string id) { - if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a part."))) + if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part."))) return new HttpUnauthorizedResult(); var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id); @@ -300,7 +300,7 @@ namespace Orchard.ContentTypes.Controllers { [HttpPost, ActionName("EditPart")] public ActionResult EditPartPOST(EditPartViewModel viewModel) { - if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a part."))) + if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part."))) return new HttpUnauthorizedResult(); var contentPartDefinition = _contentDefinitionService.GetPartDefinition(viewModel.Name); @@ -323,7 +323,7 @@ namespace Orchard.ContentTypes.Controllers { } public ActionResult AddFieldTo(string id) { - if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a part."))) + if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part."))) return new HttpUnauthorizedResult(); var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id); @@ -347,7 +347,7 @@ namespace Orchard.ContentTypes.Controllers { [HttpPost, ActionName("AddFieldTo")] public ActionResult AddFieldToPOST(string id) { - if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a part."))) + if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part."))) return new HttpUnauthorizedResult(); var viewModel = new AddFieldViewModel(); @@ -391,6 +391,50 @@ namespace Orchard.ContentTypes.Controllers { return RedirectToAction("EditPart", new { id }); } + + public ActionResult RemoveFieldFrom(string id) { + if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part."))) + return new HttpUnauthorizedResult(); + + var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id); + + var viewModel = new RemoveFieldViewModel(); + if (contentPartDefinition == null + || !TryUpdateModel(viewModel) + || !contentPartDefinition.Fields.Any(p => p.Name == viewModel.Name)) + return new NotFoundResult(); + + viewModel.Part = new EditPartViewModel { Name = contentPartDefinition.Name }; + return View(viewModel); + } + + [HttpPost, ActionName("RemoveFieldFrom")] + public ActionResult RemoveFieldFromPOST(string id) { + if (!Services.Authorizer.Authorize(Permissions.CreateContentTypes, T("Not allowed to edit a content part."))) + return new HttpUnauthorizedResult(); + + var contentPartDefinition = _contentDefinitionService.GetPartDefinition(id); + + var viewModel = new RemoveFieldViewModel(); + if (contentPartDefinition == null + || !TryUpdateModel(viewModel) + || !contentPartDefinition.Fields.Any(p => p.Name == viewModel.Name)) + return new NotFoundResult(); + + if (!ModelState.IsValid) { + viewModel.Part = new EditPartViewModel { Name = contentPartDefinition.Name }; + return View(viewModel); + } + + _contentDefinitionManager.AlterPartDefinition(id, typeBuilder => typeBuilder.RemoveField(viewModel.Name)); + Services.Notifier.Information(T("The \"{0}\" field has been removed.", viewModel.Name)); + + if (_contentDefinitionService.GetTypeDefinition(id) != null) + return RedirectToAction("Edit", new { id }); + + return RedirectToAction("EditPart", new { id }); + } + #endregion class Updater : IUpdateModel { diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj b/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj index fb24c6175..1d05c5039 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 @@ + @@ -88,6 +89,7 @@ + diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/RemoveFieldViewModel.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/RemoveFieldViewModel.cs new file mode 100644 index 000000000..08ba7a1fd --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/RemoveFieldViewModel.cs @@ -0,0 +1,8 @@ +using Orchard.Mvc.ViewModels; + +namespace Orchard.ContentTypes.ViewModels { + public class RemoveFieldViewModel : BaseViewModel { + public string Name { get; set; } + public EditPartViewModel Part { get; set; } + } +} 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 c79f8fc14..cd59452b6 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/EditPart.ascx +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/EditPart.ascx @@ -13,7 +13,7 @@ using (Html.BeginFormAntiForgeryPost()) { %>

<%:T("Fields") %>

<%: Html.ActionLink(T("Add").Text, "AddFieldTo", new { area = "Orchard.ContentTypes", id = Model.Name }, new { @class = "button" }) %>
<%:Html.EditorFor(m => m.Fields, "Fields", "") %> -
+
<% } %> diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/RemoveFieldFrom.ascx b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/RemoveFieldFrom.ascx new file mode 100644 index 000000000..1ca02dc09 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/Admin/RemoveFieldFrom.ascx @@ -0,0 +1,11 @@ +<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl" %> +<% +Html.RegisterStyle("admin.css"); %> +

<%:Html.TitleForPage(T("Remove the \"{0}\" part from \"{1}\"", Model.Name, Model.Part.Name).ToString())%>

<% +using (Html.BeginFormAntiForgeryPost()) { %> +

<%:T("Looks like you couldn't use the fancy way to remove the field. Try hitting the button below to force the issue.") %>

+
+ <%=Html.HiddenFor(m => m.Name) %> + +
<% +} %> \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/EditorTemplates/Field.ascx b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/EditorTemplates/Field.ascx index d59fbfc65..ddfe0a09e 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/EditorTemplates/Field.ascx +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/EditorTemplates/Field.ascx @@ -2,7 +2,7 @@

<%:Model.Name %> (<%:Model.FieldDefinition.Name %>)

- <%:Html.Link("[remove]", "#forshowonlyandnotintendedtowork!") %> + <%:Html.ActionLink(T("Remove").Text, "RemoveFieldFrom", new { area = "Orchard.ContentTypes", id = Model.Part.Name, Model.Name }, new { itemprop = "RemoveUrl UnsafeUrl" })%><%--// <- some experimentation--%>
<% Html.RenderTemplates(Model.Templates); %> <%:Html.HiddenFor(m => m.Name) %><%:Html.HiddenFor(m => m.FieldDefinition.Name) %>