dev/core#1409 Remove net_amount from Addtional Payment form
[civicrm-core.git] / api / v3 / CustomValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * Sets custom values for an entity.
36 *
37 * @param array $params
38 * Expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID.
39 *
40 * @example:
41 * @code
42 * // entity ID. You do not need to specify entity type, we figure it out based on the fields you're using
43 * 'entity_id' => 123,
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',
58 * @endcode
59 *
60 * @throws Exception
61 * @return array
62 * ['values' => TRUE] or ['is_error' => 1, 'error_message' => 'what went wrong']
63 */
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);
70 }
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;
78 }
79 list($c, $id) = CRM_Utils_System::explode('_', $id, 2);
80 if ($c != 'custom') {
81 continue;
82 }
83 list($i, $n, $x) = CRM_Utils_System::explode(':', $id, 3);
84 if (is_numeric($i)) {
85 $key = $i;
86 $x = $n;
87 }
88 else {
89 // Lookup names if ID was not supplied
90 $key = CRM_Core_BAO_CustomField::getCustomFieldID($n, $i);
91 if (!$key) {
92 continue;
93 }
94 }
95 if ($x && is_numeric($x)) {
96 $key .= '_' . $x;
97 }
98 $create['custom_' . $key] = $param;
99 }
100 $result = CRM_Core_BAO_CustomValueTable::setValues($create);
101 if ($result['is_error']) {
102 throw new Exception($result['error_message']);
103 }
104 return civicrm_api3_create_success(TRUE, $params, 'CustomValue');
105 }
106
107 /**
108 * Adjust Metadata for Create action.
109 *
110 * The metadata is used for setting defaults, documentation & validation.
111 *
112 * @param array $params
113 * Array of parameters determined by getfields.
114 */
115 function _civicrm_api3_custom_value_create_spec(&$params) {
116 $params['entity_id']['api.required'] = 1;
117 $params['entity_id']['title'] = 'Entity ID';
118 }
119
120 /**
121 * Use this API to get existing custom values for an entity.
122 *
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
131 *
132 * @throws API_Exception
133 * @return array
134 */
135 function civicrm_api3_custom_value_get($params) {
136
137 $getParams = [
138 'entityID' => $params['entity_id'],
139 'entityType' => CRM_Utils_Array::value('entity_table', $params, ''),
140 ];
141 if (strstr($getParams['entityType'], 'civicrm_')) {
142 $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8));
143 }
144 unset($params['entity_id'], $params['entity_table']);
145 foreach ($params as $id => $param) {
146 if ($param && substr($id, 0, 6) == 'return') {
147 $returnVal = $param;
148 if (!empty(substr($id, 7))) {
149 $returnVal = substr($id, 7);
150 }
151 if (!is_array($returnVal)) {
152 $returnVal = explode(',', $returnVal);
153 }
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;
158 $fldId = $i;
159 }
160 else {
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);
164 if (!$fldId) {
165 continue;
166 }
167 $names['custom_' . $fldId] = 'custom_' . $i;
168 }
169 $getParams['custom_' . $fldId] = 1;
170 }
171 }
172 }
173
174 $result = CRM_Core_BAO_CustomValueTable::getValues($getParams);
175
176 if ($result['is_error']) {
177 if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") {
178 $values = [];
179 return civicrm_api3_create_success($values, $params, 'CustomValue');
180 }
181 else {
182 throw new API_Exception($result['error_message']);
183 }
184 }
185 else {
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));
193 }
194
195 $idArray = explode('_', $id);
196 if ($idArray[0] != 'custom') {
197 continue;
198 }
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
203
204 if (empty($idArray[2])) {
205 $n = 0;
206 $id = $fieldNumber;
207 }
208 else {
209 $n = $idArray[2];
210 $id = $fieldNumber . "." . $idArray[2];
211 }
212 if (!empty($params['format.field_names'])) {
213 $id = $info['field_name'];
214 }
215 else {
216 $id = $fieldNumber;
217 }
218 $values[$id]['entity_id'] = $getParams['entityID'];
219 if (!empty($getParams['entityType'])) {
220 $values[$id]['entity_table'] = $getParams['entityType'];
221 }
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;
226 }
227 return civicrm_api3_create_success($values, $params, 'CustomValue');
228 }
229 }
230
231 /**
232 * Adjust Metadata for Get action.
233 *
234 * The metadata is used for setting defaults, documentation & validation.
235 *
236 * @param array $params
237 * Array of parameters determined by getfields.
238 */
239 function _civicrm_api3_custom_value_get_spec(&$params) {
240 $params['entity_id']['api.required'] = 1;
241 $params['entity_id']['title'] = 'Entity ID';
242 }
243
244 /**
245 * CustomValue.gettree API specification
246 *
247 * @param array $spec description of fields supported by this API call
248 *
249 * @throws \CiviCRM_API3_Exception
250 */
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,
256 'api.required' => 1,
257 ];
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,
264 'api.required' => 1,
265 'options' => array_combine($entities, $entities),
266 ];
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;
271 }
272 foreach (CRM_Core_DAO_CustomField::fields() as $field) {
273 $name = 'custom_field.' . $field['name'];
274 $spec[$name] = ['name' => $name] + $field;
275 }
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,
280 ];
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,
285 ];
286 $spec['custom_value.display'] = [
287 'title' => 'Custom Value (Formatted)',
288 'description' => 'Custom value formatted for display',
289 'type' => CRM_Utils_Type::T_STRING,
290 ];
291 }
292
293 /**
294 * CustomValue.gettree API
295 *
296 * @param array $params
297 *
298 * @return array API result
299 * @throws \API_Exception
300 * @throws \CRM_Core_Exception
301 * @throws \CiviCRM_API3_Exception
302 */
303 function civicrm_api3_custom_value_gettree($params) {
304 $ret = [];
305 $options = _civicrm_api3_get_options_from_params($params);
306 $toReturn = [
307 'custom_group' => [],
308 'custom_field' => [],
309 'custom_value' => [],
310 ];
311 foreach (array_keys($options['return']) as $r) {
312 list($type, $field) = explode('.', $r);
313 if (isset($toReturn[$type])) {
314 $toReturn[$type][] = $field;
315 }
316 }
317 // We must have a name if not indexing sequentially
318 if (empty($params['sequential']) && $toReturn['custom_field']) {
319 $toReturn['custom_field'][] = 'name';
320 }
321 switch ($params['entity_type']) {
322 case 'Contact':
323 $ret = ['entityType' => 'contact_type', 'subTypes' => 'contact_sub_type'];
324 break;
325
326 case 'Activity':
327 case 'Campaign':
328 case 'Case':
329 case 'Contribution':
330 case 'Event':
331 case 'Grant':
332 case 'Membership':
333 case 'Relationship':
334 $ret = ['subTypes' => strtolower($params['entity_type']) . '_type_id'];
335 break;
336
337 case 'Participant':
338 // todo
339 }
340 $treeParams = [
341 'entityType' => $params['entity_type'],
342 'subTypes' => [],
343 'subName' => NULL,
344 ];
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)),
352 ]);
353 foreach ($ret as $param => $key) {
354 if (isset($entityData[$key])) {
355 $treeParams[$param] = $entityData[$key];
356 }
357 }
358 }
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']);
361 $result = [];
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);
367 }
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);
374 }
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);
380 }
381 foreach (array_keys($field['value']) as $key) {
382 if ($toReturn['custom_value'] && !in_array($key, $toReturn['custom_value'])) {
383 unset($field['value'][$key]);
384 }
385 }
386 }
387 if (empty($params['sequential'])) {
388 $result[$group['name']]['fields'][$fieldInfo['name']] = $field;
389 }
390 else {
391 $result[$group['name']]['fields'][] = $field;
392 }
393 }
394 }
395 return civicrm_api3_create_success($result, $params, 'CustomValue', 'gettree');
396 }
397
398 /**
399 * CustomValue.getdisplayvalue API specification
400 *
401 * @param array $spec description of fields supported by this API call
402 */
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,
408 'api.required' => 1,
409 ];
410 $spec['custom_field_id'] = [
411 'title' => 'Custom Field ID',
412 'description' => 'Id of custom field',
413 'type' => CRM_Utils_Type::T_INT,
414 'api.required' => 1,
415 ];
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,
420 'api.required' => 0,
421 ];
422 }
423
424 /**
425 * CustomValue.getdisplayvalue API
426 *
427 * @param array $params
428 *
429 * @return array API result
430 * @throws \CiviCRM_API3_Exception
431 */
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'],
437 ]);
438 $params['custom_field_value'] = $params['custom_field_value']['latest'];
439 }
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');
443 }