commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / Civi / CCase / Analyzer.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 /**
30 * Class Analyzer
31 *
32 * @package Civi\CCase
33 */
34 class Analyzer {
35 /**
36 * @var int
37 */
38 private $caseId;
39
40 /**
41 * @var array per APIv3
42 */
43 private $case;
44
45 /**
46 * @var string
47 */
48 private $caseType;
49
50 /**
51 * @var array per APIv3
52 */
53 private $activities;
54
55 /**
56 * @var \SimpleXMLElement
57 */
58 private $xml;
59
60 /**
61 * @var array<string,array>
62 */
63 private $indices;
64
65 /**
66 * @param $caseId
67 */
68 public function __construct($caseId) {
69 $this->caseId = $caseId;
70 $this->flush();
71 }
72
73 /**
74 * Determine if case includes an activity of given type/status
75 *
76 * @param string $type
77 * Eg "Phone Call", "Interview Prospect", "Background Check".
78 * @param string $status
79 * Eg "Scheduled", "Completed".
80 * @return bool
81 */
82 public function hasActivity($type, $status = NULL) {
83 $idx = $this->getActivityIndex(array('activity_type_id', 'status_id'));
84 $activityTypeGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_type'));
85 $activityType = array(
86 'name' => $type,
87 'option_group_id' => $activityTypeGroup['id'],
88 );
89 $activityTypeID = civicrm_api3('option_value', 'get', $activityType);
90 $activityTypeID = $activityTypeID['values'][$activityTypeID['id']]['value'];
91 if ($status) {
92 $activityStatusGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_status'));
93 $activityStatus = array(
94 'name' => $status,
95 'option_group_id' => $activityStatusGroup['id'],
96 );
97 $activityStatusID = civicrm_api3('option_value', 'get', $activityStatus);
98 $activityStatusID = $activityStatusID['values'][$activityStatusID['id']]['value'];
99 }
100 if ($status === NULL) {
101 return !empty($idx[$activityTypeID]);
102 }
103 else {
104 return !empty($idx[$activityTypeID][$activityStatusID]);
105 }
106 }
107
108 /**
109 * Get a list of all activities in the case.
110 *
111 * @return array
112 * list of activity records (api/v3 format)
113 */
114 public function getActivities() {
115 if ($this->activities === NULL) {
116 // TODO find batch-oriented API for getting all activities in a case
117 $case = $this->getCase();
118 $activities = array();
119 if (isset($case['activities'])) {
120 foreach ($case['activities'] as $actId) {
121 $result = civicrm_api3('Activity', 'get', array(
122 'id' => $actId,
123 'is_current_revision' => 1,
124 ));
125 $activities = array_merge($activities, $result['values']);
126 }
127 }
128 $this->activities = $activities;
129 }
130 return $this->activities;
131 }
132
133 /**
134 * Get a single activity record by type.
135 *
136 * @param string $type
137 * @throws \Civi\CCase\Exception\MultipleActivityException
138 * @return array|NULL, activity record (api/v3)
139 */
140 public function getSingleActivity($type) {
141 $idx = $this->getActivityIndex(array('activity_type_id', 'id'));
142 $actTypes = array_flip(\CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name'));
143 $typeId = $actTypes[$type];
144 $count = isset($idx[$typeId]) ? count($idx[$typeId]) : 0;
145
146 if ($count === 0) {
147 return NULL;
148 }
149 elseif ($count === 1) {
150 foreach ($idx[$typeId] as $item) {
151 return $item;
152 }
153 }
154 else {
155 throw new \Civi\CCase\Exception\MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count);
156 }
157 }
158
159 /**
160 * @return int
161 */
162 public function getCaseId() {
163 return $this->caseId;
164 }
165
166 /**
167 * @return array, Case record (api/v3 format)
168 */
169 public function getCase() {
170 if ($this->case === NULL) {
171 $this->case = civicrm_api3('case', 'getsingle', array('id' => $this->caseId));
172 }
173 return $this->case;
174 }
175
176 /**
177 * @return string
178 */
179 public function getCaseType() {
180 if ($this->caseType === NULL) {
181 $case = $this->getCase();
182 $caseTypes = \CRM_Case_XMLRepository::singleton()->getAllCaseTypes();
183 if (!isset($caseTypes[$case['case_type_id']])) {
184 throw new \CRM_Core_Exception("Case does not have a recognized case-type!");
185 }
186 $this->caseType = $caseTypes[$case['case_type_id']];
187 }
188 return $this->caseType;
189 }
190
191 /**
192 * Get a list of all activities in the case (indexed by some property/properties)
193 *
194 * @param array $keys
195 * List of properties by which to index activities.
196 * @return array
197 * list of activity records (api/v3 format), indexed by $keys
198 */
199 public function getActivityIndex($keys) {
200 $key = implode(";", $keys);
201 if (!isset($this->indices[$key])) {
202 $this->indices[$key] = \CRM_Utils_Array::index($keys, $this->getActivities());
203 }
204 return $this->indices[$key];
205 }
206
207 /**
208 * @return SimpleXMLElement|NULL
209 */
210 public function getXml() {
211 if ($this->xml === NULL) {
212 $this->xml = \CRM_Case_XMLRepository::singleton()->retrieve($this->getCaseType());
213 }
214 return $this->xml;
215 }
216
217 /**
218 * Flush any cached information.
219 *
220 * @return void
221 */
222 public function flush() {
223 $this->case = NULL;
224 $this->caseType = NULL;
225 $this->activities = NULL;
226 $this->indices = array();
227 }
228
229 }