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