3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This api exposes CiviCRM custom value.
31 * @package CiviCRM_APIv3
35 * Sets custom values for an entity.
37 * @param array $params
38 * Expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID.
42 * // entity ID. You do not need to specify entity type, we figure it out based on the fields you're using
44 * // (omitting :id) inserts or updates a field in a single-valued group
45 * 'custom_6' => 'foo',
46 * // custom_24 is checkbox or multiselect, so pass items as an array
47 * 'custom_24' => array('bar', 'baz'),
48 * // in this case custom_33 is part of a multi-valued group, and we're updating record id 5
49 * 'custom_33:5' => value,
50 * // inserts new record in multi-valued group
51 * 'custom_33:-1' => value,
52 * // inserts another new record in multi-valued group
53 * 'custom_33:-2' => value,
54 * // you can use group_name:field_name instead of ID
55 * 'custom_some_group:my_field' => 'myinfo',
56 * // updates record ID 8 in my_other_field in multi-valued some_big_group
57 * 'custom_some_big_group:my_other_field:8' => 'myinfo',
62 * ['values' => TRUE] or ['is_error' => 1, 'error_message' => 'what went wrong']
64 function civicrm_api3_custom_value_create($params) {
65 // @todo it's not clear where the entity_table is used as CRM_Core_BAO_CustomValueTable::setValues($create)
66 // didn't seem to use it
67 // so not clear if it's relevant
68 if (!empty($params['entity_table']) && substr($params['entity_table'], 0, 7) == 'civicrm') {
69 $params['entity_table'] = substr($params['entity_table'], 8, 7);
71 $create = ['entityID' => $params['entity_id']];
72 // Translate names and
73 //Convert arrays to multi-value strings
74 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
75 foreach ($params as $id => $param) {
76 if (is_array($param)) {
77 $param = $sp . implode($sp, $param) . $sp;
79 list($c, $id) = CRM_Utils_System
::explode('_', $id, 2);
83 list($i, $n, $x) = CRM_Utils_System
::explode(':', $id, 3);
89 // Lookup names if ID was not supplied
90 $key = CRM_Core_BAO_CustomField
::getCustomFieldID($n, $i);
95 if ($x && is_numeric($x)) {
98 $create['custom_' . $key] = $param;
100 $result = CRM_Core_BAO_CustomValueTable
::setValues($create);
101 if ($result['is_error']) {
102 throw new Exception($result['error_message']);
104 return civicrm_api3_create_success(TRUE, $params, 'CustomValue');
108 * Adjust Metadata for Create action.
110 * The metadata is used for setting defaults, documentation & validation.
112 * @param array $params
113 * Array of parameters determined by getfields.
115 function _civicrm_api3_custom_value_create_spec(&$params) {
116 $params['entity_id']['api.required'] = 1;
117 $params['entity_id']['title'] = 'Entity ID';
121 * Use this API to get existing custom values for an entity.
123 * @param array $params
124 * Array specifying the entity_id.
125 * Optionally include entity_type param, i.e. 'entity_type' => 'Activity'
126 * If no entity_type is supplied, it will be determined based on the fields you request.
127 * If no entity_type is supplied and no fields are specified, 'Contact' will be assumed.
128 * Optionally include the desired custom data to be fetched (or else all custom data for this entity will be returned)
129 * Example: 'entity_id' => 123, 'return.custom_6' => 1, 'return.custom_33' => 1
130 * If you do not know the ID, you may use group name : field name, for example 'return.foo_stuff:my_field' => 1
132 * @throws API_Exception
135 function civicrm_api3_custom_value_get($params) {
138 'entityID' => $params['entity_id'],
139 'entityType' => CRM_Utils_Array
::value('entity_table', $params, ''),
141 if (strstr($getParams['entityType'], 'civicrm_')) {
142 $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8));
144 unset($params['entity_id'], $params['entity_table']);
145 foreach ($params as $id => $param) {
146 if ($param && substr($id, 0, 6) == 'return') {
148 if (!empty(substr($id, 7))) {
149 $returnVal = substr($id, 7);
151 if (!is_array($returnVal)) {
152 $returnVal = explode(',', $returnVal);
154 foreach ($returnVal as $value) {
155 list($c, $i) = CRM_Utils_System
::explode('_', $value, 2);
156 if ($c == 'custom' && is_numeric($i)) {
157 $names['custom_' . $i] = 'custom_' . $i;
161 // Lookup names if ID was not supplied
162 list($group, $field) = CRM_Utils_System
::explode(':', $value, 2);
163 $fldId = CRM_Core_BAO_CustomField
::getCustomFieldID($field, $group);
167 $names['custom_' . $fldId] = 'custom_' . $i;
169 $getParams['custom_' . $fldId] = 1;
174 $result = CRM_Core_BAO_CustomValueTable
::getValues($getParams);
176 if ($result['is_error']) {
177 if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") {
179 return civicrm_api3_create_success($values, $params, 'CustomValue');
182 throw new API_Exception($result['error_message']);
186 $entity_id = $result['entityID'];
187 unset($result['is_error'], $result['entityID']);
188 // Convert multi-value strings to arrays
189 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
190 foreach ($result as $id => $value) {
191 if (strpos($value, $sp) !== FALSE) {
192 $value = explode($sp, trim($value, $sp));
195 $idArray = explode('_', $id);
196 if ($idArray[0] != 'custom') {
199 $fieldNumber = $idArray[1];
200 $customFieldInfo = CRM_Core_BAO_CustomField
::getNameFromID($fieldNumber);
201 $info = array_pop($customFieldInfo);
202 // id is the index for returned results
204 if (empty($idArray[2])) {
210 $id = $fieldNumber . "." . $idArray[2];
212 if (!empty($params['format.field_names'])) {
213 $id = $info['field_name'];
218 $values[$id]['entity_id'] = $getParams['entityID'];
219 if (!empty($getParams['entityType'])) {
220 $values[$id]['entity_table'] = $getParams['entityType'];
222 //set 'latest' -useful for multi fields but set for single for consistency
223 $values[$id]['latest'] = $value;
224 $values[$id]['id'] = $id;
225 $values[$id][$n] = $value;
227 return civicrm_api3_create_success($values, $params, 'CustomValue');
232 * Adjust Metadata for Get action.
234 * The metadata is used for setting defaults, documentation & validation.
236 * @param array $params
237 * Array of parameters determined by getfields.
239 function _civicrm_api3_custom_value_get_spec(&$params) {
240 $params['entity_id']['api.required'] = 1;
241 $params['entity_id']['title'] = 'Entity ID';
245 * CustomValue.gettree API specification
247 * @param array $spec description of fields supported by this API call
249 * @throws \CiviCRM_API3_Exception
251 function _civicrm_api3_custom_value_gettree_spec(&$spec) {
252 $spec['entity_id'] = [
253 'title' => 'Entity Id',
254 'description' => 'Id of entity',
255 'type' => CRM_Utils_Type
::T_INT
,
258 $entities = civicrm_api3('Entity', 'get');
259 $entities = array_diff($entities['values'], $entities['deprecated']);
260 $spec['entity_type'] = [
261 'title' => 'Entity Type',
262 'description' => 'API name of entity type, e.g. "Contact"',
263 'type' => CRM_Utils_Type
::T_STRING
,
265 'options' => array_combine($entities, $entities),
267 // Return params for custom group, field & value
268 foreach (CRM_Core_DAO_CustomGroup
::fields() as $field) {
269 $name = 'custom_group.' . $field['name'];
270 $spec[$name] = ['name' => $name] +
$field;
272 foreach (CRM_Core_DAO_CustomField
::fields() as $field) {
273 $name = 'custom_field.' . $field['name'];
274 $spec[$name] = ['name' => $name] +
$field;
276 $spec['custom_value.id'] = [
277 'title' => 'Custom Value Id',
278 'description' => 'Id of record in custom value table',
279 'type' => CRM_Utils_Type
::T_INT
,
281 $spec['custom_value.data'] = [
282 'title' => 'Custom Value (Raw)',
283 'description' => 'Raw value as stored in the database',
284 'type' => CRM_Utils_Type
::T_STRING
,
286 $spec['custom_value.display'] = [
287 'title' => 'Custom Value (Formatted)',
288 'description' => 'Custom value formatted for display',
289 'type' => CRM_Utils_Type
::T_STRING
,
294 * CustomValue.gettree API
296 * @param array $params
298 * @return array API result
299 * @throws \API_Exception
300 * @throws \CRM_Core_Exception
301 * @throws \CiviCRM_API3_Exception
303 function civicrm_api3_custom_value_gettree($params) {
305 $options = _civicrm_api3_get_options_from_params($params);
307 'custom_group' => [],
308 'custom_field' => [],
309 'custom_value' => [],
311 foreach (array_keys($options['return']) as $r) {
312 list($type, $field) = explode('.', $r);
313 if (isset($toReturn[$type])) {
314 $toReturn[$type][] = $field;
317 // We must have a name if not indexing sequentially
318 if (empty($params['sequential']) && $toReturn['custom_field']) {
319 $toReturn['custom_field'][] = 'name';
321 switch ($params['entity_type']) {
323 $ret = ['entityType' => 'contact_type', 'subTypes' => 'contact_sub_type'];
334 $ret = ['subTypes' => strtolower($params['entity_type']) . '_type_id'];
341 'entityType' => $params['entity_type'],
345 // Fetch entity data for custom group type/sub-type
346 // Also verify access permissions (api3 will throw an exception if permission denied)
347 if ($ret ||
!empty($params['check_permissions'])) {
348 $entityData = civicrm_api3($params['entity_type'], 'getsingle', [
349 'id' => $params['entity_id'],
350 'check_permissions' => !empty($params['check_permissions']),
351 'return' => array_merge(['id'], array_values($ret)),
353 foreach ($ret as $param => $key) {
354 if (isset($entityData[$key])) {
355 $treeParams[$param] = $entityData[$key];
359 $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));
360 unset($tree['info']);
362 foreach ($tree as $group) {
363 $result[$group['name']] = [];
364 $groupToReturn = $toReturn['custom_group'] ?
$toReturn['custom_group'] : array_keys($group);
365 foreach ($groupToReturn as $item) {
366 $result[$group['name']][$item] = CRM_Utils_Array
::value($item, $group);
368 $result[$group['name']]['fields'] = [];
369 foreach ($group['fields'] as $fieldInfo) {
370 $field = ['value' => NULL];
371 $fieldToReturn = $toReturn['custom_field'] ?
$toReturn['custom_field'] : array_keys($fieldInfo);
372 foreach ($fieldToReturn as $item) {
373 $field[$item] = CRM_Utils_Array
::value($item, $fieldInfo);
375 unset($field['customValue']);
376 if (!empty($fieldInfo['customValue'])) {
377 $field['value'] = CRM_Utils_Array
::first($fieldInfo['customValue']);
378 if (!$toReturn['custom_value'] ||
in_array('display', $toReturn['custom_value'])) {
379 $field['value']['display'] = CRM_Core_BAO_CustomField
::displayValue($field['value']['data'], $fieldInfo);
381 foreach (array_keys($field['value']) as $key) {
382 if ($toReturn['custom_value'] && !in_array($key, $toReturn['custom_value'])) {
383 unset($field['value'][$key]);
387 if (empty($params['sequential'])) {
388 $result[$group['name']]['fields'][$fieldInfo['name']] = $field;
391 $result[$group['name']]['fields'][] = $field;
395 return civicrm_api3_create_success($result, $params, 'CustomValue', 'gettree');
399 * CustomValue.getdisplayvalue API specification
401 * @param array $spec description of fields supported by this API call
403 function _civicrm_api3_custom_value_getdisplayvalue_spec(&$spec) {
404 $spec['entity_id'] = [
405 'title' => 'Entity Id',
406 'description' => 'Id of entity',
407 'type' => CRM_Utils_Type
::T_INT
,
410 $spec['custom_field_id'] = [
411 'title' => 'Custom Field ID',
412 'description' => 'Id of custom field',
413 'type' => CRM_Utils_Type
::T_INT
,
416 $spec['custom_field_value'] = [
417 'title' => 'Custom Field value',
418 'description' => 'Specify the value of the custom field to return as displayed value',
419 'type' => CRM_Utils_Type
::T_STRING
,
425 * CustomValue.getdisplayvalue API
427 * @param array $params
429 * @return array API result
430 * @throws \CiviCRM_API3_Exception
432 function civicrm_api3_custom_value_getdisplayvalue($params) {
433 if (empty($params['custom_field_value'])) {
434 $params['custom_field_value'] = civicrm_api3('CustomValue', 'getsingle', [
435 'return' => ["custom_{$params['custom_field_id']}"],
436 'entity_id' => $params['entity_id'],
438 $params['custom_field_value'] = $params['custom_field_value']['latest'];
440 $values[$params['custom_field_id']]['display'] = CRM_Core_BAO_CustomField
::displayValue($params['custom_field_value'], $params['custom_field_id'], CRM_Utils_Array
::value('entity_id', $params));
441 $values[$params['custom_field_id']]['raw'] = $params['custom_field_value'];
442 return civicrm_api3_create_success($values, $params, 'CustomValue', 'getdisplayvalue');