(NFC) Civi/CCase - Update for Drupal.Commenting.VariableComment.IncorrectVarType
[civicrm-core.git] / Civi / CCase / Events.php
CommitLineData
708d8fa2
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
41498ac5 4 | Copyright CiviCRM LLC. All rights reserved. |
708d8fa2 5 | |
41498ac5
TO
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 |
708d8fa2 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
708d8fa2
TO
11namespace Civi\CCase;
12
7fe37828
EM
13/**
14 * Class Events
15 *
16 * @package Civi\CCase
17 */
708d8fa2 18class Events {
99cd5892 19
874dae9d 20 /**
99cd5892 21 * List of cases for which we are actively firing case-change event
874dae9d 22 * We do not want to fire case-change events recursively.
99cd5892
TO
23 *
24 * array (int $caseId => bool $active)
25 *
26 * @var array
874dae9d 27 */
34f3bbd9 28 public static $isActive = [];
874dae9d 29
708d8fa2
TO
30 /**
31 * Following a change to an activity or case, fire the case-change event.
32 *
33 * @param \Civi\Core\Event\PostEvent $event
34 * @throws \CRM_Core_Exception
35 */
36 public static function fireCaseChange(\Civi\Core\Event\PostEvent $event) {
c0a62573
MWMC
37 // Activities can be linked to multiple cases, so $caseIds might be an array or an int
38 $caseIds = NULL;
708d8fa2
TO
39 switch ($event->entity) {
40 case 'Activity':
12784f11 41 if (!empty($event->object->case_id)) {
c0a62573 42 $caseIds = $event->object->case_id;
708d8fa2
TO
43 }
44 break;
ea100cb5 45
708d8fa2 46 case 'Case':
1be6caec
TO
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') {
c0a62573 50 $caseIds = $event->id;
1be6caec 51 }
708d8fa2 52 break;
ea100cb5 53
708d8fa2
TO
54 default:
55 throw new \CRM_Core_Exception("CRM_Case_Listener does not support entity {$event->entity}");
56 }
57
c0a62573
MWMC
58 if ($caseIds) {
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,
c64f69d9
CW
64 [__CLASS__, 'fireCaseChangeForRealz'],
65 [$caseId],
c0a62573
MWMC
66 "Civi_CCase_Events::fire::{$caseId}"
67 );
68 }
f99a2e5d
TO
69 }
70 }
71 }
72
7fe37828 73 /**
18e0f096
CW
74 * Fire case change hook
75 *
76 * @param int|array $caseIds
7fe37828 77 */
18e0f096
CW
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]);
86 unset($tx);
87 }
708d8fa2
TO
88 }
89 }
90
91 /**
fe482240 92 * Find any extra listeners declared in XML and pass the event along to them.
708d8fa2 93 *
3bdca100 94 * @param \Civi\CCase\Event\CaseChangeEvent $event
708d8fa2
TO
95 */
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);
102 }
103 }
96025800 104
ef10e0b5 105}