Merge pull request #16545 from eileenmcnaughton/part_pend
[civicrm-core.git] / Civi / CCase / Events.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11 namespace Civi\CCase;
12
13 /**
14 * Class Events
15 *
16 * @package Civi\CCase
17 */
18 class Events {
19
20 /**
21 * List of cases for which we are actively firing case-change event
22 * We do not want to fire case-change events recursively.
23 *
24 * array (int $caseId => bool $active)
25 *
26 * @var array
27 */
28 public static $isActive = [];
29
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) {
37 // Activities can be linked to multiple cases, so $caseIds might be an array or an int
38 $caseIds = NULL;
39 switch ($event->entity) {
40 case 'Activity':
41 if (!empty($event->object->case_id)) {
42 $caseIds = $event->object->case_id;
43 }
44 break;
45
46 case 'Case':
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;
51 }
52 break;
53
54 default:
55 throw new \CRM_Core_Exception("CRM_Case_Listener does not support entity {$event->entity}");
56 }
57
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,
64 [__CLASS__, 'fireCaseChangeForRealz'],
65 [$caseId],
66 "Civi_CCase_Events::fire::{$caseId}"
67 );
68 }
69 }
70 }
71 }
72
73 /**
74 * Fire case change hook
75 *
76 * @param int|array $caseIds
77 */
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 }
88 }
89 }
90
91 /**
92 * Find any extra listeners declared in XML and pass the event along to them.
93 *
94 * @param \Civi\CCase\Event\CaseChangeEvent $event
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 }
104
105 }