APIv4 - Fix bug when using relative date filters in ON clause of a join
[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 // Computed properties.
31 unset($params['is_forkable']);
32 unset($params['is_forked']);
33
34 if (!array_key_exists('is_active', $params) && empty($params['id'])) {
35 $params['is_active'] = TRUE;
36 }
37 // This is an existing case-type.
38 if (!empty($params['id']) && isset($params['definition'])
39 // which is not yet forked
40 && !CRM_Case_BAO_CaseType::isForked($params['id'])
41 // for which new forks are prohibited
42 && !CRM_Case_BAO_CaseType::isForkable($params['id'])
43 ) {
44 unset($params['definition']);
45 }
46 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'CaseType');
47 return _civicrm_api3_case_type_get_formatResult($result);
48 }
49
50 /**
51 * Retrieve case types.
52 *
53 * @param array $params
54 *
55 * @return array
56 * case types keyed by id
57 */
58 function civicrm_api3_case_type_get($params) {
59 if (!empty($params['options']) && !empty($params['options']['is_count'])) {
60 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
61 }
62 $caseTypes = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
63 // format case type, to fetch xml definition
64 $options = _civicrm_api3_get_options_from_params($params);
65 return _civicrm_api3_case_type_get_formatResult($caseTypes, $options);
66 }
67
68 /**
69 * Format definition.
70 *
71 * @param array $result
72 * @param array $options
73 *
74 * @return array
75 * @throws \CRM_Core_Exception
76 */
77 function _civicrm_api3_case_type_get_formatResult(&$result, $options = []) {
78 foreach ($result['values'] as $key => &$caseType) {
79 if (!empty($caseType['definition'])) {
80 list($xml) = CRM_Utils_XML::parseString($caseType['definition']);
81 $caseType['definition'] = $xml ? CRM_Case_BAO_CaseType::convertXmlToDefinition($xml) : [];
82 }
83 else {
84 if (empty($options['return']) || !empty($options['return']['definition'])) {
85 $caseTypeName = (isset($caseType['name'])) ? $caseType['name'] : CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $caseType['id'], 'name', 'id', TRUE);
86 $xml = CRM_Case_XMLRepository::singleton()->retrieve($caseTypeName);
87 $caseType['definition'] = $xml ? CRM_Case_BAO_CaseType::convertXmlToDefinition($xml) : [];
88 }
89 }
90 $caseType['is_forkable'] = CRM_Case_BAO_CaseType::isForkable($caseType['id']);
91 $caseType['is_forked'] = CRM_Case_BAO_CaseType::isForked($caseType['id']);
92 }
93 return $result;
94 }
95
96 /**
97 * Function to delete case type.
98 *
99 * @param array $params
100 * Array including id of CaseType to delete.
101 *
102 * @return array
103 * API result array
104 */
105 function civicrm_api3_case_type_delete($params) {
106 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
107 }