mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Factoring out content item cloning to ContentManager
--HG-- branch : 1.x
This commit is contained in:
@@ -331,7 +331,6 @@ namespace Orchard.Core.Contents.Controllers {
|
|||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult Clone(int id, string returnUrl) {
|
public ActionResult Clone(int id, string returnUrl) {
|
||||||
// Mostly taken from: http://orchard.codeplex.com/discussions/396664
|
|
||||||
var contentItem = _contentManager.GetLatest(id);
|
var contentItem = _contentManager.GetLatest(id);
|
||||||
|
|
||||||
if (contentItem == null)
|
if (contentItem == null)
|
||||||
@@ -340,26 +339,14 @@ namespace Orchard.Core.Contents.Controllers {
|
|||||||
if (!Services.Authorizer.Authorize(Permissions.EditContent, contentItem, T("Couldn't clone content")))
|
if (!Services.Authorizer.Authorize(Permissions.EditContent, contentItem, T("Couldn't clone content")))
|
||||||
return new HttpUnauthorizedResult();
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
var importContentSession = new ImportContentSession(_contentManager);
|
try {
|
||||||
|
Services.ContentManager.Clone(contentItem);
|
||||||
var element = _contentManager.Export(contentItem);
|
}
|
||||||
|
catch (InvalidOperationException) {
|
||||||
// if a handler prevents this element from being exported, it can't be cloned
|
|
||||||
if (element == null) {
|
|
||||||
Services.Notifier.Warning(T("Could not clone the content item."));
|
Services.Notifier.Warning(T("Could not clone the content item."));
|
||||||
return this.RedirectLocal(returnUrl, () => RedirectToAction("List"));
|
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."));
|
Services.Notifier.Information(T("Successfully cloned. The clone was saved as a draft."));
|
||||||
|
|
||||||
return this.RedirectLocal(returnUrl, () => RedirectToAction("List"));
|
return this.RedirectLocal(returnUrl, () => RedirectToAction("List"));
|
||||||
|
@@ -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>
|
/// <summary>
|
||||||
/// Lookup for a content item based on a <see cref="ContentIdentity"/>. If multiple
|
/// 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
|
/// resolvers can give a result, the one with the highest priority is used. As soon as
|
||||||
|
@@ -34,6 +34,14 @@ namespace Orchard.ContentManagement {
|
|||||||
void Create(ContentItem contentItem, VersionOptions options);
|
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>
|
/// <summary>
|
||||||
/// Gets the content item with the specified id
|
/// Gets the content item with the specified id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Reference in New Issue
Block a user