Merge pull request #4111 from totten/4.5-schedrem-order
[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 *
6b359437 31 * @param string $entity type of entities to deal with
32 * @param string $action create, get, delete or some special action name.
33 * @param array $params array to be passed to function
34 *
8904518f 35 * @throws CiviCRM_API3_Exception
6b359437 36 * @return array
6b359437 37 */
67744c4e 38function civicrm_api3($entity, $action, $params = array()) {
6b359437 39 $params['version'] = 3;
40 $result = civicrm_api($entity, $action, $params);
d661281d 41 if(is_array($result) && !empty($result['is_error'])){
6b359437 42 throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result);
43 }
c4ac4df4 44 return $result;
6b359437 45}
6a488035 46
94359f7e 47/**
48 * Function to call getfields from api wrapper. This function ensures that settings that could alter
49 * getfields output (e.g. action for all api & profile_id for profile api ) are consistently passed in.
50 *
51 * We check whether the api call is 'getfields' because if getfields is being called we return an empty array
52 * as no alias swapping, validation or default filling is done on getfields & we want to avoid a loop
53 *
54 * @todo other output modifiers include contact_type
55 *
56 * @param array $apiRequest
446f0940 57 * @return array getfields output
94359f7e 58 */
59function _civicrm_api3_api_getfields(&$apiRequest) {
60 if (strtolower($apiRequest['action'] == 'getfields')) {
61 // the main param getfields takes is 'action' - however this param is not compatible with REST
62 // so we accept 'api_action' as an alias of action on getfields
a7488080 63 if (!empty($apiRequest['params']['api_action'])) {
94359f7e 64 // $apiRequest['params']['action'] = $apiRequest['params']['api_action'];
65 // unset($apiRequest['params']['api_action']);
66 }
de0a1900 67 return array('action' => array('api.aliases' => array('api_action')));
94359f7e 68 }
69 $getFieldsParams = array('action' => $apiRequest['action']);
70 $entity = $apiRequest['entity'];
71 if($entity == 'profile' && array_key_exists('profile_id', $apiRequest['params'])) {
72 $getFieldsParams['profile_id'] = $apiRequest['params']['profile_id'];
73 }
74 $fields = civicrm_api3($entity, 'getfields', $getFieldsParams);
75 return $fields['values'];
76}
8904518f 77
6a488035
TO
78/**
79 * Check if the result is an error. Note that this function has been retained from
80 * api v2 for convenience but the result is more standardised in v3 and param
81 * 'format.is_success' => 1
82 * will result in a boolean success /fail being returned if that is what you need.
83 *
8904518f 84 * @param $result
85 *
86 * @internal param array $params (reference ) input parameters
6a488035
TO
87 *
88 * @return boolean true if error, false otherwise
89 * @static void
90 * @access public
91 */
92function civicrm_error($result) {
93 if (is_array($result)) {
94 return (array_key_exists('is_error', $result) &&
95 $result['is_error']
96 ) ? TRUE : FALSE;
97 }
98 return FALSE;
99}
100
aa1b1481
EM
101/**
102 * @param $entity
103 * @param null $version
104 *
105 * @return string
106 */
6a488035 107function _civicrm_api_get_camel_name($entity, $version = NULL) {
6a488035
TO
108 $fragments = explode('_', $entity);
109 foreach ($fragments as & $fragment) {
110 $fragment = ucfirst($fragment);
111 }
112 // Special case: UFGroup, UFJoin, UFMatch, UFField
113 if ($fragments[0] === 'Uf') {
114 $fragments[0] = 'UF';
115 }
116 return implode('', $fragments);
117}
118
11e09c59 119/**
6a488035
TO
120 * Swap out any $values vars - ie. the value after $value is swapped for the parent $result
121 * 'activity_type_id' => '$value.testfield',
122 'tag_id' => '$value.api.tag.create.id',
123 'tag1_id' => '$value.api.entity.create.0.id'
124 */
125function _civicrm_api_replace_variables($entity, $action, &$params, &$parentResult, $separator = '.') {
126
127
128 foreach ($params as $field => $value) {
129
130 if (is_string($value) && substr($value, 0, 6) == '$value') {
131 $valuesubstitute = substr($value, 7);
132
133 if (!empty($parentResult[$valuesubstitute])) {
134 $params[$field] = $parentResult[$valuesubstitute];
135 }
136 else {
137
138 $stringParts = explode($separator, $value);
139 unset($stringParts[0]);
140
141 $fieldname = array_shift($stringParts);
142
143 //when our string is an array we will treat it as an array from that . onwards
144 $count = count($stringParts);
145 while ($count > 0) {
146 $fieldname .= "." . array_shift($stringParts);
147 if (array_key_exists($fieldname, $parentResult) && is_array($parentResult[$fieldname])) {
148 $arrayLocation = $parentResult[$fieldname];
149 foreach ($stringParts as $key => $value) {
150 $arrayLocation = CRM_Utils_Array::value($value, $arrayLocation);
151 }
152 $params[$field] = $arrayLocation;
153 }
154 $count = count($stringParts);
155 }
156 }
157 }
158 }
159}
160
11e09c59 161/**
6a488035
TO
162 * Convert possibly camel name to underscore separated entity name
163 *
164 * @param string $entity entity name in various formats e.g. Contribution, contribution, OptionValue, option_value, UFJoin, uf_join
165 * @return string $entity entity name in underscore separated format
47e6af81
CW
166 *
167 * FIXME: Why isn't this called first thing in civicrm_api wrapper?
6a488035
TO
168 */
169function _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}
11e09c59
TO
182
183/**
6a488035
TO
184 * Having a DAO object find the entity name
185 * @param object $bao DAO being passed in
5d3523d9 186 * @return string
6a488035
TO
187 */
188function _civicrm_api_get_entity_name_from_dao($bao){
189 $daoName = str_replace("BAO", "DAO", get_class($bao));
edc091f1 190 return _civicrm_api_get_entity_name_from_camel(CRM_Core_DAO_AllCoreTables::getBriefName($daoName));
6a488035
TO
191}
192