Last active
August 29, 2015 14:23
-
-
Save KKings/e84ea06fd05aeee356ab to your computer and use it in GitHub Desktop.
Page Editor warning for duplicate item names
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 NavArts.Insights.Pipelines.GetPageEditorNotifications | |
{ | |
using System; | |
using System.Linq; | |
using global::Sitecore.ContentSearch.Fluent; | |
using global::Sitecore.ContentSearch.Utilities; | |
using global::Sitecore.Data; | |
using global::Sitecore.Data.Items; | |
using global::Sitecore.Diagnostics; | |
using global::Sitecore.Pipelines.GetPageEditorNotifications; | |
using Search.Results; | |
using Sitecore.Extensions; | |
using SC = global::Sitecore; | |
/// <summary> | |
/// Uses an index to query for items with a name match (exact) | |
/// </summary> | |
public class DuplicateItemNameValidatorWarning : GetPageEditorNotificationsProcessor | |
{ | |
/// <summary> | |
/// Template Id to Specify the type of Content Item to Run on and to filter out search results | |
/// </summary> | |
private readonly ID _templateId; | |
/// <summary> | |
/// Index Used for searching based on name | |
/// </summary> | |
private const string IndexName = "sitecore_master_index"; | |
/// <summary> | |
/// Instantiates a new instance of the <see cref="DuplicateItemNameValidatorWarning"/> with a template id | |
/// </summary> | |
public DuplicateItemNameValidatorWarning(string templateId) | |
{ | |
Assert.IsNotNullOrEmpty(templateId, "templateId cannot be null."); | |
this._templateId = ID.Parse(templateId); | |
} | |
/// <summary> | |
/// Query the index for any matching items with the same name | |
/// </summary> | |
/// <param name="args">Page Editor Args</param> | |
public override void Process(GetPageEditorNotificationsArgs args) | |
{ | |
var item = args.ContextItem; | |
if (item == null || item.TemplateID != this._templateId) | |
{ | |
return; | |
} | |
var itemId = item.ID.ToString().NormalizeGuid(); | |
var results = | |
new SearchManager(DuplicateItemNameValidatorWarning.IndexName, DuplicateItemNameValidatorWarning.IndexName).ResultsFor<InsightsResultItem> | |
(search => search | |
.Options(o => o | |
.AddRestriction(this._templateId)) | |
.Query(query => query | |
.And(and => and | |
.Where(result => result.Name == item.Name))) | |
.Filter(filter => filter | |
.And(and => and | |
.Where(result => result.RawId != itemId)))); | |
if (results.Total > 0) | |
{ | |
var description = SC.Globalization.Translate.Text( | |
"If you publish now, the current item may not render properly because the following items have the same name. Click the links below to review these items:"); | |
var editorNotification = new PageEditorNotification(description, PageEditorNotificationType.Warning); | |
results.Results.Select(result => item.Database.GetItem(result.Id)).ForEach(i => DuplicateItemNameValidatorWarning.AddWarningOption(i, editorNotification)); | |
args.Notifications.Add(editorNotification); | |
} | |
} | |
/// <summary> | |
/// Add a warning option to the GetPageEditorNotificationsArgs | |
/// </summary> | |
/// <param name="dataSourceItem">Item to reference</param> | |
/// <param name="pageEditorNotification">Page Editor Notification</param> | |
private static void AddWarningOption(Item dataSourceItem, PageEditorNotification pageEditorNotification) | |
{ | |
pageEditorNotification.Options.Add(new PageEditorNotificationOption(dataSourceItem.Name, | |
String.Format("webedit:open(id={0},language={1},version={2})", | |
dataSourceItem.ID, | |
dataSourceItem.Language, | |
dataSourceItem.Version.Number))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment