CRM_Core_Transaction - Doc fix
[civicrm-core.git] / Civi / CCase / Events.php
index 9c5c10c588758508d51b306f12193c588adc6f3b..5ff406004669c212f09dcb87cbfd3d3fe9d2762b 100644 (file)
@@ -44,22 +44,41 @@ class Events {
     $caseId = NULL;
     switch ($event->entity) {
       case 'Activity':
-        if ($event->object->case_id) {
+        if (!empty($event->object->case_id)) {
           $caseId = $event->object->case_id;
         }
         break;
       case 'Case':
-        $caseId = $event->id;
+        // by the time we get the post-delete event, the record is gone, so
+        // there's nothing to analyze
+        if ($event->action != 'delete') {
+          $caseId = $event->id;
+        }
         break;
       default:
         throw new \CRM_Core_Exception("CRM_Case_Listener does not support entity {$event->entity}");
     }
 
-    if ($caseId && !isset(self::$isActive[$caseId])) {
+    if ($caseId) {
+      if (!isset(self::$isActive[$caseId])) {
+        \CRM_Core_Transaction::addCallback(
+          \CRM_Core_Transaction::PHASE_POST_COMMIT,
+          array(__CLASS__, 'fireCaseChangeForRealz'),
+          array($caseId),
+          "Civi_CCase_Events::fire::{$caseId}"
+        );
+      }
+    }
+  }
+
+  public static function fireCaseChangeForRealz($caseId) {
+    if (!isset(self::$isActive[$caseId])) {
+      $tx = new \CRM_Core_Transaction();
       self::$isActive[$caseId] = 1;
       $analyzer = new \Civi\CCase\Analyzer($caseId);
       \CRM_Utils_Hook::caseChange($analyzer);
       unset(self::$isActive[$caseId]);
+      unset($tx);
     }
   }