Skip to content

Instantly share code, notes, and snippets.

@trnktms
Created Feb 15, 2018
Embed
What would you like to do?
namespace Helix.Skeleton.Foundation.EditorExtensions.Commands
{
using System.Collections.Specialized;
using System.Linq;
using System.Web.Mvc;
using Helix.Skeleton.Foundation.EditorExtensions.Consts.CopyPage;
using Helix.Skeleton.Foundation.EditorExtensions.Services;
using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Shell.Framework.Commands;
using Sitecore.Shell.Framework.Pipelines;
public class CopyPage : Command
{
private const string CopyPagePipelineName = "skeletonUiCopyPage";
private readonly ICopyPageService duplicatePageService;
public CopyPage()
{
this.duplicatePageService = DependencyResolver.Current.GetService<ICopyPageService>();
}
public override void Execute(CommandContext context)
{
NameValueCollection parameters = new NameValueCollection
{
{ ParameterKeys.DatabaseKey, context.Items.First().Database.Name },
{ ParameterKeys.ItemsKey, string.Join("|", context.Items.Select(i => i.ID)) },
{ ParameterKeys.LanguageKey, context.Items.First().Language.Name }
};
Context.ClientPage.Start(CopyPagePipelineName, new CopyItemsArgs { Parameters = parameters });
}
public override CommandState QueryState(CommandContext context)
{
var currentItem = this.GetCurrentItem(context);
if (currentItem == null)
{
return CommandState.Disabled;
}
var contentFolder = this.duplicatePageService.GetContentFolder(currentItem.Paths.FullPath, currentItem.Database.Name);
if (contentFolder == null)
{
return CommandState.Disabled;
}
return base.QueryState(context);
}
private Item GetCurrentItem(CommandContext context)
{
if (context.Items == null || context.Items.Count() != 1)
{
return null;
}
return context.Items.First();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment