Last active
June 16, 2016 13:54
-
-
Save mskutta/a28a4a50bcdc005f3f95fc99b04cc12b 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
using System; | |
using System.Linq; | |
using System.Web.UI; | |
using Sitecore; | |
using Sitecore.Configuration; | |
using Sitecore.Data; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.IO; | |
using Sitecore.Links; | |
using Sitecore.Publishing; | |
using Sitecore.Resources.Media; | |
using Sitecore.Shell.Framework.Commands; | |
using Sitecore.Shell.Web.UI.WebControls; | |
using Sitecore.Sites; | |
using Sitecore.Web.UI.WebControls.Ribbons; | |
using Website.Logic.Common.Extensions; | |
namespace Website.Logic.ContentEditor | |
{ | |
public class PublishInfo : RibbonPanel | |
{ | |
/// <summary> | |
/// Publish Info - see here for details: | |
/// http://webcmd.wordpress.com/2011/08/31/sitecore-ribbon-that-displays-published-state-of-an-item/ | |
/// </summary> | |
/// <param name="output"></param> | |
/// <param name="ribbon"></param> | |
/// <param name="button"></param> | |
/// <param name="context"></param> | |
public override void Render(HtmlTextWriter output, Ribbon ribbon, Item button, CommandContext context) | |
{ | |
// Validate Arguments | |
Assert.ArgumentNotNull(output, "output"); | |
Assert.ArgumentNotNull(ribbon, "ribbon"); | |
Assert.ArgumentNotNull(button, "button"); | |
Assert.ArgumentNotNull(context, "context"); | |
try | |
{ | |
var htmlLinkOutput = string.Empty; | |
var htmlPublishOutput = string.Empty; | |
// Confirm item exists in the context | |
var contextItem = context.Items.FirstOrDefault(); | |
if (contextItem != null) | |
{ | |
var url = string.Empty; | |
if (contextItem.Paths.IsContentItem) | |
{ | |
// Find the best matching site | |
var contextItemPath = contextItem.Paths.Path.ToLower(); | |
var site = SiteContextFactory.Sites | |
.FirstOrDefault(s => s.VirtualFolder == "/" && s.RootPath != "" && contextItemPath.StartsWith(FileUtil.MakePath(s.RootPath, s.StartItem).ToLower())); | |
if (site != null) | |
{ | |
var urlOptions = LinkManager.GetDefaultUrlOptions(); | |
urlOptions.Site = new SiteContext(site); | |
url = LinkManager.GetItemUrl(contextItem, urlOptions); | |
} | |
} | |
else if (contextItem.Paths.IsMediaItem && contextItem.TemplateID != TemplateIDs.MediaFolder) | |
{ | |
using (new SiteContextSwitcher(Factory.GetSite("website"))) | |
url = MediaManager.GetMediaUrl(contextItem); | |
} | |
// Create the HTML output to render. | |
if (!string.IsNullOrEmpty(url)) | |
{ | |
htmlLinkOutput = string.Format("<div style='padding:3px 3px 5px 7px;display: inline-block;border:1px solid;'>" + | |
"<div style='padding:3px 0px 5px 0px;'>Relative Link to this page</div>" + | |
"<div style='font-weight:bold'>{0}</div>" + | |
"</div> ", url); | |
} | |
// Determine if the item is published | |
// Obtain reference to the master database | |
var masterDb = Database.GetDatabase("master"); | |
// Find all of the publishing targets and determine if the item has been published to those targets | |
var publishingTargets = PublishManager.GetPublishingTargets(masterDb); | |
foreach (var publishingTarget in publishingTargets) | |
{ | |
var targetDatabaseName = publishingTarget.GetValueOrDefault<string>("Target database"); | |
var isPreviewTarget = publishingTarget.GetValueOrDefault<bool>("Preview publishing target"); | |
if (!string.IsNullOrEmpty(targetDatabaseName) && !isPreviewTarget) | |
{ | |
var targetDatabase = Database.GetDatabase(targetDatabaseName); | |
if (targetDatabase != null) | |
{ | |
//SelectSingleItem does a direct request to the database for the item | |
var item = targetDatabase.SelectSingleItem(contextItem.ID.ToString()); | |
if (item != null) | |
{ | |
foreach (var language in item.Languages) | |
{ | |
var languageVersion = item.Versions.GetLatestVersion(language); | |
if (languageVersion != null && languageVersion.Versions.Count > 0) | |
{ | |
htmlPublishOutput += string.Format("<div>{0} - {1}</div>", languageVersion.Version.Number, languageVersion.Language.CultureInfo.DisplayName); | |
} | |
} | |
} | |
// Exit after the first publishing target | |
break; | |
} | |
} | |
} | |
if (string.IsNullOrEmpty(htmlPublishOutput)) | |
htmlPublishOutput = string.Format("<div>{0}</div>", "No"); | |
htmlPublishOutput = string.Format("<div style='padding:3px 3px 5px 7px;display: inline-block;border:1px solid;'>" + | |
"<div style='padding:3px 0px 5px 0px;font-weight:bold'>Published to Web</div>" + | |
"{0}" + | |
"</div>", htmlPublishOutput); | |
} | |
var htmlOutput = | |
string.Format( | |
"<div class='scRibbonToolbarText' style='padding:0 10px 7px 5px;height:auto;border:none !important;float:none;display:inline-block;'>" + | |
"{0}" + | |
"{1}" + | |
"</div>", | |
htmlLinkOutput, htmlPublishOutput); | |
output.Write(htmlOutput); | |
} | |
catch (Exception ex) | |
{ | |
Log.Error("Exception in custom ItemUrlInfo Ribbon: " + ex.Message, this); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment