This Actionhandler keeps nodes nicely named and sorted. The basic idea is that the webauthor creates a lot of nodes. In this case it's calendarbookings. The webautor had trouble with naming them and moving them around.
So when she creates a new node and publish it - this bit of code does the following for her:
1. Rename the node to reflect the value of one of the properties (a date value)
2. Sort the nodes (in Umbraco).
3. Republish
Now .. I'm crossing my fingers hoping that the "smilies from Hell" won't show their hate :cry:
(translate: that the code won't be messed up with smilies)
Code:
public class RenameAndSort : IActionHandler
{
// struct used for sorting
public struct MyStruture : IComparable
{
public Int32 iID;
public String sName;
public Int32 CompareTo(Object obj)
{
MyStruture tmpObj = (MyStruture)obj;
return (this.sName.CompareTo(tmpObj.sName));
}
}
/// <summary>
/// Sort and publish all siblings and parent node
/// </summary>
/// <param name="d">one of the siblings</param>
private void SortThisOut(Document d) {
// get all siblings into a array
ArrayList a = new ArrayList();
foreach (umbraco.cms.businesslogic.CMSNode c in d.Parent.Children)
{
MyStruture m = new MyStruture();
m.iID = c.Id;
m.sName = c.Text;
a.Add(m);
}
// sort the array
a.Sort();
// get all the nodes and change their sort order. publish them ..
for (int i = 0; i < a.Count; i++) {
// out of array
MyStruture m = (MyStruture) a[i];
// new sort order
new umbraco.cms.businesslogic.CMSNode(m.iID).sortOrder = i;
// update cache
library.PublishSingleNode(m.iID);
}
// seems like it's needed to update the cache for the parent node .. sometimes ...
library.PublishSingleNode(d.Parent.Id);
}
public bool Execute(Document documentObject, IAction action)
{
try
{
// Only work on one document type
if (documentObject.ContentType.Alias == "CalenderItem")
{
// the node items is named from a field/property named weekStartDate which is a datetime type.
DateTime dt = (DateTime)documentObject.getProperty("weekStartDate").Value;
string dttext = dt.ToString("yyyy-MM-dd");
// compare to prevent an endless republish mayhem
if (!documentObject.Text.Equals(dttext))
{
documentObject.Text = dttext;
documentObject.Publish(documentObject.User);
library.PublishSingleNode(documentObject.Id);
// sort siblings and publish all
SortThisOut(documentObject);
}
}
}
catch (Exception err)
{
// TODO
string msg = err.Message;
}
return true;
}
public string HandlerName()
{
return "RenameAndSort";
}
public IAction[] ReturnActions()
{
return new IAction[] { new ActionPublish() };
}
}
Kindly,
Jesper
webbureau jesper.com doing
webdesign /
development /
umbraco implementations / 2007&2008 MVP /
umbraco certified