Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
1f4ea726 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | /** | |
244bbdd8 | 37 | * Open a new case, add client and manager roles, and standard timeline. |
6a488035 | 38 | * |
6c552737 | 39 | * @param array $params |
37b8953e | 40 | * |
b081365f | 41 | * @code |
784e85a1 | 42 | * //REQUIRED: |
6a488035 TO |
43 | * 'case_type_id' => int OR |
44 | * 'case_type' => str (provide one or the other) | |
45 | * 'contact_id' => int // case client | |
46 | * 'subject' => str | |
47 | * | |
48 | * //OPTIONAL | |
49 | * 'medium_id' => int // see civicrm option values for possibilities | |
50 | * 'creator_id' => int // case manager, default to the logged in user | |
51 | * 'status_id' => int // defaults to 1 "ongoing" | |
52 | * 'location' => str | |
53 | * 'start_date' => str datestamp // defaults to: date('YmdHis') | |
54 | * 'duration' => int // in minutes | |
55 | * 'details' => str // html format | |
b081365f | 56 | * @endcode |
6a488035 | 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 | ||
211e2fca EM |
68 | civicrm_api3_verify_mandatory($params, NULL, array( |
69 | 'contact_id', | |
70 | 'subject', | |
71 | array('case_type', 'case_type_id')) | |
72 | ); | |
6a488035 TO |
73 | _civicrm_api3_case_format_params($params); |
74 | ||
75 | // If format_params didn't find what it was looking for, return error | |
76 | if (empty($params['case_type_id'])) { | |
784e85a1 | 77 | throw new API_Exception('Invalid case_type. No such case type exists.'); |
6a488035 TO |
78 | } |
79 | if (empty($params['case_type'])) { | |
784e85a1 | 80 | throw new API_Exception('Invalid case_type_id. No such case type exists.'); |
6a488035 TO |
81 | } |
82 | ||
83 | // Fixme: can we safely pass raw params to the BAO? | |
84 | $newParams = array( | |
85 | 'case_type_id' => $params['case_type_id'], | |
86 | 'creator_id' => $params['creator_id'], | |
87 | 'status_id' => $params['status_id'], | |
88 | 'start_date' => $params['start_date'], | |
89 | 'end_date' => CRM_Utils_Array::value('end_date', $params), | |
90 | 'subject' => $params['subject'], | |
91 | ); | |
92 | ||
93 | $caseBAO = CRM_Case_BAO_Case::create($newParams); | |
94 | ||
95 | if (!$caseBAO) { | |
784e85a1 | 96 | throw new API_Exception('Case not created. Please check input params.'); |
6a488035 TO |
97 | } |
98 | ||
99 | foreach ((array) $params['contact_id'] as $cid) { | |
100 | $contactParams = array('case_id' => $caseBAO->id, 'contact_id' => $cid); | |
b53bcc5d | 101 | CRM_Case_BAO_CaseContact::create($contactParams); |
6a488035 TO |
102 | } |
103 | ||
104 | // Initialize XML processor with $params | |
105 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
106 | $xmlProcessorParams = array( | |
107 | 'clientID' => $params['contact_id'], | |
108 | 'creatorID' => $params['creator_id'], | |
109 | 'standardTimeline' => 1, | |
110 | 'activityTypeName' => 'Open Case', | |
111 | 'caseID' => $caseBAO->id, | |
112 | 'subject' => $params['subject'], | |
113 | 'location' => CRM_Utils_Array::value('location', $params), | |
114 | 'activity_date_time' => $params['start_date'], | |
115 | 'duration' => CRM_Utils_Array::value('duration', $params), | |
116 | 'medium_id' => CRM_Utils_Array::value('medium_id', $params), | |
117 | 'details' => CRM_Utils_Array::value('details', $params), | |
118 | 'custom' => array(), | |
119 | ); | |
120 | ||
121 | // Do it! :-D | |
122 | $xmlProcessor->run($params['case_type'], $xmlProcessorParams); | |
123 | ||
124 | // return case | |
125 | $values = array(); | |
126 | _civicrm_api3_object_to_array($caseBAO, $values[$caseBAO->id]); | |
127 | ||
244bbdd8 | 128 | return civicrm_api3_create_success($values, $params, 'Case', 'create', $caseBAO); |
6a488035 TO |
129 | } |
130 | ||
11e09c59 | 131 | /** |
2fb1dd66 | 132 | * Adjust Metadata for Get Action. |
6a488035 | 133 | * |
cf470720 | 134 | * @param array $params |
2fb1dd66 | 135 | * Parameters determined by getfields. |
6a488035 TO |
136 | */ |
137 | function _civicrm_api3_case_get_spec(&$params) { | |
d142432b EM |
138 | $params['contact_id'] = array( |
139 | 'api.aliases' => array('client_id'), | |
140 | 'title' => 'Case Client', | |
48bdb3cd | 141 | 'description' => 'Contact id of one or more clients to retrieve cases for', |
d142432b | 142 | 'type' => CRM_Utils_Type::T_INT, |
48bdb3cd CW |
143 | ); |
144 | $params['activity_id'] = array( | |
145 | 'title' => 'Case Activity', | |
146 | 'description' => 'Id of an activity in the case', | |
147 | 'type' => CRM_Utils_Type::T_INT, | |
d142432b | 148 | ); |
9ef16723 CW |
149 | $params['tag_id'] = array( |
150 | 'title' => 'Tags', | |
151 | 'description' => 'Find activities with specified tags.', | |
152 | 'type' => 1, | |
153 | 'FKClassName' => 'CRM_Core_DAO_Tag', | |
154 | 'FKApiName' => 'Tag', | |
155 | 'supports_joins' => TRUE, | |
156 | ); | |
6a488035 TO |
157 | } |
158 | ||
11e09c59 | 159 | /** |
2fb1dd66 | 160 | * Adjust Metadata for Create Action. |
6a488035 | 161 | * |
cf470720 | 162 | * @param array $params |
b081365f | 163 | * Array of parameters determined by getfields. |
6a488035 TO |
164 | */ |
165 | function _civicrm_api3_case_create_spec(&$params) { | |
d142432b EM |
166 | $params['contact_id'] = array( |
167 | 'api.aliases' => array('client_id'), | |
168 | 'title' => 'Case Client', | |
48bdb3cd | 169 | 'description' => 'Contact id of case client(s)', |
d142432b EM |
170 | 'api.required' => 1, |
171 | 'type' => CRM_Utils_Type::T_INT, | |
172 | ); | |
6a488035 | 173 | $params['status_id']['api.default'] = 1; |
8ae90f85 | 174 | $params['status_id']['api.aliases'] = array('case_status'); |
941feb14 EM |
175 | $params['creator_id']['api.default'] = 'user_contact_id'; |
176 | $params['creator_id']['type'] = CRM_Utils_Type::T_INT; | |
b05e6d0d | 177 | $params['creator_id']['title'] = 'Case Created By'; |
ca11b9da | 178 | $params['start_date']['api.default'] = 'now'; |
16c0ec8d CW |
179 | $params['medium_id'] = array( |
180 | 'name' => 'medium_id', | |
181 | 'title' => 'Activity Medium', | |
d142432b | 182 | 'type' => CRM_Utils_Type::T_INT, |
16c0ec8d | 183 | ); |
6a488035 TO |
184 | } |
185 | ||
11e09c59 | 186 | /** |
2fb1dd66 | 187 | * Adjust Metadata for Update action. |
6a488035 | 188 | * |
cf470720 | 189 | * @param array $params |
b081365f | 190 | * Array of parameters determined by getfields. |
6a488035 TO |
191 | */ |
192 | function _civicrm_api3_case_update_spec(&$params) { | |
193 | $params['id']['api.required'] = 1; | |
194 | } | |
195 | ||
11e09c59 | 196 | /** |
c1a920f1 | 197 | * Adjust Metadata for Delete action. |
6a488035 | 198 | * |
cf470720 | 199 | * @param array $params |
b081365f | 200 | * Array of parameters determined by getfields. |
6a488035 TO |
201 | */ |
202 | function _civicrm_api3_case_delete_spec(&$params) { | |
203 | $params['id']['api.required'] = 1; | |
204 | } | |
205 | ||
6a488035 | 206 | /** |
211e2fca | 207 | * Get details of a particular case, or search for cases, depending on params. |
6a488035 TO |
208 | * |
209 | * Please provide one (and only one) of the four get/search parameters: | |
210 | * | |
6c552737 TO |
211 | * @param array $params |
212 | * 'id' => if set, will get all available info about a case, including contacts and activities | |
6a488035 | 213 | * |
6c552737 TO |
214 | * // if no case_id provided, this function will use one of the following search parameters: |
215 | * 'client_id' => finds all cases with a specific client | |
216 | * 'activity_id' => returns the case containing a specific activity | |
217 | * 'contact_id' => finds all cases associated with a contact (in any role, not just client) | |
09f6c512 CW |
218 | * $params CRM_Utils_SQL_Select $sql |
219 | * Other apis wishing to wrap & extend this one can pass in a $sql object with extra clauses | |
6a488035 | 220 | * |
77b97be7 | 221 | * @throws API_Exception |
a6c01b45 | 222 | * @return array |
72b3a70c | 223 | * (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 | 224 | */ |
09f6c512 | 225 | function civicrm_api3_case_get($params, $sql = NULL) { |
6a488035 | 226 | $options = _civicrm_api3_get_options_from_params($params); |
09f6c512 CW |
227 | if (!is_a($sql, 'CRM_Utils_SQL_Select')) { |
228 | $sql = CRM_Utils_SQL_Select::fragment(); | |
229 | } | |
48bdb3cd CW |
230 | |
231 | // Add clause to search by client | |
6a488035 | 232 | if (!empty($params['contact_id'])) { |
9808e205 CW |
233 | // Legacy support - this field historically supports a nonstandard format of array(1,2,3) as a synonym for array('IN' => array(1,2,3)) |
234 | if (is_array($params['contact_id'])) { | |
235 | $operator = CRM_Utils_Array::first(array_keys($params['contact_id'])); | |
236 | if (!in_array($operator, \CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { | |
237 | $params['contact_id'] = array('IN' => $params['contact_id']); | |
6a488035 TO |
238 | } |
239 | } | |
9808e205 CW |
240 | else { |
241 | $params['contact_id'] = array('=' => $params['contact_id']); | |
242 | } | |
243 | $clause = CRM_Core_DAO::createSQLFilter('contact_id', $params['contact_id']); | |
244 | $sql->where("a.id IN (SELECT case_id FROM civicrm_case_contact WHERE $clause)"); | |
6a488035 TO |
245 | } |
246 | ||
242220e2 CW |
247 | // Order by case contact (primary client) |
248 | // Ex: "contact_id", "contact_id.display_name", "contact_id.sort_name DESC". | |
249 | if (!empty($options['sort']) && strpos($options['sort'], 'contact_id') !== FALSE) { | |
250 | $sort = explode(', ', $options['sort']); | |
251 | $contactSort = NULL; | |
4c6cc364 | 252 | foreach ($sort as $index => &$sortString) { |
242220e2 CW |
253 | if (strpos($sortString, 'contact_id') === 0) { |
254 | $contactSort = $sortString; | |
4c6cc364 | 255 | $sortString = '(1)'; |
242220e2 CW |
256 | // Get sort field and direction |
257 | list($sortField, $dir) = array_pad(explode(' ', $contactSort), 2, 'ASC'); | |
258 | list(, $sortField) = array_pad(explode('.', $sortField), 2, 'id'); | |
259 | // Validate inputs | |
260 | if (!array_key_exists($sortField, CRM_Contact_DAO_Contact::fieldKeys()) || ($dir != 'ASC' && $dir != 'DESC')) { | |
261 | throw new API_Exception("Unknown field specified for sort. Cannot order by '$contactSort'"); | |
262 | } | |
4c6cc364 | 263 | $sql->orderBy("case_contact.$sortField $dir", NULL, $index); |
242220e2 CW |
264 | } |
265 | } | |
266 | // Remove contact sort params so the basic_get function doesn't see them | |
267 | $params['options']['sort'] = implode(', ', $sort); | |
268 | unset($params['option_sort'], $params['option.sort'], $params['sort']); | |
269 | // Add necessary joins to the first case client | |
270 | if ($contactSort) { | |
271 | $sql->join('ccc', 'LEFT JOIN (SELECT * FROM civicrm_case_contact WHERE id IN (SELECT MIN(id) FROM civicrm_case_contact GROUP BY case_id)) AS ccc ON ccc.case_id = a.id'); | |
7a51786d | 272 | $sql->join('case_contact', 'LEFT JOIN civicrm_contact AS case_contact ON ccc.contact_id = case_contact.id AND case_contact.is_deleted <> 1'); |
242220e2 CW |
273 | } |
274 | } | |
275 | ||
48bdb3cd | 276 | // Add clause to search by activity |
6a488035 | 277 | if (!empty($params['activity_id'])) { |
48bdb3cd | 278 | if (!CRM_Utils_Rule::positiveInteger($params['activity_id'])) { |
784e85a1 | 279 | throw new API_Exception('Invalid parameter: activity_id. Must provide a numeric value.'); |
6a488035 | 280 | } |
48bdb3cd CW |
281 | $activityId = $params['activity_id']; |
282 | $originalId = CRM_Core_DAO::getFieldValue('CRM_Activity_BAO_Activity', $activityId, 'original_id'); | |
283 | if ($originalId) { | |
284 | $activityId .= ',' . $originalId; | |
6a488035 | 285 | } |
48bdb3cd CW |
286 | $sql |
287 | ->join('civicrm_case_activity', 'INNER JOIN civicrm_case_activity ON civicrm_case_activity.case_id = a.id') | |
288 | ->where("civicrm_case_activity.activity_id IN ($activityId)"); | |
6a488035 | 289 | } |
f21557af | 290 | |
9ef16723 CW |
291 | // Clause to search by tag |
292 | if (!empty($params['tag_id'])) { | |
293 | $dummySpec = array(); | |
294 | _civicrm_api3_validate_integer($params, 'tag_id', $dummySpec, 'Case'); | |
295 | if (!is_array($params['tag_id'])) { | |
296 | $params['tag_id'] = array('=' => $params['tag_id']); | |
297 | } | |
298 | $clause = \CRM_Core_DAO::createSQLFilter('tag_id', $params['tag_id']); | |
299 | if ($clause) { | |
300 | $sql->where('a.id IN (SELECT entity_id FROM civicrm_entity_tag WHERE entity_table = "civicrm_case" AND !clause)', array('!clause' => $clause)); | |
301 | } | |
302 | } | |
303 | ||
304 | $cases = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), array('sequential' => 0) + $params, TRUE, 'Case', $sql); | |
a54ac083 | 305 | |
9ef16723 | 306 | if (empty($options['is_count']) && !empty($cases['values'])) { |
abd06efc | 307 | // For historic reasons we return these by default only when fetching a case by id |
10befc1f | 308 | if (!empty($params['id']) && is_numeric($params['id']) && empty($options['return'])) { |
abd06efc CW |
309 | $options['return'] = array( |
310 | 'contacts' => 1, | |
311 | 'activities' => 1, | |
312 | 'contact_id' => 1, | |
313 | ); | |
314 | } | |
e01eccc0 | 315 | |
9ef16723 CW |
316 | _civicrm_api3_case_read($cases['values'], $options); |
317 | ||
318 | // We disabled sequential to keep the list indexed for case_read(). Now add it back. | |
319 | if (!empty($params['sequential'])) { | |
320 | $cases['values'] = array_values($cases['values']); | |
abd06efc | 321 | } |
35671d00 | 322 | } |
f21557af | 323 | |
9ef16723 | 324 | return $cases; |
6a488035 TO |
325 | } |
326 | ||
327 | /** | |
35823763 EM |
328 | * Deprecated API. |
329 | * | |
330 | * Use activity API instead. | |
9657ccf2 EM |
331 | * |
332 | * @param array $params | |
333 | * | |
334 | * @throws API_Exception | |
335 | * @return array | |
6a488035 TO |
336 | */ |
337 | function civicrm_api3_case_activity_create($params) { | |
a14e9d08 CW |
338 | require_once "api/v3/Activity.php"; |
339 | return civicrm_api3_activity_create($params) + array( | |
340 | 'deprecated' => CRM_Utils_Array::value('activity_create', _civicrm_api3_case_deprecation()), | |
341 | ); | |
ae76ce5e CW |
342 | } |
343 | ||
344 | /** | |
345 | * Add a timeline to a case. | |
346 | * | |
347 | * @param array $params | |
348 | * | |
349 | * @throws API_Exception | |
350 | * @return array | |
351 | */ | |
352 | function civicrm_api3_case_addtimeline($params) { | |
353 | $caseType = CRM_Case_BAO_Case::getCaseType($params['case_id'], 'name'); | |
354 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
355 | $xmlProcessorParams = array( | |
356 | 'clientID' => CRM_Case_BAO_Case::getCaseClients($params['case_id']), | |
357 | 'creatorID' => $params['creator_id'], | |
358 | 'standardTimeline' => 0, | |
359 | 'activity_date_time' => $params['activity_date_time'], | |
360 | 'caseID' => $params['case_id'], | |
361 | 'caseType' => $caseType, | |
362 | 'activitySetName' => $params['timeline'], | |
363 | ); | |
364 | $xmlProcessor->run($caseType, $xmlProcessorParams); | |
365 | return civicrm_api3_create_success(); | |
366 | } | |
367 | ||
368 | /** | |
369 | * Adjust Metadata for addtimeline action. | |
370 | * | |
371 | * @param array $params | |
372 | * Array of parameters determined by getfields. | |
373 | */ | |
374 | function _civicrm_api3_case_addtimeline_spec(&$params) { | |
375 | $params['case_id'] = array( | |
376 | 'title' => 'Case ID', | |
377 | 'description' => 'Id of case to update', | |
378 | 'type' => CRM_Utils_Type::T_INT, | |
379 | 'api.required' => 1, | |
380 | ); | |
381 | $params['timeline'] = array( | |
382 | 'title' => 'Timeline', | |
383 | 'description' => 'Name of activity set', | |
384 | 'type' => CRM_Utils_Type::T_STRING, | |
385 | 'api.required' => 1, | |
386 | ); | |
387 | $params['activity_date_time'] = array( | |
388 | 'api.default' => 'now', | |
389 | 'title' => 'Activity date time', | |
390 | 'description' => 'Timeline start date', | |
391 | 'type' => CRM_Utils_Type::T_DATE, | |
392 | ); | |
393 | $params['creator_id'] = array( | |
394 | 'api.default' => 'user_contact_id', | |
395 | 'title' => 'Activity creator', | |
396 | 'description' => 'Contact id of timeline creator', | |
397 | 'type' => CRM_Utils_Type::T_INT, | |
398 | ); | |
a14e9d08 CW |
399 | } |
400 | ||
a6bc7218 CW |
401 | /** |
402 | * Merge 2 cases. | |
403 | * | |
404 | * @param array $params | |
405 | * | |
406 | * @throws API_Exception | |
407 | * @return array | |
408 | */ | |
409 | function civicrm_api3_case_merge($params) { | |
410 | $clients1 = CRM_Case_BAO_Case::getCaseClients($params['case_id_1']); | |
411 | $clients2 = CRM_Case_BAO_Case::getCaseClients($params['case_id_2']); | |
412 | CRM_Case_BAO_Case::mergeCases($clients1[0], $params['case_id_1'], $clients2[0], $params['case_id_2']); | |
413 | return civicrm_api3_create_success(); | |
414 | } | |
415 | ||
416 | /** | |
417 | * Adjust Metadata for merge action. | |
418 | * | |
419 | * @param array $params | |
420 | * Array of parameters determined by getfields. | |
421 | */ | |
422 | function _civicrm_api3_case_merge_spec(&$params) { | |
423 | $params['case_id_1'] = array( | |
424 | 'title' => 'Case ID 1', | |
425 | 'description' => 'Id of main case', | |
426 | 'type' => CRM_Utils_Type::T_INT, | |
427 | 'api.required' => 1, | |
428 | ); | |
429 | $params['case_id_2'] = array( | |
430 | 'title' => 'Case ID 2', | |
431 | 'description' => 'Id of second case', | |
432 | 'type' => CRM_Utils_Type::T_INT, | |
433 | 'api.required' => 1, | |
434 | ); | |
435 | } | |
436 | ||
a14e9d08 | 437 | /** |
35823763 EM |
438 | * Declare deprecated api functions. |
439 | * | |
a14e9d08 | 440 | * @deprecated api notice |
a6c01b45 | 441 | * @return array |
16b10e64 | 442 | * Array of deprecated actions |
a14e9d08 CW |
443 | */ |
444 | function _civicrm_api3_case_deprecation() { | |
445 | return array('activity_create' => 'Case api "activity_create" action is deprecated. Use the activity api instead.'); | |
6a488035 TO |
446 | } |
447 | ||
448 | /** | |
449 | * Update a specified case. | |
450 | * | |
6c552737 TO |
451 | * @param array $params |
452 | * //REQUIRED: | |
453 | * 'case_id' => int | |
6a488035 | 454 | * |
6c552737 TO |
455 | * //OPTIONAL |
456 | * 'status_id' => int | |
457 | * 'start_date' => str datestamp | |
458 | * 'contact_id' => int // case client | |
6a488035 | 459 | * |
784e85a1 | 460 | * @throws API_Exception |
a6c01b45 | 461 | * @return array |
72b3a70c | 462 | * api result array |
6a488035 TO |
463 | */ |
464 | function civicrm_api3_case_update($params) { | |
e720ce6e CB |
465 | if (!isset($params['case_id']) && isset($params['id'])) { |
466 | $params['case_id'] = $params['id']; | |
467 | } | |
468 | ||
6a488035 TO |
469 | //check parameters |
470 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
471 | ||
784e85a1 | 472 | // return error if modifying creator id |
6a488035 | 473 | if (array_key_exists('creator_id', $params)) { |
784e85a1 | 474 | throw new API_Exception(ts('You cannot update creator id')); |
6a488035 TO |
475 | } |
476 | ||
784e85a1 | 477 | $mCaseId = $origContactIds = array(); |
6a488035 TO |
478 | |
479 | // get original contact id and creator id of case | |
480 | if (!empty($params['contact_id'])) { | |
c2e81506 | 481 | $origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['id']); |
6a488035 TO |
482 | $origContactId = $origContactIds[1]; |
483 | } | |
484 | ||
485 | if (count($origContactIds) > 1) { | |
486 | // check valid orig contact id | |
487 | if (!empty($params['orig_contact_id']) && !in_array($params['orig_contact_id'], $origContactIds)) { | |
784e85a1 | 488 | throw new API_Exception('Invalid case contact id (orig_contact_id)'); |
6a488035 TO |
489 | } |
490 | elseif (empty($params['orig_contact_id'])) { | |
784e85a1 | 491 | 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 |
492 | } |
493 | $origContactId = $params['orig_contact_id']; | |
494 | } | |
495 | ||
496 | // check for same contact id for edit Client | |
497 | if (!empty($params['contact_id']) && !in_array($params['contact_id'], $origContactIds)) { | |
498 | $mCaseId = CRM_Case_BAO_Case::mergeCases($params['contact_id'], $params['case_id'], $origContactId, NULL, TRUE); | |
499 | } | |
500 | ||
501 | if (!empty($mCaseId[0])) { | |
c2e81506 | 502 | $params['id'] = $mCaseId[0]; |
6a488035 TO |
503 | } |
504 | ||
505 | $dao = new CRM_Case_BAO_Case(); | |
506 | $dao->id = $params['id']; | |
507 | ||
508 | $dao->copyValues($params); | |
509 | $dao->save(); | |
510 | ||
511 | $case = array(); | |
6a488035 TO |
512 | _civicrm_api3_object_to_array($dao, $case); |
513 | ||
a54ac083 | 514 | return civicrm_api3_create_success(array($dao->id => $case), $params, 'Case', 'update', $dao); |
6a488035 TO |
515 | } |
516 | ||
517 | /** | |
518 | * Delete a specified case. | |
519 | * | |
6c552737 | 520 | * @param array $params |
37b8953e | 521 | * |
244bbdd8 | 522 | * @code |
6c552737 TO |
523 | * //REQUIRED: |
524 | * 'id' => int | |
6a488035 | 525 | * |
6c552737 TO |
526 | * //OPTIONAL |
527 | * 'move_to_trash' => bool (defaults to false) | |
244bbdd8 | 528 | * @endcode |
6a488035 | 529 | * |
77b97be7 | 530 | * @throws API_Exception |
8572e6de | 531 | * @return mixed |
6a488035 TO |
532 | */ |
533 | function civicrm_api3_case_delete($params) { | |
534 | //check parameters | |
535 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
536 | ||
537 | if (CRM_Case_BAO_Case::deleteCase($params['id'], CRM_Utils_Array::value('move_to_trash', $params, FALSE))) { | |
244bbdd8 | 538 | return civicrm_api3_create_success($params, $params, 'Case', 'delete'); |
6a488035 TO |
539 | } |
540 | else { | |
784e85a1 | 541 | throw new API_Exception('Could not delete case.'); |
6a488035 TO |
542 | } |
543 | } | |
544 | ||
8572e6de CW |
545 | /** |
546 | * Case.restore API specification | |
547 | * | |
548 | * @param array $spec description of fields supported by this API call | |
549 | * @return void | |
550 | */ | |
551 | function _civicrm_api3_case_restore_spec(&$spec) { | |
552 | $result = civicrm_api3('Case', 'getfields', array('api_action' => 'delete')); | |
553 | $spec = array('id' => $result['values']['id']); | |
554 | } | |
555 | ||
556 | /** | |
557 | * Restore a specified case from the trash. | |
558 | * | |
559 | * @param array $params | |
560 | * @throws API_Exception | |
561 | * @return mixed | |
562 | */ | |
563 | function civicrm_api3_case_restore($params) { | |
564 | if (CRM_Case_BAO_Case::restoreCase($params['id'])) { | |
565 | return civicrm_api3_create_success($params, $params, 'Case', 'restore'); | |
566 | } | |
567 | else { | |
568 | throw new API_Exception('Could not restore case.'); | |
569 | } | |
570 | } | |
571 | ||
6a488035 | 572 | /** |
9ef16723 | 573 | * Augment case results with extra data. |
6a488035 | 574 | * |
9ef16723 | 575 | * @param array $cases |
e9ff5391 | 576 | * @param array $options |
6a488035 | 577 | */ |
9ef16723 CW |
578 | function _civicrm_api3_case_read(&$cases, $options) { |
579 | foreach ($cases as &$case) { | |
580 | if (empty($options['return']) || !empty($options['return']['contact_id'])) { | |
581 | // Legacy support for client_id - TODO: in apiv4 remove 'client_id' | |
582 | $case['client_id'] = $case['contact_id'] = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($case['id']); | |
583 | } | |
584 | if (!empty($options['return']['contacts'])) { | |
585 | //get case contacts | |
586 | $contacts = CRM_Case_BAO_Case::getcontactNames($case['id']); | |
587 | $relations = CRM_Case_BAO_Case::getRelatedContacts($case['id']); | |
588 | $case['contacts'] = array_unique(array_merge($contacts, $relations), SORT_REGULAR); | |
589 | } | |
590 | if (!empty($options['return']['activities'])) { | |
591 | // add case activities array - we'll populate them in bulk below | |
592 | $case['activities'] = array(); | |
593 | } | |
594 | // Properly render this joined field | |
595 | if (!empty($options['return']['case_type_id.definition'])) { | |
596 | if (!empty($case['case_type_id.definition'])) { | |
597 | list($xml) = CRM_Utils_XML::parseString($case['case_type_id.definition']); | |
598 | } | |
599 | else { | |
600 | $caseTypeId = !empty($case['case_type_id']) ? $case['case_type_id'] : CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $case['id'], 'case_type_id'); | |
601 | $caseTypeName = !empty($case['case_type_id.name']) ? $case['case_type_id.name'] : CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $caseTypeId, 'name'); | |
602 | $xml = CRM_Case_XMLRepository::singleton()->retrieve($caseTypeName); | |
603 | } | |
604 | $case['case_type_id.definition'] = array(); | |
605 | if ($xml) { | |
606 | $case['case_type_id.definition'] = CRM_Case_BAO_CaseType::convertXmlToDefinition($xml); | |
607 | } | |
608 | } | |
a54ac083 | 609 | } |
9ef16723 | 610 | // Bulk-load activities |
a54ac083 | 611 | if (!empty($options['return']['activities'])) { |
9ef16723 CW |
612 | $query = "SELECT case_id, activity_id FROM civicrm_case_activity WHERE case_id IN (%1)"; |
613 | $params = array(1 => array(implode(',', array_keys($cases)), 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES)); | |
614 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
a54ac083 | 615 | while ($dao->fetch()) { |
9ef16723 | 616 | $cases[$dao->case_id]['activities'][] = $dao->activity_id; |
6a488035 | 617 | } |
6a488035 | 618 | } |
9ef16723 CW |
619 | // Bulk-load tags. Supports joins onto the tag entity. |
620 | $tagGet = array('tag_id', 'entity_id'); | |
621 | foreach (array_keys($options['return']) as $key) { | |
622 | if (strpos($key, 'tag_id.') === 0) { | |
623 | $tagGet[] = $key; | |
624 | $options['return']['tag_id'] = 1; | |
69f9c562 | 625 | } |
9ef16723 CW |
626 | } |
627 | if (!empty($options['return']['tag_id'])) { | |
628 | $tags = civicrm_api3('EntityTag', 'get', array( | |
629 | 'entity_table' => 'civicrm_case', | |
630 | 'entity_id' => array('IN' => array_keys($cases)), | |
631 | 'return' => $tagGet, | |
632 | 'options' => array('limit' => 0), | |
633 | )); | |
634 | foreach ($tags['values'] as $tag) { | |
635 | $key = (int) $tag['entity_id']; | |
636 | unset($tag['entity_id'], $tag['id']); | |
637 | $cases[$key]['tag_id'][$tag['tag_id']] = $tag; | |
69f9c562 CW |
638 | } |
639 | } | |
6a488035 TO |
640 | } |
641 | ||
642 | /** | |
61fe4988 | 643 | * Internal function to format create params for processing. |
9657ccf2 EM |
644 | * |
645 | * @param array $params | |
6a488035 TO |
646 | */ |
647 | function _civicrm_api3_case_format_params(&$params) { | |
6a488035 | 648 | // figure out case type id from case type and vice-versa |
1be6caec | 649 | $caseTypes = CRM_Case_PseudoConstant::caseType('name', FALSE); |
6a488035 TO |
650 | if (empty($params['case_type_id'])) { |
651 | $params['case_type_id'] = array_search($params['case_type'], $caseTypes); | |
3f25e694 TO |
652 | |
653 | // DEPRECATED: lookup by label for backward compatibility | |
654 | if (!$params['case_type_id']) { | |
655 | $caseTypeLabels = CRM_Case_PseudoConstant::caseType('title', FALSE); | |
656 | $params['case_type_id'] = array_search($params['case_type'], $caseTypeLabels); | |
61fe4988 | 657 | $params['case_type'] = $caseTypes[$params['case_type_id']]; |
3f25e694 | 658 | } |
6a488035 TO |
659 | } |
660 | elseif (empty($params['case_type'])) { | |
661 | $params['case_type'] = $caseTypes[$params['case_type_id']]; | |
662 | } | |
6a488035 | 663 | } |
ff9340a4 CW |
664 | |
665 | /** | |
666 | * It actually works a lot better to use the CaseContact api instead of the Case api | |
667 | * for entityRef fields so we can perform the necessary joins, | |
668 | * so we pass off getlist requests to the CaseContact api. | |
669 | * | |
670 | * @param array $params | |
671 | * @return mixed | |
672 | */ | |
673 | function civicrm_api3_case_getList($params) { | |
674 | require_once 'api/v3/Generic/Getlist.php'; | |
675 | require_once 'api/v3/CaseContact.php'; | |
09d55aa3 | 676 | //CRM:19956 - Assign case_id param if both id and case_id is passed to retrieve the case |
677 | if (!empty($params['id']) && !empty($params['params']) && !empty($params['params']['case_id'])) { | |
678 | $params['params']['case_id'] = array('IN' => $params['id']); | |
679 | unset($params['id']); | |
680 | } | |
ff9340a4 CW |
681 | $params['id_field'] = 'case_id'; |
682 | $params['label_field'] = $params['search_field'] = 'contact_id.sort_name'; | |
683 | $params['description_field'] = array( | |
684 | 'case_id', | |
685 | 'case_id.case_type_id.title', | |
686 | 'case_id.subject', | |
687 | 'case_id.status_id', | |
688 | 'case_id.start_date', | |
689 | ); | |
690 | $apiRequest = array( | |
09d55aa3 | 691 | 'version' => 3, |
ff9340a4 CW |
692 | 'entity' => 'CaseContact', |
693 | 'action' => 'getlist', | |
694 | 'params' => $params, | |
695 | ); | |
696 | return civicrm_api3_generic_getList($apiRequest); | |
697 | } | |
698 | ||
699 | /** | |
700 | * Needed due to the above override | |
701 | * @param $params | |
702 | * @param $apiRequest | |
703 | */ | |
704 | function _civicrm_api3_case_getlist_spec(&$params, $apiRequest) { | |
705 | require_once 'api/v3/Generic/Getlist.php'; | |
706 | _civicrm_api3_generic_getlist_spec($params, $apiRequest); | |
707 | } |