dev/core#149 - Fatal Error on customvalue get api
[civicrm-core.git] / api / v3 / CustomValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This api exposes CiviCRM custom value.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34
35 /**
36 * Sets custom values for an entity.
37 *
38 * @param array $params
39 * Expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID.
40 *
41 * @example:
42 * @code
43 * // entity ID. You do not need to specify entity type, we figure it out based on the fields you're using
44 * 'entity_id' => 123,
45 * // (omitting :id) inserts or updates a field in a single-valued group
46 * 'custom_6' => 'foo',
47 * // custom_24 is checkbox or multiselect, so pass items as an array
48 * 'custom_24' => array('bar', 'baz'),
49 * // in this case custom_33 is part of a multi-valued group, and we're updating record id 5
50 * 'custom_33:5' => value,
51 * // inserts new record in multi-valued group
52 * 'custom_33:-1' => value,
53 * // inserts another new record in multi-valued group
54 * 'custom_33:-2' => value,
55 * // you can use group_name:field_name instead of ID
56 * 'custom_some_group:my_field' => 'myinfo',
57 * // updates record ID 8 in my_other_field in multi-valued some_big_group
58 * 'custom_some_big_group:my_other_field:8' => 'myinfo',
59 * @endcode
60 *
61 * @throws Exception
62 * @return array
63 * ['values' => TRUE] or ['is_error' => 1, 'error_message' => 'what went wrong']
64 */
65 function civicrm_api3_custom_value_create($params) {
66 // @todo it's not clear where the entity_table is used as CRM_Core_BAO_CustomValueTable::setValues($create)
67 // didn't seem to use it
68 // so not clear if it's relevant
69 if (!empty($params['entity_table']) && substr($params['entity_table'], 0, 7) == 'civicrm') {
70 $params['entity_table'] = substr($params['entity_table'], 8, 7);
71 }
72 $create = array('entityID' => $params['entity_id']);
73 // Translate names and
74 //Convert arrays to multi-value strings
75 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
76 foreach ($params as $id => $param) {
77 if (is_array($param)) {
78 $param = $sp . implode($sp, $param) . $sp;
79 }
80 list($c, $id) = CRM_Utils_System::explode('_', $id, 2);
81 if ($c != 'custom') {
82 continue;
83 }
84 list($i, $n, $x) = CRM_Utils_System::explode(':', $id, 3);
85 if (is_numeric($i)) {
86 $key = $i;
87 $x = $n;
88 }
89 else {
90 // Lookup names if ID was not supplied
91 $key = CRM_Core_BAO_CustomField::getCustomFieldID($n, $i);
92 if (!$key) {
93 continue;
94 }
95 }
96 if ($x && is_numeric($x)) {
97 $key .= '_' . $x;
98 }
99 $create['custom_' . $key] = $param;
100 }
101 $result = CRM_Core_BAO_CustomValueTable::setValues($create);
102 if ($result['is_error']) {
103 throw new Exception($result['error_message']);
104 }
105 return civicrm_api3_create_success(TRUE, $params, 'CustomValue');
106 }
107
108 /**
109 * Adjust Metadata for Create action.
110 *
111 * The metadata is used for setting defaults, documentation & validation.
112 *
113 * @param array $params
114 * Array of parameters determined by getfields.
115 */
116 function _civicrm_api3_custom_value_create_spec(&$params) {
117 $params['entity_id']['api.required'] = 1;
118 $params['entity_id']['title'] = 'Entity ID';
119 }
120
121 /**
122 * Use this API to get existing custom values for an entity.
123 *
124 * @param array $params
125 * Array specifying the entity_id.
126 * Optionally include entity_type param, i.e. 'entity_type' => 'Activity'
127 * If no entity_type is supplied, it will be determined based on the fields you request.
128 * If no entity_type is supplied and no fields are specified, 'Contact' will be assumed.
129 * Optionally include the desired custom data to be fetched (or else all custom data for this entity will be returned)
130 * Example: 'entity_id' => 123, 'return.custom_6' => 1, 'return.custom_33' => 1
131 * If you do not know the ID, you may use group name : field name, for example 'return.foo_stuff:my_field' => 1
132 *
133 * @throws API_Exception
134 * @return array
135 */
136 function civicrm_api3_custom_value_get($params) {
137
138 $getParams = array(
139 'entityID' => $params['entity_id'],
140 'entityType' => CRM_Utils_Array::value('entity_table', $params, ''),
141 );
142 if (strstr($getParams['entityType'], 'civicrm_')) {
143 $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8));
144 }
145 unset($params['entity_id'], $params['entity_table']);
146 foreach ($params as $id => $param) {
147 if ($param && substr($id, 0, 6) == 'return') {
148 $returnVal = $param;
149 if (!empty(substr($id, 7))) {
150 $returnVal = substr($id, 7);
151 }
152 foreach ((array) $returnVal as $value) {
153 list($c, $i) = CRM_Utils_System::explode('_', $value, 2);
154 if ($c == 'custom' && is_numeric($i)) {
155 $names['custom_' . $i] = 'custom_' . $i;
156 $fldId = $i;
157 }
158 else {
159 // Lookup names if ID was not supplied
160 list($group, $field) = CRM_Utils_System::explode(':', $value, 2);
161 $fldId = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
162 if (!$fldId) {
163 continue;
164 }
165 $names['custom_' . $fldId] = 'custom_' . $i;
166 }
167 $getParams['custom_' . $fldId] = 1;
168 }
169 }
170 }
171
172 $result = CRM_Core_BAO_CustomValueTable::getValues($getParams);
173
174 if ($result['is_error']) {
175 if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") {
176 $values = array();
177 return civicrm_api3_create_success($values, $params, 'CustomValue');
178 }
179 else {
180 throw new API_Exception($result['error_message']);
181 }
182 }
183 else {
184 $entity_id = $result['entityID'];
185 unset($result['is_error'], $result['entityID']);
186 // Convert multi-value strings to arrays
187 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
188 foreach ($result as $id => $value) {
189 if (strpos($value, $sp) !== FALSE) {
190 $value = explode($sp, trim($value, $sp));
191 }
192
193 $idArray = explode('_', $id);
194 if ($idArray[0] != 'custom') {
195 continue;
196 }
197 $fieldNumber = $idArray[1];
198 $customFieldInfo = CRM_Core_BAO_CustomField::getNameFromID($fieldNumber);
199 $info = array_pop($customFieldInfo);
200 // id is the index for returned results
201
202 if (empty($idArray[2])) {
203 $n = 0;
204 $id = $fieldNumber;
205 }
206 else {
207 $n = $idArray[2];
208 $id = $fieldNumber . "." . $idArray[2];
209 }
210 if (!empty($params['format.field_names'])) {
211 $id = $info['field_name'];
212 }
213 else {
214 $id = $fieldNumber;
215 }
216 $values[$id]['entity_id'] = $getParams['entityID'];
217 if (!empty($getParams['entityType'])) {
218 $values[$id]['entity_table'] = $getParams['entityType'];
219 }
220 //set 'latest' -useful for multi fields but set for single for consistency
221 $values[$id]['latest'] = $value;
222 $values[$id]['id'] = $id;
223 $values[$id][$n] = $value;
224 }
225 return civicrm_api3_create_success($values, $params, 'CustomValue');
226 }
227 }
228
229 /**
230 * Adjust Metadata for Get action.
231 *
232 * The metadata is used for setting defaults, documentation & validation.
233 *
234 * @param array $params
235 * Array of parameters determined by getfields.
236 */
237 function _civicrm_api3_custom_value_get_spec(&$params) {
238 $params['entity_id']['api.required'] = 1;
239 $params['entity_id']['title'] = 'Entity ID';
240 }
241
242 /**
243 * CustomValue.gettree API specification
244 *
245 * @param array $spec description of fields supported by this API call
246 * @return void
247 */
248 function _civicrm_api3_custom_value_gettree_spec(&$spec) {
249 $spec['entity_id'] = array(
250 'title' => 'Entity Id',
251 'description' => 'Id of entity',
252 'type' => CRM_Utils_Type::T_INT,
253 'api.required' => 1,
254 );
255 $entities = civicrm_api3('Entity', 'get');
256 $entities = array_diff($entities['values'], $entities['deprecated']);
257 $spec['entity_type'] = array(
258 'title' => 'Entity Type',
259 'description' => 'API name of entity type, e.g. "Contact"',
260 'type' => CRM_Utils_Type::T_STRING,
261 'api.required' => 1,
262 'options' => array_combine($entities, $entities),
263 );
264 // Return params for custom group, field & value
265 foreach (CRM_Core_DAO_CustomGroup::fields() as $field) {
266 $name = 'custom_group.' . $field['name'];
267 $spec[$name] = array('name' => $name) + $field;
268 }
269 foreach (CRM_Core_DAO_CustomField::fields() as $field) {
270 $name = 'custom_field.' . $field['name'];
271 $spec[$name] = array('name' => $name) + $field;
272 }
273 $spec['custom_value.id'] = array(
274 'title' => 'Custom Value Id',
275 'description' => 'Id of record in custom value table',
276 'type' => CRM_Utils_Type::T_INT,
277 );
278 $spec['custom_value.data'] = array(
279 'title' => 'Custom Value (Raw)',
280 'description' => 'Raw value as stored in the database',
281 'type' => CRM_Utils_Type::T_STRING,
282 );
283 $spec['custom_value.display'] = array(
284 'title' => 'Custom Value (Formatted)',
285 'description' => 'Custom value formatted for display',
286 'type' => CRM_Utils_Type::T_STRING,
287 );
288 }
289
290 /**
291 * CustomValue.gettree API
292 *
293 * @param array $params
294 * @return array API result
295 * @throws API_Exception
296 */
297 function civicrm_api3_custom_value_gettree($params) {
298 $ret = array();
299 $options = _civicrm_api3_get_options_from_params($params);
300 $toReturn = array(
301 'custom_group' => array(),
302 'custom_field' => array(),
303 'custom_value' => array(),
304 );
305 foreach (array_keys($options['return']) as $r) {
306 list($type, $field) = explode('.', $r);
307 if (isset($toReturn[$type])) {
308 $toReturn[$type][] = $field;
309 }
310 }
311 // We must have a name if not indexing sequentially
312 if (empty($params['sequential']) && $toReturn['custom_field']) {
313 $toReturn['custom_field'][] = 'name';
314 }
315 switch ($params['entity_type']) {
316 case 'Contact':
317 $ret = array('entityType' => 'contact_type', 'subTypes' => 'contact_sub_type');
318 break;
319
320 case 'Activity':
321 case 'Campaign':
322 case 'Case':
323 case 'Contribution':
324 case 'Event':
325 case 'Grant':
326 case 'Membership':
327 case 'Relationship':
328 $ret = array('subTypes' => strtolower($params['entity_type']) . '_type_id');
329 break;
330
331 case 'Participant':
332 // todo
333 }
334 $treeParams = array(
335 'entityType' => $params['entity_type'],
336 'subTypes' => array(),
337 'subName' => NULL,
338 );
339 // Fetch entity data for custom group type/sub-type
340 // Also verify access permissions (api3 will throw an exception if permission denied)
341 if ($ret || !empty($params['check_permissions'])) {
342 $entityData = civicrm_api3($params['entity_type'], 'getsingle', array(
343 'id' => $params['entity_id'],
344 'return' => array_merge(array('id'), array_values($ret)),
345 ));
346 foreach ($ret as $param => $key) {
347 if (isset($entityData[$key])) {
348 $treeParams[$param] = $entityData[$key];
349 }
350 }
351 }
352 $tree = CRM_Core_BAO_CustomGroup::getTree($treeParams['entityType'], $toReturn, $params['entity_id'], NULL, $treeParams['subTypes'], $treeParams['subName'], TRUE, NULL, FALSE, CRM_Utils_Array::value('check_permissions', $params, TRUE));
353 unset($tree['info']);
354 $result = array();
355 foreach ($tree as $group) {
356 $result[$group['name']] = array();
357 $groupToReturn = $toReturn['custom_group'] ? $toReturn['custom_group'] : array_keys($group);
358 foreach ($groupToReturn as $item) {
359 $result[$group['name']][$item] = CRM_Utils_Array::value($item, $group);
360 }
361 $result[$group['name']]['fields'] = array();
362 foreach ($group['fields'] as $fieldInfo) {
363 $field = array('value' => NULL);
364 $fieldToReturn = $toReturn['custom_field'] ? $toReturn['custom_field'] : array_keys($fieldInfo);
365 foreach ($fieldToReturn as $item) {
366 $field[$item] = CRM_Utils_Array::value($item, $fieldInfo);
367 }
368 unset($field['customValue']);
369 if (!empty($fieldInfo['customValue'])) {
370 $field['value'] = CRM_Utils_Array::first($fieldInfo['customValue']);
371 if (!$toReturn['custom_value'] || in_array('display', $toReturn['custom_value'])) {
372 $field['value']['display'] = CRM_Core_BAO_CustomField::displayValue($field['value']['data'], $fieldInfo);
373 }
374 foreach (array_keys($field['value']) as $key) {
375 if ($toReturn['custom_value'] && !in_array($key, $toReturn['custom_value'])) {
376 unset($field['value'][$key]);
377 }
378 }
379 }
380 if (empty($params['sequential'])) {
381 $result[$group['name']]['fields'][$fieldInfo['name']] = $field;
382 }
383 else {
384 $result[$group['name']]['fields'][] = $field;
385 }
386 }
387 }
388 return civicrm_api3_create_success($result, $params, 'CustomValue', 'gettree');
389 }