3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
20 * @var array (int $caseId => bool $active) list of cases for which we are actively firing case-change event
22 * We do not want to fire case-change events recursively.
24 public static $isActive = [];
27 * Following a change to an activity or case, fire the case-change event.
29 * @param \Civi\Core\Event\PostEvent $event
30 * @throws \CRM_Core_Exception
32 public static function fireCaseChange(\Civi\Core\Event\PostEvent
$event) {
33 // Activities can be linked to multiple cases, so $caseIds might be an array or an int
35 switch ($event->entity
) {
37 if (!empty($event->object->case_id
)) {
38 $caseIds = $event->object->case_id
;
43 // by the time we get the post-delete event, the record is gone, so
44 // there's nothing to analyze
45 if ($event->action
!= 'delete') {
46 $caseIds = $event->id
;
51 throw new \
CRM_Core_Exception("CRM_Case_Listener does not support entity {$event->entity}");
55 foreach ((array) $caseIds as $caseId) {
56 if (!isset(self
::$isActive[$caseId])) {
57 $tx = new \
CRM_Core_Transaction();
58 \CRM_Core_Transaction
::addCallback(
59 \CRM_Core_Transaction
::PHASE_POST_COMMIT
,
60 [__CLASS__
, 'fireCaseChangeForRealz'],
62 "Civi_CCase_Events::fire::{$caseId}"
70 * Fire case change hook
72 * @param int|array $caseIds
74 public static function fireCaseChangeForRealz($caseIds) {
75 foreach ((array) $caseIds as $caseId) {
76 if (!isset(self
::$isActive[$caseId])) {
77 $tx = new \
CRM_Core_Transaction();
78 self
::$isActive[$caseId] = 1;
79 $analyzer = new \Civi\CCase\
Analyzer($caseId);
80 \CRM_Utils_Hook
::caseChange($analyzer);
81 unset(self
::$isActive[$caseId]);
88 * Find any extra listeners declared in XML and pass the event along to them.
90 * @param \Civi\CCase\Event\CaseChangeEvent $event
92 public static function delegateToXmlListeners(\Civi\CCase\Event\CaseChangeEvent
$event) {
93 $p = new \
CRM_Case_XMLProcessor_Process();
94 $listeners = $p->getListeners($event->analyzer
->getCaseType());
95 foreach ($listeners as $listener) {
96 /** @var $listener \Civi\CCase\CaseChangeListener */
97 $listener->onCaseChange($event);