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