INFRA-132 - Batch #8
[civicrm-core.git] / Civi / CCase / Events.php
CommitLineData
708d8fa2
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
708d8fa2
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27namespace Civi\CCase;
28
29class Events {
874dae9d
TO
30 /**
31 * @var array (int $caseId => bool $active) list of cases for which we are actively firing case-change event
32 *
33 * We do not want to fire case-change events recursively.
34 */
35 static $isActive = array();
36
708d8fa2
TO
37 /**
38 * Following a change to an activity or case, fire the case-change event.
39 *
40 * @param \Civi\Core\Event\PostEvent $event
41 * @throws \CRM_Core_Exception
42 */
43 public static function fireCaseChange(\Civi\Core\Event\PostEvent $event) {
44 $caseId = NULL;
45 switch ($event->entity) {
46 case 'Activity':
12784f11 47 if (!empty($event->object->case_id)) {
708d8fa2
TO
48 $caseId = $event->object->case_id;
49 }
50 break;
ea100cb5 51
708d8fa2 52 case 'Case':
1be6caec
TO
53 // by the time we get the post-delete event, the record is gone, so
54 // there's nothing to analyze
55 if ($event->action != 'delete') {
56 $caseId = $event->id;
57 }
708d8fa2 58 break;
ea100cb5 59
708d8fa2
TO
60 default:
61 throw new \CRM_Core_Exception("CRM_Case_Listener does not support entity {$event->entity}");
62 }
63
f99a2e5d
TO
64 if ($caseId) {
65 if (!isset(self::$isActive[$caseId])) {
66 \CRM_Core_Transaction::addCallback(
67 \CRM_Core_Transaction::PHASE_POST_COMMIT,
68 array(__CLASS__, 'fireCaseChangeForRealz'),
69 array($caseId),
70 "Civi_CCase_Events::fire::{$caseId}"
71 );
72 }
73 }
74 }
75
76 public static function fireCaseChangeForRealz($caseId) {
77 if (!isset(self::$isActive[$caseId])) {
78 $tx = new \CRM_Core_Transaction();
874dae9d 79 self::$isActive[$caseId] = 1;
708d8fa2
TO
80 $analyzer = new \Civi\CCase\Analyzer($caseId);
81 \CRM_Utils_Hook::caseChange($analyzer);
874dae9d 82 unset(self::$isActive[$caseId]);
f99a2e5d 83 unset($tx);
708d8fa2
TO
84 }
85 }
86
87 /**
88 * Find any extra listeners declared in XML and pass the event along to them
89 *
3bdca100 90 * @param \Civi\CCase\Event\CaseChangeEvent $event
708d8fa2
TO
91 */
92 public static function delegateToXmlListeners(\Civi\CCase\Event\CaseChangeEvent $event) {
93 $p = new \CRM_Case_XMLProcessor_Process();
94 $listeners = $p->getListeners($event->analyzer->getCaseType());
95 foreach ($listeners as $listener) {
96 /** @var $listener \Civi\CCase\CaseChangeListener */
97 $listener->onCaseChange($event);
98 }
99 }
ef10e0b5 100}