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