3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This api exposes CiviCRM custom value.
15 * @package CiviCRM_APIv3
19 * Sets custom values for an entity.
21 * @param array $params
22 * Expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID.
26 * // entity ID. You do not need to specify entity type, we figure it out based on the fields you're using
28 * // (omitting :id) inserts or updates a field in a single-valued group
29 * 'custom_6' => 'foo',
30 * // custom_24 is checkbox or multiselect, so pass items as an array
31 * 'custom_24' => array('bar', 'baz'),
32 * // in this case custom_33 is part of a multi-valued group, and we're updating record id 5
33 * 'custom_33:5' => value,
34 * // inserts new record in multi-valued group
35 * 'custom_33:-1' => value,
36 * // inserts another new record in multi-valued group
37 * 'custom_33:-2' => value,
38 * // you can use group_name:field_name instead of ID
39 * 'custom_some_group:my_field' => 'myinfo',
40 * // updates record ID 8 in my_other_field in multi-valued some_big_group
41 * 'custom_some_big_group:my_other_field:8' => 'myinfo',
46 * ['values' => TRUE] or ['is_error' => 1, 'error_message' => 'what went wrong']
48 function civicrm_api3_custom_value_create($params) {
49 // @todo it's not clear where the entity_table is used as CRM_Core_BAO_CustomValueTable::setValues($create)
50 // didn't seem to use it
51 // so not clear if it's relevant
52 if (!empty($params['entity_table']) && substr($params['entity_table'], 0, 7) == 'civicrm') {
53 $params['entity_table'] = substr($params['entity_table'], 8, 7);
55 $create = ['entityID' => $params['entity_id']];
56 // Translate names and
57 //Convert arrays to multi-value strings
58 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
59 foreach ($params as $id => $param) {
60 if (is_array($param)) {
61 $param = $sp . implode($sp, $param) . $sp;
63 list($c, $id) = CRM_Utils_System
::explode('_', $id, 2);
67 list($i, $n, $x) = CRM_Utils_System
::explode(':', $id, 3);
73 // Lookup names if ID was not supplied
74 $key = CRM_Core_BAO_CustomField
::getCustomFieldID($n, $i);
79 if ($x && is_numeric($x)) {
82 $create['custom_' . $key] = $param;
84 $result = CRM_Core_BAO_CustomValueTable
::setValues($create);
85 if ($result['is_error']) {
86 throw new Exception($result['error_message']);
88 return civicrm_api3_create_success(TRUE, $params, 'CustomValue');
92 * Adjust Metadata for Create action.
94 * The metadata is used for setting defaults, documentation & validation.
96 * @param array $params
97 * Array of parameters determined by getfields.
99 function _civicrm_api3_custom_value_create_spec(&$params) {
100 $params['entity_id']['api.required'] = 1;
101 $params['entity_id']['title'] = 'Entity ID';
105 * Use this API to get existing custom values for an entity.
107 * @param array $params
108 * Array specifying the entity_id.
109 * Optionally include entity_type param, i.e. 'entity_type' => 'Activity'
110 * If no entity_type is supplied, it will be determined based on the fields you request.
111 * If no entity_type is supplied and no fields are specified, 'Contact' will be assumed.
112 * Optionally include the desired custom data to be fetched (or else all custom data for this entity will be returned)
113 * Example: 'entity_id' => 123, 'return.custom_6' => 1, 'return.custom_33' => 1
114 * If you do not know the ID, you may use group name : field name, for example 'return.foo_stuff:my_field' => 1
116 * @throws API_Exception
119 function civicrm_api3_custom_value_get($params) {
122 'entityID' => $params['entity_id'],
123 'entityType' => CRM_Utils_Array
::value('entity_table', $params, ''),
125 if (strstr($getParams['entityType'], 'civicrm_')) {
126 $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8));
128 unset($params['entity_id'], $params['entity_table']);
129 foreach ($params as $id => $param) {
130 if ($param && substr($id, 0, 6) == 'return') {
132 if (!empty(substr($id, 7))) {
133 $returnVal = substr($id, 7);
135 if (!is_array($returnVal)) {
136 $returnVal = explode(',', $returnVal);
138 foreach ($returnVal as $value) {
139 list($c, $i) = CRM_Utils_System
::explode('_', $value, 2);
140 if ($c == 'custom' && is_numeric($i)) {
141 $names['custom_' . $i] = 'custom_' . $i;
145 // Lookup names if ID was not supplied
146 list($group, $field) = CRM_Utils_System
::explode(':', $value, 2);
147 $fldId = CRM_Core_BAO_CustomField
::getCustomFieldID($field, $group);
151 $names['custom_' . $fldId] = 'custom_' . $i;
153 $getParams['custom_' . $fldId] = 1;
158 $result = CRM_Core_BAO_CustomValueTable
::getValues($getParams);
160 if ($result['is_error']) {
161 if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") {
163 return civicrm_api3_create_success($values, $params, 'CustomValue');
166 throw new API_Exception($result['error_message']);
170 $entity_id = $result['entityID'];
171 unset($result['is_error'], $result['entityID']);
172 // Convert multi-value strings to arrays
173 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
174 foreach ($result as $id => $value) {
175 if (strpos($value, $sp) !== FALSE) {
176 $value = explode($sp, trim($value, $sp));
179 $idArray = explode('_', $id);
180 if ($idArray[0] != 'custom') {
183 $fieldNumber = $idArray[1];
184 $customFieldInfo = CRM_Core_BAO_CustomField
::getNameFromID($fieldNumber);
185 $info = array_pop($customFieldInfo);
186 // id is the index for returned results
188 if (empty($idArray[2])) {
194 $id = $fieldNumber . "." . $idArray[2];
196 if (!empty($params['format.field_names'])) {
197 $id = $info['field_name'];
202 $values[$id]['entity_id'] = $getParams['entityID'];
203 if (!empty($getParams['entityType'])) {
204 $values[$id]['entity_table'] = $getParams['entityType'];
206 //set 'latest' -useful for multi fields but set for single for consistency
207 $values[$id]['latest'] = $value;
208 $values[$id]['id'] = $id;
209 $values[$id][$n] = $value;
211 return civicrm_api3_create_success($values, $params, 'CustomValue');
216 * Adjust Metadata for Get action.
218 * The metadata is used for setting defaults, documentation & validation.
220 * @param array $params
221 * Array of parameters determined by getfields.
223 function _civicrm_api3_custom_value_get_spec(&$params) {
224 $params['entity_id']['api.required'] = 1;
225 $params['entity_id']['title'] = 'Entity ID';
229 * CustomValue.gettree API specification
231 * @param array $spec description of fields supported by this API call
233 * @throws \CiviCRM_API3_Exception
235 function _civicrm_api3_custom_value_gettree_spec(&$spec) {
236 $spec['entity_id'] = [
237 'title' => 'Entity Id',
238 'description' => 'Id of entity',
239 'type' => CRM_Utils_Type
::T_INT
,
242 $entities = civicrm_api3('Entity', 'get');
243 $entities = array_diff($entities['values'], $entities['deprecated']);
244 $spec['entity_type'] = [
245 'title' => 'Entity Type',
246 'description' => 'API name of entity type, e.g. "Contact"',
247 'type' => CRM_Utils_Type
::T_STRING
,
249 'options' => array_combine($entities, $entities),
251 // Return params for custom group, field & value
252 foreach (CRM_Core_DAO_CustomGroup
::fields() as $field) {
253 $name = 'custom_group.' . $field['name'];
254 $spec[$name] = ['name' => $name] +
$field;
256 foreach (CRM_Core_DAO_CustomField
::fields() as $field) {
257 $name = 'custom_field.' . $field['name'];
258 $spec[$name] = ['name' => $name] +
$field;
260 $spec['custom_value.id'] = [
261 'title' => 'Custom Value Id',
262 'description' => 'Id of record in custom value table',
263 'type' => CRM_Utils_Type
::T_INT
,
265 $spec['custom_value.data'] = [
266 'title' => 'Custom Value (Raw)',
267 'description' => 'Raw value as stored in the database',
268 'type' => CRM_Utils_Type
::T_STRING
,
270 $spec['custom_value.display'] = [
271 'title' => 'Custom Value (Formatted)',
272 'description' => 'Custom value formatted for display',
273 'type' => CRM_Utils_Type
::T_STRING
,
278 * CustomValue.gettree API
280 * @param array $params
282 * @return array API result
283 * @throws \API_Exception
284 * @throws \CRM_Core_Exception
285 * @throws \CiviCRM_API3_Exception
287 function civicrm_api3_custom_value_gettree($params) {
289 $options = _civicrm_api3_get_options_from_params($params);
291 'custom_group' => [],
292 'custom_field' => [],
293 'custom_value' => [],
295 foreach (array_keys($options['return']) as $r) {
296 list($type, $field) = explode('.', $r);
297 if (isset($toReturn[$type])) {
298 $toReturn[$type][] = $field;
301 // We must have a name if not indexing sequentially
302 if (empty($params['sequential']) && $toReturn['custom_field']) {
303 $toReturn['custom_field'][] = 'name';
305 switch ($params['entity_type']) {
307 $ret = ['entityType' => 'contact_type', 'subTypes' => 'contact_sub_type'];
318 $ret = ['subTypes' => strtolower($params['entity_type']) . '_type_id'];
325 'entityType' => $params['entity_type'],
329 // Fetch entity data for custom group type/sub-type
330 // Also verify access permissions (api3 will throw an exception if permission denied)
331 if ($ret ||
!empty($params['check_permissions'])) {
332 $entityData = civicrm_api3($params['entity_type'], 'getsingle', [
333 'id' => $params['entity_id'],
334 'check_permissions' => !empty($params['check_permissions']),
335 'return' => array_merge(['id'], array_values($ret)),
337 foreach ($ret as $param => $key) {
338 if (isset($entityData[$key])) {
339 $treeParams[$param] = $entityData[$key];
343 $permission = empty($params['check_permissions']) ?
FALSE : CRM_Core_Permission
::VIEW
;
344 $tree = CRM_Core_BAO_CustomGroup
::getTree($treeParams['entityType'], $toReturn, $params['entity_id'], NULL, $treeParams['subTypes'], $treeParams['subName'], TRUE, NULL, FALSE, $permission);
345 unset($tree['info']);
347 foreach ($tree as $group) {
348 $result[$group['name']] = [];
349 $groupToReturn = $toReturn['custom_group'] ?
$toReturn['custom_group'] : array_keys($group);
350 foreach ($groupToReturn as $item) {
351 $result[$group['name']][$item] = $group[$item] ??
NULL;
353 $result[$group['name']]['fields'] = [];
354 foreach ($group['fields'] as $fieldInfo) {
355 $field = ['value' => NULL];
356 $fieldToReturn = $toReturn['custom_field'] ?
$toReturn['custom_field'] : array_keys($fieldInfo);
357 foreach ($fieldToReturn as $item) {
358 $field[$item] = $fieldInfo[$item] ??
NULL;
360 unset($field['customValue']);
361 if (!empty($fieldInfo['customValue'])) {
362 $field['value'] = CRM_Utils_Array
::first($fieldInfo['customValue']);
363 if (!$toReturn['custom_value'] ||
in_array('display', $toReturn['custom_value'])) {
364 $field['value']['display'] = CRM_Core_BAO_CustomField
::displayValue($field['value']['data'], $fieldInfo);
366 foreach (array_keys($field['value']) as $key) {
367 if ($toReturn['custom_value'] && !in_array($key, $toReturn['custom_value'])) {
368 unset($field['value'][$key]);
372 if (empty($params['sequential'])) {
373 $result[$group['name']]['fields'][$fieldInfo['name']] = $field;
376 $result[$group['name']]['fields'][] = $field;
380 return civicrm_api3_create_success($result, $params, 'CustomValue', 'gettree');
384 * CustomValue.getdisplayvalue API specification
386 * @param array $spec description of fields supported by this API call
388 function _civicrm_api3_custom_value_getdisplayvalue_spec(&$spec) {
389 $spec['entity_id'] = [
390 'title' => 'Entity Id',
391 'description' => 'Id of entity',
392 'type' => CRM_Utils_Type
::T_INT
,
395 $spec['custom_field_id'] = [
396 'title' => 'Custom Field ID',
397 'description' => 'Id of custom field',
398 'type' => CRM_Utils_Type
::T_INT
,
401 $spec['custom_field_value'] = [
402 'title' => 'Custom Field value',
403 'description' => 'Specify the value of the custom field to return as displayed value, or omit to use the current value.',
404 'type' => CRM_Utils_Type
::T_STRING
,
410 * CustomValue.getdisplayvalue API
412 * @param array $params
414 * @return array API result
415 * @throws \CiviCRM_API3_Exception
417 function civicrm_api3_custom_value_getdisplayvalue($params) {
418 // Null or missing means use the current db value, but treat '0', 0, and '' as legitimate values to look up.
419 if (($params['custom_field_value'] ??
NULL) === NULL) {
420 $params['custom_field_value'] = civicrm_api3('CustomValue', 'getsingle', [
421 'return' => ["custom_{$params['custom_field_id']}"],
422 'entity_id' => $params['entity_id'],
424 $params['custom_field_value'] = $params['custom_field_value']['latest'];
426 $values[$params['custom_field_id']]['display'] = CRM_Core_BAO_CustomField
::displayValue($params['custom_field_value'], $params['custom_field_id'], $params['entity_id'] ??
NULL);
427 $values[$params['custom_field_id']]['raw'] = $params['custom_field_value'];
428 return civicrm_api3_create_success($values, $params, 'CustomValue', 'getdisplayvalue');