Skip to content

Instantly share code, notes, and snippets.

@moo2u2 moo2u2/CustomEditor.aspx.cs Secret
Created May 4, 2016

Embed
What would you like to do?
namespace Sitecore.Common.Website.Editors
{
using System;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Links;
using Sitecore.Publishing;
using Sitecore.Shell.Framework.Commands;
using Sitecore.Shell.Web.UI;
using Sitecore.Sites;
using Sitecore.Text;
using Sitecore.Web;
using Sitecore.Web.UI.HtmlControls;
/// <summary>
/// Custom editor based on the Sitecore preview editor
/// </summary>
public class Components : SecurePage, IHasCommandContext
{
protected Border DisabledNotice;
protected Literal AlertMessage;
protected Frame Editor;
public string DeviceID
{
get
{
return StringUtil.GetString(ViewState["DeviceID"]);
}
set
{
Assert.ArgumentNotNull(value, "value");
ViewState["DeviceID"] = value;
}
}
public string App { get; set; }
public CommandContext GetCommandContext()
{
CommandContext commandContext = null;
ItemUri uri = ItemUri.ParseQueryString();
if (uri != null)
{
Item obj = Database.GetItem(uri);
if (obj != null)
commandContext = new CommandContext(obj);
}
if (commandContext == null)
commandContext = new CommandContext();
commandContext.Parameters["device"] = DeviceID;
commandContext.RibbonSourceUri = new ItemUri("/sitecore/content/Applications/Content Editor/Editors/Layouts/Preview/Ribbon", Factory.GetDatabase("core"));
commandContext.Parameters["Ribbon.RenderTabs"] = "true";
commandContext.Parameters["Ribbon.RenderAsContextual"] = "true";
commandContext.Parameters["Ribbon.RenderContextualStripTitles"] = "true";
commandContext.Parameters["device"] = DeviceID;
commandContext.CustomData = this;
return commandContext;
}
protected override void OnPreRender(EventArgs e)
{
Assert.ArgumentNotNull(e, "e");
Assert.CanRunApplication("/sitecore/content/Applications/Content Editor/Ribbons/Chunks/Preview/Preview");
base.OnPreRender(e);
ItemUri uri = ItemUri.ParseQueryString();
if (uri == null)
return;
Item obj = Database.GetItem(uri);
if (obj == null)
return;
DeviceItem device = GetDevice(obj);
Assert.IsNotNull(device, typeof(Item), "Default device not found.");
DeviceID = device.ID.ToString();
Refresh(obj);
}
private UrlString GetUrl(Item item, SiteContext site)
{
Assert.ArgumentNotNull(item, "item");
UrlOptions defaultOptions = UrlOptions.DefaultOptions;
defaultOptions.Site = site;
string itemUrl = LinkManager.GetItemUrl(item, defaultOptions);
UrlString urlString;
if (itemUrl.ToLowerInvariant().StartsWith("http") && itemUrl.ToLowerInvariant().Contains("://"))
{
Uri uri = new Uri(itemUrl);
urlString = new UrlString(ReplaceUrlHost(new UrlString(itemUrl).ToString(), uri.Host, WebUtil.GetHostName()));
}
else
urlString = new UrlString(itemUrl);
string key = "pd_" + item.ID.ToShortID();
if (!string.IsNullOrEmpty(WebUtil.GetSessionString(key, null)))
{
urlString["sc_pd"] = key;
urlString["sc_pd_view"] = "1";
}
urlString["uri"] = item.Uri.ToString();
urlString["sc_site"] = defaultOptions.Site.Name;
urlString["sc_lang"] = item.Language.Name;
urlString["sc_mode"] = "preview";
urlString["sc_duration"] = "temporary";
urlString["sc_device"] = DeviceID;
urlString["custom_editor"] = "true"; // our custom editor
return urlString;
}
private void Refresh(Item item)
{
Assert.ArgumentNotNull(item, "item");
if (!item.Paths.IsDescendantOf(Sitecore.Context.Database.GetItem(ItemIDs.ContentRoot)))
{
Editor.Visible = false;
DisabledNotice.Visible = true;
AlertMessage.Text = Translate.Text("Preview is enabled for content items only");
}
else
{
SiteContext previewSiteContext = LinkManager.GetPreviewSiteContext(item);
if (previewSiteContext == null)
{
Editor.Visible = false;
DisabledNotice.Visible = true;
AlertMessage.Text = Translate.Text("Site \"{0}\" not found", (object)Settings.Preview.DefaultSite);
}
else
{
previewSiteContext.SetDisplayMode(DisplayMode.Preview, DisplayModeDuration.Remember);
PreviewManager.StoreShellUser(Settings.Preview.AsAnonymous);
Editor.SourceUri = GetUrl(item, previewSiteContext).ToString();
}
}
}
private string ReplaceUrlHost(string Source, string Find, string Replace)
{
Source = Source.ToLowerInvariant();
Find = Find.ToLowerInvariant();
int startIndex = Source.IndexOf(Find);
return Source.Remove(startIndex, Find.Length).Insert(startIndex, Replace);
}
private DeviceItem GetDevice(Item item)
{
DeviceItem deviceItem = null;
SiteContext previewSiteContext = LinkManager.GetPreviewSiteContext(item);
if (previewSiteContext != null)
deviceItem = !string.IsNullOrEmpty(previewSiteContext.Device) ? item.Database.Resources.Devices[previewSiteContext.Device] : DeviceItem.ResolveDevice(item.Database);
return deviceItem ?? DeviceItem.ResolveDevice(item.Database);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.