Commit | Line | Data |
---|---|---|
708d8fa2 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
708d8fa2 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | */ | |
40 | static $isActive = array(); | |
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) { | |
49 | $caseId = NULL; | |
50 | switch ($event->entity) { | |
51 | case 'Activity': | |
12784f11 | 52 | if (!empty($event->object->case_id)) { |
708d8fa2 TO |
53 | $caseId = $event->object->case_id; |
54 | } | |
55 | break; | |
ea100cb5 | 56 | |
708d8fa2 | 57 | case 'Case': |
1be6caec TO |
58 | // by the time we get the post-delete event, the record is gone, so |
59 | // there's nothing to analyze | |
60 | if ($event->action != 'delete') { | |
61 | $caseId = $event->id; | |
62 | } | |
708d8fa2 | 63 | break; |
ea100cb5 | 64 | |
708d8fa2 TO |
65 | default: |
66 | throw new \CRM_Core_Exception("CRM_Case_Listener does not support entity {$event->entity}"); | |
67 | } | |
68 | ||
f99a2e5d TO |
69 | if ($caseId) { |
70 | if (!isset(self::$isActive[$caseId])) { | |
6ff10bf1 | 71 | $tx = new \CRM_Core_Transaction(); |
f99a2e5d TO |
72 | \CRM_Core_Transaction::addCallback( |
73 | \CRM_Core_Transaction::PHASE_POST_COMMIT, | |
74 | array(__CLASS__, 'fireCaseChangeForRealz'), | |
75 | array($caseId), | |
76 | "Civi_CCase_Events::fire::{$caseId}" | |
77 | ); | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
7fe37828 | 82 | /** |
18e0f096 CW |
83 | * Fire case change hook |
84 | * | |
85 | * @param int|array $caseIds | |
7fe37828 | 86 | */ |
18e0f096 CW |
87 | public static function fireCaseChangeForRealz($caseIds) { |
88 | foreach ((array) $caseIds as $caseId) { | |
89 | if (!isset(self::$isActive[$caseId])) { | |
90 | $tx = new \CRM_Core_Transaction(); | |
91 | self::$isActive[$caseId] = 1; | |
92 | $analyzer = new \Civi\CCase\Analyzer($caseId); | |
93 | \CRM_Utils_Hook::caseChange($analyzer); | |
94 | unset(self::$isActive[$caseId]); | |
95 | unset($tx); | |
96 | } | |
708d8fa2 TO |
97 | } |
98 | } | |
99 | ||
100 | /** | |
fe482240 | 101 | * Find any extra listeners declared in XML and pass the event along to them. |
708d8fa2 | 102 | * |
3bdca100 | 103 | * @param \Civi\CCase\Event\CaseChangeEvent $event |
708d8fa2 TO |
104 | */ |
105 | public static function delegateToXmlListeners(\Civi\CCase\Event\CaseChangeEvent $event) { | |
106 | $p = new \CRM_Case_XMLProcessor_Process(); | |
107 | $listeners = $p->getListeners($event->analyzer->getCaseType()); | |
108 | foreach ($listeners as $listener) { | |
109 | /** @var $listener \Civi\CCase\CaseChangeListener */ | |
110 | $listener->onCaseChange($event); | |
111 | } | |
112 | } | |
96025800 | 113 | |
ef10e0b5 | 114 | } |