Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
232624b1 | 5 | | CiviCRM version 4.4 | |
6a488035 TO |
6 | +--------------------------------------------------------------------+ |
7 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * File for the CiviCRM APIv3 Case functions | |
31 | * Developed by woolman.org | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | * @subpackage API_Case | |
35 | * @copyright CiviCRM LLC (c) 2004-2013 | |
36 | * | |
37 | */ | |
38 | ||
39 | ||
40 | /** | |
41 | * Open a new case, add client and manager roles, and add standard timeline | |
42 | * | |
43 | * @param array( | |
44 | //REQUIRED: | |
45 | * 'case_type_id' => int OR | |
46 | * 'case_type' => str (provide one or the other) | |
47 | * 'contact_id' => int // case client | |
48 | * 'subject' => str | |
49 | * | |
50 | * //OPTIONAL | |
51 | * 'medium_id' => int // see civicrm option values for possibilities | |
52 | * 'creator_id' => int // case manager, default to the logged in user | |
53 | * 'status_id' => int // defaults to 1 "ongoing" | |
54 | * 'location' => str | |
55 | * 'start_date' => str datestamp // defaults to: date('YmdHis') | |
56 | * 'duration' => int // in minutes | |
57 | * 'details' => str // html format | |
58 | * | |
59 | * @return sucessfully opened case | |
60 | * | |
61 | * @access public | |
62 | * {@getfields case_create} | |
63 | */ | |
64 | function civicrm_api3_case_create($params) { | |
65 | ||
66 | if (!empty($params['id'])) { | |
67 | return civicrm_api3_case_update($params); | |
68 | } | |
69 | ||
70 | civicrm_api3_verify_mandatory($params, NULL, array('contact_id', 'subject', array('case_type', 'case_type_id'))); | |
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'])) { | |
75 | return civicrm_api3_create_error('Invalid case_type. No such case type exists.'); | |
76 | } | |
77 | if (empty($params['case_type'])) { | |
78 | return civicrm_api3_create_error('Invalid case_type_id. No such case type exists.'); | |
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) { | |
94 | return civicrm_api3_create_error('Case not created. Please check input params.'); | |
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 | /** |
6a488035 TO |
130 | * Adjust Metadata for Get Action |
131 | * | |
132 | * @param array $params array or parameters determined by getfields | |
133 | */ | |
134 | function _civicrm_api3_case_get_spec(&$params) { | |
135 | $params['contact_id']['api.aliases'] = array('client_id'); | |
136 | $params['contact_id']['title'] = 'Case Client'; | |
137 | } | |
138 | ||
11e09c59 | 139 | /** |
6a488035 TO |
140 | * Adjust Metadata for Create Action |
141 | * | |
142 | * @param array $params array or parameters determined by getfields | |
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; | |
16c0ec8d CW |
149 | $params['medium_id'] = array( |
150 | 'name' => 'medium_id', | |
151 | 'title' => 'Activity Medium', | |
152 | ); | |
6a488035 TO |
153 | } |
154 | ||
11e09c59 | 155 | /** |
6a488035 TO |
156 | * Adjust Metadata for Update action |
157 | * | |
158 | * @param array $params array or parameters determined by getfields | |
159 | */ | |
160 | function _civicrm_api3_case_update_spec(&$params) { | |
161 | $params['id']['api.required'] = 1; | |
162 | } | |
163 | ||
11e09c59 | 164 | /** |
6a488035 TO |
165 | * Adjust Metadata for Delete action |
166 | * | |
167 | * @param array $params array or parameters determined by getfields | |
168 | */ | |
169 | function _civicrm_api3_case_delete_spec(&$params) { | |
170 | $params['id']['api.required'] = 1; | |
171 | } | |
172 | ||
6a488035 TO |
173 | /** |
174 | * Get details of a particular case, or search for cases, depending on params | |
175 | * | |
176 | * Please provide one (and only one) of the four get/search parameters: | |
177 | * | |
178 | * @param array( | |
179 | 'id' => if set, will get all available info about a case, including contacts and activities | |
180 | * | |
181 | * // if no case_id provided, this function will use one of the following search parameters: | |
182 | * 'client_id' => finds all cases with a specific client | |
183 | * 'activity_id' => returns the case containing a specific activity | |
184 | * 'contact_id' => finds all cases associated with a contact (in any role, not just client) | |
185 | * | |
186 | * {@getfields case_get} | |
187 | * | |
188 | * @return (get mode, case_id provided): Array with case details, case roles, case activity ids, (search mode, case_id not provided): Array of cases found | |
189 | * @access public | |
190 | * @todo Erik Hommel 16 dec 2010 check if all DB fields are returned | |
191 | */ | |
192 | function civicrm_api3_case_get($params) { | |
1e4d0859 TO |
193 | civicrm_api3_verify_mandatory($params, NULL, array( |
194 | array('case_id', 'contact_id', 'activity_id', 'contact_id') | |
195 | )); | |
196 | ||
6a488035 TO |
197 | $options = _civicrm_api3_get_options_from_params($params); |
198 | ||
199 | // Get by id | |
200 | $caseId = CRM_Utils_Array::value('id', $params); | |
201 | if ($caseId) { | |
202 | // Validate param | |
203 | if (!is_numeric($caseId)) { | |
204 | return civicrm_api3_create_error('Invalid parameter: case_id. Must provide a numeric value.'); | |
205 | } | |
206 | // For historic reasons we always return these when an id is provided | |
207 | $options['return'] = array('contacts' => 1, 'activities' => 1); | |
208 | $case = _civicrm_api3_case_read($caseId, $options); | |
209 | ||
210 | if ($case) { | |
211 | return civicrm_api3_create_success(array($caseId => $case), $params, 'case', 'get'); | |
212 | } | |
213 | else { | |
214 | return civicrm_api3_create_success(array(), $params, 'case', 'get'); | |
215 | } | |
216 | } | |
217 | ||
218 | //search by client | |
219 | if (!empty($params['contact_id'])) { | |
220 | $ids = array(); | |
221 | foreach ((array) $params['contact_id'] as $cid) { | |
222 | if (is_numeric($cid)) { | |
223 | $ids = array_merge($ids, CRM_Case_BAO_Case::retrieveCaseIdsByContactId($cid, TRUE)); | |
224 | } | |
225 | } | |
226 | $cases = array(); | |
227 | foreach ($ids as $id) { | |
228 | if ($case = _civicrm_api3_case_read($id, $options)) { | |
229 | $cases[$id] = $case; | |
230 | } | |
231 | } | |
232 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
233 | } | |
234 | ||
235 | //search by activity | |
236 | if (!empty($params['activity_id'])) { | |
237 | if (!is_numeric($params['activity_id'])) { | |
238 | return civicrm_api3_create_error('Invalid parameter: activity_id. Must provide a numeric value.'); | |
239 | } | |
240 | $caseId = CRM_Case_BAO_Case::getCaseIdByActivityId($params['activity_id']); | |
241 | if (!$caseId) { | |
242 | return civicrm_api3_create_success(array(), $params, 'case', 'get'); | |
243 | } | |
244 | $case = array($caseId => _civicrm_api3_case_read($caseId, $options)); | |
245 | return civicrm_api3_create_success($case, $params, 'case', 'get'); | |
246 | } | |
247 | ||
248 | //search by contacts | |
249 | if ($contact = CRM_Utils_Array::value('contact_id', $params)) { | |
250 | if (!is_numeric($contact)) { | |
251 | return civicrm_api3_create_error('Invalid parameter: contact_id. Must provide a numeric value.'); | |
252 | } | |
253 | ||
254 | $sql = " | |
255 | SELECT DISTINCT case_id | |
256 | FROM civicrm_relationship | |
257 | WHERE (contact_id_a = $contact | |
258 | OR contact_id_b = $contact) | |
259 | AND case_id IS NOT NULL"; | |
260 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
261 | ||
262 | $cases = array(); | |
263 | while ($dao->fetch()) { | |
264 | $cases[$dao->case_id] = _civicrm_api3_case_read($dao->case_id, $options); | |
265 | } | |
266 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
267 | } | |
6a488035 TO |
268 | } |
269 | ||
270 | /** | |
271 | * Deprecated. Use activity API instead | |
272 | */ | |
273 | function civicrm_api3_case_activity_create($params) { | |
274 | return civicrm_api3_activity_create($params); | |
275 | } | |
276 | ||
277 | /** | |
278 | * Update a specified case. | |
279 | * | |
280 | * @param array( | |
281 | //REQUIRED: | |
282 | * 'case_id' => int | |
283 | * | |
284 | * //OPTIONAL | |
285 | * 'status_id' => int | |
286 | * 'start_date' => str datestamp | |
287 | * 'contact_id' => int // case client | |
288 | * | |
289 | * @return Updated case | |
290 | * | |
291 | * @access public | |
292 | * | |
293 | */ | |
294 | function civicrm_api3_case_update($params) { | |
295 | //check parameters | |
296 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
297 | ||
298 | // return error if modifing creator id | |
299 | if (array_key_exists('creator_id', $params)) { | |
300 | return civicrm_api3_create_error(ts('You cannot update creator id')); | |
301 | } | |
302 | ||
303 | $mCaseId = array(); | |
304 | $origContactIds = array(); | |
305 | ||
306 | // get original contact id and creator id of case | |
307 | if (!empty($params['contact_id'])) { | |
308 | $origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['case_id']); | |
309 | $origContactId = $origContactIds[1]; | |
310 | } | |
311 | ||
312 | if (count($origContactIds) > 1) { | |
313 | // check valid orig contact id | |
314 | if (!empty($params['orig_contact_id']) && !in_array($params['orig_contact_id'], $origContactIds)) { | |
315 | return civicrm_api3_create_error('Invalid case contact id (orig_contact_id)'); | |
316 | } | |
317 | elseif (empty($params['orig_contact_id'])) { | |
318 | return civicrm_api3_create_error('Case is linked with more than one contact id. Provide the required params orig_contact_id to be replaced'); | |
319 | } | |
320 | $origContactId = $params['orig_contact_id']; | |
321 | } | |
322 | ||
323 | // check for same contact id for edit Client | |
324 | if (!empty($params['contact_id']) && !in_array($params['contact_id'], $origContactIds)) { | |
325 | $mCaseId = CRM_Case_BAO_Case::mergeCases($params['contact_id'], $params['case_id'], $origContactId, NULL, TRUE); | |
326 | } | |
327 | ||
328 | if (!empty($mCaseId[0])) { | |
329 | $params['case_id'] = $mCaseId[0]; | |
330 | } | |
331 | ||
332 | $dao = new CRM_Case_BAO_Case(); | |
333 | $dao->id = $params['id']; | |
334 | ||
335 | $dao->copyValues($params); | |
336 | $dao->save(); | |
337 | ||
338 | $case = array(); | |
339 | ||
340 | _civicrm_api3_object_to_array($dao, $case); | |
341 | ||
342 | return civicrm_api3_create_success($case, $params, 'case', 'update', $dao); | |
343 | } | |
344 | ||
345 | /** | |
346 | * Delete a specified case. | |
347 | * | |
348 | * @param array( | |
349 | //REQUIRED: | |
350 | * 'id' => int | |
351 | * | |
352 | * //OPTIONAL | |
353 | * 'move_to_trash' => bool (defaults to false) | |
354 | * | |
355 | * @return boolean: true if success, else false | |
356 | * {@getfields case_delete} | |
357 | * @access public | |
358 | */ | |
359 | function civicrm_api3_case_delete($params) { | |
360 | //check parameters | |
361 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
362 | ||
363 | if (CRM_Case_BAO_Case::deleteCase($params['id'], CRM_Utils_Array::value('move_to_trash', $params, FALSE))) { | |
364 | return civicrm_api3_create_success($params, $params, 'case', 'delete'); | |
365 | } | |
366 | else { | |
367 | return civicrm_api3_create_error('Could not delete case.'); | |
368 | } | |
369 | } | |
370 | ||
371 | /***********************************/ | |
372 | /* */ | |
373 | /* INTERNAL FUNCTIONS */ | |
374 | /* */ | |
375 | /***********************************/ | |
376 | ||
377 | /** | |
378 | * Internal function to retrieve a case. | |
379 | * | |
380 | * @param int $caseId | |
381 | * | |
382 | * @return array (reference) case object | |
383 | * | |
384 | */ | |
385 | function _civicrm_api3_case_read($caseId, $options) { | |
386 | $return = CRM_Utils_Array::value('return', $options, array()); | |
387 | $dao = new CRM_Case_BAO_Case(); | |
388 | $dao->id = $caseId; | |
389 | if ($dao->find(TRUE)) { | |
390 | $case = array(); | |
391 | _civicrm_api3_object_to_array($dao, $case); | |
392 | // Legacy support for client_id - TODO: in apiv4 remove 'client_id' | |
393 | $case['client_id'] = $case['contact_id'] = $dao->retrieveContactIdsByCaseId($caseId); | |
394 | ||
395 | //handle multi-value case type | |
396 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
397 | $case['case_type_id'] = trim(str_replace($sep, ',', $case['case_type_id']), ','); | |
398 | ||
399 | if (!empty($return['contacts'])) { | |
400 | //get case contacts | |
401 | $contacts = CRM_Case_BAO_Case::getcontactNames($caseId); | |
402 | $relations = CRM_Case_BAO_Case::getRelatedContacts($caseId); | |
403 | $case['contacts'] = array_merge($contacts, $relations); | |
404 | } | |
405 | if (!empty($return['activities'])) { | |
406 | //get case activities | |
407 | $case['activities'] = array(); | |
408 | $query = "SELECT activity_id FROM civicrm_case_activity WHERE case_id = $caseId"; | |
409 | $dao = CRM_Core_DAO::executeQuery($query); | |
410 | while ($dao->fetch()) { | |
411 | $case['activities'][] = $dao->activity_id; | |
412 | } | |
413 | } | |
414 | return $case; | |
415 | } | |
416 | } | |
417 | ||
418 | /** | |
419 | * Internal function to format create params for processing | |
420 | */ | |
421 | function _civicrm_api3_case_format_params(&$params) { | |
422 | if (!array_key_exists('creator_id', $params)) { | |
423 | $session = CRM_Core_Session::singleton(); | |
424 | $params['creator_id'] = $session->get('userID'); | |
425 | } | |
426 | if (empty($params['start_date'])) { | |
427 | $params['start_date'] = date('YmdHis'); | |
428 | } | |
429 | // figure out case type id from case type and vice-versa | |
430 | $caseTypes = CRM_Case_PseudoConstant::caseType('label', FALSE); | |
431 | if (empty($params['case_type_id'])) { | |
432 | $params['case_type_id'] = array_search($params['case_type'], $caseTypes); | |
433 | } | |
434 | elseif (empty($params['case_type'])) { | |
435 | $params['case_type'] = $caseTypes[$params['case_type_id']]; | |
436 | } | |
437 | // format input with value separators | |
438 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
439 | $params['case_type_id'] = $sep . implode($sep, (array) $params['case_type_id']) . $sep; | |
440 | } |