Thursday, February 2, 2012

The Customer Engagement Series: A quick time check

In the spirit of quick rules examples, I recently had a question about simply doing something on the site based on what time it is.  Easy with Sitecore’s Rules Engine, but let’s mock something up to go through putting the pieces together.

First, the item to represent the Rule Condition.  I created an item in this location:

/sitecore/system/Settings/Rules/Conditional Renderings/Conditions/Events/Time Is Before Condition

….where the item itself is named “Time is Before Condition”.  Sitecore 6.5 has lots of categorization folders in this area (or you could create your own).  I decided Events was a good enough categorization for this condition.  This item is based on the data template:

/sitecore/templates/System/Rules/Condition

Here’s how the item looks:

image

Now someone who’s not a developer should be able to simply add a time to this condition.  Using the very handy Rule Set Editor in 6.5, we could easily string together a bunch of time conditions from top to bottom (“if it’s before 12:30 PM do this, if not but it’s still before 1:30 PM do this, etc.”)

From the Rules Engine Cookbook, in describing the four parameters in the bracketed expression within the  Text field above:

1. The name of the property that the parameter sets in the .NET class that implements the condition.
2. The name of an item beneath the /Sitecore/System/Settings/Rules/Common/Macros item providing predefined functionality, or an empty string. For example, to activate a user interface allowing the user to select an item from the content tree, specify the value tree.
3. URL parameters to the user interface activated by the macro specified by the previous parameter, or an empty string. For example, to specify the /Sitecore/Content/Home item as the root for a selection tree, enter root=/sitecore/content/home.
4. Text to display if the user has not specified a value for the parameter.

With that, the condition will now display in the Rule Set Editor like this:

image

And the simple class that is called on when this rule is evaluated:

using Sitecore.Rules;
using Sitecore.Diagnostics;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Rules.Conditions;

namespace Examples
{
public class TimeIsBeforeCondition<T> : WhenCondition<T> where T : RuleContext
{
public TimeIsBeforeCondition()
{
}

protected override bool Execute(T ruleContext)
{
Assert.ArgumentNotNull(ruleContext, "ruleContext");

//if t1 is less than t2 then result is Less than zero
//if t1 equals t2 then result is Zero
//if t1 is greater than t2 then result isGreater zero
System.DateTime t1 = System.DateTime.Now;
System.DateTime t2 = System.Convert.ToDateTime(this.Value);
int i = System.DateTime.Compare(t1,t2);
return i < 0;

}

public string Value { get; set; }

}
}




Note that the Value property is set by the value entered into the textbox above.  This certainly could be improved (beyond checking for errors) by enforcing a content item representing each hour to be chosen from a Tree View (though this of course doesn’t make sense when you want to provide minute by minute choices.