INFRA-132 - Fix comment spacing
[civicrm-core.git] / api / v3 / Case.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
39de6fd5 5 | CiviCRM version 4.6 |
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 *
cf470720 43 * @param array (
784e85a1 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 */
65function 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 *
cf470720
TO
133 * @param array $params
134 * Array or parameters determined by getfields.
6a488035
TO
135 */
136function _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/**
6a488035
TO
142 * Adjust Metadata for Create Action
143 *
cf470720
TO
144 * @param array $params
145 * Array or parameters determined by getfields.
6a488035
TO
146 */
147function _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/**
6a488035
TO
164 * Adjust Metadata for Update action
165 *
cf470720
TO
166 * @param array $params
167 * Array or parameters determined by getfields.
6a488035
TO
168 */
169function _civicrm_api3_case_update_spec(&$params) {
170 $params['id']['api.required'] = 1;
171}
172
11e09c59 173/**
6a488035
TO
174 * Adjust Metadata for Delete action
175 *
cf470720
TO
176 * @param array $params
177 * Array or parameters determined by getfields.
6a488035
TO
178 */
179function _civicrm_api3_case_delete_spec(&$params) {
180 $params['id']['api.required'] = 1;
181}
182
6a488035
TO
183/**
184 * Get details of a particular case, or search for cases, depending on params
185 *
186 * Please provide one (and only one) of the four get/search parameters:
187 *
784e85a1
EM
188 * @param array (
189 * 'id' => if set, will get all available info about a case, including contacts and activities
6a488035
TO
190 *
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)
195 *
196 * {@getfields case_get}
197 *
77b97be7 198 * @throws API_Exception
784e85a1 199 * @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
200 * @todo Erik Hommel 16 dec 2010 check if all DB fields are returned
201 */
202function civicrm_api3_case_get($params) {
203 $options = _civicrm_api3_get_options_from_params($params);
6a488035
TO
204 //search by client
205 if (!empty($params['contact_id'])) {
206 $ids = array();
207 foreach ((array) $params['contact_id'] as $cid) {
208 if (is_numeric($cid)) {
209 $ids = array_merge($ids, CRM_Case_BAO_Case::retrieveCaseIdsByContactId($cid, TRUE));
210 }
211 }
212 $cases = array();
213 foreach ($ids as $id) {
214 if ($case = _civicrm_api3_case_read($id, $options)) {
215 $cases[$id] = $case;
216 }
217 }
218 return civicrm_api3_create_success($cases, $params, 'case', 'get');
219 }
220
221 //search by activity
222 if (!empty($params['activity_id'])) {
223 if (!is_numeric($params['activity_id'])) {
784e85a1 224 throw new API_Exception('Invalid parameter: activity_id. Must provide a numeric value.');
6a488035
TO
225 }
226 $caseId = CRM_Case_BAO_Case::getCaseIdByActivityId($params['activity_id']);
227 if (!$caseId) {
228 return civicrm_api3_create_success(array(), $params, 'case', 'get');
229 }
230 $case = array($caseId => _civicrm_api3_case_read($caseId, $options));
231 return civicrm_api3_create_success($case, $params, 'case', 'get');
232 }
233
234 //search by contacts
bf38705f 235 if (($contact = CRM_Utils_Array::value('contact_id', $params)) != FALSE) {
6a488035 236 if (!is_numeric($contact)) {
784e85a1 237 throw new API_Exception('Invalid parameter: contact_id. Must provide a numeric value.');
6a488035
TO
238 }
239
240 $sql = "
241SELECT DISTINCT case_id
242 FROM civicrm_relationship
243 WHERE (contact_id_a = $contact
244 OR contact_id_b = $contact)
245 AND case_id IS NOT NULL";
9af2925b 246 $dao = CRM_Core_DAO::executeQuery($sql);
6a488035
TO
247
248 $cases = array();
249 while ($dao->fetch()) {
250 $cases[$dao->case_id] = _civicrm_api3_case_read($dao->case_id, $options);
251 }
252 return civicrm_api3_create_success($cases, $params, 'case', 'get');
253 }
f21557af 254
e01eccc0
RB
255 // For historic reasons we always return these when an id is provided
256 $caseId = CRM_Utils_Array::value('id', $params);
257 if ($caseId) {
258 $options['return'] = array('contacts' => 1, 'activities' => 1);
259 }
260
35671d00 261 $foundcases = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Case');
f21557af 262 $cases = array();
263 foreach ($foundcases['values'] as $foundcase) {
35671d00
TO
264 if ($case = _civicrm_api3_case_read($foundcase['id'], $options)) {
265 $cases[$foundcase['id']] = $case;
f21557af 266 }
35671d00 267 }
f21557af 268
269 return civicrm_api3_create_success($cases, $params, 'case', 'get');
6a488035
TO
270}
271
272/**
273 * Deprecated. Use activity API instead
9657ccf2
EM
274 *
275 * @param array $params
276 *
277 * @throws API_Exception
278 * @return array
6a488035
TO
279 */
280function 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/**
288 * @deprecated api notice
289 * @return array of deprecated actions
290 */
291function _civicrm_api3_case_deprecation() {
292 return array('activity_create' => 'Case api "activity_create" action is deprecated. Use the activity api instead.');
6a488035
TO
293}
294
295/**
296 * Update a specified case.
297 *
cf470720 298 * @param array (
784e85a1 299 * //REQUIRED:
6a488035
TO
300 * 'case_id' => int
301 *
302 * //OPTIONAL
303 * 'status_id' => int
304 * 'start_date' => str datestamp
305 * 'contact_id' => int // case client
306 *
784e85a1
EM
307 * @throws API_Exception
308 * @return array api result array
6a488035
TO
309 *
310 * @access public
6a488035
TO
311 */
312function civicrm_api3_case_update($params) {
313 //check parameters
314 civicrm_api3_verify_mandatory($params, NULL, array('id'));
315
784e85a1 316 // return error if modifying creator id
6a488035 317 if (array_key_exists('creator_id', $params)) {
784e85a1 318 throw new API_Exception(ts('You cannot update creator id'));
6a488035
TO
319 }
320
784e85a1 321 $mCaseId = $origContactIds = array();
6a488035
TO
322
323 // get original contact id and creator id of case
324 if (!empty($params['contact_id'])) {
c2e81506 325 $origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['id']);
6a488035
TO
326 $origContactId = $origContactIds[1];
327 }
328
329 if (count($origContactIds) > 1) {
330 // check valid orig contact id
331 if (!empty($params['orig_contact_id']) && !in_array($params['orig_contact_id'], $origContactIds)) {
784e85a1 332 throw new API_Exception('Invalid case contact id (orig_contact_id)');
6a488035
TO
333 }
334 elseif (empty($params['orig_contact_id'])) {
784e85a1 335 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
336 }
337 $origContactId = $params['orig_contact_id'];
338 }
339
340 // check for same contact id for edit Client
341 if (!empty($params['contact_id']) && !in_array($params['contact_id'], $origContactIds)) {
342 $mCaseId = CRM_Case_BAO_Case::mergeCases($params['contact_id'], $params['case_id'], $origContactId, NULL, TRUE);
343 }
344
345 if (!empty($mCaseId[0])) {
c2e81506 346 $params['id'] = $mCaseId[0];
6a488035
TO
347 }
348
349 $dao = new CRM_Case_BAO_Case();
350 $dao->id = $params['id'];
351
352 $dao->copyValues($params);
353 $dao->save();
354
355 $case = array();
356
357 _civicrm_api3_object_to_array($dao, $case);
8c4de140 358 $values[$dao->id] = $case;
6a488035 359
8c4de140 360 return civicrm_api3_create_success($values, $params, 'case', 'update', $dao);
6a488035
TO
361}
362
363/**
364 * Delete a specified case.
365 *
cf470720 366 * @param array (
77b97be7 367 * //REQUIRED:
6a488035
TO
368 * 'id' => int
369 *
370 * //OPTIONAL
371 * 'move_to_trash' => bool (defaults to false)
372 *
77b97be7 373 * @throws API_Exception
6a488035
TO
374 * @return boolean: true if success, else false
375 * {@getfields case_delete}
376 * @access public
377 */
378function civicrm_api3_case_delete($params) {
379 //check parameters
380 civicrm_api3_verify_mandatory($params, NULL, array('id'));
381
382 if (CRM_Case_BAO_Case::deleteCase($params['id'], CRM_Utils_Array::value('move_to_trash', $params, FALSE))) {
383 return civicrm_api3_create_success($params, $params, 'case', 'delete');
384 }
385 else {
784e85a1 386 throw new API_Exception('Could not delete case.');
6a488035
TO
387 }
388}
389
390/***********************************/
391/* */
392/* INTERNAL FUNCTIONS */
393/* */
394/***********************************/
395
396/**
397 * Internal function to retrieve a case.
398 *
399 * @param int $caseId
400 *
784e85a1
EM
401 * @param $options
402 *
c490a46a 403 * @return array case object
6a488035
TO
404 */
405function _civicrm_api3_case_read($caseId, $options) {
406 $return = CRM_Utils_Array::value('return', $options, array());
407 $dao = new CRM_Case_BAO_Case();
408 $dao->id = $caseId;
409 if ($dao->find(TRUE)) {
410 $case = array();
411 _civicrm_api3_object_to_array($dao, $case);
412 // Legacy support for client_id - TODO: in apiv4 remove 'client_id'
413 $case['client_id'] = $case['contact_id'] = $dao->retrieveContactIdsByCaseId($caseId);
414
6a488035
TO
415 if (!empty($return['contacts'])) {
416 //get case contacts
417 $contacts = CRM_Case_BAO_Case::getcontactNames($caseId);
418 $relations = CRM_Case_BAO_Case::getRelatedContacts($caseId);
419 $case['contacts'] = array_merge($contacts, $relations);
420 }
421 if (!empty($return['activities'])) {
422 //get case activities
423 $case['activities'] = array();
424 $query = "SELECT activity_id FROM civicrm_case_activity WHERE case_id = $caseId";
425 $dao = CRM_Core_DAO::executeQuery($query);
426 while ($dao->fetch()) {
427 $case['activities'][] = $dao->activity_id;
428 }
429 }
430 return $case;
431 }
432}
433
434/**
435 * Internal function to format create params for processing
9657ccf2
EM
436 *
437 * @param array $params
6a488035
TO
438 */
439function _civicrm_api3_case_format_params(&$params) {
6a488035 440 // figure out case type id from case type and vice-versa
1be6caec 441 $caseTypes = CRM_Case_PseudoConstant::caseType('name', FALSE);
6a488035
TO
442 if (empty($params['case_type_id'])) {
443 $params['case_type_id'] = array_search($params['case_type'], $caseTypes);
3f25e694
TO
444
445 // DEPRECATED: lookup by label for backward compatibility
446 if (!$params['case_type_id']) {
447 $caseTypeLabels = CRM_Case_PseudoConstant::caseType('title', FALSE);
448 $params['case_type_id'] = array_search($params['case_type'], $caseTypeLabels);
449 $params['case_type'] = $caseTypes[$params['case_type_id']]; // label => name
450 }
6a488035
TO
451 }
452 elseif (empty($params['case_type'])) {
453 $params['case_type'] = $caseTypes[$params['case_type_id']];
454 }
6a488035 455}