using Sitecore.Rules;
using Sitecore.Rules.Conditions;
namespace RulesEngine.Rules
{
public class RegistrationDateCondition<T> :
OperatorCondition<T> where T : RuleContext
{
//This is the value the editor will enter
public string StartDate { get; set; }
/// <summary>
/// This function will be called to test the condition
/// </summary>
protected override bool Execute(T ruleContext)
{
//Get the current user from our dummy user session manager
var user = UserSession.GetCurrentUser();
//Check if the user is logged in, otherwise return false
if (user == null)
return false;
//Check the editor entered a value
if (string.IsNullOrEmpty(StartDate))
{
return false;
}
DateTime userRegistrationDate = user.RegistrationDate;
DateTime startDate =
Sitecore.DateUtil.IsoDateToDateTime(StartDate);
//Do a date comparison and take the result return a value
var compareResult =
userRegistrationDate.Date.CompareTo(startDate.Date);
//Get the operator the editor selected
switch (this.GetOperator())
{
case ConditionOperator.Equal:
return compareResult == 0;
case ConditionOperator.GreaterThanOrEqual:
return compareResult == 0 || compareResult > 0;
case ConditionOperator.GreaterThan:
return compareResult > 0;
case ConditionOperator.LessThanOrEqual:
return compareResult == 0 || compareResult < 0;
case ConditionOperator.LessThan:
return compareResult < 0;
case ConditionOperator.NotEqual:
return compareResult != 0;
default:
return false;
}
}
}
}