INFRA-132 - Put space after flow-control (if/switch/for/foreach/while)
[civicrm-core.git] / api / api.php
CommitLineData
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 */
24function 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 41function civicrm_api3($entity, $action, $params = array()) {
6b359437 42 $params['version'] = 3;
43 $result = civicrm_api($entity, $action, $params);
22e263ad 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/**
c490a46a 51 * call getfields from api wrapper. This function ensures that settings that could alter
94359f7e 52 * getfields output (e.g. action for all api & profile_id for profile api ) are consistently passed in.
53 *
54 * We check whether the api call is 'getfields' because if getfields is being called we return an empty array
55 * as no alias swapping, validation or default filling is done on getfields & we want to avoid a loop
56 *
57 * @todo other output modifiers include contact_type
58 *
59 * @param array $apiRequest
446f0940 60 * @return array getfields output
94359f7e 61 */
62function _civicrm_api3_api_getfields(&$apiRequest) {
63 if (strtolower($apiRequest['action'] == 'getfields')) {
64 // the main param getfields takes is 'action' - however this param is not compatible with REST
65 // so we accept 'api_action' as an alias of action on getfields
a7488080 66 if (!empty($apiRequest['params']['api_action'])) {
35671d00
TO
67 // $apiRequest['params']['action'] = $apiRequest['params']['api_action'];
68 // unset($apiRequest['params']['api_action']);
94359f7e 69 }
de0a1900 70 return array('action' => array('api.aliases' => array('api_action')));
94359f7e 71 }
72 $getFieldsParams = array('action' => $apiRequest['action']);
73 $entity = $apiRequest['entity'];
22e263ad 74 if ($entity == 'profile' && array_key_exists('profile_id', $apiRequest['params'])) {
94359f7e 75 $getFieldsParams['profile_id'] = $apiRequest['params']['profile_id'];
76 }
77 $fields = civicrm_api3($entity, 'getfields', $getFieldsParams);
78 return $fields['values'];
79}
8904518f 80
6a488035
TO
81/**
82 * Check if the result is an error. Note that this function has been retained from
83 * api v2 for convenience but the result is more standardised in v3 and param
84 * 'format.is_success' => 1
85 * will result in a boolean success /fail being returned if that is what you need.
86 *
8904518f 87 * @param $result
88 *
6a488035
TO
89 * @return boolean true if error, false otherwise
90 * @static void
91 * @access public
92 */
93function civicrm_error($result) {
94 if (is_array($result)) {
95 return (array_key_exists('is_error', $result) &&
96 $result['is_error']
97 ) ? TRUE : FALSE;
98 }
99 return FALSE;
100}
101
aa1b1481
EM
102/**
103 * @param $entity
aa1b1481
EM
104 *
105 * @return string
106 */
dc913073
EM
107function _civicrm_api_get_camel_name($entity) {
108 return CRM_Utils_String::convertStringToCamel($entity);
6a488035
TO
109}
110
11e09c59 111/**
6a488035
TO
112 * Swap out any $values vars - ie. the value after $value is swapped for the parent $result
113 * 'activity_type_id' => '$value.testfield',
114 'tag_id' => '$value.api.tag.create.id',
115 'tag1_id' => '$value.api.entity.create.0.id'
116 */
117function _civicrm_api_replace_variables($entity, $action, &$params, &$parentResult, $separator = '.') {
118
6a488035
TO
119 foreach ($params as $field => $value) {
120
121 if (is_string($value) && substr($value, 0, 6) == '$value') {
122 $valuesubstitute = substr($value, 7);
123
124 if (!empty($parentResult[$valuesubstitute])) {
125 $params[$field] = $parentResult[$valuesubstitute];
126 }
127 else {
128
129 $stringParts = explode($separator, $value);
130 unset($stringParts[0]);
131
132 $fieldname = array_shift($stringParts);
133
134 //when our string is an array we will treat it as an array from that . onwards
135 $count = count($stringParts);
136 while ($count > 0) {
137 $fieldname .= "." . array_shift($stringParts);
138 if (array_key_exists($fieldname, $parentResult) && is_array($parentResult[$fieldname])) {
139 $arrayLocation = $parentResult[$fieldname];
140 foreach ($stringParts as $key => $value) {
141 $arrayLocation = CRM_Utils_Array::value($value, $arrayLocation);
142 }
143 $params[$field] = $arrayLocation;
144 }
145 $count = count($stringParts);
146 }
147 }
148 }
149 }
150}
151
11e09c59 152/**
6a488035
TO
153 * Convert possibly camel name to underscore separated entity name
154 *
cf470720
TO
155 * @param string $entity
156 * Entity name in various formats e.g. Contribution, contribution, OptionValue, option_value, UFJoin, uf_join.
6a488035 157 * @return string $entity entity name in underscore separated format
47e6af81
CW
158 *
159 * FIXME: Why isn't this called first thing in civicrm_api wrapper?
6a488035
TO
160 */
161function _civicrm_api_get_entity_name_from_camel($entity) {
162 if ($entity == strtolower($entity)) {
163 return $entity;
164 }
165 else {
166 $entity = ltrim(strtolower(str_replace('U_F',
167 'uf',
168 // That's CamelCase, beside an odd UFCamel that is expected as uf_camel
169 preg_replace('/(?=[A-Z])/', '_$0', $entity)
170 )), '_');
171 }
172 return $entity;
173}
11e09c59
TO
174
175/**
6a488035 176 * Having a DAO object find the entity name
cf470720
TO
177 * @param object $bao
178 * DAO being passed in.
5d3523d9 179 * @return string
6a488035
TO
180 */
181function _civicrm_api_get_entity_name_from_dao($bao){
182 $daoName = str_replace("BAO", "DAO", get_class($bao));
edc091f1 183 return _civicrm_api_get_entity_name_from_camel(CRM_Core_DAO_AllCoreTables::getBriefName($daoName));
6a488035 184}