From 822a33f094702144b6a5001116a1d309c6b938d9 Mon Sep 17 00:00:00 2001
From: Jaap Jansma <jaap@edeveloper.nl>
Date: Mon, 12 Oct 2015 23:57:27 +0200
Subject: [PATCH] proof of concept of logger

---
 CRM/Civirules/Engine.php | 26 +++++++++++++++++++-------
 CRM/Civirules/Event.php  | 16 ++++++++++++++++
 2 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/CRM/Civirules/Engine.php b/CRM/Civirules/Engine.php
index 283e1c2..d4bfd29 100644
--- a/CRM/Civirules/Engine.php
+++ b/CRM/Civirules/Engine.php
@@ -22,13 +22,17 @@ class CRM_Civirules_Engine {
    * @static
    */
   public static function triggerRule(CRM_Civirules_Event $event, CRM_Civirules_EventData_EventData $eventData) {
-    $eventData->setEvent($event);
-    $isRuleValid = self::areConditionsValid($eventData);
+    try {
+      $eventData->setEvent($event);
+      $isRuleValid = self::areConditionsValid($eventData);
 
-    if ($isRuleValid) {
-      self::logRule($eventData);
-      self::executeActions($eventData);
-      return true;
+      if ($isRuleValid) {
+        self::logRule($eventData);
+        self::executeActions($eventData);
+        return true;
+      }
+    } catch (Exception $e) {
+      CRM_Civirules_Utils_LoggerFactory::logError("Failed to execute rule",  $e->getMessage(), $eventData);
     }
     return false;
   }
@@ -120,7 +124,11 @@ class CRM_Civirules_Engine {
    * @return bool
    */
   public static function executeDelayedAction(CRM_Queue_TaskContext $ctx, CRM_Civirules_Action $action, CRM_Civirules_EventData_EventData $eventData) {
-    $action->processAction($eventData);
+    try {
+      $action->processAction($eventData);
+    } catch (Exception $e) {
+      CRM_Civirules_Utils_LoggerFactory::logError("Failed to execute delayed action",  $e->getMessage(), $eventData);
+    }
     return true;
   }
 
@@ -248,6 +256,9 @@ class CRM_Civirules_Engine {
   /**
    * This function writes a record to the log table to indicate that this rule for this event is triggered
    *
+   * The data this function stores is required by the cron type events.
+   * @todo: think of a better handling for cron type events
+   *
    * @param CRM_Civirules_EventData_EventData $eventData
    */
   protected static function logRule(CRM_Civirules_EventData_EventData $eventData) {
@@ -256,4 +267,5 @@ class CRM_Civirules_Engine {
     $params[2] = array($eventData->getContactId(), 'Integer');
     CRM_Core_DAO::executeQuery($sql, $params);
   }
+
 }
\ No newline at end of file
diff --git a/CRM/Civirules/Event.php b/CRM/Civirules/Event.php
index dc6ae5b..b0ae6ba 100644
--- a/CRM/Civirules/Event.php
+++ b/CRM/Civirules/Event.php
@@ -8,6 +8,11 @@ abstract class CRM_Civirules_Event {
 
   protected $eventParams;
 
+  /**
+   * @var string
+   */
+  protected $ruleTitle;
+
   public function setRuleId($ruleId) {
     $this->ruleId = $ruleId;
   }
@@ -28,6 +33,17 @@ abstract class CRM_Civirules_Event {
     return $this->eventId;
   }
 
+  public function getRuleTitle() {
+    if (empty($this->ruleTitle) && !empty($this->ruleId)) {
+      $rule = new CRM_Civirules_BAO_Rule();
+      $rule->id = $this->ruleId;
+      if ($rule->find(true)) {
+        $this->ruleTitle = $rule->label;
+      }
+    }
+    return $this->ruleTitle;
+  }
+
   /**
    * Returns an array of entities on which the event reacts
    *
-- 
GitLab