Factoring out content item cloning to ContentManager

--HG--
branch : 1.x
This commit is contained in:
Zoltán Lehóczky
2013-07-18 22:38:38 +02:00
parent 27d8275916
commit 184d023912
3 changed files with 36 additions and 17 deletions

View File

@@ -331,7 +331,6 @@ namespace Orchard.Core.Contents.Controllers {
[HttpPost]
public ActionResult Clone(int id, string returnUrl) {
// Mostly taken from: http://orchard.codeplex.com/discussions/396664
var contentItem = _contentManager.GetLatest(id);
if (contentItem == null)
@@ -340,26 +339,14 @@ namespace Orchard.Core.Contents.Controllers {
if (!Services.Authorizer.Authorize(Permissions.EditContent, contentItem, T("Couldn't clone content")))
return new HttpUnauthorizedResult();
var importContentSession = new ImportContentSession(_contentManager);
var element = _contentManager.Export(contentItem);
// if a handler prevents this element from being exported, it can't be cloned
if (element == null) {
try {
Services.ContentManager.Clone(contentItem);
}
catch (InvalidOperationException) {
Services.Notifier.Warning(T("Could not clone the content item."));
return this.RedirectLocal(returnUrl, () => RedirectToAction("List"));
}
var elementId = element.Attribute("Id");
var copyId = elementId.Value + "-copy";
elementId.SetValue(copyId);
var status = element.Attribute("Status");
if (status != null) status.SetValue("Draft"); // So the copy is always a draft.
importContentSession.Set(copyId, element.Name.LocalName);
_contentManager.Import(element, importContentSession);
Services.Notifier.Information(T("Successfully cloned. The clone was saved as a draft."));
return this.RedirectLocal(returnUrl, () => RedirectToAction("List"));

View File

@@ -534,6 +534,30 @@ namespace Orchard.ContentManagement {
}
}
public virtual ContentItem Clone(ContentItem contentItem) {
// Mostly taken from: http://orchard.codeplex.com/discussions/396664
var importContentSession = new ImportContentSession(this);
var element = Export(contentItem);
// If a handler prevents this element from being exported, it can't be cloned
if (element == null) {
throw new InvalidOperationException("The content item couldn't be cloned because a handler prevented it from being exported.");
}
var elementId = element.Attribute("Id");
var copyId = elementId.Value + "-copy";
elementId.SetValue(copyId);
var status = element.Attribute("Status");
if (status != null) status.SetValue("Draft"); // So the copy is always a draft.
importContentSession.Set(copyId, element.Name.LocalName);
Import(element, importContentSession);
return importContentSession.Get(copyId, element.Name.LocalName);
}
/// <summary>
/// Lookup for a content item based on a <see cref="ContentIdentity"/>. If multiple
/// resolvers can give a result, the one with the highest priority is used. As soon as

View File

@@ -34,6 +34,14 @@ namespace Orchard.ContentManagement {
void Create(ContentItem contentItem, VersionOptions options);
/// <summary>
/// Makes a clone of the content item
/// </summary>
/// <param name="contentItem">The content item to clone</param>
/// <returns>Clone of the item</returns>
ContentItem Clone(ContentItem contentItem);
/// <summary>
/// Gets the content item with the specified id
/// </summary>