3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
29 * This api exposes CiviCRM Case objects.
30 * Developed by woolman.org
32 * @package CiviCRM_APIv3
37 * Open a new case, add client and manager roles, and standard timeline.
39 * @param array $params
43 * 'case_type_id' => int OR
44 * 'case_type' => str (provide one or the other)
45 * 'contact_id' => int // case client
49 * 'medium_id' => int // see civicrm option values for possibilities
50 * 'creator_id' => int // case manager, default to the logged in user
51 * 'status_id' => int // defaults to 1 "ongoing"
53 * 'start_date' => str datestamp // defaults to: date('YmdHis')
54 * 'duration' => int // in minutes
55 * 'details' => str // html format
58 * @throws API_Exception
62 function civicrm_api3_case_create($params) {
64 if (!empty($params['id'])) {
65 return civicrm_api3_case_update($params);
68 civicrm_api3_verify_mandatory($params, NULL, array(
71 array('case_type', 'case_type_id'))
73 _civicrm_api3_case_format_params($params);
75 // If format_params didn't find what it was looking for, return error
76 if (empty($params['case_type_id'])) {
77 throw new API_Exception('Invalid case_type. No such case type exists.');
79 if (empty($params['case_type'])) {
80 throw new API_Exception('Invalid case_type_id. No such case type exists.');
83 // Fixme: can we safely pass raw params to the BAO?
85 'case_type_id' => $params['case_type_id'],
86 'creator_id' => $params['creator_id'],
87 'status_id' => $params['status_id'],
88 'start_date' => $params['start_date'],
89 'end_date' => CRM_Utils_Array
::value('end_date', $params),
90 'subject' => $params['subject'],
93 $caseBAO = CRM_Case_BAO_Case
::create($newParams);
96 throw new API_Exception('Case not created. Please check input params.');
99 foreach ((array) $params['contact_id'] as $cid) {
100 $contactParams = array('case_id' => $caseBAO->id
, 'contact_id' => $cid);
101 CRM_Case_BAO_CaseContact
::create($contactParams);
104 // Initialize XML processor with $params
105 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
106 $xmlProcessorParams = array(
107 'clientID' => $params['contact_id'],
108 'creatorID' => $params['creator_id'],
109 'standardTimeline' => 1,
110 'activityTypeName' => 'Open Case',
111 'caseID' => $caseBAO->id
,
112 'subject' => $params['subject'],
113 'location' => CRM_Utils_Array
::value('location', $params),
114 'activity_date_time' => $params['start_date'],
115 'duration' => CRM_Utils_Array
::value('duration', $params),
116 'medium_id' => CRM_Utils_Array
::value('medium_id', $params),
117 'details' => CRM_Utils_Array
::value('details', $params),
122 $xmlProcessor->run($params['case_type'], $xmlProcessorParams);
126 _civicrm_api3_object_to_array($caseBAO, $values[$caseBAO->id
]);
128 return civicrm_api3_create_success($values, $params, 'Case', 'create', $caseBAO);
132 * Adjust Metadata for Get Action.
134 * @param array $params
135 * Parameters determined by getfields.
137 function _civicrm_api3_case_get_spec(&$params) {
138 $params['contact_id'] = array(
139 'api.aliases' => array('client_id'),
140 'title' => 'Case Client',
141 'description' => 'Contact id of one or more clients to retrieve cases for',
142 'type' => CRM_Utils_Type
::T_INT
,
144 $params['activity_id'] = array(
145 'title' => 'Case Activity',
146 'description' => 'Id of an activity in the case',
147 'type' => CRM_Utils_Type
::T_INT
,
149 $params['tag_id'] = array(
151 'description' => 'Find activities with specified tags.',
153 'FKClassName' => 'CRM_Core_DAO_Tag',
154 'FKApiName' => 'Tag',
155 'supports_joins' => TRUE,
160 * Adjust Metadata for Create Action.
162 * @param array $params
163 * Array of parameters determined by getfields.
165 function _civicrm_api3_case_create_spec(&$params) {
166 $params['contact_id'] = array(
167 'api.aliases' => array('client_id'),
168 'title' => 'Case Client',
169 'description' => 'Contact id of case client(s)',
171 'type' => CRM_Utils_Type
::T_INT
,
173 $params['status_id']['api.default'] = 1;
174 $params['status_id']['api.aliases'] = array('case_status');
175 $params['creator_id']['api.default'] = 'user_contact_id';
176 $params['creator_id']['type'] = CRM_Utils_Type
::T_INT
;
177 $params['creator_id']['title'] = 'Case Created By';
178 $params['start_date']['api.default'] = 'now';
179 $params['medium_id'] = array(
180 'name' => 'medium_id',
181 'title' => 'Activity Medium',
182 'type' => CRM_Utils_Type
::T_INT
,
187 * Adjust Metadata for Update action.
189 * @param array $params
190 * Array of parameters determined by getfields.
192 function _civicrm_api3_case_update_spec(&$params) {
193 $params['id']['api.required'] = 1;
197 * Adjust Metadata for Delete action.
199 * @param array $params
200 * Array of parameters determined by getfields.
202 function _civicrm_api3_case_delete_spec(&$params) {
203 $params['id']['api.required'] = 1;
207 * Get details of a particular case, or search for cases, depending on params.
209 * Please provide one (and only one) of the four get/search parameters:
211 * @param array $params
212 * 'id' => if set, will get all available info about a case, including contacts and activities
214 * // if no case_id provided, this function will use one of the following search parameters:
215 * 'client_id' => finds all cases with a specific client
216 * 'activity_id' => returns the case containing a specific activity
217 * 'contact_id' => finds all cases associated with a contact (in any role, not just client)
219 * @throws API_Exception
221 * (get mode, case_id provided): Array with case details, case roles, case activity ids, (search mode, case_id not provided): Array of cases found
223 function civicrm_api3_case_get($params) {
224 $options = _civicrm_api3_get_options_from_params($params);
225 $sql = CRM_Utils_SQL_Select
::fragment();
227 // Add clause to search by client
228 if (!empty($params['contact_id'])) {
229 // Legacy support - this field historically supports a nonstandard format of array(1,2,3) as a synonym for array('IN' => array(1,2,3))
230 if (is_array($params['contact_id'])) {
231 $operator = CRM_Utils_Array
::first(array_keys($params['contact_id']));
232 if (!in_array($operator, \CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
233 $params['contact_id'] = array('IN' => $params['contact_id']);
237 $params['contact_id'] = array('=' => $params['contact_id']);
239 $clause = CRM_Core_DAO
::createSQLFilter('contact_id', $params['contact_id']);
240 $sql->where("a.id IN (SELECT case_id FROM civicrm_case_contact WHERE $clause)");
243 // Add clause to search by activity
244 if (!empty($params['activity_id'])) {
245 if (!CRM_Utils_Rule
::positiveInteger($params['activity_id'])) {
246 throw new API_Exception('Invalid parameter: activity_id. Must provide a numeric value.');
248 $activityId = $params['activity_id'];
249 $originalId = CRM_Core_DAO
::getFieldValue('CRM_Activity_BAO_Activity', $activityId, 'original_id');
251 $activityId .= ',' . $originalId;
254 ->join('civicrm_case_activity', 'INNER JOIN civicrm_case_activity ON civicrm_case_activity.case_id = a.id')
255 ->where("civicrm_case_activity.activity_id IN ($activityId)");
258 // Clause to search by tag
259 if (!empty($params['tag_id'])) {
260 $dummySpec = array();
261 _civicrm_api3_validate_integer($params, 'tag_id', $dummySpec, 'Case');
262 if (!is_array($params['tag_id'])) {
263 $params['tag_id'] = array('=' => $params['tag_id']);
265 $clause = \CRM_Core_DAO
::createSQLFilter('tag_id', $params['tag_id']);
267 $sql->where('a.id IN (SELECT entity_id FROM civicrm_entity_tag WHERE entity_table = "civicrm_case" AND !clause)', array('!clause' => $clause));
271 $cases = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), array('sequential' => 0) +
$params, TRUE, 'Case', $sql);
273 if (empty($options['is_count']) && !empty($cases['values'])) {
274 // For historic reasons we return these by default only when fetching a case by id
275 if (!empty($params['id']) && empty($options['return'])) {
276 $options['return'] = array(
283 _civicrm_api3_case_read($cases['values'], $options);
285 // We disabled sequential to keep the list indexed for case_read(). Now add it back.
286 if (!empty($params['sequential'])) {
287 $cases['values'] = array_values($cases['values']);
297 * Use activity API instead.
299 * @param array $params
301 * @throws API_Exception
304 function civicrm_api3_case_activity_create($params) {
305 require_once "api/v3/Activity.php";
306 return civicrm_api3_activity_create($params) +
array(
307 'deprecated' => CRM_Utils_Array
::value('activity_create', _civicrm_api3_case_deprecation()),
312 * Declare deprecated api functions.
314 * @deprecated api notice
316 * Array of deprecated actions
318 function _civicrm_api3_case_deprecation() {
319 return array('activity_create' => 'Case api "activity_create" action is deprecated. Use the activity api instead.');
323 * Update a specified case.
325 * @param array $params
331 * 'start_date' => str datestamp
332 * 'contact_id' => int // case client
334 * @throws API_Exception
338 function civicrm_api3_case_update($params) {
339 if (!isset($params['case_id']) && isset($params['id'])) {
340 $params['case_id'] = $params['id'];
344 civicrm_api3_verify_mandatory($params, NULL, array('id'));
346 // return error if modifying creator id
347 if (array_key_exists('creator_id', $params)) {
348 throw new API_Exception(ts('You cannot update creator id'));
351 $mCaseId = $origContactIds = array();
353 // get original contact id and creator id of case
354 if (!empty($params['contact_id'])) {
355 $origContactIds = CRM_Case_BAO_Case
::retrieveContactIdsByCaseId($params['id']);
356 $origContactId = $origContactIds[1];
359 if (count($origContactIds) > 1) {
360 // check valid orig contact id
361 if (!empty($params['orig_contact_id']) && !in_array($params['orig_contact_id'], $origContactIds)) {
362 throw new API_Exception('Invalid case contact id (orig_contact_id)');
364 elseif (empty($params['orig_contact_id'])) {
365 throw new API_Exception('Case is linked with more than one contact id. Provide the required params orig_contact_id to be replaced');
367 $origContactId = $params['orig_contact_id'];
370 // check for same contact id for edit Client
371 if (!empty($params['contact_id']) && !in_array($params['contact_id'], $origContactIds)) {
372 $mCaseId = CRM_Case_BAO_Case
::mergeCases($params['contact_id'], $params['case_id'], $origContactId, NULL, TRUE);
375 if (!empty($mCaseId[0])) {
376 $params['id'] = $mCaseId[0];
379 $dao = new CRM_Case_BAO_Case();
380 $dao->id
= $params['id'];
382 $dao->copyValues($params);
386 _civicrm_api3_object_to_array($dao, $case);
388 return civicrm_api3_create_success(array($dao->id
=> $case), $params, 'Case', 'update', $dao);
392 * Delete a specified case.
394 * @param array $params
401 * 'move_to_trash' => bool (defaults to false)
404 * @throws API_Exception
406 * true if success, else false
408 function civicrm_api3_case_delete($params) {
410 civicrm_api3_verify_mandatory($params, NULL, array('id'));
412 if (CRM_Case_BAO_Case
::deleteCase($params['id'], CRM_Utils_Array
::value('move_to_trash', $params, FALSE))) {
413 return civicrm_api3_create_success($params, $params, 'Case', 'delete');
416 throw new API_Exception('Could not delete case.');
421 * Augment case results with extra data.
423 * @param array $cases
424 * @param array $options
426 function _civicrm_api3_case_read(&$cases, $options) {
427 foreach ($cases as &$case) {
428 if (empty($options['return']) ||
!empty($options['return']['contact_id'])) {
429 // Legacy support for client_id - TODO: in apiv4 remove 'client_id'
430 $case['client_id'] = $case['contact_id'] = CRM_Case_BAO_Case
::retrieveContactIdsByCaseId($case['id']);
432 if (!empty($options['return']['contacts'])) {
434 $contacts = CRM_Case_BAO_Case
::getcontactNames($case['id']);
435 $relations = CRM_Case_BAO_Case
::getRelatedContacts($case['id']);
436 $case['contacts'] = array_unique(array_merge($contacts, $relations), SORT_REGULAR
);
438 if (!empty($options['return']['activities'])) {
439 // add case activities array - we'll populate them in bulk below
440 $case['activities'] = array();
442 // Properly render this joined field
443 if (!empty($options['return']['case_type_id.definition'])) {
444 if (!empty($case['case_type_id.definition'])) {
445 list($xml) = CRM_Utils_XML
::parseString($case['case_type_id.definition']);
448 $caseTypeId = !empty($case['case_type_id']) ?
$case['case_type_id'] : CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_Case', $case['id'], 'case_type_id');
449 $caseTypeName = !empty($case['case_type_id.name']) ?
$case['case_type_id.name'] : CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name');
450 $xml = CRM_Case_XMLRepository
::singleton()->retrieve($caseTypeName);
452 $case['case_type_id.definition'] = array();
454 $case['case_type_id.definition'] = CRM_Case_BAO_CaseType
::convertXmlToDefinition($xml);
458 // Bulk-load activities
459 if (!empty($options['return']['activities'])) {
460 $query = "SELECT case_id, activity_id FROM civicrm_case_activity WHERE case_id IN (%1)";
461 $params = array(1 => array(implode(',', array_keys($cases)), 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
));
462 $dao = CRM_Core_DAO
::executeQuery($query, $params);
463 while ($dao->fetch()) {
464 $cases[$dao->case_id
]['activities'][] = $dao->activity_id
;
467 // Bulk-load tags. Supports joins onto the tag entity.
468 $tagGet = array('tag_id', 'entity_id');
469 foreach (array_keys($options['return']) as $key) {
470 if (strpos($key, 'tag_id.') === 0) {
472 $options['return']['tag_id'] = 1;
475 if (!empty($options['return']['tag_id'])) {
476 $tags = civicrm_api3('EntityTag', 'get', array(
477 'entity_table' => 'civicrm_case',
478 'entity_id' => array('IN' => array_keys($cases)),
480 'options' => array('limit' => 0),
482 foreach ($tags['values'] as $tag) {
483 $key = (int) $tag['entity_id'];
484 unset($tag['entity_id'], $tag['id']);
485 $cases[$key]['tag_id'][$tag['tag_id']] = $tag;
491 * Internal function to format create params for processing.
493 * @param array $params
495 function _civicrm_api3_case_format_params(&$params) {
496 // figure out case type id from case type and vice-versa
497 $caseTypes = CRM_Case_PseudoConstant
::caseType('name', FALSE);
498 if (empty($params['case_type_id'])) {
499 $params['case_type_id'] = array_search($params['case_type'], $caseTypes);
501 // DEPRECATED: lookup by label for backward compatibility
502 if (!$params['case_type_id']) {
503 $caseTypeLabels = CRM_Case_PseudoConstant
::caseType('title', FALSE);
504 $params['case_type_id'] = array_search($params['case_type'], $caseTypeLabels);
505 $params['case_type'] = $caseTypes[$params['case_type_id']];
508 elseif (empty($params['case_type'])) {
509 $params['case_type'] = $caseTypes[$params['case_type_id']];
514 * It actually works a lot better to use the CaseContact api instead of the Case api
515 * for entityRef fields so we can perform the necessary joins,
516 * so we pass off getlist requests to the CaseContact api.
518 * @param array $params
521 function civicrm_api3_case_getList($params) {
522 require_once 'api/v3/Generic/Getlist.php';
523 require_once 'api/v3/CaseContact.php';
524 //CRM:19956 - Assign case_id param if both id and case_id is passed to retrieve the case
525 if (!empty($params['id']) && !empty($params['params']) && !empty($params['params']['case_id'])) {
526 $params['params']['case_id'] = array('IN' => $params['id']);
527 unset($params['id']);
529 $params['id_field'] = 'case_id';
530 $params['label_field'] = $params['search_field'] = 'contact_id.sort_name';
531 $params['description_field'] = array(
533 'case_id.case_type_id.title',
536 'case_id.start_date',
540 'entity' => 'CaseContact',
541 'action' => 'getlist',
544 return civicrm_api3_generic_getList($apiRequest);
548 * Needed due to the above override
552 function _civicrm_api3_case_getlist_spec(&$params, $apiRequest) {
553 require_once 'api/v3/Generic/Getlist.php';
554 _civicrm_api3_generic_getlist_spec($params, $apiRequest);