diff --git a/CRM/Civirules/Action.php b/CRM/Civirules/Action.php index 2487c926c230c8b8b3f01286ba9821f22550f064..ebb687cc0c0ddb0bde0bb2b19c708c9399bb4fc3 100644 --- a/CRM/Civirules/Action.php +++ b/CRM/Civirules/Action.php @@ -20,6 +20,24 @@ abstract class CRM_Civirules_Action { */ abstract public function processAction(CRM_Civirules_EventData_EventData $eventData); + /** + * You could override this method to create a delay for your action + * + * You might have a specific action which is Send Thank you and which + * includes sending thank you SMS to the donor but only between office hours + * + * If you have a delay you should return a DateTime object with a future date and time + * for when this action should be processed. + * + * If you don't have a delay you should return false + * + * @param DateTime $date the current scheduled date/time + * @return bool|DateTime + */ + public function delayTo(DateTime $date) { + return false; + } + /** * Method to set RuleActionData * diff --git a/CRM/Civirules/Engine.php b/CRM/Civirules/Engine.php index 828a851ba50640fb21f389ed2d7e81765e90da63..e8a88540f5948ea7bc9e9a6bcfd0fc03835e60c1 100644 --- a/CRM/Civirules/Engine.php +++ b/CRM/Civirules/Engine.php @@ -64,7 +64,7 @@ class CRM_Civirules_Engine { $object->setRuleActionData($ruleAction); //determine if the action should be executed with a delay - $delay = self::getActionDelay($ruleAction); + $delay = self::getActionDelay($ruleAction, $object); if ($delay instanceof DateTime) { self::delayAction($delay, $object, $eventData); } else { @@ -158,28 +158,32 @@ class CRM_Civirules_Engine { * The delay is calculated by a seperate delay class. See CRM_Civirules_DelayDelay * * @param $ruleAction + * @param CRM_Civirules_Action $actionObject * @return bool|\DateTime */ - protected static function getActionDelay($ruleAction) { + protected static function getActionDelay($ruleAction, CRM_Civirules_Action $actionObject) { //if the delay is empty the if (empty($ruleAction['delay'])) { return false; } - $delayClass = unserialize(($ruleAction['delay'])); - if (! ($delayClass instanceof CRM_Civirules_Delay_Delay)) { - return false; - } - $delayedTo = new DateTime(); $now = new DateTime(); - $delayedTo = $delayClass->delayTo($delayedTo); + $delayClass = unserialize(($ruleAction['delay'])); + if ($delayClass instanceof CRM_Civirules_Delay_Delay) { + $delayedTo = $delayClass->delayTo($delayedTo); + } - if ($now >= $delayedTo) { + $actionDelayedTo = $actionObject->delayTo($delayedTo); + if ($actionDelayedTo instanceof DateTime) { + if ($now < $delayedTo) { + return $actionDelayedTo; + } return false; + } elseif ($delayedTo instanceof DateTime and $now < $delayedTo) { + return $delayedTo; } - - return $delayedTo; + return false; } /**