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 | /** | |
b081365f | 29 | * This api exposes CiviCRM Case objects. |
6a488035 TO |
30 | * Developed by woolman.org |
31 | * | |
32 | * @package CiviCRM_APIv3 | |
6a488035 TO |
33 | */ |
34 | ||
35 | ||
36 | /** | |
2fb1dd66 | 37 | * Open a new case, add client and manager roles, and add standard timeline. |
6a488035 | 38 | * |
6c552737 | 39 | * @param array $params |
b081365f | 40 | * @code |
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 | |
b081365f | 55 | * @endcode |
6a488035 | 56 | * |
784e85a1 | 57 | * @throws API_Exception |
a6c01b45 | 58 | * @return array |
72b3a70c | 59 | * api result array |
6a488035 TO |
60 | */ |
61 | function civicrm_api3_case_create($params) { | |
62 | ||
63 | if (!empty($params['id'])) { | |
64 | return civicrm_api3_case_update($params); | |
65 | } | |
66 | ||
211e2fca EM |
67 | civicrm_api3_verify_mandatory($params, NULL, array( |
68 | 'contact_id', | |
69 | 'subject', | |
70 | array('case_type', 'case_type_id')) | |
71 | ); | |
6a488035 TO |
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'])) { | |
784e85a1 | 76 | throw new API_Exception('Invalid case_type. No such case type exists.'); |
6a488035 TO |
77 | } |
78 | if (empty($params['case_type'])) { | |
784e85a1 | 79 | throw new API_Exception('Invalid case_type_id. No such case type exists.'); |
6a488035 TO |
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) { | |
784e85a1 | 95 | throw new API_Exception('Case not created. Please check input params.'); |
6a488035 TO |
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 | ||
11e09c59 | 130 | /** |
2fb1dd66 | 131 | * Adjust Metadata for Get Action. |
6a488035 | 132 | * |
cf470720 | 133 | * @param array $params |
2fb1dd66 | 134 | * Parameters determined by getfields. |
6a488035 TO |
135 | */ |
136 | function _civicrm_api3_case_get_spec(&$params) { | |
137 | $params['contact_id']['api.aliases'] = array('client_id'); | |
138 | $params['contact_id']['title'] = 'Case Client'; | |
139 | } | |
140 | ||
11e09c59 | 141 | /** |
2fb1dd66 | 142 | * Adjust Metadata for Create Action. |
6a488035 | 143 | * |
cf470720 | 144 | * @param array $params |
b081365f | 145 | * Array of parameters determined by getfields. |
6a488035 TO |
146 | */ |
147 | function _civicrm_api3_case_create_spec(&$params) { | |
148 | $params['contact_id']['api.aliases'] = array('client_id'); | |
149 | $params['contact_id']['title'] = 'Case Client'; | |
150 | $params['contact_id']['api.required'] = 1; | |
151 | $params['status_id']['api.default'] = 1; | |
8ae90f85 | 152 | $params['status_id']['api.aliases'] = array('case_status'); |
941feb14 EM |
153 | $params['creator_id']['api.default'] = 'user_contact_id'; |
154 | $params['creator_id']['type'] = CRM_Utils_Type::T_INT; | |
b05e6d0d | 155 | $params['creator_id']['title'] = 'Case Created By'; |
ca11b9da | 156 | $params['start_date']['api.default'] = 'now'; |
16c0ec8d CW |
157 | $params['medium_id'] = array( |
158 | 'name' => 'medium_id', | |
159 | 'title' => 'Activity Medium', | |
160 | ); | |
6a488035 TO |
161 | } |
162 | ||
11e09c59 | 163 | /** |
2fb1dd66 | 164 | * Adjust Metadata for Update action. |
6a488035 | 165 | * |
cf470720 | 166 | * @param array $params |
b081365f | 167 | * Array of parameters determined by getfields. |
6a488035 TO |
168 | */ |
169 | function _civicrm_api3_case_update_spec(&$params) { | |
170 | $params['id']['api.required'] = 1; | |
171 | } | |
172 | ||
11e09c59 | 173 | /** |
c1a920f1 | 174 | * Adjust Metadata for Delete action. |
6a488035 | 175 | * |
cf470720 | 176 | * @param array $params |
b081365f | 177 | * Array of parameters determined by getfields. |
6a488035 TO |
178 | */ |
179 | function _civicrm_api3_case_delete_spec(&$params) { | |
180 | $params['id']['api.required'] = 1; | |
181 | } | |
182 | ||
6a488035 | 183 | /** |
211e2fca | 184 | * Get details of a particular case, or search for cases, depending on params. |
6a488035 TO |
185 | * |
186 | * Please provide one (and only one) of the four get/search parameters: | |
187 | * | |
6c552737 TO |
188 | * @param array $params |
189 | * 'id' => if set, will get all available info about a case, including contacts and activities | |
6a488035 | 190 | * |
6c552737 TO |
191 | * // if no case_id provided, this function will use one of the following search parameters: |
192 | * 'client_id' => finds all cases with a specific client | |
193 | * 'activity_id' => returns the case containing a specific activity | |
194 | * 'contact_id' => finds all cases associated with a contact (in any role, not just client) | |
6a488035 | 195 | * |
77b97be7 | 196 | * @throws API_Exception |
a6c01b45 | 197 | * @return array |
72b3a70c | 198 | * (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 |
199 | */ |
200 | function civicrm_api3_case_get($params) { | |
201 | $options = _civicrm_api3_get_options_from_params($params); | |
6a488035 TO |
202 | //search by client |
203 | if (!empty($params['contact_id'])) { | |
204 | $ids = array(); | |
205 | foreach ((array) $params['contact_id'] as $cid) { | |
206 | if (is_numeric($cid)) { | |
207 | $ids = array_merge($ids, CRM_Case_BAO_Case::retrieveCaseIdsByContactId($cid, TRUE)); | |
208 | } | |
209 | } | |
210 | $cases = array(); | |
211 | foreach ($ids as $id) { | |
212 | if ($case = _civicrm_api3_case_read($id, $options)) { | |
213 | $cases[$id] = $case; | |
214 | } | |
215 | } | |
216 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
217 | } | |
218 | ||
219 | //search by activity | |
220 | if (!empty($params['activity_id'])) { | |
221 | if (!is_numeric($params['activity_id'])) { | |
784e85a1 | 222 | throw new API_Exception('Invalid parameter: activity_id. Must provide a numeric value.'); |
6a488035 TO |
223 | } |
224 | $caseId = CRM_Case_BAO_Case::getCaseIdByActivityId($params['activity_id']); | |
225 | if (!$caseId) { | |
226 | return civicrm_api3_create_success(array(), $params, 'case', 'get'); | |
227 | } | |
228 | $case = array($caseId => _civicrm_api3_case_read($caseId, $options)); | |
229 | return civicrm_api3_create_success($case, $params, 'case', 'get'); | |
230 | } | |
231 | ||
232 | //search by contacts | |
bf38705f | 233 | if (($contact = CRM_Utils_Array::value('contact_id', $params)) != FALSE) { |
6a488035 | 234 | if (!is_numeric($contact)) { |
784e85a1 | 235 | throw new API_Exception('Invalid parameter: contact_id. Must provide a numeric value.'); |
6a488035 TO |
236 | } |
237 | ||
238 | $sql = " | |
239 | SELECT DISTINCT case_id | |
240 | FROM civicrm_relationship | |
241 | WHERE (contact_id_a = $contact | |
242 | OR contact_id_b = $contact) | |
243 | AND case_id IS NOT NULL"; | |
9af2925b | 244 | $dao = CRM_Core_DAO::executeQuery($sql); |
6a488035 TO |
245 | |
246 | $cases = array(); | |
247 | while ($dao->fetch()) { | |
248 | $cases[$dao->case_id] = _civicrm_api3_case_read($dao->case_id, $options); | |
249 | } | |
250 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
251 | } | |
f21557af | 252 | |
e01eccc0 RB |
253 | // For historic reasons we always return these when an id is provided |
254 | $caseId = CRM_Utils_Array::value('id', $params); | |
255 | if ($caseId) { | |
256 | $options['return'] = array('contacts' => 1, 'activities' => 1); | |
257 | } | |
258 | ||
35671d00 | 259 | $foundcases = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Case'); |
f21557af | 260 | $cases = array(); |
261 | foreach ($foundcases['values'] as $foundcase) { | |
35671d00 TO |
262 | if ($case = _civicrm_api3_case_read($foundcase['id'], $options)) { |
263 | $cases[$foundcase['id']] = $case; | |
f21557af | 264 | } |
35671d00 | 265 | } |
f21557af | 266 | |
267 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
6a488035 TO |
268 | } |
269 | ||
270 | /** | |
35823763 EM |
271 | * Deprecated API. |
272 | * | |
273 | * Use activity API instead. | |
9657ccf2 EM |
274 | * |
275 | * @param array $params | |
276 | * | |
277 | * @throws API_Exception | |
278 | * @return array | |
6a488035 TO |
279 | */ |
280 | function civicrm_api3_case_activity_create($params) { | |
a14e9d08 CW |
281 | require_once "api/v3/Activity.php"; |
282 | return civicrm_api3_activity_create($params) + array( | |
283 | 'deprecated' => CRM_Utils_Array::value('activity_create', _civicrm_api3_case_deprecation()), | |
284 | ); | |
285 | } | |
286 | ||
287 | /** | |
35823763 EM |
288 | * Declare deprecated api functions. |
289 | * | |
a14e9d08 | 290 | * @deprecated api notice |
a6c01b45 | 291 | * @return array |
16b10e64 | 292 | * Array of deprecated actions |
a14e9d08 CW |
293 | */ |
294 | function _civicrm_api3_case_deprecation() { | |
295 | return array('activity_create' => 'Case api "activity_create" action is deprecated. Use the activity api instead.'); | |
6a488035 TO |
296 | } |
297 | ||
298 | /** | |
299 | * Update a specified case. | |
300 | * | |
6c552737 TO |
301 | * @param array $params |
302 | * //REQUIRED: | |
303 | * 'case_id' => int | |
6a488035 | 304 | * |
6c552737 TO |
305 | * //OPTIONAL |
306 | * 'status_id' => int | |
307 | * 'start_date' => str datestamp | |
308 | * 'contact_id' => int // case client | |
6a488035 | 309 | * |
784e85a1 | 310 | * @throws API_Exception |
a6c01b45 | 311 | * @return array |
72b3a70c | 312 | * api result array |
6a488035 TO |
313 | */ |
314 | function civicrm_api3_case_update($params) { | |
315 | //check parameters | |
316 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
317 | ||
784e85a1 | 318 | // return error if modifying creator id |
6a488035 | 319 | if (array_key_exists('creator_id', $params)) { |
784e85a1 | 320 | throw new API_Exception(ts('You cannot update creator id')); |
6a488035 TO |
321 | } |
322 | ||
784e85a1 | 323 | $mCaseId = $origContactIds = array(); |
6a488035 TO |
324 | |
325 | // get original contact id and creator id of case | |
326 | if (!empty($params['contact_id'])) { | |
c2e81506 | 327 | $origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['id']); |
6a488035 TO |
328 | $origContactId = $origContactIds[1]; |
329 | } | |
330 | ||
331 | if (count($origContactIds) > 1) { | |
332 | // check valid orig contact id | |
333 | if (!empty($params['orig_contact_id']) && !in_array($params['orig_contact_id'], $origContactIds)) { | |
784e85a1 | 334 | throw new API_Exception('Invalid case contact id (orig_contact_id)'); |
6a488035 TO |
335 | } |
336 | elseif (empty($params['orig_contact_id'])) { | |
784e85a1 | 337 | 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 |
338 | } |
339 | $origContactId = $params['orig_contact_id']; | |
340 | } | |
341 | ||
342 | // check for same contact id for edit Client | |
343 | if (!empty($params['contact_id']) && !in_array($params['contact_id'], $origContactIds)) { | |
344 | $mCaseId = CRM_Case_BAO_Case::mergeCases($params['contact_id'], $params['case_id'], $origContactId, NULL, TRUE); | |
345 | } | |
346 | ||
347 | if (!empty($mCaseId[0])) { | |
c2e81506 | 348 | $params['id'] = $mCaseId[0]; |
6a488035 TO |
349 | } |
350 | ||
351 | $dao = new CRM_Case_BAO_Case(); | |
352 | $dao->id = $params['id']; | |
353 | ||
354 | $dao->copyValues($params); | |
355 | $dao->save(); | |
356 | ||
357 | $case = array(); | |
358 | ||
359 | _civicrm_api3_object_to_array($dao, $case); | |
8c4de140 | 360 | $values[$dao->id] = $case; |
6a488035 | 361 | |
8c4de140 | 362 | return civicrm_api3_create_success($values, $params, 'case', 'update', $dao); |
6a488035 TO |
363 | } |
364 | ||
365 | /** | |
366 | * Delete a specified case. | |
367 | * | |
6c552737 TO |
368 | * @param array $params |
369 | * //REQUIRED: | |
370 | * 'id' => int | |
6a488035 | 371 | * |
6c552737 TO |
372 | * //OPTIONAL |
373 | * 'move_to_trash' => bool (defaults to false) | |
6a488035 | 374 | * |
77b97be7 | 375 | * @throws API_Exception |
5c766a0b | 376 | * @return bool |
72b3a70c | 377 | * true if success, else false |
6a488035 TO |
378 | */ |
379 | function civicrm_api3_case_delete($params) { | |
380 | //check parameters | |
381 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
382 | ||
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'); | |
385 | } | |
386 | else { | |
784e85a1 | 387 | throw new API_Exception('Could not delete case.'); |
6a488035 TO |
388 | } |
389 | } | |
390 | ||
6a488035 TO |
391 | /** |
392 | * Internal function to retrieve a case. | |
393 | * | |
394 | * @param int $caseId | |
395 | * | |
784e85a1 EM |
396 | * @param $options |
397 | * | |
a6c01b45 | 398 | * @return array |
72b3a70c | 399 | * case object |
6a488035 TO |
400 | */ |
401 | function _civicrm_api3_case_read($caseId, $options) { | |
402 | $return = CRM_Utils_Array::value('return', $options, array()); | |
403 | $dao = new CRM_Case_BAO_Case(); | |
404 | $dao->id = $caseId; | |
405 | if ($dao->find(TRUE)) { | |
406 | $case = array(); | |
407 | _civicrm_api3_object_to_array($dao, $case); | |
408 | // Legacy support for client_id - TODO: in apiv4 remove 'client_id' | |
409 | $case['client_id'] = $case['contact_id'] = $dao->retrieveContactIdsByCaseId($caseId); | |
410 | ||
6a488035 TO |
411 | if (!empty($return['contacts'])) { |
412 | //get case contacts | |
413 | $contacts = CRM_Case_BAO_Case::getcontactNames($caseId); | |
414 | $relations = CRM_Case_BAO_Case::getRelatedContacts($caseId); | |
415 | $case['contacts'] = array_merge($contacts, $relations); | |
416 | } | |
417 | if (!empty($return['activities'])) { | |
418 | //get case activities | |
419 | $case['activities'] = array(); | |
420 | $query = "SELECT activity_id FROM civicrm_case_activity WHERE case_id = $caseId"; | |
421 | $dao = CRM_Core_DAO::executeQuery($query); | |
422 | while ($dao->fetch()) { | |
423 | $case['activities'][] = $dao->activity_id; | |
424 | } | |
425 | } | |
426 | return $case; | |
427 | } | |
428 | } | |
429 | ||
430 | /** | |
61fe4988 | 431 | * Internal function to format create params for processing. |
9657ccf2 EM |
432 | * |
433 | * @param array $params | |
6a488035 TO |
434 | */ |
435 | function _civicrm_api3_case_format_params(&$params) { | |
6a488035 | 436 | // figure out case type id from case type and vice-versa |
1be6caec | 437 | $caseTypes = CRM_Case_PseudoConstant::caseType('name', FALSE); |
6a488035 TO |
438 | if (empty($params['case_type_id'])) { |
439 | $params['case_type_id'] = array_search($params['case_type'], $caseTypes); | |
3f25e694 TO |
440 | |
441 | // DEPRECATED: lookup by label for backward compatibility | |
442 | if (!$params['case_type_id']) { | |
443 | $caseTypeLabels = CRM_Case_PseudoConstant::caseType('title', FALSE); | |
444 | $params['case_type_id'] = array_search($params['case_type'], $caseTypeLabels); | |
61fe4988 | 445 | $params['case_type'] = $caseTypes[$params['case_type_id']]; |
3f25e694 | 446 | } |
6a488035 TO |
447 | } |
448 | elseif (empty($params['case_type'])) { | |
449 | $params['case_type'] = $caseTypes[$params['case_type_id']]; | |
450 | } | |
6a488035 | 451 | } |