| 1 | <?php |
| 2 | // $Id$ |
| 3 | |
| 4 | /* |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | CiviCRM version 4.3 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | | This file is a part of CiviCRM. | |
| 11 | | | |
| 12 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 13 | | under the terms of the GNU Affero General Public License | |
| 14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 15 | | | |
| 16 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 19 | | See the GNU Affero General Public License for more details. | |
| 20 | | | |
| 21 | | You should have received a copy of the GNU Affero General Public | |
| 22 | | License and the CiviCRM Licensing Exception along | |
| 23 | | with this program; if not, contact CiviCRM LLC | |
| 24 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 25 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 27 | +--------------------------------------------------------------------+ |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * File for the CiviCRM APIv3 Case functions |
| 32 | * Developed by woolman.org |
| 33 | * |
| 34 | * @package CiviCRM_APIv3 |
| 35 | * @subpackage API_Case |
| 36 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 37 | * |
| 38 | */ |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Open a new case, add client and manager roles, and add standard timeline |
| 43 | * |
| 44 | * @param array( |
| 45 | //REQUIRED: |
| 46 | * 'case_type_id' => int OR |
| 47 | * 'case_type' => str (provide one or the other) |
| 48 | * 'contact_id' => int // case client |
| 49 | * 'subject' => str |
| 50 | * |
| 51 | * //OPTIONAL |
| 52 | * 'medium_id' => int // see civicrm option values for possibilities |
| 53 | * 'creator_id' => int // case manager, default to the logged in user |
| 54 | * 'status_id' => int // defaults to 1 "ongoing" |
| 55 | * 'location' => str |
| 56 | * 'start_date' => str datestamp // defaults to: date('YmdHis') |
| 57 | * 'duration' => int // in minutes |
| 58 | * 'details' => str // html format |
| 59 | * |
| 60 | * @return sucessfully opened case |
| 61 | * |
| 62 | * @access public |
| 63 | * {@getfields case_create} |
| 64 | */ |
| 65 | function civicrm_api3_case_create($params) { |
| 66 | |
| 67 | if (!empty($params['id'])) { |
| 68 | return civicrm_api3_case_update($params); |
| 69 | } |
| 70 | |
| 71 | civicrm_api3_verify_mandatory($params, NULL, array('contact_id', 'subject', array('case_type', 'case_type_id'))); |
| 72 | _civicrm_api3_case_format_params($params); |
| 73 | |
| 74 | // If format_params didn't find what it was looking for, return error |
| 75 | if (empty($params['case_type_id'])) { |
| 76 | return civicrm_api3_create_error('Invalid case_type. No such case type exists.'); |
| 77 | } |
| 78 | if (empty($params['case_type'])) { |
| 79 | return civicrm_api3_create_error('Invalid case_type_id. No such case type exists.'); |
| 80 | } |
| 81 | |
| 82 | // Fixme: can we safely pass raw params to the BAO? |
| 83 | $newParams = array( |
| 84 | 'case_type_id' => $params['case_type_id'], |
| 85 | 'creator_id' => $params['creator_id'], |
| 86 | 'status_id' => $params['status_id'], |
| 87 | 'start_date' => $params['start_date'], |
| 88 | 'end_date' => CRM_Utils_Array::value('end_date', $params), |
| 89 | 'subject' => $params['subject'], |
| 90 | ); |
| 91 | |
| 92 | $caseBAO = CRM_Case_BAO_Case::create($newParams); |
| 93 | |
| 94 | if (!$caseBAO) { |
| 95 | return civicrm_api3_create_error('Case not created. Please check input params.'); |
| 96 | } |
| 97 | |
| 98 | foreach ((array) $params['contact_id'] as $cid) { |
| 99 | $contactParams = array('case_id' => $caseBAO->id, 'contact_id' => $cid); |
| 100 | CRM_Case_BAO_Case::addCaseToContact($contactParams); |
| 101 | } |
| 102 | |
| 103 | // Initialize XML processor with $params |
| 104 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); |
| 105 | $xmlProcessorParams = array( |
| 106 | 'clientID' => $params['contact_id'], |
| 107 | 'creatorID' => $params['creator_id'], |
| 108 | 'standardTimeline' => 1, |
| 109 | 'activityTypeName' => 'Open Case', |
| 110 | 'caseID' => $caseBAO->id, |
| 111 | 'subject' => $params['subject'], |
| 112 | 'location' => CRM_Utils_Array::value('location', $params), |
| 113 | 'activity_date_time' => $params['start_date'], |
| 114 | 'duration' => CRM_Utils_Array::value('duration', $params), |
| 115 | 'medium_id' => CRM_Utils_Array::value('medium_id', $params), |
| 116 | 'details' => CRM_Utils_Array::value('details', $params), |
| 117 | 'custom' => array(), |
| 118 | ); |
| 119 | |
| 120 | // Do it! :-D |
| 121 | $xmlProcessor->run($params['case_type'], $xmlProcessorParams); |
| 122 | |
| 123 | // return case |
| 124 | $values = array(); |
| 125 | _civicrm_api3_object_to_array($caseBAO, $values[$caseBAO->id]); |
| 126 | |
| 127 | return civicrm_api3_create_success($values, $params, 'case', 'create', $caseBAO); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Adjust Metadata for Get Action |
| 132 | * |
| 133 | * @param array $params array or parameters determined by getfields |
| 134 | */ |
| 135 | function _civicrm_api3_case_get_spec(&$params) { |
| 136 | $params['contact_id']['api.aliases'] = array('client_id'); |
| 137 | $params['contact_id']['title'] = 'Case Client'; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Adjust Metadata for Create Action |
| 142 | * |
| 143 | * @param array $params array or parameters determined by getfields |
| 144 | */ |
| 145 | function _civicrm_api3_case_create_spec(&$params) { |
| 146 | $params['contact_id']['api.aliases'] = array('client_id'); |
| 147 | $params['contact_id']['title'] = 'Case Client'; |
| 148 | $params['contact_id']['api.required'] = 1; |
| 149 | $params['status_id']['api.default'] = 1; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Adjust Metadata for Update action |
| 154 | * |
| 155 | * @param array $params array or parameters determined by getfields |
| 156 | */ |
| 157 | function _civicrm_api3_case_update_spec(&$params) { |
| 158 | $params['id']['api.required'] = 1; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Adjust Metadata for Delete action |
| 163 | * |
| 164 | * @param array $params array or parameters determined by getfields |
| 165 | */ |
| 166 | function _civicrm_api3_case_delete_spec(&$params) { |
| 167 | $params['id']['api.required'] = 1; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get details of a particular case, or search for cases, depending on params |
| 172 | * |
| 173 | * Please provide one (and only one) of the four get/search parameters: |
| 174 | * |
| 175 | * @param array( |
| 176 | 'id' => if set, will get all available info about a case, including contacts and activities |
| 177 | * |
| 178 | * // if no case_id provided, this function will use one of the following search parameters: |
| 179 | * 'client_id' => finds all cases with a specific client |
| 180 | * 'activity_id' => returns the case containing a specific activity |
| 181 | * 'contact_id' => finds all cases associated with a contact (in any role, not just client) |
| 182 | * |
| 183 | * {@getfields case_get} |
| 184 | * |
| 185 | * @return (get mode, case_id provided): Array with case details, case roles, case activity ids, (search mode, case_id not provided): Array of cases found |
| 186 | * @access public |
| 187 | * @todo Erik Hommel 16 dec 2010 check if all DB fields are returned |
| 188 | */ |
| 189 | function civicrm_api3_case_get($params) { |
| 190 | $options = _civicrm_api3_get_options_from_params($params); |
| 191 | |
| 192 | // Get by id |
| 193 | $caseId = CRM_Utils_Array::value('id', $params); |
| 194 | if ($caseId) { |
| 195 | // Validate param |
| 196 | if (!is_numeric($caseId)) { |
| 197 | return civicrm_api3_create_error('Invalid parameter: case_id. Must provide a numeric value.'); |
| 198 | } |
| 199 | // For historic reasons we always return these when an id is provided |
| 200 | $options['return'] = array('contacts' => 1, 'activities' => 1); |
| 201 | $case = _civicrm_api3_case_read($caseId, $options); |
| 202 | |
| 203 | if ($case) { |
| 204 | return civicrm_api3_create_success(array($caseId => $case), $params, 'case', 'get'); |
| 205 | } |
| 206 | else { |
| 207 | return civicrm_api3_create_success(array(), $params, 'case', 'get'); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | //search by client |
| 212 | if (!empty($params['contact_id'])) { |
| 213 | $ids = array(); |
| 214 | foreach ((array) $params['contact_id'] as $cid) { |
| 215 | if (is_numeric($cid)) { |
| 216 | $ids = array_merge($ids, CRM_Case_BAO_Case::retrieveCaseIdsByContactId($cid, TRUE)); |
| 217 | } |
| 218 | } |
| 219 | $cases = array(); |
| 220 | foreach ($ids as $id) { |
| 221 | if ($case = _civicrm_api3_case_read($id, $options)) { |
| 222 | $cases[$id] = $case; |
| 223 | } |
| 224 | } |
| 225 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); |
| 226 | } |
| 227 | |
| 228 | //search by activity |
| 229 | if (!empty($params['activity_id'])) { |
| 230 | if (!is_numeric($params['activity_id'])) { |
| 231 | return civicrm_api3_create_error('Invalid parameter: activity_id. Must provide a numeric value.'); |
| 232 | } |
| 233 | $caseId = CRM_Case_BAO_Case::getCaseIdByActivityId($params['activity_id']); |
| 234 | if (!$caseId) { |
| 235 | return civicrm_api3_create_success(array(), $params, 'case', 'get'); |
| 236 | } |
| 237 | $case = array($caseId => _civicrm_api3_case_read($caseId, $options)); |
| 238 | return civicrm_api3_create_success($case, $params, 'case', 'get'); |
| 239 | } |
| 240 | |
| 241 | //search by contacts |
| 242 | if ($contact = CRM_Utils_Array::value('contact_id', $params)) { |
| 243 | if (!is_numeric($contact)) { |
| 244 | return civicrm_api3_create_error('Invalid parameter: contact_id. Must provide a numeric value.'); |
| 245 | } |
| 246 | |
| 247 | $sql = " |
| 248 | SELECT DISTINCT case_id |
| 249 | FROM civicrm_relationship |
| 250 | WHERE (contact_id_a = $contact |
| 251 | OR contact_id_b = $contact) |
| 252 | AND case_id IS NOT NULL"; |
| 253 | $dao = &CRM_Core_DAO::executeQuery($sql); |
| 254 | |
| 255 | $cases = array(); |
| 256 | while ($dao->fetch()) { |
| 257 | $cases[$dao->case_id] = _civicrm_api3_case_read($dao->case_id, $options); |
| 258 | } |
| 259 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); |
| 260 | } |
| 261 | |
| 262 | return civicrm_api3_create_error('Missing required parameter. Must provide case_id, contact_id, activity_id, or contact_id.'); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Deprecated. Use activity API instead |
| 267 | */ |
| 268 | function civicrm_api3_case_activity_create($params) { |
| 269 | return civicrm_api3_activity_create($params); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Update a specified case. |
| 274 | * |
| 275 | * @param array( |
| 276 | //REQUIRED: |
| 277 | * 'case_id' => int |
| 278 | * |
| 279 | * //OPTIONAL |
| 280 | * 'status_id' => int |
| 281 | * 'start_date' => str datestamp |
| 282 | * 'contact_id' => int // case client |
| 283 | * |
| 284 | * @return Updated case |
| 285 | * |
| 286 | * @access public |
| 287 | * |
| 288 | */ |
| 289 | function civicrm_api3_case_update($params) { |
| 290 | //check parameters |
| 291 | civicrm_api3_verify_mandatory($params, NULL, array('id')); |
| 292 | |
| 293 | // return error if modifing creator id |
| 294 | if (array_key_exists('creator_id', $params)) { |
| 295 | return civicrm_api3_create_error(ts('You cannot update creator id')); |
| 296 | } |
| 297 | |
| 298 | $mCaseId = array(); |
| 299 | $origContactIds = array(); |
| 300 | |
| 301 | // get original contact id and creator id of case |
| 302 | if (!empty($params['contact_id'])) { |
| 303 | $origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['case_id']); |
| 304 | $origContactId = $origContactIds[1]; |
| 305 | } |
| 306 | |
| 307 | if (count($origContactIds) > 1) { |
| 308 | // check valid orig contact id |
| 309 | if (!empty($params['orig_contact_id']) && !in_array($params['orig_contact_id'], $origContactIds)) { |
| 310 | return civicrm_api3_create_error('Invalid case contact id (orig_contact_id)'); |
| 311 | } |
| 312 | elseif (empty($params['orig_contact_id'])) { |
| 313 | return civicrm_api3_create_error('Case is linked with more than one contact id. Provide the required params orig_contact_id to be replaced'); |
| 314 | } |
| 315 | $origContactId = $params['orig_contact_id']; |
| 316 | } |
| 317 | |
| 318 | // check for same contact id for edit Client |
| 319 | if (!empty($params['contact_id']) && !in_array($params['contact_id'], $origContactIds)) { |
| 320 | $mCaseId = CRM_Case_BAO_Case::mergeCases($params['contact_id'], $params['case_id'], $origContactId, NULL, TRUE); |
| 321 | } |
| 322 | |
| 323 | if (!empty($mCaseId[0])) { |
| 324 | $params['case_id'] = $mCaseId[0]; |
| 325 | } |
| 326 | |
| 327 | $dao = new CRM_Case_BAO_Case(); |
| 328 | $dao->id = $params['id']; |
| 329 | |
| 330 | $dao->copyValues($params); |
| 331 | $dao->save(); |
| 332 | |
| 333 | $case = array(); |
| 334 | |
| 335 | _civicrm_api3_object_to_array($dao, $case); |
| 336 | |
| 337 | return civicrm_api3_create_success($case, $params, 'case', 'update', $dao); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Delete a specified case. |
| 342 | * |
| 343 | * @param array( |
| 344 | //REQUIRED: |
| 345 | * 'id' => int |
| 346 | * |
| 347 | * //OPTIONAL |
| 348 | * 'move_to_trash' => bool (defaults to false) |
| 349 | * |
| 350 | * @return boolean: true if success, else false |
| 351 | * {@getfields case_delete} |
| 352 | * @access public |
| 353 | */ |
| 354 | function civicrm_api3_case_delete($params) { |
| 355 | //check parameters |
| 356 | civicrm_api3_verify_mandatory($params, NULL, array('id')); |
| 357 | |
| 358 | if (CRM_Case_BAO_Case::deleteCase($params['id'], CRM_Utils_Array::value('move_to_trash', $params, FALSE))) { |
| 359 | return civicrm_api3_create_success($params, $params, 'case', 'delete'); |
| 360 | } |
| 361 | else { |
| 362 | return civicrm_api3_create_error('Could not delete case.'); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /***********************************/ |
| 367 | /* */ |
| 368 | /* INTERNAL FUNCTIONS */ |
| 369 | /* */ |
| 370 | /***********************************/ |
| 371 | |
| 372 | /** |
| 373 | * Internal function to retrieve a case. |
| 374 | * |
| 375 | * @param int $caseId |
| 376 | * |
| 377 | * @return array (reference) case object |
| 378 | * |
| 379 | */ |
| 380 | function _civicrm_api3_case_read($caseId, $options) { |
| 381 | $return = CRM_Utils_Array::value('return', $options, array()); |
| 382 | $dao = new CRM_Case_BAO_Case(); |
| 383 | $dao->id = $caseId; |
| 384 | if ($dao->find(TRUE)) { |
| 385 | $case = array(); |
| 386 | _civicrm_api3_object_to_array($dao, $case); |
| 387 | // Legacy support for client_id - TODO: in apiv4 remove 'client_id' |
| 388 | $case['client_id'] = $case['contact_id'] = $dao->retrieveContactIdsByCaseId($caseId); |
| 389 | |
| 390 | //handle multi-value case type |
| 391 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; |
| 392 | $case['case_type_id'] = trim(str_replace($sep, ',', $case['case_type_id']), ','); |
| 393 | |
| 394 | if (!empty($return['contacts'])) { |
| 395 | //get case contacts |
| 396 | $contacts = CRM_Case_BAO_Case::getcontactNames($caseId); |
| 397 | $relations = CRM_Case_BAO_Case::getRelatedContacts($caseId); |
| 398 | $case['contacts'] = array_merge($contacts, $relations); |
| 399 | } |
| 400 | if (!empty($return['activities'])) { |
| 401 | //get case activities |
| 402 | $case['activities'] = array(); |
| 403 | $query = "SELECT activity_id FROM civicrm_case_activity WHERE case_id = $caseId"; |
| 404 | $dao = CRM_Core_DAO::executeQuery($query); |
| 405 | while ($dao->fetch()) { |
| 406 | $case['activities'][] = $dao->activity_id; |
| 407 | } |
| 408 | } |
| 409 | return $case; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Internal function to format create params for processing |
| 415 | */ |
| 416 | function _civicrm_api3_case_format_params(&$params) { |
| 417 | if (!array_key_exists('creator_id', $params)) { |
| 418 | $session = CRM_Core_Session::singleton(); |
| 419 | $params['creator_id'] = $session->get('userID'); |
| 420 | } |
| 421 | if (empty($params['start_date'])) { |
| 422 | $params['start_date'] = date('YmdHis'); |
| 423 | } |
| 424 | // figure out case type id from case type and vice-versa |
| 425 | $caseTypes = CRM_Case_PseudoConstant::caseType('label', FALSE); |
| 426 | if (empty($params['case_type_id'])) { |
| 427 | $params['case_type_id'] = array_search($params['case_type'], $caseTypes); |
| 428 | } |
| 429 | elseif (empty($params['case_type'])) { |
| 430 | $params['case_type'] = $caseTypes[$params['case_type_id']]; |
| 431 | } |
| 432 | // format input with value separators |
| 433 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; |
| 434 | $params['case_type_id'] = $sep . implode($sep, (array) $params['case_type_id']) . $sep; |
| 435 | } |