Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /** | |
b081365f | 4 | * @file CiviCRM APIv3 API wrapper. |
6a488035 TO |
5 | * |
6 | * @package CiviCRM_APIv3 | |
6a488035 TO |
7 | */ |
8 | ||
11e09c59 | 9 | /** |
9d32e6f7 EM |
10 | * CiviCRM API wrapper function. |
11 | * | |
6a488035 TO |
12 | * @param string $entity |
13 | * type of entities to deal with | |
14 | * @param string $action | |
15 | * create, get, delete or some special action name. | |
16 | * @param array $params | |
17 | * array to be passed to function | |
8904518f | 18 | * @param null $extra |
19 | * | |
20 | * @return array|int | |
6a488035 TO |
21 | */ |
22 | function civicrm_api($entity, $action, $params, $extra = NULL) { | |
048222df | 23 | return \Civi::service('civi_api_kernel')->run($entity, $action, $params, $extra); |
6a488035 TO |
24 | } |
25 | ||
6b359437 | 26 | /** |
9d32e6f7 EM |
27 | * Version 3 wrapper for civicrm_api. |
28 | * | |
29 | * Throws exception. | |
8904518f | 30 | * |
cf470720 TO |
31 | * @param string $entity |
32 | * Type of entities to deal with. | |
33 | * @param string $action | |
34 | * Create, get, delete or some special action name. | |
35 | * @param array $params | |
36 | * Array to be passed to function. | |
6b359437 | 37 | * |
8904518f | 38 | * @throws CiviCRM_API3_Exception |
6b359437 | 39 | * @return array |
6b359437 | 40 | */ |
67744c4e | 41 | function civicrm_api3($entity, $action, $params = array()) { |
6b359437 | 42 | $params['version'] = 3; |
43 | $result = civicrm_api($entity, $action, $params); | |
9b873358 | 44 | if (is_array($result) && !empty($result['is_error'])) { |
6b359437 | 45 | throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result); |
46 | } | |
c4ac4df4 | 47 | return $result; |
6b359437 | 48 | } |
6a488035 | 49 | |
94359f7e | 50 | /** |
dc64d047 EM |
51 | * Call getfields from api wrapper. |
52 | * | |
53 | * This function ensures that settings that | |
7cdbcb16 TO |
54 | * could alter getfields output (e.g. action for all api & profile_id for |
55 | * profile api ) are consistently passed in. | |
94359f7e | 56 | * |
7cdbcb16 TO |
57 | * We check whether the api call is 'getfields' because if getfields is |
58 | * being called we return an empty array as no alias swapping, validation or | |
59 | * default filling is done on getfields & we want to avoid a loop | |
94359f7e | 60 | * |
61 | * @todo other output modifiers include contact_type | |
62 | * | |
63 | * @param array $apiRequest | |
dc64d047 | 64 | * |
a6c01b45 | 65 | * @return array |
e94b9071 | 66 | * getfields output |
94359f7e | 67 | */ |
68 | function _civicrm_api3_api_getfields(&$apiRequest) { | |
69 | if (strtolower($apiRequest['action'] == 'getfields')) { | |
70 | // the main param getfields takes is 'action' - however this param is not compatible with REST | |
71 | // so we accept 'api_action' as an alias of action on getfields | |
a7488080 | 72 | if (!empty($apiRequest['params']['api_action'])) { |
35671d00 TO |
73 | // $apiRequest['params']['action'] = $apiRequest['params']['api_action']; |
74 | // unset($apiRequest['params']['api_action']); | |
94359f7e | 75 | } |
de0a1900 | 76 | return array('action' => array('api.aliases' => array('api_action'))); |
94359f7e | 77 | } |
78 | $getFieldsParams = array('action' => $apiRequest['action']); | |
79 | $entity = $apiRequest['entity']; | |
1644b908 | 80 | if ($entity == 'Profile' && array_key_exists('profile_id', $apiRequest['params'])) { |
94359f7e | 81 | $getFieldsParams['profile_id'] = $apiRequest['params']['profile_id']; |
82 | } | |
83 | $fields = civicrm_api3($entity, 'getfields', $getFieldsParams); | |
84 | return $fields['values']; | |
85 | } | |
8904518f | 86 | |
6a488035 TO |
87 | /** |
88 | * Check if the result is an error. Note that this function has been retained from | |
89 | * api v2 for convenience but the result is more standardised in v3 and param | |
90 | * 'format.is_success' => 1 | |
91 | * will result in a boolean success /fail being returned if that is what you need. | |
92 | * | |
8904518f | 93 | * @param $result |
94 | * | |
5c766a0b | 95 | * @return bool |
e94b9071 | 96 | * true if error, false otherwise |
6a488035 TO |
97 | */ |
98 | function civicrm_error($result) { | |
99 | if (is_array($result)) { | |
100 | return (array_key_exists('is_error', $result) && | |
101 | $result['is_error'] | |
102 | ) ? TRUE : FALSE; | |
103 | } | |
104 | return FALSE; | |
105 | } | |
106 | ||
aa1b1481 | 107 | /** |
a828d7b8 | 108 | * Get camel case version of entity name. |
22242c87 | 109 | * |
4846df91 | 110 | * @param string|null $entity |
aa1b1481 | 111 | * |
4846df91 | 112 | * @return string|null |
aa1b1481 | 113 | */ |
dc913073 | 114 | function _civicrm_api_get_camel_name($entity) { |
4846df91 | 115 | return is_string($entity) ? CRM_Utils_String::convertStringToCamel($entity) : NULL; |
6a488035 TO |
116 | } |
117 | ||
11e09c59 | 118 | /** |
22242c87 EM |
119 | * Swap out any $values vars. |
120 | * | |
121 | * Ie. the value after $value is swapped for the parent $result | |
6a488035 | 122 | * 'activity_type_id' => '$value.testfield', |
e94b9071 CW |
123 | * 'tag_id' => '$value.api.tag.create.id', |
124 | * 'tag1_id' => '$value.api.entity.create.0.id' | |
d424ffde | 125 | * |
e94b9071 CW |
126 | * @param array $params |
127 | * @param array $parentResult | |
128 | * @param string $separator | |
6a488035 | 129 | */ |
845d6d75 | 130 | function _civicrm_api_replace_variables(&$params, &$parentResult, $separator = '.') { |
6a488035 | 131 | |
6a488035 TO |
132 | foreach ($params as $field => $value) { |
133 | ||
134 | if (is_string($value) && substr($value, 0, 6) == '$value') { | |
08d0c34b | 135 | $valueSubstitute = substr($value, 7); |
6a488035 | 136 | |
08d0c34b EM |
137 | if (!empty($parentResult[$valueSubstitute])) { |
138 | $params[$field] = $parentResult[$valueSubstitute]; | |
6a488035 TO |
139 | } |
140 | else { | |
141 | ||
142 | $stringParts = explode($separator, $value); | |
143 | unset($stringParts[0]); | |
144 | ||
145 | $fieldname = array_shift($stringParts); | |
146 | ||
147 | //when our string is an array we will treat it as an array from that . onwards | |
148 | $count = count($stringParts); | |
149 | while ($count > 0) { | |
150 | $fieldname .= "." . array_shift($stringParts); | |
151 | if (array_key_exists($fieldname, $parentResult) && is_array($parentResult[$fieldname])) { | |
152 | $arrayLocation = $parentResult[$fieldname]; | |
08d0c34b EM |
153 | foreach ($stringParts as $key => $innerValue) { |
154 | $arrayLocation = CRM_Utils_Array::value($innerValue, $arrayLocation); | |
6a488035 TO |
155 | } |
156 | $params[$field] = $arrayLocation; | |
157 | } | |
158 | $count = count($stringParts); | |
159 | } | |
160 | } | |
aa1904e1 | 161 | // CRM-16168 If we have failed to swap it out we should unset it rather than leave the placeholder. |
162 | if (substr($params[$field], 0, 6) == '$value') { | |
163 | $params[$field] = NULL; | |
164 | } | |
6a488035 TO |
165 | } |
166 | } | |
167 | } | |
168 | ||
11e09c59 | 169 | /** |
dc64d047 | 170 | * Convert possibly camel name to underscore separated entity name. |
6a488035 | 171 | * |
cf470720 | 172 | * @param string $entity |
7cdbcb16 TO |
173 | * Entity name in various formats e.g. Contribution, contribution, |
174 | * OptionValue, option_value, UFJoin, uf_join. | |
dc64d047 | 175 | * |
a6c01b45 | 176 | * @return string |
7cdbcb16 | 177 | * Entity name in underscore separated format. |
6a488035 TO |
178 | */ |
179 | function _civicrm_api_get_entity_name_from_camel($entity) { | |
1644b908 | 180 | if (!$entity || $entity === strtolower($entity)) { |
6a488035 TO |
181 | return $entity; |
182 | } | |
183 | else { | |
184 | $entity = ltrim(strtolower(str_replace('U_F', | |
185 | 'uf', | |
186 | // That's CamelCase, beside an odd UFCamel that is expected as uf_camel | |
187 | preg_replace('/(?=[A-Z])/', '_$0', $entity) | |
188 | )), '_'); | |
189 | } | |
190 | return $entity; | |
191 | } | |
11e09c59 TO |
192 | |
193 | /** | |
dc64d047 EM |
194 | * Having a DAO object find the entity name. |
195 | * | |
cf470720 TO |
196 | * @param object $bao |
197 | * DAO being passed in. | |
dc64d047 | 198 | * |
5d3523d9 | 199 | * @return string |
6a488035 | 200 | */ |
9b873358 | 201 | function _civicrm_api_get_entity_name_from_dao($bao) { |
6a488035 | 202 | $daoName = str_replace("BAO", "DAO", get_class($bao)); |
4846df91 | 203 | return CRM_Core_DAO_AllCoreTables::getBriefName($daoName); |
6a488035 | 204 | } |