From 184d02391287f916406d4283b53ecdc92b46d0b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Thu, 18 Jul 2013 22:38:38 +0200 Subject: [PATCH] Factoring out content item cloning to ContentManager --HG-- branch : 1.x --- .../Contents/Controllers/AdminController.cs | 21 ++++------------ .../DefaultContentManager.cs | 24 +++++++++++++++++++ .../ContentManagement/IContentManager.cs | 8 +++++++ 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/Orchard.Web/Core/Contents/Controllers/AdminController.cs b/src/Orchard.Web/Core/Contents/Controllers/AdminController.cs index 0e9dd99dd..eb67d5405 100644 --- a/src/Orchard.Web/Core/Contents/Controllers/AdminController.cs +++ b/src/Orchard.Web/Core/Contents/Controllers/AdminController.cs @@ -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")); diff --git a/src/Orchard/ContentManagement/DefaultContentManager.cs b/src/Orchard/ContentManagement/DefaultContentManager.cs index 3dee024a6..53896d4a0 100644 --- a/src/Orchard/ContentManagement/DefaultContentManager.cs +++ b/src/Orchard/ContentManagement/DefaultContentManager.cs @@ -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); + } + /// /// Lookup for a content item based on a . If multiple /// resolvers can give a result, the one with the highest priority is used. As soon as diff --git a/src/Orchard/ContentManagement/IContentManager.cs b/src/Orchard/ContentManagement/IContentManager.cs index bd03d3708..d024c57e5 100644 --- a/src/Orchard/ContentManagement/IContentManager.cs +++ b/src/Orchard/ContentManagement/IContentManager.cs @@ -34,6 +34,14 @@ namespace Orchard.ContentManagement { void Create(ContentItem contentItem, VersionOptions options); + /// + /// Makes a clone of the content item + /// + /// The content item to clone + /// Clone of the item + ContentItem Clone(ContentItem contentItem); + + /// /// Gets the content item with the specified id ///