3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
36 * @var array per APIv3
46 * @var array per APIv3
51 * @var \SimpleXMLElement
56 * @var array<string,array>
60 public function __construct($caseId) {
61 $this->caseId
= $caseId;
66 * Determine if case includes an activity of given type/status
68 * @param string $type eg "Phone Call", "Interview Prospect", "Background Check"
69 * @param string $status eg "Scheduled", "Completed"
72 public function hasActivity($type, $status = NULL) {
73 $idx = $this->getActivityIndex(array('activity_type_id', 'status_id'));
74 $activityTypeGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_type'));
75 $activityType = array(
77 'option_group_id' => $activityTypeGroup['id'],
79 $activityTypeID = civicrm_api3('option_value', 'get', $activityType);
80 $activityTypeID = $activityTypeID['values'][$activityTypeID['id']]['value'];
82 $activityStatusGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_status'));
83 $activityStatus = array(
85 'option_group_id' => $activityStatusGroup['id']
87 $activityStatusID = civicrm_api3('option_value', 'get', $activityStatus);
88 $activityStatusID = $activityStatusID['values'][$activityStatusID['id']]['value'];
90 if ($status === NULL) {
91 return !empty($idx[$activityTypeID]);
94 return !empty($idx[$activityTypeID][$activityStatusID]);
99 * Get a list of all activities in the case
101 * @return array list of activity records (api/v3 format)
103 public function getActivities() {
104 if ($this->activities
=== NULL) {
105 $result = civicrm_api3('Activity', 'get', array('case_id' => $this->caseId
));
106 $this->activities
= $result['values'];
108 return $this->activities
;
112 * Get a single activity record by type
114 * @param string $type
115 * @throws \Civi\CCase\Exception\MultipleActivityException
116 * @return array|NULL, activity record (api/v3)
118 public function getSingleActivity($type) {
119 $idx = $this->getActivityIndex(array('activity_type_id', 'id'));
120 $actTypes = array_flip(\CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'name'));
121 $typeId = $actTypes[$type];
122 $count = isset($idx[$typeId]) ?
count($idx[$typeId]) : 0;
127 elseif ($count === 1) {
128 foreach ($idx[$typeId] as $item) {
133 throw new \Civi\CCase\Exception\
MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count);
140 public function getCaseId() {
141 return $this->caseId
;
145 * @return array, Case record (api/v3 format)
147 public function getCase() {
148 if ($this->case === NULL) {
149 $this->case = civicrm_api3('case', 'getsingle', array('id' => $this->caseId
));
157 public function getCaseType() {
158 if ($this->caseType
=== NULL) {
159 $case = $this->getCase();
160 $caseTypes = \CRM_Case_XMLRepository
::singleton()->getAllCaseTypes();
161 if (!isset($caseTypes[$case['case_type_id']])) {
162 throw new \
CRM_Core_Exception("Case does not have a recognized case-type!");
164 $this->caseType
= $caseTypes[$case['case_type_id']];
166 return $this->caseType
;
170 * Get a list of all activities in the case (indexed by some property/properties)
172 * @param array $keys list of properties by which to index activities
173 * @return array list of activity records (api/v3 format), indexed by $keys
175 public function getActivityIndex($keys) {
176 $key = implode(";", $keys);
177 if (!isset($this->indices
[$key])) {
178 $this->indices
[$key] = \CRM_Utils_Array
::index($keys, $this->getActivities());
180 return $this->indices
[$key];
184 * @return SimpleXMLElement|NULL
186 public function getXml() {
187 if ($this->xml
=== NULL) {
188 $this->xml
= \CRM_Case_XMLRepository
::singleton()->retrieve($this->getCaseType());
194 * Flush any cached information
198 public function flush() {
200 $this->caseType
= NULL;
201 $this->activities
= NULL;
202 $this->indices
= array();