Merge pull request #3971 from davecivicrm/CRM-15121
[civicrm-core.git] / api / v3 / CustomValue.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
731a0992 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
731a0992 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29/**
30 * File for the CiviCRM APIv3 custom value functions
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_CustomField
34 *
731a0992 35 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
36 * @version $Id: CustomField.php 30879 2010-11-22 15:45:55Z shot $
37 */
38
39/**
40 * Files required for this package
41 */
42
43
44/**
45 * Sets custom values for an entity.
46 *
47 *
48 * @param $params expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID
49 * for example:
77b97be7 50 * // entity ID. You do not need to specify entity type, we figure it out based on the fields you're using
6a488035 51 * 'entity_id' => 123,
77b97be7 52 * // (omitting :id) inserts or updates a field in a single-valued group
6a488035 53 * 'custom_6' => 'foo',
77b97be7 54 * // custom_24 is checkbox or multiselect, so pass items as an array
6a488035 55 * 'custom_24' => array('bar', 'baz'),
77b97be7 56 * // in this case custom_33 is part of a multi-valued group, and we're updating record id 5
6a488035 57 * 'custom_33:5' => value,
77b97be7 58 * // inserts new record in multi-valued group
6a488035 59 * 'custom_33:-1' => value,
77b97be7 60 * // inserts another new record in multi-valued group
6a488035 61 * 'custom_33:-2' => value,
77b97be7 62 * // you can use group_name:field_name instead of ID
6a488035 63 * 'custom_some_group:my_field => 'myinfo',
77b97be7 64 * // updates record ID 8 in my_other_field in multi-valued some_big_group
6a488035
TO
65 * 'custom_some_big_group:my_other_field:8 => 'myinfo',
66 *
67 *
77b97be7 68 * @throws Exception
6a488035
TO
69 * @return array('values' => TRUE) or array('is_error' => 1, 'error_message' => 'what went wrong')
70 *
71 * @access public
6a488035
TO
72 */
73function civicrm_api3_custom_value_create($params) {
74 // @todo it's not clear where the entity_table is used as CRM_Core_BAO_CustomValueTable::setValues($create)
75 // didn't seem to use it
76 // so not clear if it's relevant
77 if (!empty($params['entity_table']) && substr($params['entity_table'], 0, 7) == 'civicrm') {
78 $params['entity_table'] = substr($params['entity_table'], 8, 7);
79 }
80 $create = array('entityID' => $params['entity_id']);
81 // Translate names and
82 //Convert arrays to multi-value strings
83 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
84 foreach ($params as $id => $param) {
85 if (is_array($param)) {
86 $param = $sp . implode($sp, $param) . $sp;
87 }
88 list($c, $id) = CRM_Utils_System::explode('_', $id, 2);
89 if ($c != 'custom') {
90 continue;
91 }
92 list($i, $n, $x) = CRM_Utils_System::explode(':', $id, 3);
93 if (is_numeric($i)) {
94 $key = $i;
95 $x = $n;
96 }
97 else {
98 // Lookup names if ID was not supplied
99 $key = CRM_Core_BAO_CustomField::getCustomFieldID($n, $i);
100 if (!$key) {
101 continue;
102 }
103 }
104 if ($x && is_numeric($x)) {
105 $key .= '_' . $x;
106 }
107 $create['custom_' . $key] = $param;
108 }
109 $result = CRM_Core_BAO_CustomValueTable::setValues($create);
110 if ($result['is_error']) {
111 throw new Exception($result['error_message']);
112 }
113 return civicrm_api3_create_success(TRUE, $params);
114}
11e09c59
TO
115
116/**
6a488035 117 * Adjust Metadata for Create action
11e09c59
TO
118 *
119 * The metadata is used for setting defaults, documentation & validation
120 * @param array $params array or parameters determined by getfields
121 */
6a488035
TO
122function _civicrm_api3_custom_value_create_spec(&$params) {
123 $params['entity_id']['api.required'] = 1;
1fdb479f 124 $params['entity_id']['title'] = 'Entity ID';
6a488035 125}
77b97be7 126
6a488035
TO
127/**
128 * Use this API to get existing custom values for an entity.
129 *
130 * @param $params array specifying the entity_id
131 * Optionally include entity_type param, i.e. 'entity_type' => 'Activity'
132 * If no entity_type is supplied, it will be determined based on the fields you request.
133 * If no entity_type is supplied and no fields are specified, 'Contact' will be assumed.
134 * Optionally include the desired custom data to be fetched (or else all custom data for this entity will be returned)
135 * Example: 'entity_id' => 123, 'return.custom_6' => 1, 'return.custom_33' => 1
136 * If you do not know the ID, you may use group name : field name, for example 'return.foo_stuff:my_field' => 1
137 *
77b97be7 138 * @throws API_Exception
6a488035
TO
139 * @return array.
140 *
141 * @access public
11e09c59 142 */
6a488035
TO
143function civicrm_api3_custom_value_get($params) {
144
145 $getParams = array(
146 'entityID' => $params['entity_id'],
147 'entityType' => CRM_Utils_Array::value('entity_table', $params, ''),
148 );
149 if (strstr($getParams['entityType'], 'civicrm_')) {
150 $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8));
151 }
152 unset($params['entity_id'], $params['entity_table']);
153 foreach ($params as $id => $param) {
154 if ($param && substr($id, 0, 6) == 'return') {
155 $id = substr($id, 7);
156 list($c, $i) = CRM_Utils_System::explode('_', $id, 2);
157 if ($c == 'custom' && is_numeric($i)) {
158 $names['custom_' . $i] = 'custom_' . $i;
159 $id = $i;
160 }
161 else {
162 // Lookup names if ID was not supplied
163 list($group, $field) = CRM_Utils_System::explode(':', $id, 2);
164 $id = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
165 if (!$id) {
166 continue;
167 }
168 $names['custom_' . $id] = 'custom_' . $i;
169 }
170 $getParams['custom_' . $id] = 1;
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 = array();
179 return civicrm_api3_create_success($values, $params);
180 }
181 else {
b422b715 182 throw new API_Exception($result['error_message']);
6a488035
TO
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];
512cceb4
PJ
200 $customFieldInfo = CRM_Core_BAO_CustomField::getNameFromID($fieldNumber);
201 $info = array_pop($customFieldInfo);
6a488035
TO
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 }
a7488080 212 if (!empty($params['format.field_names'])) {
6a488035
TO
213 $id = $info['field_name'];
214 }
215 else {
216 $id = $fieldNumber;
217 }
218 $values[$id]['entity_id'] = $getParams['entityID'];
a7488080 219 if (!empty($getParams['entityType'])) {
6a488035
TO
220 $values[$n]['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);
228 }
229}
230
11e09c59 231/**
6a488035 232 * Adjust Metadata for Get action
11e09c59
TO
233 *
234 * The metadata is used for setting defaults, documentation & validation
235 * @param array $params array or parameters determined by getfields
236 */
6a488035
TO
237function _civicrm_api3_custom_value_get_spec(&$params) {
238 $params['entity_id']['api.required'] = 1;
4c41ecb2 239 $params['entity_id']['title'] = 'Entity ID';
232624b1 240}