Merge pull request #3367 from kirkatcaat/empty-labels-on-profile-fields
[civicrm-core.git] / Civi / CCase / Events.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 */
27 namespace Civi\CCase;
28
29 class Events {
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
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':
47 if (!empty($event->object->case_id)) {
48 $caseId = $event->object->case_id;
49 }
50 break;
51 case 'Case':
52 $caseId = $event->id;
53 break;
54 default:
55 throw new \CRM_Core_Exception("CRM_Case_Listener does not support entity {$event->entity}");
56 }
57
58 if ($caseId) {
59 if (!isset(self::$isActive[$caseId])) {
60 \CRM_Core_Transaction::addCallback(
61 \CRM_Core_Transaction::PHASE_POST_COMMIT,
62 array(__CLASS__, 'fireCaseChangeForRealz'),
63 array($caseId),
64 "Civi_CCase_Events::fire::{$caseId}"
65 );
66 }
67 }
68 }
69
70 public static function fireCaseChangeForRealz($caseId) {
71 if (!isset(self::$isActive[$caseId])) {
72 $tx = new \CRM_Core_Transaction();
73 self::$isActive[$caseId] = 1;
74 $analyzer = new \Civi\CCase\Analyzer($caseId);
75 \CRM_Utils_Hook::caseChange($analyzer);
76 unset(self::$isActive[$caseId]);
77 unset($tx);
78 }
79 }
80
81 /**
82 * Find any extra listeners declared in XML and pass the event along to them
83 *
84 * @param Event\CaseChangeEvent $event
85 */
86 public static function delegateToXmlListeners(\Civi\CCase\Event\CaseChangeEvent $event) {
87 $p = new \CRM_Case_XMLProcessor_Process();
88 $listeners = $p->getListeners($event->analyzer->getCaseType());
89 foreach ($listeners as $listener) {
90 /** @var $listener \Civi\CCase\CaseChangeListener */
91 $listener->onCaseChange($event);
92 }
93 }
94 }