#16476: Adding delete content types and parts actions

Work Items: 16476

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2011-09-13 14:25:12 -07:00
parent 4794725202
commit b834744ff9
10 changed files with 131 additions and 12 deletions

View File

@@ -7,6 +7,7 @@ using Moq;
using NHibernate;
using NUnit.Framework;
using Orchard.Caching;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.ContentManagement.MetaData.Models;
@@ -204,6 +205,43 @@ namespace Orchard.Core.Tests.Settings.Metadata {
Assert.That(manager.ListPartDefinitions().Count(), Is.EqualTo(3));
}
[Test]
public void PartsCanBeDeleted() {
var manager = _container.Resolve<IContentDefinitionManager>();
manager.StoreTypeDefinition(
new ContentTypeDefinitionBuilder()
.Named("alpha")
.WithPart("foo", pb => { })
.WithPart("bar", pb => { })
.Build());
AssertThatTypeHasParts("alpha", "foo", "bar");
Assert.That(manager.ListPartDefinitions().Count(), Is.EqualTo(2));
manager.DeletePartDefinition("foo");
ResetSession();
AssertThatTypeHasParts("alpha", "bar");
Assert.That(manager.ListPartDefinitions().Count(), Is.EqualTo(1));
}
[Test]
public void ContentTypesCanBeDeleted() {
var manager = _container.Resolve<IContentDefinitionManager>();
manager.StoreTypeDefinition(
new ContentTypeDefinitionBuilder()
.Named("alpha")
.WithPart("foo", pb => { })
.WithPart("bar", pb => { })
.Build());
Assert.That(manager.GetTypeDefinition("alpha"), Is.Not.Null);
manager.DeleteTypeDefinition("alpha");
ResetSession();
Assert.That(manager.GetTypeDefinition("alpha"), Is.Null);
}
private void AssertThatTypeHasParts(string typeName, params string[] partNames) {
var type = _container.Resolve<IContentDefinitionManager>().GetTypeDefinition(typeName);
Assert.That(type, Is.Not.Null);

View File

@@ -83,6 +83,39 @@ namespace Orchard.Core.Settings.Metadata {
Apply(contentPartDefinition, Acquire(contentPartDefinition));
}
public void DeleteTypeDefinition(string name) {
var record = _typeDefinitionRepository.Fetch(x => x.Name == name).SingleOrDefault();
// deletes the content type record associated
if (record != null) {
_typeDefinitionRepository.Delete(record);
}
// invalidates the cache
TriggerContentDefinitionSignal();
}
public void DeletePartDefinition(string name) {
// remove parts from current types
var typesWithPart = ListTypeDefinitions().Where(typeDefinition => typeDefinition.Parts.Any(part => part.PartDefinition.Name == name));
foreach (var typeDefinition in typesWithPart) {
this.AlterTypeDefinition(typeDefinition.Name, builder => builder.RemovePart(name));
}
// delete part
var record = _partDefinitionRepository.Fetch(x => x.Name == name).SingleOrDefault();
if (record != null) {
_partDefinitionRepository.Delete(record);
}
// invalidates the cache
TriggerContentDefinitionSignal();
}
private void MonitorContentDefinitionSignal(AcquireContext<string> ctx) {
ctx.Monitor(_signals.When(ContentDefinitionSignal));
}

View File

@@ -7,10 +7,13 @@ namespace Orchard.ContentTypes {
public Localizer T { get; set; }
public string MenuName { get { return "admin"; } }
public void GetNavigation(NavigationBuilder builder) {
public void GetNavigation(NavigationBuilder builder)
{
builder.Add(T("Content"),
menu => menu.Add(T("Content Types"), "3", item => item.Action("Index", "Admin", new { area = "Orchard.ContentTypes" }).LocalNav()));
menu => menu
.Add(T("Content Types"), "3", item => item.Action("Index", "Admin", new {area = "Orchard.ContentTypes"}).LocalNav())
.Add(T("Content Parts"), "4", item => item.Action("ListParts", "Admin", new {area = "Orchard.ContentTypes"}).LocalNav()));
}
}
}

View File

