/// <summary> /// Executes on the current document object when the specified actions occur /// </summary> /// <param name="documentObject">The document object.</param> /// <param name="action">The action.</param> /// <returns>Returns true if successfull, otherwise false</returns> public bool Execute(umbraco.cms.businesslogic.web.Document documentObject, interfaces.IAction action) { if (UmbracoSettings.EnsureUniqueNaming) { string currentName = documentObject.Text; int uniqueNumber = 1; // Check for all items underneath the parent to see if they match // as any new created documents are stored in the bottom, we can just // keep checking for other documents with a uniquenumber from //store children array here because iterating over an Array property object is very inneficient. var c = Document.GetChildrenBySearch(documentObject.ParentId, currentName + "%"); // must sort the list or else duplicate name will exist if pages are out out sequence //e.g. Page (1), Page (3), Page (2) var results = c.OrderBy(x => x.Text, new SimilarNodeNameComparer()); foreach (Document d in results) { if (d.Id != documentObject.Id && d.Text.ToLower() == currentName.ToLower()) { currentName = documentObject.Text + " (" + uniqueNumber.ToString() + ")"; uniqueNumber++; } } // if name has been changed, update the documentobject if (currentName != documentObject.Text) { // add name change to the log LogHelper.Debug <umbEnsureUniqueName>("Title changed from '" + documentObject.Text + "' to '" + currentName + "' for document id" + documentObject.Id); documentObject.Text = currentName; return(true); } } return(false); }
/// <summary> /// Executes on the current document object when the specified actions occur /// </summary> /// <param name="documentObject">The document object.</param> /// <param name="action">The action.</param> /// <returns>Returns true if successfull, otherwise false</returns> public bool Execute(umbraco.cms.businesslogic.web.Document documentObject, interfaces.IAction action) { if (UmbracoSettings.EnsureUniqueNaming) { string currentName = documentObject.Text; int uniqueNumber = 1; // Check for all items underneath the parent to see if they match // as any new created documents are stored in the bottom, we can just // keep checking for other documents with a uniquenumber from //store children array here because iterating over an Array property object is very inneficient. var c = Document.GetChildrenBySearch(documentObject.ParentId, currentName + "%"); foreach (Document d in c) { if (d.Id != documentObject.Id && d.Text.ToLower() == currentName.ToLower()) { currentName = documentObject.Text + " (" + uniqueNumber.ToString() + ")"; uniqueNumber++; } } // if name has been changed, update the documentobject if (currentName != documentObject.Text) { // add name change to the log umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, umbraco.BusinessLogic.User.GetUser(0), documentObject.Id, "Title changed from '" + documentObject.Text + "' to '" + currentName + "'"); documentObject.Text = currentName; return(true); } } return(false); }