Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
731a0992 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
731a0992 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
731a0992 | 35 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
36 | * |
37 | */ | |
38 | ||
39 | ||
40 | /** | |
41 | * Open a new case, add client and manager roles, and add standard timeline | |
42 | * | |
784e85a1 EM |
43 | * @param array ( |
44 | * //REQUIRED: | |
6a488035 TO |
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 | * | |
784e85a1 EM |
59 | * @throws API_Exception |
60 | * @return array api result array | |
6a488035 TO |
61 | * |
62 | * @access public | |
63 | * {@getfields case_create} | |
64 | */ | |
65 | function civicrm_api3_case_create($params) { | |
66 | ||
67 | if (!empty($params['id'])) { | |
68 | return civicrm_api3_case_update($params); | |
69 | } | |
70 | ||
71 | civicrm_api3_verify_mandatory($params, NULL, array('contact_id', 'subject', array('case_type', 'case_type_id'))); | |
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 | /** |
6a488035 TO |
131 | * Adjust Metadata for Get Action |
132 | * | |
133 | * @param array $params array or parameters determined by getfields | |
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 | /** |
6a488035 TO |
141 | * Adjust Metadata for Create Action |
142 | * | |
143 | * @param array $params array or parameters determined by getfields | |
144 | */ | |
145 | function _civicrm_api3_case_create_spec(&$params) { | |
146 | $params['contact_id']['api.aliases'] = array('client_id'); | |
147 | $params['contact_id']['title'] = 'Case Client'; | |
148 | $params['contact_id']['api.required'] = 1; | |
149 | $params['status_id']['api.default'] = 1; | |
941feb14 EM |
150 | $params['creator_id']['api.default'] = 'user_contact_id'; |
151 | $params['creator_id']['type'] = CRM_Utils_Type::T_INT; | |
ca11b9da | 152 | $params['start_date']['api.default'] = 'now'; |
16c0ec8d CW |
153 | $params['medium_id'] = array( |
154 | 'name' => 'medium_id', | |
155 | 'title' => 'Activity Medium', | |
156 | ); | |
6a488035 TO |
157 | } |
158 | ||
11e09c59 | 159 | /** |
6a488035 TO |
160 | * Adjust Metadata for Update action |
161 | * | |
162 | * @param array $params array or parameters determined by getfields | |
163 | */ | |
164 | function _civicrm_api3_case_update_spec(&$params) { | |
165 | $params['id']['api.required'] = 1; | |
166 | } | |
167 | ||
11e09c59 | 168 | /** |
6a488035 TO |
169 | * Adjust Metadata for Delete action |
170 | * | |
171 | * @param array $params array or parameters determined by getfields | |
172 | */ | |
173 | function _civicrm_api3_case_delete_spec(&$params) { | |
174 | $params['id']['api.required'] = 1; | |
175 | } | |
176 | ||
6a488035 TO |
177 | /** |
178 | * Get details of a particular case, or search for cases, depending on params | |
179 | * | |
180 | * Please provide one (and only one) of the four get/search parameters: | |
181 | * | |
784e85a1 EM |
182 | * @param array ( |
183 | * 'id' => if set, will get all available info about a case, including contacts and activities | |
6a488035 TO |
184 | * |
185 | * // if no case_id provided, this function will use one of the following search parameters: | |
186 | * 'client_id' => finds all cases with a specific client | |
187 | * 'activity_id' => returns the case containing a specific activity | |
188 | * 'contact_id' => finds all cases associated with a contact (in any role, not just client) | |
189 | * | |
190 | * {@getfields case_get} | |
191 | * | |
784e85a1 | 192 | * @return array (get mode, case_id provided): Array with case details, case roles, case activity ids, (search mode, case_id not provided): Array of cases found@access public |
6a488035 TO |
193 | * @todo Erik Hommel 16 dec 2010 check if all DB fields are returned |
194 | */ | |
195 | function civicrm_api3_case_get($params) { | |
196 | $options = _civicrm_api3_get_options_from_params($params); | |
6a488035 TO |
197 | //search by client |
198 | if (!empty($params['contact_id'])) { | |
199 | $ids = array(); | |
200 | foreach ((array) $params['contact_id'] as $cid) { | |
201 | if (is_numeric($cid)) { | |
202 | $ids = array_merge($ids, CRM_Case_BAO_Case::retrieveCaseIdsByContactId($cid, TRUE)); | |
203 | } | |
204 | } | |
205 | $cases = array(); | |
206 | foreach ($ids as $id) { | |
207 | if ($case = _civicrm_api3_case_read($id, $options)) { | |
208 | $cases[$id] = $case; | |
209 | } | |
210 | } | |
211 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
212 | } | |
213 | ||
214 | //search by activity | |
215 | if (!empty($params['activity_id'])) { | |
216 | if (!is_numeric($params['activity_id'])) { | |
784e85a1 | 217 | throw new API_Exception('Invalid parameter: activity_id. Must provide a numeric value.'); |
6a488035 TO |
218 | } |
219 | $caseId = CRM_Case_BAO_Case::getCaseIdByActivityId($params['activity_id']); | |
220 | if (!$caseId) { | |
221 | return civicrm_api3_create_success(array(), $params, 'case', 'get'); | |
222 | } | |
223 | $case = array($caseId => _civicrm_api3_case_read($caseId, $options)); | |
224 | return civicrm_api3_create_success($case, $params, 'case', 'get'); | |
225 | } | |
226 | ||
227 | //search by contacts | |
228 | if ($contact = CRM_Utils_Array::value('contact_id', $params)) { | |
229 | if (!is_numeric($contact)) { | |
784e85a1 | 230 | throw new API_Exception('Invalid parameter: contact_id. Must provide a numeric value.'); |
6a488035 TO |
231 | } |
232 | ||
233 | $sql = " | |
234 | SELECT DISTINCT case_id | |
235 | FROM civicrm_relationship | |
236 | WHERE (contact_id_a = $contact | |
237 | OR contact_id_b = $contact) | |
238 | AND case_id IS NOT NULL"; | |
9af2925b | 239 | $dao = CRM_Core_DAO::executeQuery($sql); |
6a488035 TO |
240 | |
241 | $cases = array(); | |
242 | while ($dao->fetch()) { | |
243 | $cases[$dao->case_id] = _civicrm_api3_case_read($dao->case_id, $options); | |
244 | } | |
245 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
246 | } | |
f21557af | 247 | |
e01eccc0 RB |
248 | // For historic reasons we always return these when an id is provided |
249 | $caseId = CRM_Utils_Array::value('id', $params); | |
250 | if ($caseId) { | |
251 | $options['return'] = array('contacts' => 1, 'activities' => 1); | |
252 | } | |
253 | ||
f21557af | 254 | $foundcases = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Case'); |
255 | $cases = array(); | |
256 | foreach ($foundcases['values'] as $foundcase) { | |
257 | if ($case = _civicrm_api3_case_read($foundcase['id'], $options)) { | |
258 | $cases[$foundcase['id']] = $case; | |
259 | } | |
260 | } | |
261 | ||
262 | return civicrm_api3_create_success($cases, $params, 'case', 'get'); | |
6a488035 TO |
263 | } |
264 | ||
265 | /** | |
266 | * Deprecated. Use activity API instead | |
267 | */ | |
268 | function civicrm_api3_case_activity_create($params) { | |
269 | return civicrm_api3_activity_create($params); | |
270 | } | |
271 | ||
272 | /** | |
273 | * Update a specified case. | |
274 | * | |
784e85a1 EM |
275 | * @param array ( |
276 | * //REQUIRED: | |
6a488035 TO |
277 | * 'case_id' => int |
278 | * | |
279 | * //OPTIONAL | |
280 | * 'status_id' => int | |
281 | * 'start_date' => str datestamp | |
282 | * 'contact_id' => int // case client | |
283 | * | |
784e85a1 EM |
284 | * @throws API_Exception |
285 | * @return array api result array | |
6a488035 TO |
286 | * |
287 | * @access public | |
6a488035 TO |
288 | */ |
289 | function civicrm_api3_case_update($params) { | |
290 | //check parameters | |
291 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
292 | ||
784e85a1 | 293 | // return error if modifying creator id |
6a488035 | 294 | if (array_key_exists('creator_id', $params)) { |
784e85a1 | 295 | throw new API_Exception(ts('You cannot update creator id')); |
6a488035 TO |
296 | } |
297 | ||
784e85a1 | 298 | $mCaseId = $origContactIds = array(); |
6a488035 TO |
299 | |
300 | // get original contact id and creator id of case | |
301 | if (!empty($params['contact_id'])) { | |
c2e81506 | 302 | $origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['id']); |
6a488035 TO |
303 | $origContactId = $origContactIds[1]; |
304 | } | |
305 | ||
306 | if (count($origContactIds) > 1) { | |
307 | // check valid orig contact id | |
308 | if (!empty($params['orig_contact_id']) && !in_array($params['orig_contact_id'], $origContactIds)) { | |
784e85a1 | 309 | throw new API_Exception('Invalid case contact id (orig_contact_id)'); |
6a488035 TO |
310 | } |
311 | elseif (empty($params['orig_contact_id'])) { | |
784e85a1 | 312 | 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 |
313 | } |
314 | $origContactId = $params['orig_contact_id']; | |
315 | } | |
316 | ||
317 | // check for same contact id for edit Client | |
318 | if (!empty($params['contact_id']) && !in_array($params['contact_id'], $origContactIds)) { | |
319 | $mCaseId = CRM_Case_BAO_Case::mergeCases($params['contact_id'], $params['case_id'], $origContactId, NULL, TRUE); | |
320 | } | |
321 | ||
322 | if (!empty($mCaseId[0])) { | |
c2e81506 | 323 | $params['id'] = $mCaseId[0]; |
6a488035 TO |
324 | } |
325 | ||
326 | $dao = new CRM_Case_BAO_Case(); | |
327 | $dao->id = $params['id']; | |
328 | ||
329 | $dao->copyValues($params); | |
330 | $dao->save(); | |
331 | ||
332 | $case = array(); | |
333 | ||
334 | _civicrm_api3_object_to_array($dao, $case); | |
335 | ||
336 | return civicrm_api3_create_success($case, $params, 'case', 'update', $dao); | |
337 | } | |
338 | ||
339 | /** | |
340 | * Delete a specified case. | |
341 | * | |
342 | * @param array( | |
343 | //REQUIRED: | |
344 | * 'id' => int | |
345 | * | |
346 | * //OPTIONAL | |
347 | * 'move_to_trash' => bool (defaults to false) | |
348 | * | |
349 | * @return boolean: true if success, else false | |
350 | * {@getfields case_delete} | |
351 | * @access public | |
352 | */ | |
353 | function civicrm_api3_case_delete($params) { | |
354 | //check parameters | |
355 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
356 | ||
357 | if (CRM_Case_BAO_Case::deleteCase($params['id'], CRM_Utils_Array::value('move_to_trash', $params, FALSE))) { | |
358 | return civicrm_api3_create_success($params, $params, 'case', 'delete'); | |
359 | } | |
360 | else { | |
784e85a1 | 361 | throw new API_Exception('Could not delete case.'); |
6a488035 TO |
362 | } |
363 | } | |
364 | ||
365 | /***********************************/ | |
366 | /* */ | |
367 | /* INTERNAL FUNCTIONS */ | |
368 | /* */ | |
369 | /***********************************/ | |
370 | ||
371 | /** | |
372 | * Internal function to retrieve a case. | |
373 | * | |
374 | * @param int $caseId | |
375 | * | |
784e85a1 EM |
376 | * @param $options |
377 | * | |
378 | * @internal param $params | |
9af2925b EM |
379 | * |
380 | * @internal param $options | |
6a488035 | 381 | * |
9af2925b | 382 | * @return array (reference) case object |
6a488035 TO |
383 | */ |
384 | function _civicrm_api3_case_read($caseId, $options) { | |
385 | $return = CRM_Utils_Array::value('return', $options, array()); | |
386 | $dao = new CRM_Case_BAO_Case(); | |
387 | $dao->id = $caseId; | |
388 | if ($dao->find(TRUE)) { | |
389 | $case = array(); | |
390 | _civicrm_api3_object_to_array($dao, $case); | |
391 | // Legacy support for client_id - TODO: in apiv4 remove 'client_id' | |
392 | $case['client_id'] = $case['contact_id'] = $dao->retrieveContactIdsByCaseId($caseId); | |
393 | ||
6a488035 TO |
394 | if (!empty($return['contacts'])) { |
395 | //get case contacts | |
396 | $contacts = CRM_Case_BAO_Case::getcontactNames($caseId); | |
397 | $relations = CRM_Case_BAO_Case::getRelatedContacts($caseId); | |
398 | $case['contacts'] = array_merge($contacts, $relations); | |
399 | } | |
400 | if (!empty($return['activities'])) { | |
401 | //get case activities | |
402 | $case['activities'] = array(); | |
403 | $query = "SELECT activity_id FROM civicrm_case_activity WHERE case_id = $caseId"; | |
404 | $dao = CRM_Core_DAO::executeQuery($query); | |
405 | while ($dao->fetch()) { | |
406 | $case['activities'][] = $dao->activity_id; | |
407 | } | |
408 | } | |
409 | return $case; | |
410 | } | |
411 | } | |
412 | ||
413 | /** | |
414 | * Internal function to format create params for processing | |
415 | */ | |
416 | function _civicrm_api3_case_format_params(&$params) { | |
6a488035 | 417 | // figure out case type id from case type and vice-versa |
8ffdec17 | 418 | $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE); |
6a488035 TO |
419 | if (empty($params['case_type_id'])) { |
420 | $params['case_type_id'] = array_search($params['case_type'], $caseTypes); | |
421 | } | |
422 | elseif (empty($params['case_type'])) { | |
423 | $params['case_type'] = $caseTypes[$params['case_type_id']]; | |
424 | } | |
6a488035 | 425 | } |