Created
February 15, 2018 09:58
-
-
Save trnktms/a28953215b7c118c06f1455b4d5841f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Helix.Skeleton.Foundation.EditorExtensions.Pipelines.CopyPage | |
{ | |
using System.Collections.Specialized; | |
using System.Web.Mvc; | |
using Helix.Skeleton.Foundation.EditorExtensions.Consts.CopyPage; | |
using Helix.Skeleton.Foundation.EditorExtensions.Models.CopyPage; | |
using Helix.Skeleton.Foundation.EditorExtensions.Services; | |
using Sitecore; | |
using Sitecore.Configuration; | |
using Sitecore.Data; | |
using Sitecore.Data.Items; | |
using Sitecore.Data.Managers; | |
using Sitecore.Globalization; | |
using Sitecore.Shell.Framework.Pipelines; | |
using Sitecore.Web.UI.Sheer; | |
public class CopyPage : CopyItems | |
{ | |
private readonly ICopyPageService copyPageService; | |
public CopyPage() | |
{ | |
this.copyPageService = DependencyResolver.Current.GetService<ICopyPageService>(); | |
} | |
public override void Execute(CopyItemsArgs args) | |
{ | |
if (args == null) | |
{ | |
return; | |
} | |
var parameters = this.MapParameters(args.Parameters); | |
if (!this.AreParametersValid(parameters)) | |
{ | |
SheerResponse.Alert("Parameters are not valid."); | |
return; | |
} | |
var database = Factory.GetDatabase(parameters.Database); | |
if (database == null) | |
{ | |
SheerResponse.Alert("Database is null."); | |
return; | |
} | |
var oldItem = database.GetItem(ID.Parse(parameters.ItemId), Language.Parse(parameters.Language)); | |
if (oldItem == null) | |
{ | |
SheerResponse.Alert("Current item is null."); | |
return; | |
} | |
var targetItem = database.GetItem(ID.Parse(parameters.DestinationId)); | |
if (targetItem == null) | |
{ | |
SheerResponse.Alert("Target item is null."); | |
return; | |
} | |
var oldContentFolder = this.copyPageService.GetContentFolder(oldItem.Paths.FullPath, parameters.Database); | |
if (oldContentFolder == null) | |
{ | |
SheerResponse.Alert("Current item is not a page."); | |
return; | |
} | |
var newItemName = ItemUtil.GetCopyOfName(targetItem, oldItem.Name); | |
// Create new page item without version and language | |
var newItemWithoutVersion = ItemManager.CreateItem(newItemName, oldItem.Parent, oldItem.TemplateID); | |
// Create new content folder item | |
oldContentFolder.CopyTo(newItemWithoutVersion, oldContentFolder.Name); | |
foreach (var language in oldItem.Languages) | |
{ | |
var languageSpecificNewItem = this.copyPageService.CopyLastVersion(database, language, oldItem, newItemWithoutVersion); | |
if (languageSpecificNewItem == null) | |
{ | |
continue; | |
} | |
this.copyPageService.AdjustLayout(database, languageSpecificNewItem, oldItem, FieldIDs.LayoutField); | |
this.copyPageService.AdjustLayout(database, languageSpecificNewItem, oldItem, FieldIDs.FinalLayoutField); | |
} | |
} | |
private IParameters MapParameters(NameValueCollection parameters) | |
{ | |
return new Parameters | |
{ | |
Database = parameters.Get(ParameterKeys.DatabaseKey), | |
ItemId = parameters.Get(ParameterKeys.ItemsKey), | |
Language = parameters.Get(ParameterKeys.LanguageKey), | |
DestinationId = parameters.Get(ParameterKeys.DestinationKey) | |
}; | |
} | |
private bool AreParametersValid(IParameters parameters) | |
{ | |
return parameters != null | |
&& !string.IsNullOrEmpty(parameters.DestinationId) | |
&& !string.IsNullOrEmpty(parameters.Database) | |
&& !string.IsNullOrEmpty(parameters.ItemId) | |
&& !string.IsNullOrEmpty(parameters.Language); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment