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