3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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_Case
::addCaseToContact($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
,
143 'FKApiName' => 'Contact',
145 $params['activity_id'] = array(
146 'title' => 'Case Activity',
147 'description' => 'Id of an activity in the case',
148 'type' => CRM_Utils_Type
::T_INT
,
149 'FKApiName' => 'Activity',
154 * Adjust Metadata for Create Action.
156 * @param array $params
157 * Array of parameters determined by getfields.
159 function _civicrm_api3_case_create_spec(&$params) {
160 $params['contact_id'] = array(
161 'api.aliases' => array('client_id'),
162 'title' => 'Case Client',
163 'description' => 'Contact id of case client(s)',
165 'type' => CRM_Utils_Type
::T_INT
,
166 'FKApiName' => 'Contact',
168 $params['status_id']['api.default'] = 1;
169 $params['status_id']['api.aliases'] = array('case_status');
170 $params['creator_id']['api.default'] = 'user_contact_id';
171 $params['creator_id']['type'] = CRM_Utils_Type
::T_INT
;
172 $params['creator_id']['title'] = 'Case Created By';
173 $params['start_date']['api.default'] = 'now';
174 $params['medium_id'] = array(
175 'name' => 'medium_id',
176 'title' => 'Activity Medium',
177 'type' => CRM_Utils_Type
::T_INT
,
182 * Adjust Metadata for Update action.
184 * @param array $params
185 * Array of parameters determined by getfields.
187 function _civicrm_api3_case_update_spec(&$params) {
188 $params['id']['api.required'] = 1;
192 * Adjust Metadata for Delete action.
194 * @param array $params
195 * Array of parameters determined by getfields.
197 function _civicrm_api3_case_delete_spec(&$params) {
198 $params['id']['api.required'] = 1;
202 * Get details of a particular case, or search for cases, depending on params.
204 * Please provide one (and only one) of the four get/search parameters:
206 * @param array $params
207 * 'id' => if set, will get all available info about a case, including contacts and activities
209 * // if no case_id provided, this function will use one of the following search parameters:
210 * 'client_id' => finds all cases with a specific client
211 * 'activity_id' => returns the case containing a specific activity
212 * 'contact_id' => finds all cases associated with a contact (in any role, not just client)
214 * @throws API_Exception
216 * (get mode, case_id provided): Array with case details, case roles, case activity ids, (search mode, case_id not provided): Array of cases found
218 function civicrm_api3_case_get($params) {
219 $options = _civicrm_api3_get_options_from_params($params);
220 $sql = CRM_Utils_SQL_Select
::fragment();
222 // Add clause to search by client
223 if (!empty($params['contact_id'])) {
225 foreach ((array) $params['contact_id'] as $c) {
226 if (!CRM_Utils_Rule
::positiveInteger($c)) {
227 throw new API_Exception('Invalid parameter: contact_id. Must provide numeric value(s).');
232 ->join('civicrm_case_contact', 'INNER JOIN civicrm_case_contact ON civicrm_case_contact.case_id = a.id')
233 ->where('civicrm_case_contact.contact_id IN (' . implode(',', $contacts) . ')');
236 // Add clause to search by activity
237 if (!empty($params['activity_id'])) {
238 if (!CRM_Utils_Rule
::positiveInteger($params['activity_id'])) {
239 throw new API_Exception('Invalid parameter: activity_id. Must provide a numeric value.');
241 $activityId = $params['activity_id'];
242 $originalId = CRM_Core_DAO
::getFieldValue('CRM_Activity_BAO_Activity', $activityId, 'original_id');
244 $activityId .= ',' . $originalId;
247 ->join('civicrm_case_activity', 'INNER JOIN civicrm_case_activity ON civicrm_case_activity.case_id = a.id')
248 ->where("civicrm_case_activity.activity_id IN ($activityId)");
251 // For historic reasons we always return these when an id is provided
252 if (!empty($params['id'])) {
253 $options['return'] = array('contacts' => 1, 'activities' => 1);
256 $foundcases = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, TRUE, 'Case', $sql);
258 foreach ($foundcases['values'] as $foundcase) {
259 if ($case = _civicrm_api3_case_read($foundcase['id'], $options, !empty($params['check_permissions']))) {
260 $cases[$foundcase['id']] = $case;
264 return civicrm_api3_create_success($cases, $params, 'Case', 'get');
270 * Use activity API instead.
272 * @param array $params
274 * @throws API_Exception
277 function civicrm_api3_case_activity_create($params) {
278 require_once "api/v3/Activity.php";
279 return civicrm_api3_activity_create($params) +
array(
280 'deprecated' => CRM_Utils_Array
::value('activity_create', _civicrm_api3_case_deprecation()),
285 * Declare deprecated api functions.
287 * @deprecated api notice
289 * Array of deprecated actions
291 function _civicrm_api3_case_deprecation() {
292 return array('activity_create' => 'Case api "activity_create" action is deprecated. Use the activity api instead.');
296 * Update a specified case.
298 * @param array $params
304 * 'start_date' => str datestamp
305 * 'contact_id' => int // case client
307 * @throws API_Exception
311 function civicrm_api3_case_update($params) {
313 civicrm_api3_verify_mandatory($params, NULL, array('id'));
315 // return error if modifying creator id
316 if (array_key_exists('creator_id', $params)) {
317 throw new API_Exception(ts('You cannot update creator id'));
320 $mCaseId = $origContactIds = array();
322 // get original contact id and creator id of case
323 if (!empty($params['contact_id'])) {
324 $origContactIds = CRM_Case_BAO_Case
::retrieveContactIdsByCaseId($params['id']);
325 $origContactId = $origContactIds[1];
328 if (count($origContactIds) > 1) {
329 // check valid orig contact id
330 if (!empty($params['orig_contact_id']) && !in_array($params['orig_contact_id'], $origContactIds)) {
331 throw new API_Exception('Invalid case contact id (orig_contact_id)');
333 elseif (empty($params['orig_contact_id'])) {
334 throw new API_Exception('Case is linked with more than one contact id. Provide the required params orig_contact_id to be replaced');
336 $origContactId = $params['orig_contact_id'];
339 // check for same contact id for edit Client
340 if (!empty($params['contact_id']) && !in_array($params['contact_id'], $origContactIds)) {
341 $mCaseId = CRM_Case_BAO_Case
::mergeCases($params['contact_id'], $params['case_id'], $origContactId, NULL, TRUE);
344 if (!empty($mCaseId[0])) {
345 $params['id'] = $mCaseId[0];
348 $dao = new CRM_Case_BAO_Case();
349 $dao->id
= $params['id'];
351 $dao->copyValues($params);
356 _civicrm_api3_object_to_array($dao, $case);
357 $values[$dao->id
] = $case;
359 return civicrm_api3_create_success($values, $params, 'Case', 'update', $dao);
363 * Delete a specified case.
365 * @param array $params
372 * 'move_to_trash' => bool (defaults to false)
375 * @throws API_Exception
377 * true if success, else false
379 function civicrm_api3_case_delete($params) {
381 civicrm_api3_verify_mandatory($params, NULL, array('id'));
383 if (CRM_Case_BAO_Case
::deleteCase($params['id'], CRM_Utils_Array
::value('move_to_trash', $params, FALSE))) {
384 return civicrm_api3_create_success($params, $params, 'Case', 'delete');
387 throw new API_Exception('Could not delete case.');
392 * Internal function to retrieve a case.
396 * @param array $options
398 * @param bool $checkPermission
403 function _civicrm_api3_case_read($caseId, $options, $checkPermission) {
404 $return = CRM_Utils_Array
::value('return', $options, array());
405 $dao = new CRM_Case_BAO_Case();
407 if ($dao->find(TRUE)) {
409 _civicrm_api3_object_to_array($dao, $case);
411 _civicrm_api3_custom_data_get($case, $checkPermission, 'Case', $caseId);
413 // Legacy support for client_id - TODO: in apiv4 remove 'client_id'
414 $case['client_id'] = $case['contact_id'] = $dao->retrieveContactIdsByCaseId($caseId);
416 if (!empty($return['contacts'])) {
418 $contacts = CRM_Case_BAO_Case
::getcontactNames($caseId);
419 $relations = CRM_Case_BAO_Case
::getRelatedContacts($caseId);
420 $case['contacts'] = array_merge($contacts, $relations);
422 if (!empty($return['activities'])) {
423 //get case activities
424 $case['activities'] = array();
425 $query = "SELECT activity_id FROM civicrm_case_activity WHERE case_id = $caseId";
426 $dao = CRM_Core_DAO
::executeQuery($query);
427 while ($dao->fetch()) {
428 $case['activities'][] = $dao->activity_id
;
436 * Internal function to format create params for processing.
438 * @param array $params
440 function _civicrm_api3_case_format_params(&$params) {
441 // figure out case type id from case type and vice-versa
442 $caseTypes = CRM_Case_PseudoConstant
::caseType('name', FALSE);
443 if (empty($params['case_type_id'])) {
444 $params['case_type_id'] = array_search($params['case_type'], $caseTypes);
446 // DEPRECATED: lookup by label for backward compatibility
447 if (!$params['case_type_id']) {
448 $caseTypeLabels = CRM_Case_PseudoConstant
::caseType('title', FALSE);
449 $params['case_type_id'] = array_search($params['case_type'], $caseTypeLabels);
450 $params['case_type'] = $caseTypes[$params['case_type_id']];
453 elseif (empty($params['case_type'])) {
454 $params['case_type'] = $caseTypes[$params['case_type_id']];