Skip to content

Instantly share code, notes, and snippets.

@trnktms
Created February 15, 2018 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trnktms/5040c3df7a4c2ed65fe9249279fa0a0e to your computer and use it in GitHub Desktop.
Save trnktms/5040c3df7a4c2ed65fe9249279fa0a0e to your computer and use it in GitHub Desktop.
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