Merge pull request #4983 from colemanw/CRM-15842
[civicrm-core.git] / api / api.php
1 <?php
2
3 /**
4 * File for the CiviCRM APIv3 API wrapper
5 *
6 * @package CiviCRM_APIv3
7 * @subpackage API
8 *
9 * @copyright CiviCRM LLC (c) 2004-2014
10 * @version $Id: api.php 30486 2010-11-02 16:12:09Z shot $
11 */
12
13 /**
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
20 * @param null $extra
21 *
22 * @return array|int
23 */
24 function civicrm_api($entity, $action, $params, $extra = NULL) {
25 return \Civi\Core\Container::singleton()->get('civi_api_kernel')->run($entity, $action, $params, $extra);
26 }
27
28 /**
29 * Version 3 wrapper for civicrm_api. Throws exception
30 *
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.
37 *
38 * @throws CiviCRM_API3_Exception
39 * @return array
40 */
41 function civicrm_api3($entity, $action, $params = array()) {
42 $params['version'] = 3;
43 $result = civicrm_api($entity, $action, $params);
44 if (is_array($result) && !empty($result['is_error'])) {
45 throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result);
46 }
47 return $result;
48 }
49
50 /**
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.
54 *
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
58 *
59 * @todo other output modifiers include contact_type
60 *
61 * @param array $apiRequest
62 * @return array
63 * getfields output
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
69 if (!empty($apiRequest['params']['api_action'])) {
70 // $apiRequest['params']['action'] = $apiRequest['params']['api_action'];
71 // unset($apiRequest['params']['api_action']);
72 }
73 return array('action' => array('api.aliases' => array('api_action')));
74 }
75 $getFieldsParams = array('action' => $apiRequest['action']);
76 $entity = $apiRequest['entity'];
77 if ($entity == 'profile' && array_key_exists('profile_id', $apiRequest['params'])) {
78 $getFieldsParams['profile_id'] = $apiRequest['params']['profile_id'];
79 }
80 $fields = civicrm_api3($entity, 'getfields', $getFieldsParams);
81 return $fields['values'];
82 }
83
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 *
90 * @param $result
91 *
92 * @return bool
93 * true if error, false otherwise
94 */
95 function civicrm_error($result) {
96 if (is_array($result)) {
97 return (array_key_exists('is_error', $result) &&
98 $result['is_error']
99 ) ? TRUE : FALSE;
100 }
101 return FALSE;
102 }
103
104 /**
105 * @param $entity
106 *
107 * @return string
108 */
109 function _civicrm_api_get_camel_name($entity) {
110 return CRM_Utils_String::convertStringToCamel($entity);
111 }
112
113 /**
114 * Swap out any $values vars - ie. the value after $value is swapped for the parent $result
115 * 'activity_type_id' => '$value.testfield',
116 * 'tag_id' => '$value.api.tag.create.id',
117 * 'tag1_id' => '$value.api.entity.create.0.id'
118 *
119 * @param array $params
120 * @param array $parentResult
121 * @param string $separator
122 */
123 function _civicrm_api_replace_variables(&$params, &$parentResult, $separator = '.') {
124
125 foreach ($params as $field => $value) {
126
127 if (is_string($value) && substr($value, 0, 6) == '$value') {
128 $valueSubstitute = substr($value, 7);
129
130 if (!empty($parentResult[$valueSubstitute])) {
131 $params[$field] = $parentResult[$valueSubstitute];
132 }
133 else {
134
135 $stringParts = explode($separator, $value);
136 unset($stringParts[0]);
137
138 $fieldname = array_shift($stringParts);
139
140 //when our string is an array we will treat it as an array from that . onwards
141 $count = count($stringParts);
142 while ($count > 0) {
143 $fieldname .= "." . array_shift($stringParts);
144 if (array_key_exists($fieldname, $parentResult) && is_array($parentResult[$fieldname])) {
145 $arrayLocation = $parentResult[$fieldname];
146 foreach ($stringParts as $key => $innerValue) {
147 $arrayLocation = CRM_Utils_Array::value($innerValue, $arrayLocation);
148 }
149 $params[$field] = $arrayLocation;
150 }
151 $count = count($stringParts);
152 }
153 }
154 }
155 }
156 }
157
158 /**
159 * Convert possibly camel name to underscore separated entity name
160 *
161 * @param string $entity
162 * Entity name in various formats e.g. Contribution, contribution,
163 * OptionValue, option_value, UFJoin, uf_join.
164 * @return string
165 * Entity name in underscore separated format.
166 *
167 * @fixme Why isn't this called first thing in civicrm_api wrapper?
168 */
169 function _civicrm_api_get_entity_name_from_camel($entity) {
170 if ($entity == strtolower($entity)) {
171 return $entity;
172 }
173 else {
174 $entity = ltrim(strtolower(str_replace('U_F',
175 'uf',
176 // That's CamelCase, beside an odd UFCamel that is expected as uf_camel
177 preg_replace('/(?=[A-Z])/', '_$0', $entity)
178 )), '_');
179 }
180 return $entity;
181 }
182
183 /**
184 * Having a DAO object find the entity name
185 * @param object $bao
186 * DAO being passed in.
187 * @return string
188 */
189 function _civicrm_api_get_entity_name_from_dao($bao) {
190 $daoName = str_replace("BAO", "DAO", get_class($bao));
191 return _civicrm_api_get_entity_name_from_camel(CRM_Core_DAO_AllCoreTables::getBriefName($daoName));
192 }