Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
731a0992 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
c28e1768 | 29 | * This api exposes CiviCRM Case. |
6a488035 TO |
30 | * Developed by woolman.org |
31 | * | |
32 | * @package CiviCRM_APIv3 | |
33 | * @subpackage API_Case | |
6a488035 TO |
34 | */ |
35 | ||
36 | ||
37 | /** | |
2fb1dd66 | 38 | * Open a new case, add client and manager roles, and add standard timeline. |
6a488035 | 39 | * |
6c552737 | 40 | * @param array $params |
784e85a1 | 41 | * //REQUIRED: |
6a488035 TO |
42 | * 'case_type_id' => int OR |
43 | * 'case_type' => str (provide one or the other) | |
44 | * 'contact_id' => int // case client | |
45 | * 'subject' => str | |
46 | * | |
47 | * //OPTIONAL | |
48 | * 'medium_id' => int // see civicrm option values for possibilities | |
49 | * 'creator_id' => int // case manager, default to the logged in user | |
50 | * 'status_id' => int // defaults to 1 "ongoing" | |
51 | * 'location' => str | |
52 | * 'start_date' => str datestamp // defaults to: date('YmdHis') | |
53 | * 'duration' => int // in minutes | |
54 | * 'details' => str // html format | |
55 | * | |
784e85a1 | 56 | * @throws API_Exception |
a6c01b45 | 57 | * @return array |
72b3a70c | 58 | * api result array |
6a488035 TO |
59 | */ |
60 | function civicrm_api3_case_create($params) { | |
61 | ||
62 | if (!empty($params['id'])) { | |
63 | return civicrm_api3_case_update($params); | |
64 | } | |
65 | ||
211e2fca EM |
66 | civicrm_api3_verify_mandatory($params, NULL, array( |
67 | 'contact_id', | |
68 | 'subject', | |
69 | array('case_type', 'case_type_id')) | |
70 | ); | |
6a488035 TO |
71 | _civicrm_api3_case_format_params($params); |
72 | ||
73 | // If format_params didn't find what it was looking for, return error | |
74 | if (empty($params['case_type_id'])) { | |
784e85a1 | 75 | throw new API_Exception('Invalid case_type. No such case type exists.'); |
6a488035 TO |
76 | } |
77 | if (empty($params['case_type'])) { | |
784e85a1 | 78 | throw new API_Exception('Invalid case_type_id. No such case type exists.'); |
6a488035 TO |
79 | } |
80 | ||
81 | // Fixme: can we safely pass raw params to the BAO? | |
82 | $newParams = array( | |
83 | 'case_type_id' => $params['case_type_id'], | |
84 | 'creator_id' => $params['creator_id'], | |
85 | 'status_id' => $params['status_id'], | |
86 | 'start_date' => $params['start_date'], | |
87 | 'end_date' => CRM_Utils_Array::value('end_date', $params), | |
88 | 'subject' => $params['subject'], | |
89 | ); | |
90 | ||
91 | $caseBAO = CRM_Case_BAO_Case::create($newParams); | |
92 | ||
93 | if (!$caseBAO) { | |
784e85a1 | 94 | throw new API_Exception('Case not created. Please check input params.'); |
6a488035 TO |
95 | } |
96 | ||
97 | foreach ((array) $params['contact_id'] as $cid) { | |
98 | $contactParams = array('case_id' => $caseBAO->id, 'contact_id' => $cid); | |
99 | CRM_Case_BAO_Case::addCaseToContact($contactParams); | |
100 | } | |
101 | ||
102 | // Initialize XML processor with $params | |
103 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
104 | $xmlProcessorParams = array( | |
105 | 'clientID' => $params['contact_id'], | |
106 | 'creatorID' => $params['creator_id'], | |
107 | 'standardTimeline' => 1, | |
108 | 'activityTypeName' => 'Open Case', | |
109 | 'caseID' => $caseBAO->id, | |
110 | 'subject' => $params['subject'], | |
111 | 'location' => CRM_Utils_Array::value('location', $params), | |
112 | 'activity_date_time' => $params['start_date'], | |
113 | 'duration' => CRM_Utils_Array::value('duration', $params), | |
114 | 'medium_id' => CRM_Utils_Array::value('medium_id', $params), | |
115 | 'details' => CRM_Utils_Array::value('details', $params), | |
116 | 'custom' => array(), | |
117 | ); | |
118 | ||
119 | // Do it! :-D | |
120 | $xmlProcessor->run($params['case_type'], $xmlProcessorParams); | |
121 | ||
122 | // return case | |
123 | $values = array(); | |
124 | _civicrm_api3_object_to_array($caseBAO, $values[$caseBAO->id]); | |
125 | ||
126 | return civicrm_api3_create_success($values, $params, 'case', 'create', $caseBAO); | |
127 | } | |
128 | ||
11e09c59 | 129 | /** |
2fb1dd66 | 130 | * Adjust Metadata for Get Action. |
6a488035 | 131 | * |
cf470720 | 132 | * @param array $params |
2fb1dd66 | 133 | * Parameters determined by getfields. |
6a488035 TO |
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 | ||
11e09c59 | 140 | /** |
2fb1dd66 | 141 | * Adjust Metadata for Create Action. |
6a488035 | 142 | * |
cf470720 TO |
143 | * @param array $params |
144 | * Array or parameters determined by getfields. | |
6a488035 TO |
145 | */ |
146 | function _civicrm_api3_case_create_spec(&$params) { | |
147 | $params['contact_id']['api.aliases'] = array('client_id'); | |
148 | $params['contact_id']['title'] = 'Case Client'; | |
149 | $params['contact_id']['api.required'] = 1; | |
150 | $params['status_id']['api.default'] = 1; | |
8ae90f85 | 151 | $params['status_id']['api.aliases'] = array('case_status'); |
941feb14 EM |
152 | $params['creator_id']['api.default'] = 'user_contact_id'; |
153 | $params['creator_id']['type'] = CRM_Utils_Type::T_INT; | |
b05e6d0d | 154 | $params['creator_id']['title'] = 'Case Created By'; |
ca11b9da | 155 | $params['start_date']['api.default'] = 'now'; |
16c0ec8d CW |
156 | $params['medium_id'] = array( |
157 | 'name' => 'medium_id', | |
158 | 'title' => 'Activity Medium', | |
159 | ); | |
6a488035 TO |
160 | } |
161 | ||
11e09c59 | 162 | /** |
2fb1dd66 | 163 | * Adjust Metadata for Update action. |
6a488035 | 164 | * |
cf470720 TO |
165 | * @param array $params |
166 | * Array or parameters determined by getfields. | |
6a488035 TO |
167 | */ |
168 | function _civicrm_api3_case_update_spec(&$params) { | |
169 | $params['id']['api.required'] = 1; | |
170 | } | |
171 | ||
11e09c59 | 172 | /** |
c1a920f1 | 173 | * Adjust Metadata for Delete action. |
6a488035 | 174 | * |
cf470720 TO |
175 | * @param array $params |
176 | * Array or parameters determined by getfields. | |
6a488035 TO |
177 | */ |
178 | function _civicrm_api3_case_delete_spec(&$params) { | |
179 | $params['id']['api.required'] = 1; | |
180 | } | |
181 | ||
6a488035 | 182 | /** |
211e2fca | 183 | * Get details of a particular case, or search for cases, depending on params. |
6a488035 TO |
184 | * |
185 | * Please provide one (and only one) of the four get/search parameters: | |
186 | * | |
6c552737 TO |
187 | * @param array $params |
188 | * 'id' => if set, will get all available info about a case, including contacts and activities | |
6a488035 | 189 | * |
6c552737 TO |
190 | * // if no case_id provided, this function will use one of the following search parameters: |
191 | * 'client_id' => finds all cases with a specific client | |
192 | * 'activity_id' => returns the case containing a specific activity | |
193 | * 'contact_id' => finds all cases associated with a contact (in any role, not just client) | |
6a488035 | 194 | * |
77b97be7 | 195 | * @throws API_Exception |
a6c01b45 | 196 | * @return array |
72b3a70c | 197 | * (get mode, case_id provided): Array with case details, case roles, case activity ids, (search mode, case_id not provided): Array of cases found |
6a488035 TO |
198 | */ |
199 | function civicrm_api3_case_get($params) { | |
200 | $options = _civicrm_api3_get_options_from_params($params); | |
6a488035 TO |
201 | //search by client |
202 | if (!empty($params['contact_id'])) { | |
203 | $ids = array(); | |
204 | foreach ((array) $params['contact_id'] as $cid) { | |
205 | if (is_numeric($cid)) { | |
206 | $ids = array_merge($ids, CRM_Case_BAO_Case::retrieveCaseIdsByContactId($cid, TRUE)); | |
207 | } | |
208 | } | |
209 | $cases = array(); | |
210 | foreach ($ids as $id) { | |
211 | if ($case = _civicrm_api3_case_read($id, $options)) { | |
212 | $cases[$id] = $case; | |
213 | } | |
214 | } | |
215 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
216 | } | |
217 | ||
218 | //search by activity | |
219 | if (!empty($params['activity_id'])) { | |
220 | if (!is_numeric($params['activity_id'])) { | |
784e85a1 | 221 | throw new API_Exception('Invalid parameter: activity_id. Must provide a numeric value.'); |
6a488035 TO |
222 | } |
223 | $caseId = CRM_Case_BAO_Case::getCaseIdByActivityId($params['activity_id']); | |
224 | if (!$caseId) { | |
225 | return civicrm_api3_create_success(array(), $params, 'case', 'get'); | |
226 | } | |
227 | $case = array($caseId => _civicrm_api3_case_read($caseId, $options)); | |
228 | return civicrm_api3_create_success($case, $params, 'case', 'get'); | |
229 | } | |
230 | ||
231 | //search by contacts | |
bf38705f | 232 | if (($contact = CRM_Utils_Array::value('contact_id', $params)) != FALSE) { |
6a488035 | 233 | if (!is_numeric($contact)) { |
784e85a1 | 234 | throw new API_Exception('Invalid parameter: contact_id. Must provide a numeric value.'); |
6a488035 TO |
235 | } |
236 | ||
237 | $sql = " | |
238 | SELECT DISTINCT case_id | |
239 | FROM civicrm_relationship | |
240 | WHERE (contact_id_a = $contact | |
241 | OR contact_id_b = $contact) | |
242 | AND case_id IS NOT NULL"; | |
9af2925b | 243 | $dao = CRM_Core_DAO::executeQuery($sql); |
6a488035 TO |
244 | |
245 | $cases = array(); | |
246 | while ($dao->fetch()) { | |
247 | $cases[$dao->case_id] = _civicrm_api3_case_read($dao->case_id, $options); | |
248 | } | |
249 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
250 | } | |
f21557af | 251 | |
e01eccc0 RB |
252 | // For historic reasons we always return these when an id is provided |
253 | $caseId = CRM_Utils_Array::value('id', $params); | |
254 | if ($caseId) { | |
255 | $options['return'] = array('contacts' => 1, 'activities' => 1); | |
256 | } | |
257 | ||
35671d00 | 258 | $foundcases = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Case'); |
f21557af | 259 | $cases = array(); |
260 | foreach ($foundcases['values'] as $foundcase) { | |
35671d00 TO |
261 | if ($case = _civicrm_api3_case_read($foundcase['id'], $options)) { |
262 | $cases[$foundcase['id']] = $case; | |
f21557af | 263 | } |
35671d00 | 264 | } |
f21557af | 265 | |
266 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
6a488035 TO |
267 | } |
268 | ||
269 | /** | |
35823763 EM |
270 | * Deprecated API. |
271 | * | |
272 | * Use activity API instead. | |
9657ccf2 EM |
273 | * |
274 | * @param array $params | |
275 | * | |
276 | * @throws API_Exception | |
277 | * @return array | |
6a488035 TO |
278 | */ |
279 | function civicrm_api3_case_activity_create($params) { | |
a14e9d08 CW |
280 | require_once "api/v3/Activity.php"; |
281 | return civicrm_api3_activity_create($params) + array( | |
282 | 'deprecated' => CRM_Utils_Array::value('activity_create', _civicrm_api3_case_deprecation()), | |
283 | ); | |
284 | } | |
285 | ||
286 | /** | |
35823763 EM |
287 | * Declare deprecated api functions. |
288 | * | |
a14e9d08 | 289 | * @deprecated api notice |
a6c01b45 | 290 | * @return array |
16b10e64 | 291 | * Array of deprecated actions |
a14e9d08 CW |
292 | */ |
293 | function _civicrm_api3_case_deprecation() { | |
294 | return array('activity_create' => 'Case api "activity_create" action is deprecated. Use the activity api instead.'); | |
6a488035 TO |
295 | } |
296 | ||
297 | /** | |
298 | * Update a specified case. | |
299 | * | |
6c552737 TO |
300 | * @param array $params |
301 | * //REQUIRED: | |
302 | * 'case_id' => int | |
6a488035 | 303 | * |
6c552737 TO |
304 | * //OPTIONAL |
305 | * 'status_id' => int | |
306 | * 'start_date' => str datestamp | |
307 | * 'contact_id' => int // case client | |
6a488035 | 308 | * |
784e85a1 | 309 | * @throws API_Exception |
a6c01b45 | 310 | * @return array |
72b3a70c | 311 | * api result array |
6a488035 TO |
312 | */ |
313 | function civicrm_api3_case_update($params) { | |
314 | //check parameters | |
315 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
316 | ||
784e85a1 | 317 | // return error if modifying creator id |
6a488035 | 318 | if (array_key_exists('creator_id', $params)) { |
784e85a1 | 319 | throw new API_Exception(ts('You cannot update creator id')); |
6a488035 TO |
320 | } |
321 | ||
784e85a1 | 322 | $mCaseId = $origContactIds = array(); |
6a488035 TO |
323 | |
324 | // get original contact id and creator id of case | |
325 | if (!empty($params['contact_id'])) { | |
c2e81506 | 326 | $origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['id']); |
6a488035 TO |
327 | $origContactId = $origContactIds[1]; |
328 | } | |
329 | ||
330 | if (count($origContactIds) > 1) { | |
331 | // check valid orig contact id | |
332 | if (!empty($params['orig_contact_id']) && !in_array($params['orig_contact_id'], $origContactIds)) { | |
784e85a1 | 333 | throw new API_Exception('Invalid case contact id (orig_contact_id)'); |
6a488035 TO |
334 | } |
335 | elseif (empty($params['orig_contact_id'])) { | |
784e85a1 | 336 | throw new API_Exception('Case is linked with more than one contact id. Provide the required params orig_contact_id to be replaced'); |
6a488035 TO |
337 | } |
338 | $origContactId = $params['orig_contact_id']; | |
339 | } | |
340 | ||
341 | // check for same contact id for edit Client | |
342 | if (!empty($params['contact_id']) && !in_array($params['contact_id'], $origContactIds)) { | |
343 | $mCaseId = CRM_Case_BAO_Case::mergeCases($params['contact_id'], $params['case_id'], $origContactId, NULL, TRUE); | |
344 | } | |
345 | ||
346 | if (!empty($mCaseId[0])) { | |
c2e81506 | 347 | $params['id'] = $mCaseId[0]; |
6a488035 TO |
348 | } |
349 | ||
350 | $dao = new CRM_Case_BAO_Case(); | |
351 | $dao->id = $params['id']; | |
352 | ||
353 | $dao->copyValues($params); | |
354 | $dao->save(); | |
355 | ||
356 | $case = array(); | |
357 | ||
358 | _civicrm_api3_object_to_array($dao, $case); | |
8c4de140 | 359 | $values[$dao->id] = $case; |
6a488035 | 360 | |
8c4de140 | 361 | return civicrm_api3_create_success($values, $params, 'case', 'update', $dao); |
6a488035 TO |
362 | } |
363 | ||
364 | /** | |
365 | * Delete a specified case. | |
366 | * | |
6c552737 TO |
367 | * @param array $params |
368 | * //REQUIRED: | |
369 | * 'id' => int | |
6a488035 | 370 | * |
6c552737 TO |
371 | * //OPTIONAL |
372 | * 'move_to_trash' => bool (defaults to false) | |
6a488035 | 373 | * |
77b97be7 | 374 | * @throws API_Exception |
5c766a0b | 375 | * @return bool |
72b3a70c | 376 | * true if success, else false |
6a488035 TO |
377 | */ |
378 | function civicrm_api3_case_delete($params) { | |
379 | //check parameters | |
380 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
381 | ||
382 | if (CRM_Case_BAO_Case::deleteCase($params['id'], CRM_Utils_Array::value('move_to_trash', $params, FALSE))) { | |
383 | return civicrm_api3_create_success($params, $params, 'case', 'delete'); | |
384 | } | |
385 | else { | |
784e85a1 | 386 | throw new API_Exception('Could not delete case.'); |
6a488035 TO |
387 | } |
388 | } | |
389 | ||
390 | /***********************************/ | |
391 | /* */ | |
392 | /* INTERNAL FUNCTIONS */ | |
393 | /* */ | |
394 | /***********************************/ | |
395 | ||
396 | /** | |
397 | * Internal function to retrieve a case. | |
398 | * | |
399 | * @param int $caseId | |
400 | * | |
784e85a1 EM |
401 | * @param $options |
402 | * | |
a6c01b45 | 403 | * @return array |
72b3a70c | 404 | * case object |
6a488035 TO |
405 | */ |
406 | function _civicrm_api3_case_read($caseId, $options) { | |
407 | $return = CRM_Utils_Array::value('return', $options, array()); | |
408 | $dao = new CRM_Case_BAO_Case(); | |
409 | $dao->id = $caseId; | |
410 | if ($dao->find(TRUE)) { | |
411 | $case = array(); | |
412 | _civicrm_api3_object_to_array($dao, $case); | |
413 | // Legacy support for client_id - TODO: in apiv4 remove 'client_id' | |
414 | $case['client_id'] = $case['contact_id'] = $dao->retrieveContactIdsByCaseId($caseId); | |
415 | ||
6a488035 TO |
416 | if (!empty($return['contacts'])) { |
417 | //get case contacts | |
418 | $contacts = CRM_Case_BAO_Case::getcontactNames($caseId); | |
419 | $relations = CRM_Case_BAO_Case::getRelatedContacts($caseId); | |
420 | $case['contacts'] = array_merge($contacts, $relations); | |
421 | } | |
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; | |
429 | } | |
430 | } | |
431 | return $case; | |
432 | } | |
433 | } | |
434 | ||
435 | /** | |
61fe4988 | 436 | * Internal function to format create params for processing. |
9657ccf2 EM |
437 | * |
438 | * @param array $params | |
6a488035 TO |
439 | */ |
440 | function _civicrm_api3_case_format_params(&$params) { | |
6a488035 | 441 | // figure out case type id from case type and vice-versa |
1be6caec | 442 | $caseTypes = CRM_Case_PseudoConstant::caseType('name', FALSE); |
6a488035 TO |
443 | if (empty($params['case_type_id'])) { |
444 | $params['case_type_id'] = array_search($params['case_type'], $caseTypes); | |
3f25e694 TO |
445 | |
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); | |
61fe4988 | 450 | $params['case_type'] = $caseTypes[$params['case_type_id']]; |
3f25e694 | 451 | } |
6a488035 TO |
452 | } |
453 | elseif (empty($params['case_type'])) { | |
454 | $params['case_type'] = $caseTypes[$params['case_type_id']]; | |
455 | } | |
6a488035 | 456 | } |