| 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 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 eg "Phone Call", "Interview Prospect", "Background Check" |
| 69 | * @param string $status eg "Scheduled", "Completed" |
| 70 | * @return bool |
| 71 | */ |
| 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( |
| 76 | 'name' => $type, |
| 77 | 'option_group_id' => $activityTypeGroup['id'], |
| 78 | ); |
| 79 | $activityTypeID = civicrm_api3('option_value', 'get', $activityType); |
| 80 | $activityTypeID = $activityTypeID['values'][$activityTypeID['id']]['value']; |
| 81 | if ($status) { |
| 82 | $activityStatusGroup = civicrm_api3('option_group', 'get', array('name' => 'activity_status')); |
| 83 | $activityStatus = array( |
| 84 | 'name' => $status, |
| 85 | 'option_group_id' => $activityStatusGroup['id'] |
| 86 | ); |
| 87 | $activityStatusID = civicrm_api3('option_value', 'get', $activityStatus); |
| 88 | $activityStatusID = $activityStatusID['values'][$activityStatusID['id']]['value']; |
| 89 | } |
| 90 | if ($status === NULL) { |
| 91 | return !empty($idx[$activityTypeID]); |
| 92 | } |
| 93 | else { |
| 94 | return !empty($idx[$activityTypeID][$activityStatusID]); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get a list of all activities in the case |
| 100 | * |
| 101 | * @return array list of activity records (api/v3 format) |
| 102 | */ |
| 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']; |
| 107 | } |
| 108 | return $this->activities; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get a single activity record by type |
| 113 | * |
| 114 | * @param string $type |
| 115 | * @throws \Civi\CCase\Exception\MultipleActivityException |
| 116 | * @return array|NULL, activity record (api/v3) |
| 117 | */ |
| 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; |
| 123 | |
| 124 | if ($count === 0) { |
| 125 | return NULL; |
| 126 | } |
| 127 | elseif ($count === 1) { |
| 128 | foreach ($idx[$typeId] as $item) { |
| 129 | return $item; |
| 130 | } |
| 131 | } |
| 132 | else { |
| 133 | throw new \Civi\CCase\Exception\MultipleActivityException("Wrong quantity of [$type] records. Expected 1 but found " . $count); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @return int |
| 139 | */ |
| 140 | public function getCaseId() { |
| 141 | return $this->caseId; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @return array, Case record (api/v3 format) |
| 146 | */ |
| 147 | public function getCase() { |
| 148 | if ($this->case === NULL) { |
| 149 | $this->case = civicrm_api3('case', 'getsingle', array('id' => $this->caseId)); |
| 150 | } |
| 151 | return $this->case; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @return string |
| 156 | */ |
| 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!"); |
| 163 | } |
| 164 | $this->caseType = $caseTypes[$case['case_type_id']]; |
| 165 | } |
| 166 | return $this->caseType; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get a list of all activities in the case (indexed by some property/properties) |
| 171 | * |
| 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 |
| 174 | */ |
| 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()); |
| 179 | } |
| 180 | return $this->indices[$key]; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return SimpleXMLElement|NULL |
| 185 | */ |
| 186 | public function getXml() { |
| 187 | if ($this->xml === NULL) { |
| 188 | $this->xml = \CRM_Case_XMLRepository::singleton()->retrieve($this->getCaseType()); |
| 189 | } |
| 190 | return $this->xml; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Flush any cached information |
| 195 | * |
| 196 | * @return void |
| 197 | */ |
| 198 | public function flush() { |
| 199 | $this->case = NULL; |
| 200 | $this->caseType = NULL; |
| 201 | $this->activities = NULL; |
| 202 | $this->indices = array(); |
| 203 | } |
| 204 | } |