Commit | Line | Data |
---|---|---|
708d8fa2 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
708d8fa2 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
708d8fa2 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
708d8fa2 TO |
27 | namespace Civi\CCase; |
28 | ||
7fe37828 EM |
29 | /** |
30 | * Class Events | |
31 | * | |
32 | * @package Civi\CCase | |
33 | */ | |
708d8fa2 | 34 | class Events { |
874dae9d TO |
35 | /** |
36 | * @var array (int $caseId => bool $active) list of cases for which we are actively firing case-change event | |
37 | * | |
38 | * We do not want to fire case-change events recursively. | |
39 | */ | |
c64f69d9 | 40 | static $isActive = []; |
874dae9d | 41 | |
708d8fa2 TO |
42 | /** |
43 | * Following a change to an activity or case, fire the case-change event. | |
44 | * | |
45 | * @param \Civi\Core\Event\PostEvent $event | |
46 | * @throws \CRM_Core_Exception | |
47 | */ | |
48 | public static function fireCaseChange(\Civi\Core\Event\PostEvent $event) { | |
c0a62573 MWMC |
49 | // Activities can be linked to multiple cases, so $caseIds might be an array or an int |
50 | $caseIds = NULL; | |
708d8fa2 TO |
51 | switch ($event->entity) { |
52 | case 'Activity': | |
12784f11 | 53 | if (!empty($event->object->case_id)) { |
c0a62573 | 54 | $caseIds = $event->object->case_id; |
708d8fa2 TO |
55 | } |
56 | break; | |
ea100cb5 | 57 | |
708d8fa2 | 58 | case 'Case': |
1be6caec TO |
59 | // by the time we get the post-delete event, the record is gone, so |
60 | // there's nothing to analyze | |
61 | if ($event->action != 'delete') { | |
c0a62573 | 62 | $caseIds = $event->id; |
1be6caec | 63 | } |
708d8fa2 | 64 | break; |
ea100cb5 | 65 | |
708d8fa2 TO |
66 | default: |
67 | throw new \CRM_Core_Exception("CRM_Case_Listener does not support entity {$event->entity}"); | |
68 | } | |
69 | ||
c0a62573 MWMC |
70 | if ($caseIds) { |
71 | foreach ((array) $caseIds as $caseId) { | |
72 | if (!isset(self::$isActive[$caseId])) { | |
73 | $tx = new \CRM_Core_Transaction(); | |
74 | \CRM_Core_Transaction::addCallback( | |
75 | \CRM_Core_Transaction::PHASE_POST_COMMIT, | |
c64f69d9 CW |
76 | [__CLASS__, 'fireCaseChangeForRealz'], |
77 | [$caseId], | |
c0a62573 MWMC |
78 | "Civi_CCase_Events::fire::{$caseId}" |
79 | ); | |
80 | } | |
f99a2e5d TO |
81 | } |
82 | } | |
83 | } | |
84 | ||
7fe37828 | 85 | /** |
18e0f096 CW |
86 | * Fire case change hook |
87 | * | |
88 | * @param int|array $caseIds | |
7fe37828 | 89 | */ |
18e0f096 CW |
90 | public static function fireCaseChangeForRealz($caseIds) { |
91 | foreach ((array) $caseIds as $caseId) { | |
92 | if (!isset(self::$isActive[$caseId])) { | |
93 | $tx = new \CRM_Core_Transaction(); | |
94 | self::$isActive[$caseId] = 1; | |
95 | $analyzer = new \Civi\CCase\Analyzer($caseId); | |
96 | \CRM_Utils_Hook::caseChange($analyzer); | |
97 | unset(self::$isActive[$caseId]); | |
98 | unset($tx); | |
99 | } | |
708d8fa2 TO |
100 | } |
101 | } | |
102 | ||
103 | /** | |
fe482240 | 104 | * Find any extra listeners declared in XML and pass the event along to them. |
708d8fa2 | 105 | * |
3bdca100 | 106 | * @param \Civi\CCase\Event\CaseChangeEvent $event |
708d8fa2 TO |
107 | */ |
108 | public static function delegateToXmlListeners(\Civi\CCase\Event\CaseChangeEvent $event) { | |
109 | $p = new \CRM_Case_XMLProcessor_Process(); | |
110 | $listeners = $p->getListeners($event->analyzer->getCaseType()); | |
111 | foreach ($listeners as $listener) { | |
112 | /** @var $listener \Civi\CCase\CaseChangeListener */ | |
113 | $listener->onCaseChange($event); | |
114 | } | |
115 | } | |
96025800 | 116 | |
ef10e0b5 | 117 | } |