@@ -6,6 +6,7 @@ using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentTypes.Services;
using Orchard.ContentTypes.ViewModels;
using Orchard.Core.Contents.Controllers;
using Orchard.Core.Contents.Settings;
using Orchard.Localization;
using Orchard.UI.Notify;
@@ -97,6 +98,7 @@ namespace Orchard.ContentTypes.Controllers {
}
[HttpPost, ActionName("Edit")]
[FormValueRequired("submit.Save")]
public ActionResult EditPOST(string id) {
if (!Services.Authorizer.Authorize(Permissions.EditContentTypes, T("Not allowed to edit a content type.")))
return new HttpUnauthorizedResult();
@@ -121,7 +123,6 @@ namespace Orchard.ContentTypes.Controllers {
if (!ModelState.IsValid)
return View(typeViewModel);
_contentDefinitionService.AlterType(typeViewModel, this);
if (!ModelState.IsValid) {
@@ -131,7 +132,32 @@ namespace Orchard.ContentTypes.Controllers {
Services.Notifier.Information(T("\"{0}\" settings have been saved.", typeViewModel.DisplayName));
return RedirectToAction("Index");
return RedirectToAction("List");
}
[HttpPost, ActionName("Edit")]
[FormValueRequired("submit.Delete")]
public ActionResult Delete(string id) {
if (!Services.Authorizer.Authorize(Permissions.EditContentTypes, T("Not allowed to delete a content type.")))
return new HttpUnauthorizedResult();
var typeViewModel = _contentDefinitionService.GetType(id);
if (typeViewModel == null)
return HttpNotFound();
_contentDefinitionManager.DeleteTypeDefinition(id);
// delete all content items (but keep versions)
var contentItems = Services.ContentManager.Query(id).List();
foreach (var contentItem in contentItems) {
Services.ContentManager.Remove(contentItem);
}
Services.Notifier.Information(T("\"{0}\" has been removed.", typeViewModel.DisplayName));
return RedirectToAction("List");
}
public ActionResult AddPartsTo(string id) {
@@ -273,6 +299,7 @@ namespace Orchard.ContentTypes.Controllers {
}
[HttpPost, ActionName("EditPart")]
[FormValueRequired("submit.Save")]
public ActionResult EditPartPOST(string id) {
if (!Services.Authorizer.Authorize(Permissions.EditContentTypes, T("Not allowed to edit a content part.")))
return new HttpUnauthorizedResult();
@@ -297,6 +324,24 @@ namespace Orchard.ContentTypes.Controllers {
return RedirectToAction("ListParts");
}
[HttpPost, ActionName("EditPart")]
[FormValueRequired("submit.Delete")]
public ActionResult DeletePart(string id)
{
if (!Services.Authorizer.Authorize(Permissions.EditContentTypes, T("Not allowed to delete a content part.")))
return new HttpUnauthorizedResult();
var partViewModel = _contentDefinitionService.GetPart(id);
if (partViewModel == null)
return HttpNotFound();
_contentDefinitionManager.DeletePartDefinition(id);
Services.Notifier.Information(T("\"{0}\" has been removed.", partViewModel.DisplayName));
return RedirectToAction("ListParts");
}
public ActionResult AddFieldTo(string id) {
if (!Services.Authorizer.Authorize(Permissions.EditContentTypes, T("Not allowed to edit a content part.")))
return new HttpUnauthorizedResult();

View File

@@ -5,7 +5,6 @@
Layout.Title = T("Edit Content Type").ToString();
}
<p class="breadcrumb">@Html.ActionLink(T("Content Types").Text, "index")@T(" &#62; ")@T("Edit Content Type")</p>
@using (Html.BeginFormAntiForgeryPost()) {
// todo: come up with real itemtype definitions and locations for said definitions
<div itemscope="itemscope" itemid="@Model.Name" itemtype="http://orchardproject.net/data/ContentType">
@@ -34,7 +33,8 @@
@Html.EditorFor(m => m.Parts, "TypeParts", "")
</div>
<fieldset class="action">
<button class="primaryAction" type="submit">@T("Save")</button>
<button class="primaryAction" type="submit" name="submit.Save" value="Save">@T("Save")</button>
<button class="primaryAction" type="submit" name="submit.Delete" value="Delete">@T("Delete")</button>
</fieldset>
</div>
}

View File

@@ -5,7 +5,6 @@
Layout.Title = T("Edit Part").ToString();
}
<p class="breadcrumb">@Html.ActionLink(T("Content Types").Text, "index")@T(" &#62; ")@Html.ActionLink(T("Content Parts").Text, "listparts")@T(" &#62; ")@T("Edit Part")</p>
@using (Html.BeginFormAntiForgeryPost()) {
@Html.ValidationSummary()
<fieldset>
@@ -23,7 +22,8 @@
@Html.EditorFor(m => m.Fields, "Fields", "")
</div>
<fieldset class="action">
<button class="primaryAction" type="submit">@T("Save")</button>
<button class="primaryAction" type="submit" name="submit.Save" value="Save">@T("Save")</button>
<button class="primaryAction" type="submit" name="submit.Delete" value="Delete">@T("Delete")</button>
</fieldset>
}

View File

@@ -6,7 +6,6 @@
<div class="manage">
@Html.ActionLink(T("Create new type").ToString(), "Create", new{Controller="Admin",Area="Orchard.ContentTypes"}, new { @class = "button primaryAction" })
@Html.ActionLink(T("Content Parts").ToString(), "ListParts", new{Controller="Admin",Area="Orchard.ContentTypes"}, new { @class = "button" })
</div>
@Html.UnorderedList(
Model.Types,

View File

@@ -2,7 +2,6 @@
@{ Layout.Title = T("Content Parts").ToString(); }
<p class="breadcrumb">@Html.ActionLink(T("Content Types").Text, "index")@T(" &#62; ")@T("Content Parts")</p>
<div class="manage">
@Html.ActionLink(T("Create new part").ToString(), "CreatePart", new{Controller="Admin",Area="Orchard.ContentTypes"}, new { @class = "button primaryAction" })
</div>

View File

@@ -4,6 +4,6 @@
<h3>@Model.DisplayName</h3>
</div>
<div class="related">
@Html.ActionLink(T("Edit").ToString(), "EditPart", new {area = "Orchard.ContentTypes", id = Model.Name})
@Html.ActionLink(T("Edit").ToString(), "EditPart", new { area = "Orchard.ContentTypes", id = Model.Name })
</div>
</div>

View File

@@ -12,6 +12,8 @@ namespace Orchard.ContentManagement.MetaData {
ContentTypeDefinition GetTypeDefinition(string name);
ContentPartDefinition GetPartDefinition(string name);
void DeleteTypeDefinition(string name);
void DeletePartDefinition(string name);
void StoreTypeDefinition(ContentTypeDefinition contentTypeDefinition);
void StorePartDefinition(ContentPartDefinition contentPartDefinition);