0e6912c3b704d69ff613edfc8c9221e7724cc022
[civicrm-core.git] / api / v3 / CustomValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * @subpackage API_CustomField
33 *
34 */
35
36
37 /**
38 * Sets custom values for an entity.
39 *
40 * @param array $params
41 * Expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID.
42 *
43 * @example:
44 * @code
45 * // entity ID. You do not need to specify entity type, we figure it out based on the fields you're using
46 * 'entity_id' => 123,
47 * // (omitting :id) inserts or updates a field in a single-valued group
48 * 'custom_6' => 'foo',
49 * // custom_24 is checkbox or multiselect, so pass items as an array
50 * 'custom_24' => array('bar', 'baz'),
51 * // in this case custom_33 is part of a multi-valued group, and we're updating record id 5
52 * 'custom_33:5' => value,
53 * // inserts new record in multi-valued group
54 * 'custom_33:-1' => value,
55 * // inserts another new record in multi-valued group
56 * 'custom_33:-2' => value,
57 * // you can use group_name:field_name instead of ID
58 * 'custom_some_group:my_field' => 'myinfo',
59 * // updates record ID 8 in my_other_field in multi-valued some_big_group
60 * 'custom_some_big_group:my_other_field:8' => 'myinfo',
61 * @endcode
62 *
63 * @throws Exception
64 * @return array
65 * ['values' => TRUE] or ['is_error' => 1, 'error_message' => 'what went wrong']
66 */
67 function civicrm_api3_custom_value_create($params) {
68 // @todo it's not clear where the entity_table is used as CRM_Core_BAO_CustomValueTable::setValues($create)
69 // didn't seem to use it
70 // so not clear if it's relevant
71 if (!empty($params['entity_table']) && substr($params['entity_table'], 0, 7) == 'civicrm') {
72 $params['entity_table'] = substr($params['entity_table'], 8, 7);
73 }
74 $create = array('entityID' => $params['entity_id']);
75 // Translate names and
76 //Convert arrays to multi-value strings
77 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
78 foreach ($params as $id => $param) {
79 if (is_array($param)) {
80 $param = $sp . implode($sp, $param) . $sp;
81 }
82 list($c, $id) = CRM_Utils_System::explode('_', $id, 2);
83 if ($c != 'custom') {
84 continue;
85 }
86 list($i, $n, $x) = CRM_Utils_System::explode(':', $id, 3);
87 if (is_numeric($i)) {
88 $key = $i;
89 $x = $n;
90 }
91 else {
92 // Lookup names if ID was not supplied
93 $key = CRM_Core_BAO_CustomField::getCustomFieldID($n, $i);
94 if (!$key) {
95 continue;
96 }
97 }
98 if ($x && is_numeric($x)) {
99 $key .= '_' . $x;
100 }
101 $create['custom_' . $key] = $param;
102 }
103 $result = CRM_Core_BAO_CustomValueTable::setValues($create);
104 if ($result['is_error']) {
105 throw new Exception($result['error_message']);
106 }
107 return civicrm_api3_create_success(TRUE, $params);
108 }
109
110 /**
111 * Adjust Metadata for Create action.
112 *
113 * The metadata is used for setting defaults, documentation & validation.
114 *
115 * @param array $params
116 * Array or parameters determined by getfields.
117 */
118 function _civicrm_api3_custom_value_create_spec(&$params) {
119 $params['entity_id']['api.required'] = 1;
120 $params['entity_id']['title'] = 'Entity ID';
121 }
122
123 /**
124 * Use this API to get existing custom values for an entity.
125 *
126 * @param array $params
127 * Array specifying the entity_id.
128 * Optionally include entity_type param, i.e. 'entity_type' => 'Activity'
129 * If no entity_type is supplied, it will be determined based on the fields you request.
130 * If no entity_type is supplied and no fields are specified, 'Contact' will be assumed.
131 * Optionally include the desired custom data to be fetched (or else all custom data for this entity will be returned)
132 * Example: 'entity_id' => 123, 'return.custom_6' => 1, 'return.custom_33' => 1
133 * If you do not know the ID, you may use group name : field name, for example 'return.foo_stuff:my_field' => 1
134 *
135 * @throws API_Exception
136 * @return array
137 */
138 function civicrm_api3_custom_value_get($params) {
139
140 $getParams = array(
141 'entityID' => $params['entity_id'],
142 'entityType' => CRM_Utils_Array::value('entity_table', $params, ''),
143 );
144 if (strstr($getParams['entityType'], 'civicrm_')) {
145 $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8));
146 }
147 unset($params['entity_id'], $params['entity_table']);
148 foreach ($params as $id => $param) {
149 if ($param && substr($id, 0, 6) == 'return') {
150 $id = substr($id, 7);
151 list($c, $i) = CRM_Utils_System::explode('_', $id, 2);
152 if ($c == 'custom' && is_numeric($i)) {
153 $names['custom_' . $i] = 'custom_' . $i;
154 $id = $i;
155 }
156 else {
157 // Lookup names if ID was not supplied
158 list($group, $field) = CRM_Utils_System::explode(':', $id, 2);
159 $id = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
160 if (!$id) {
161 continue;
162 }
163 $names['custom_' . $id] = 'custom_' . $i;
164 }
165 $getParams['custom_' . $id] = 1;
166 }
167 }
168
169 $result = CRM_Core_BAO_CustomValueTable::getValues($getParams);
170
171 if ($result['is_error']) {
172 if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") {
173 $values = array();
174 return civicrm_api3_create_success($values, $params);
175 }
176 else {
177 throw new API_Exception($result['error_message']);
178 }
179 }
180 else {
181 $entity_id = $result['entityID'];
182 unset($result['is_error'], $result['entityID']);
183 // Convert multi-value strings to arrays
184 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
185 foreach ($result as $id => $value) {
186 if (strpos($value, $sp) !== FALSE) {
187 $value = explode($sp, trim($value, $sp));
188 }
189
190 $idArray = explode('_', $id);
191 if ($idArray[0] != 'custom') {
192 continue;
193 }
194 $fieldNumber = $idArray[1];
195 $customFieldInfo = CRM_Core_BAO_CustomField::getNameFromID($fieldNumber);
196 $info = array_pop($customFieldInfo);
197 // id is the index for returned results
198
199 if (empty($idArray[2])) {
200 $n = 0;
201 $id = $fieldNumber;
202 }
203 else {
204 $n = $idArray[2];
205 $id = $fieldNumber . "." . $idArray[2];
206 }
207 if (!empty($params['format.field_names'])) {
208 $id = $info['field_name'];
209 }
210 else {
211 $id = $fieldNumber;
212 }
213 $values[$id]['entity_id'] = $getParams['entityID'];
214 if (!empty($getParams['entityType'])) {
215 $values[$n]['entity_table'] = $getParams['entityType'];
216 }
217 //set 'latest' -useful for multi fields but set for single for consistency
218 $values[$id]['latest'] = $value;
219 $values[$id]['id'] = $id;
220 $values[$id][$n] = $value;
221 }
222 return civicrm_api3_create_success($values, $params);
223 }
224 }
225
226 /**
227 * Adjust Metadata for Get action.
228 *
229 * The metadata is used for setting defaults, documentation & validation.
230 *
231 * @param array $params
232 * Array or parameters determined by getfields.
233 */
234 function _civicrm_api3_custom_value_get_spec(&$params) {
235 $params['entity_id']['api.required'] = 1;
236 $params['entity_id']['title'] = 'Entity ID';
237 }