3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
36 * Sets custom values for an entity.
38 * @param array $params
39 * Expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID.
43 * // entity ID. You do not need to specify entity type, we figure it out based on the fields you're using
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',
63 * ['values' => TRUE] or ['is_error' => 1, 'error_message' => 'what went wrong']
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);
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;
80 list($c, $id) = CRM_Utils_System
::explode('_', $id, 2);
84 list($i, $n, $x) = CRM_Utils_System
::explode(':', $id, 3);
90 // Lookup names if ID was not supplied
91 $key = CRM_Core_BAO_CustomField
::getCustomFieldID($n, $i);
96 if ($x && is_numeric($x)) {
99 $create['custom_' . $key] = $param;
101 $result = CRM_Core_BAO_CustomValueTable
::setValues($create);
102 if ($result['is_error']) {
103 throw new Exception($result['error_message']);
105 return civicrm_api3_create_success(TRUE, $params, 'CustomValue');
109 * Adjust Metadata for Create action.
111 * The metadata is used for setting defaults, documentation & validation.
113 * @param array $params
114 * Array of parameters determined by getfields.
116 function _civicrm_api3_custom_value_create_spec(&$params) {
117 $params['entity_id']['api.required'] = 1;
118 $params['entity_id']['title'] = 'Entity ID';
122 * Use this API to get existing custom values for an entity.
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
133 * @throws API_Exception
136 function civicrm_api3_custom_value_get($params) {
139 'entityID' => $params['entity_id'],
140 'entityType' => CRM_Utils_Array
::value('entity_table', $params, ''),
142 if (strstr($getParams['entityType'], 'civicrm_')) {
143 $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8));
145 unset($params['entity_id'], $params['entity_table']);
146 foreach ($params as $id => $param) {
147 if ($param && substr($id, 0, 6) == 'return') {
148 $id = substr($id, 7);
149 list($c, $i) = CRM_Utils_System
::explode('_', $id, 2);
150 if ($c == 'custom' && is_numeric($i)) {
151 $names['custom_' . $i] = 'custom_' . $i;
155 // Lookup names if ID was not supplied
156 list($group, $field) = CRM_Utils_System
::explode(':', $id, 2);
157 $id = CRM_Core_BAO_CustomField
::getCustomFieldID($field, $group);
161 $names['custom_' . $id] = 'custom_' . $i;
163 $getParams['custom_' . $id] = 1;
167 $result = CRM_Core_BAO_CustomValueTable
::getValues($getParams);
169 if ($result['is_error']) {
170 if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") {
172 return civicrm_api3_create_success($values, $params, 'CustomValue');
175 throw new API_Exception($result['error_message']);
179 $entity_id = $result['entityID'];
180 unset($result['is_error'], $result['entityID']);
181 // Convert multi-value strings to arrays
182 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
183 foreach ($result as $id => $value) {
184 if (strpos($value, $sp) !== FALSE) {
185 $value = explode($sp, trim($value, $sp));
188 $idArray = explode('_', $id);
189 if ($idArray[0] != 'custom') {
192 $fieldNumber = $idArray[1];
193 $customFieldInfo = CRM_Core_BAO_CustomField
::getNameFromID($fieldNumber);
194 $info = array_pop($customFieldInfo);
195 // id is the index for returned results
197 if (empty($idArray[2])) {
203 $id = $fieldNumber . "." . $idArray[2];
205 if (!empty($params['format.field_names'])) {
206 $id = $info['field_name'];
211 $values[$id]['entity_id'] = $getParams['entityID'];
212 if (!empty($getParams['entityType'])) {
213 $values[$id]['entity_table'] = $getParams['entityType'];
215 //set 'latest' -useful for multi fields but set for single for consistency
216 $values[$id]['latest'] = $value;
217 $values[$id]['id'] = $id;
218 $values[$id][$n] = $value;
220 return civicrm_api3_create_success($values, $params, 'CustomValue');
225 * Adjust Metadata for Get action.
227 * The metadata is used for setting defaults, documentation & validation.
229 * @param array $params
230 * Array of parameters determined by getfields.
232 function _civicrm_api3_custom_value_get_spec(&$params) {
233 $params['entity_id']['api.required'] = 1;
234 $params['entity_id']['title'] = 'Entity ID';