Merge pull request #19979 from civicrm/5.36
[civicrm-core.git] / api / v3 / CaseType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM Case.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Create or update case type.
20 *
21 * @param array $params
22 * Input parameters.
23 *
24 * @throws API_Exception
25 * @return array
26 * API result array
27 */
28 function civicrm_api3_case_type_create($params) {
29 civicrm_api3_verify_mandatory($params, _civicrm_api3_get_DAO(__FUNCTION__));
30
31 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'CaseType');
32 return _civicrm_api3_case_type_get_formatResult($result);
33 }
34
35 /**
36 * Retrieve case types.
37 *
38 * @param array $params
39 *
40 * @return array
41 * case types keyed by id
42 */
43 function civicrm_api3_case_type_get($params) {
44 if (!empty($params['options']) && !empty($params['options']['is_count'])) {
45 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
46 }
47 $caseTypes = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
48 // format case type, to fetch xml definition
49 $options = _civicrm_api3_get_options_from_params($params);
50 return _civicrm_api3_case_type_get_formatResult($caseTypes, $options);
51 }
52
53 /**
54 * Format definition.
55 *
56 * @param array $result
57 * @param array $options
58 *
59 * @return array
60 * @throws \CRM_Core_Exception
61 */
62 function _civicrm_api3_case_type_get_formatResult(&$result, $options = []) {
63 foreach ($result['values'] as $key => &$caseType) {
64 if (!empty($caseType['definition']) || empty($options['return']) || !empty($options['return']['definition'])) {
65 $caseType += ['definition' => NULL];
66 CRM_Case_BAO_CaseType::formatOutputDefinition($caseType['definition'], $caseType);
67 }
68 $caseType['is_forkable'] = CRM_Case_BAO_CaseType::isForkable($caseType['id']);
69 $caseType['is_forked'] = CRM_Case_BAO_CaseType::isForked($caseType['id']);
70 }
71 return $result;
72 }
73
74 /**
75 * Function to delete case type.
76 *
77 * @param array $params
78 * Array including id of CaseType to delete.
79 *
80 * @return array
81 * API result array
82 */
83 function civicrm_api3_case_type_delete($params) {
84 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
85 }