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 +--------------------------------------------------------------------+
21 * List of cases for which we are actively firing case-change event
22 * We do not want to fire case-change events recursively.
24 * array (int $caseId => bool $active)
28 public static $isActive = [];
31 * Following a change to an activity or case, fire the case-change event.
33 * @param \Civi\Core\Event\PostEvent $event
34 * @throws \CRM_Core_Exception
36 public static function fireCaseChange(\Civi\Core\Event\PostEvent
$event) {
37 // Activities can be linked to multiple cases, so $caseIds might be an array or an int
39 switch ($event->entity
) {
41 if (!empty($event->object->case_id
)) {
42 $caseIds = $event->object->case_id
;
47 // by the time we get the post-delete event, the record is gone, so
48 // there's nothing to analyze
49 if ($event->action
!= 'delete') {
50 $caseIds = $event->id
;
55 throw new \
CRM_Core_Exception("CRM_Case_Listener does not support entity {$event->entity}");
59 foreach ((array) $caseIds as $caseId) {
60 if (!isset(self
::$isActive[$caseId])) {
61 $tx = new \
CRM_Core_Transaction();
62 \CRM_Core_Transaction
::addCallback(
63 \CRM_Core_Transaction
::PHASE_POST_COMMIT
,
64 [__CLASS__
, 'fireCaseChangeForRealz'],
66 "Civi_CCase_Events::fire::{$caseId}"
74 * Fire case change hook
76 * @param int|array $caseIds
78 public static function fireCaseChangeForRealz($caseIds) {
79 foreach ((array) $caseIds as $caseId) {
80 if (!isset(self
::$isActive[$caseId])) {
81 $tx = new \
CRM_Core_Transaction();
82 self
::$isActive[$caseId] = 1;
83 $analyzer = new \Civi\CCase\
Analyzer($caseId);
84 \CRM_Utils_Hook
::caseChange($analyzer);
85 unset(self
::$isActive[$caseId]);
92 * Find any extra listeners declared in XML and pass the event along to them.
94 * @param \Civi\CCase\Event\CaseChangeEvent $event
96 public static function delegateToXmlListeners(\Civi\CCase\Event\CaseChangeEvent
$event) {
97 $p = new \
CRM_Case_XMLProcessor_Process();
98 $listeners = $p->getListeners($event->analyzer
->getCaseType());
99 foreach ($listeners as $listener) {
100 /** @var $listener \Civi\CCase\CaseChangeListener */
101 $listener->onCaseChange($event);