Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
1f4ea726 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
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 | /** | |
c28e1768 | 29 | * This api exposes CiviCRM custom value. |
6a488035 TO |
30 | * |
31 | * @package CiviCRM_APIv3 | |
6a488035 TO |
32 | */ |
33 | ||
6a488035 TO |
34 | |
35 | /** | |
36 | * Sets custom values for an entity. | |
37 | * | |
16b10e64 | 38 | * @param array $params |
cf470720 | 39 | * Expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID. |
16b10e64 | 40 | * |
c28e1768 CW |
41 | * @example: |
42 | * @code | |
16b10e64 CW |
43 | * // entity ID. You do not need to specify entity type, we figure it out based on the fields you're using |
44 | * 'entity_id' => 123, | |
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 | |
c28e1768 | 56 | * 'custom_some_group:my_field' => 'myinfo', |
16b10e64 | 57 | * // updates record ID 8 in my_other_field in multi-valued some_big_group |
c28e1768 CW |
58 | * 'custom_some_big_group:my_other_field:8' => 'myinfo', |
59 | * @endcode | |
6a488035 | 60 | * |
77b97be7 | 61 | * @throws Exception |
16b10e64 CW |
62 | * @return array |
63 | * ['values' => TRUE] or ['is_error' => 1, 'error_message' => 'what went wrong'] | |
6a488035 TO |
64 | */ |
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); | |
71 | } | |
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; | |
79 | } | |
80 | list($c, $id) = CRM_Utils_System::explode('_', $id, 2); | |
81 | if ($c != 'custom') { | |
82 | continue; | |
83 | } | |
84 | list($i, $n, $x) = CRM_Utils_System::explode(':', $id, 3); | |
85 | if (is_numeric($i)) { | |
86 | $key = $i; | |
87 | $x = $n; | |
88 | } | |
89 | else { | |
90 | // Lookup names if ID was not supplied | |
91 | $key = CRM_Core_BAO_CustomField::getCustomFieldID($n, $i); | |
92 | if (!$key) { | |
93 | continue; | |
94 | } | |
95 | } | |
96 | if ($x && is_numeric($x)) { | |
97 | $key .= '_' . $x; | |
98 | } | |
99 | $create['custom_' . $key] = $param; | |
100 | } | |
101 | $result = CRM_Core_BAO_CustomValueTable::setValues($create); | |
102 | if ($result['is_error']) { | |
103 | throw new Exception($result['error_message']); | |
104 | } | |
244bbdd8 | 105 | return civicrm_api3_create_success(TRUE, $params, 'CustomValue'); |
6a488035 | 106 | } |
11e09c59 TO |
107 | |
108 | /** | |
0aa0303c EM |
109 | * Adjust Metadata for Create action. |
110 | * | |
111 | * The metadata is used for setting defaults, documentation & validation. | |
11e09c59 | 112 | * |
cf470720 | 113 | * @param array $params |
b081365f | 114 | * Array of parameters determined by getfields. |
11e09c59 | 115 | */ |
6a488035 TO |
116 | function _civicrm_api3_custom_value_create_spec(&$params) { |
117 | $params['entity_id']['api.required'] = 1; | |
1fdb479f | 118 | $params['entity_id']['title'] = 'Entity ID'; |
6a488035 | 119 | } |
77b97be7 | 120 | |
6a488035 TO |
121 | /** |
122 | * Use this API to get existing custom values for an entity. | |
123 | * | |
16b10e64 | 124 | * @param array $params |
cf470720 | 125 | * Array specifying the entity_id. |
16b10e64 CW |
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 | |
6a488035 | 132 | * |
77b97be7 | 133 | * @throws API_Exception |
a6c01b45 | 134 | * @return array |
11e09c59 | 135 | */ |
6a488035 TO |
136 | function civicrm_api3_custom_value_get($params) { |
137 | ||
138 | $getParams = array( | |
139 | 'entityID' => $params['entity_id'], | |
140 | 'entityType' => CRM_Utils_Array::value('entity_table', $params, ''), | |
141 | ); | |
142 | if (strstr($getParams['entityType'], 'civicrm_')) { | |
143 | $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8)); | |
144 | } | |
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; | |
152 | $id = $i; | |
153 | } | |
154 | else { | |
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); | |
158 | if (!$id) { | |
159 | continue; | |
160 | } | |
161 | $names['custom_' . $id] = 'custom_' . $i; | |
162 | } | |
163 | $getParams['custom_' . $id] = 1; | |
164 | } | |
165 | } | |
166 | ||
167 | $result = CRM_Core_BAO_CustomValueTable::getValues($getParams); | |
168 | ||
169 | if ($result['is_error']) { | |
170 | if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") { | |
171 | $values = array(); | |
244bbdd8 | 172 | return civicrm_api3_create_success($values, $params, 'CustomValue'); |
6a488035 TO |
173 | } |
174 | else { | |
b422b715 | 175 | throw new API_Exception($result['error_message']); |
6a488035 TO |
176 | } |
177 | } | |
178 | else { | |
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)); | |
186 | } | |
187 | ||
188 | $idArray = explode('_', $id); | |
189 | if ($idArray[0] != 'custom') { | |
190 | continue; | |
191 | } | |
192 | $fieldNumber = $idArray[1]; | |
512cceb4 PJ |
193 | $customFieldInfo = CRM_Core_BAO_CustomField::getNameFromID($fieldNumber); |
194 | $info = array_pop($customFieldInfo); | |
6a488035 TO |
195 | // id is the index for returned results |
196 | ||
197 | if (empty($idArray[2])) { | |
198 | $n = 0; | |
199 | $id = $fieldNumber; | |
200 | } | |
92e4c2a5 | 201 | else { |
6a488035 TO |
202 | $n = $idArray[2]; |
203 | $id = $fieldNumber . "." . $idArray[2]; | |
204 | } | |
a7488080 | 205 | if (!empty($params['format.field_names'])) { |
6a488035 TO |
206 | $id = $info['field_name']; |
207 | } | |
208 | else { | |
209 | $id = $fieldNumber; | |
210 | } | |
211 | $values[$id]['entity_id'] = $getParams['entityID']; | |
a7488080 | 212 | if (!empty($getParams['entityType'])) { |
4f2aa1a2 | 213 | $values[$id]['entity_table'] = $getParams['entityType']; |
6a488035 TO |
214 | } |
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; | |
219 | } | |
244bbdd8 | 220 | return civicrm_api3_create_success($values, $params, 'CustomValue'); |
6a488035 TO |
221 | } |
222 | } | |
223 | ||
11e09c59 | 224 | /** |
dc64d047 | 225 | * Adjust Metadata for Get action. |
11e09c59 | 226 | * |
0aa0303c EM |
227 | * The metadata is used for setting defaults, documentation & validation. |
228 | * | |
cf470720 | 229 | * @param array $params |
b081365f | 230 | * Array of parameters determined by getfields. |
11e09c59 | 231 | */ |
6a488035 TO |
232 | function _civicrm_api3_custom_value_get_spec(&$params) { |
233 | $params['entity_id']['api.required'] = 1; | |
4c41ecb2 | 234 | $params['entity_id']['title'] = 'Entity ID'; |
232624b1 | 235 | } |
24871985 CW |
236 | |
237 | /** | |
238 | * CustomValue.gettree API specification | |
239 | * | |
240 | * @param array $spec description of fields supported by this API call | |
241 | * @return void | |
242 | */ | |
243 | function _civicrm_api3_custom_value_gettree_spec(&$spec) { | |
244 | $spec['entity_id'] = array( | |
245 | 'title' => 'Entity Id', | |
246 | 'description' => 'Id of entity', | |
247 | 'type' => CRM_Utils_Type::T_INT, | |
248 | 'api.required' => 1, | |
249 | ); | |
250 | $entities = civicrm_api3('Entity', 'get'); | |
251 | $entities = array_diff($entities['values'], $entities['deprecated']); | |
252 | $spec['entity_type'] = array( | |
253 | 'title' => 'Entity Type', | |
254 | 'description' => 'API name of entity type, e.g. "Contact"', | |
255 | 'type' => CRM_Utils_Type::T_STRING, | |
256 | 'api.required' => 1, | |
257 | 'options' => array_combine($entities, $entities), | |
258 | ); | |
0b330e6d CW |
259 | // Return params for custom group, field & value |
260 | foreach (CRM_Core_DAO_CustomGroup::fields() as $field) { | |
261 | $name = 'custom_group.' . $field['name']; | |
262 | $spec[$name] = array('name' => $name) + $field; | |
263 | } | |
264 | foreach (CRM_Core_DAO_CustomField::fields() as $field) { | |
265 | $name = 'custom_field.' . $field['name']; | |
266 | $spec[$name] = array('name' => $name) + $field; | |
267 | } | |
268 | $spec['custom_value.id'] = array( | |
269 | 'title' => 'Custom Value Id', | |
270 | 'description' => 'Id of record in custom value table', | |
271 | 'type' => CRM_Utils_Type::T_INT, | |
272 | ); | |
273 | $spec['custom_value.data'] = array( | |
274 | 'title' => 'Custom Value (Raw)', | |
275 | 'description' => 'Raw value as stored in the database', | |
276 | 'type' => CRM_Utils_Type::T_STRING, | |
277 | ); | |
278 | $spec['custom_value.display'] = array( | |
279 | 'title' => 'Custom Value (Formatted)', | |
280 | 'description' => 'Custom value formatted for display', | |
281 | 'type' => CRM_Utils_Type::T_STRING, | |
282 | ); | |
24871985 CW |
283 | } |
284 | ||
285 | /** | |
286 | * CustomValue.gettree API | |
287 | * | |
288 | * @param array $params | |
289 | * @return array API result | |
290 | * @throws API_Exception | |
291 | */ | |
292 | function civicrm_api3_custom_value_gettree($params) { | |
293 | $ret = array(); | |
294 | $options = _civicrm_api3_get_options_from_params($params); | |
295 | $toReturn = array( | |
296 | 'custom_group' => array(), | |
297 | 'custom_field' => array(), | |
298 | 'custom_value' => array(), | |
299 | ); | |
300 | foreach (array_keys($options['return']) as $r) { | |
301 | list($type, $field) = explode('.', $r); | |
302 | if (isset($toReturn[$type])) { | |
303 | $toReturn[$type][] = $field; | |
304 | } | |
305 | } | |
0b330e6d CW |
306 | // We must have a name if not indexing sequentially |
307 | if (empty($params['sequential']) && $toReturn['custom_field']) { | |
308 | $toReturn['custom_field'][] = 'name'; | |
309 | } | |
24871985 CW |
310 | switch ($params['entity_type']) { |
311 | case 'Contact': | |
312 | $ret = array('entityType' => 'contact_type', 'subTypes' => 'contact_sub_type'); | |
313 | break; | |
314 | ||
315 | case 'Activity': | |
316 | case 'Campaign': | |
317 | case 'Case': | |
318 | case 'Contribution': | |
319 | case 'Event': | |
320 | case 'Grant': | |
321 | case 'Membership': | |
322 | case 'Relationship': | |
323 | $ret = array('subTypes' => strtolower($params['entity_type']) . '_type_id'); | |
324 | break; | |
325 | ||
326 | case 'Participant': | |
327 | // todo | |
328 | } | |
329 | $treeParams = array( | |
330 | 'entityType' => $params['entity_type'], | |
331 | 'subTypes' => array(), | |
332 | 'subName' => NULL, | |
333 | ); | |
334 | // Fetch entity data for custom group type/sub-type | |
335 | // Also verify access permissions (api3 will throw an exception if permission denied) | |
336 | if ($ret || !empty($params['check_permissions'])) { | |
337 | $entityData = civicrm_api3($params['entity_type'], 'getsingle', array( | |
338 | 'id' => $params['entity_id'], | |
339 | 'return' => array_merge(array('id'), array_values($ret)), | |
340 | )); | |
341 | foreach ($ret as $param => $key) { | |
342 | if (isset($entityData[$key])) { | |
343 | $treeParams[$param] = $entityData[$key]; | |
344 | } | |
345 | } | |
346 | } | |
0b330e6d | 347 | $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)); |
24871985 CW |
348 | unset($tree['info']); |
349 | $result = array(); | |
350 | foreach ($tree as $group) { | |
351 | $result[$group['name']] = array(); | |
352 | $groupToReturn = $toReturn['custom_group'] ? $toReturn['custom_group'] : array_keys($group); | |
353 | foreach ($groupToReturn as $item) { | |
354 | $result[$group['name']][$item] = CRM_Utils_Array::value($item, $group); | |
355 | } | |
356 | $result[$group['name']]['fields'] = array(); | |
357 | foreach ($group['fields'] as $fieldInfo) { | |
358 | $field = array('value' => NULL); | |
359 | $fieldToReturn = $toReturn['custom_field'] ? $toReturn['custom_field'] : array_keys($fieldInfo); | |
360 | foreach ($fieldToReturn as $item) { | |
361 | $field[$item] = CRM_Utils_Array::value($item, $fieldInfo); | |
362 | } | |
363 | unset($field['customValue']); | |
364 | if (!empty($fieldInfo['customValue'])) { | |
365 | $field['value'] = CRM_Utils_Array::first($fieldInfo['customValue']); | |
e6446db2 CW |
366 | if (!$toReturn['custom_value'] || in_array('display', $toReturn['custom_value'])) { |
367 | $field['value']['display'] = CRM_Core_BAO_CustomField::displayValue($field['value']['data'], $fieldInfo); | |
368 | } | |
24871985 CW |
369 | foreach (array_keys($field['value']) as $key) { |
370 | if ($toReturn['custom_value'] && !in_array($key, $toReturn['custom_value'])) { | |
371 | unset($field['value'][$key]); | |
372 | } | |
373 | } | |
24871985 CW |
374 | } |
375 | if (empty($params['sequential'])) { | |
376 | $result[$group['name']]['fields'][$fieldInfo['name']] = $field; | |
377 | } | |
378 | else { | |
379 | $result[$group['name']]['fields'][] = $field; | |
380 | } | |
381 | } | |
382 | } | |
383 | return civicrm_api3_create_success($result, $params, 'CustomValue', 'gettree'); | |
384 | } |