Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | * Sets custom values for an entity. | |
36 | * | |
16b10e64 | 37 | * @param array $params |
cf470720 | 38 | * Expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID. |
16b10e64 | 39 | * |
c28e1768 CW |
40 | * @example: |
41 | * @code | |
16b10e64 CW |
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 | |
c28e1768 | 55 | * 'custom_some_group:my_field' => 'myinfo', |
16b10e64 | 56 | * // updates record ID 8 in my_other_field in multi-valued some_big_group |
c28e1768 CW |
57 | * 'custom_some_big_group:my_other_field:8' => 'myinfo', |
58 | * @endcode | |
6a488035 | 59 | * |
77b97be7 | 60 | * @throws Exception |
16b10e64 CW |
61 | * @return array |
62 | * ['values' => TRUE] or ['is_error' => 1, 'error_message' => 'what went wrong'] | |
6a488035 TO |
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 | } | |
cf8f0fff | 71 | $create = ['entityID' => $params['entity_id']]; |
6a488035 TO |
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 | } | |
244bbdd8 | 104 | return civicrm_api3_create_success(TRUE, $params, 'CustomValue'); |
6a488035 | 105 | } |
11e09c59 TO |
106 | |
107 | /** | |
0aa0303c EM |
108 | * Adjust Metadata for Create action. |
109 | * | |
110 | * The metadata is used for setting defaults, documentation & validation. | |
11e09c59 | 111 | * |
cf470720 | 112 | * @param array $params |
b081365f | 113 | * Array of parameters determined by getfields. |
11e09c59 | 114 | */ |
6a488035 TO |
115 | function _civicrm_api3_custom_value_create_spec(&$params) { |
116 | $params['entity_id']['api.required'] = 1; | |
1fdb479f | 117 | $params['entity_id']['title'] = 'Entity ID'; |
6a488035 | 118 | } |
77b97be7 | 119 | |
6a488035 TO |
120 | /** |
121 | * Use this API to get existing custom values for an entity. | |
122 | * | |
16b10e64 | 123 | * @param array $params |
cf470720 | 124 | * Array specifying the entity_id. |
16b10e64 CW |
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 | |
6a488035 | 131 | * |
77b97be7 | 132 | * @throws API_Exception |
a6c01b45 | 133 | * @return array |
11e09c59 | 134 | */ |
6a488035 TO |
135 | function civicrm_api3_custom_value_get($params) { |
136 | ||
cf8f0fff | 137 | $getParams = [ |
6a488035 TO |
138 | 'entityID' => $params['entity_id'], |
139 | 'entityType' => CRM_Utils_Array::value('entity_table', $params, ''), | |
cf8f0fff | 140 | ]; |
6a488035 TO |
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') { | |
bc3c9f57 JP |
147 | $returnVal = $param; |
148 | if (!empty(substr($id, 7))) { | |
149 | $returnVal = substr($id, 7); | |
6a488035 | 150 | } |
55000cc7 T |
151 | if (!is_array($returnVal)) { |
152 | $returnVal = explode(',', $returnVal); | |
153 | } | |
154 | foreach ($returnVal as $value) { | |
bc3c9f57 JP |
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; | |
6a488035 | 159 | } |
bc3c9f57 JP |
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; | |
6a488035 | 170 | } |
6a488035 TO |
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).") { | |
cf8f0fff | 178 | $values = []; |
244bbdd8 | 179 | return civicrm_api3_create_success($values, $params, 'CustomValue'); |
6a488035 TO |
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 | } | |
92e4c2a5 | 208 | else { |
6a488035 TO |
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'])) { |
4f2aa1a2 | 220 | $values[$id]['entity_table'] = $getParams['entityType']; |
6a488035 TO |
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 | } | |
244bbdd8 | 227 | return civicrm_api3_create_success($values, $params, 'CustomValue'); |
6a488035 TO |
228 | } |
229 | } | |
230 | ||
11e09c59 | 231 | /** |
dc64d047 | 232 | * Adjust Metadata for Get action. |
11e09c59 | 233 | * |
0aa0303c EM |
234 | * The metadata is used for setting defaults, documentation & validation. |
235 | * | |
cf470720 | 236 | * @param array $params |
b081365f | 237 | * Array of parameters determined by getfields. |
11e09c59 | 238 | */ |
6a488035 TO |
239 | function _civicrm_api3_custom_value_get_spec(&$params) { |
240 | $params['entity_id']['api.required'] = 1; | |
4c41ecb2 | 241 | $params['entity_id']['title'] = 'Entity ID'; |
232624b1 | 242 | } |
24871985 CW |
243 | |
244 | /** | |
245 | * CustomValue.gettree API specification | |
246 | * | |
247 | * @param array $spec description of fields supported by this API call | |
aa965915 MWMC |
248 | * |
249 | * @throws \CiviCRM_API3_Exception | |
24871985 CW |
250 | */ |
251 | function _civicrm_api3_custom_value_gettree_spec(&$spec) { | |
cf8f0fff | 252 | $spec['entity_id'] = [ |
24871985 CW |
253 | 'title' => 'Entity Id', |
254 | 'description' => 'Id of entity', | |
255 | 'type' => CRM_Utils_Type::T_INT, | |
256 | 'api.required' => 1, | |
cf8f0fff | 257 | ]; |
24871985 CW |
258 | $entities = civicrm_api3('Entity', 'get'); |
259 | $entities = array_diff($entities['values'], $entities['deprecated']); | |
cf8f0fff | 260 | $spec['entity_type'] = [ |
24871985 CW |
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), | |
cf8f0fff | 266 | ]; |
0b330e6d CW |
267 | // Return params for custom group, field & value |
268 | foreach (CRM_Core_DAO_CustomGroup::fields() as $field) { | |
269 | $name = 'custom_group.' . $field['name']; | |
cf8f0fff | 270 | $spec[$name] = ['name' => $name] + $field; |
0b330e6d CW |
271 | } |
272 | foreach (CRM_Core_DAO_CustomField::fields() as $field) { | |
273 | $name = 'custom_field.' . $field['name']; | |
cf8f0fff | 274 | $spec[$name] = ['name' => $name] + $field; |
0b330e6d | 275 | } |
cf8f0fff | 276 | $spec['custom_value.id'] = [ |
0b330e6d CW |
277 | 'title' => 'Custom Value Id', |
278 | 'description' => 'Id of record in custom value table', | |
279 | 'type' => CRM_Utils_Type::T_INT, | |
cf8f0fff CW |
280 | ]; |
281 | $spec['custom_value.data'] = [ | |
0b330e6d CW |
282 | 'title' => 'Custom Value (Raw)', |
283 | 'description' => 'Raw value as stored in the database', | |
284 | 'type' => CRM_Utils_Type::T_STRING, | |
cf8f0fff CW |
285 | ]; |
286 | $spec['custom_value.display'] = [ | |
0b330e6d CW |
287 | 'title' => 'Custom Value (Formatted)', |
288 | 'description' => 'Custom value formatted for display', | |
289 | 'type' => CRM_Utils_Type::T_STRING, | |
cf8f0fff | 290 | ]; |
24871985 CW |
291 | } |
292 | ||
293 | /** | |
294 | * CustomValue.gettree API | |
295 | * | |
296 | * @param array $params | |
aa965915 | 297 | * |
24871985 | 298 | * @return array API result |
aa965915 MWMC |
299 | * @throws \API_Exception |
300 | * @throws \CRM_Core_Exception | |
301 | * @throws \CiviCRM_API3_Exception | |
24871985 CW |
302 | */ |
303 | function civicrm_api3_custom_value_gettree($params) { | |
cf8f0fff | 304 | $ret = []; |
24871985 | 305 | $options = _civicrm_api3_get_options_from_params($params); |
cf8f0fff CW |
306 | $toReturn = [ |
307 | 'custom_group' => [], | |
308 | 'custom_field' => [], | |
309 | 'custom_value' => [], | |
310 | ]; | |
24871985 CW |
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 | } | |
0b330e6d CW |
317 | // We must have a name if not indexing sequentially |
318 | if (empty($params['sequential']) && $toReturn['custom_field']) { | |
319 | $toReturn['custom_field'][] = 'name'; | |
320 | } | |
24871985 CW |
321 | switch ($params['entity_type']) { |
322 | case 'Contact': | |
cf8f0fff | 323 | $ret = ['entityType' => 'contact_type', 'subTypes' => 'contact_sub_type']; |
24871985 CW |
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': | |
cf8f0fff | 334 | $ret = ['subTypes' => strtolower($params['entity_type']) . '_type_id']; |
24871985 CW |
335 | break; |
336 | ||
337 | case 'Participant': | |
338 | // todo | |
339 | } | |
cf8f0fff | 340 | $treeParams = [ |
24871985 | 341 | 'entityType' => $params['entity_type'], |
cf8f0fff | 342 | 'subTypes' => [], |
24871985 | 343 | 'subName' => NULL, |
cf8f0fff | 344 | ]; |
24871985 CW |
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'])) { | |
cf8f0fff | 348 | $entityData = civicrm_api3($params['entity_type'], 'getsingle', [ |
24871985 | 349 | 'id' => $params['entity_id'], |
f26fa703 | 350 | 'check_permissions' => !empty($params['check_permissions']), |
cf8f0fff CW |
351 | 'return' => array_merge(['id'], array_values($ret)), |
352 | ]); | |
24871985 CW |
353 | foreach ($ret as $param => $key) { |
354 | if (isset($entityData[$key])) { | |
355 | $treeParams[$param] = $entityData[$key]; | |
356 | } | |
357 | } | |
358 | } | |
0b330e6d | 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)); |
24871985 | 360 | unset($tree['info']); |
cf8f0fff | 361 | $result = []; |
24871985 | 362 | foreach ($tree as $group) { |
cf8f0fff | 363 | $result[$group['name']] = []; |
24871985 CW |
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 | } | |
cf8f0fff | 368 | $result[$group['name']]['fields'] = []; |
24871985 | 369 | foreach ($group['fields'] as $fieldInfo) { |
cf8f0fff | 370 | $field = ['value' => NULL]; |
24871985 CW |
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']); | |
e6446db2 CW |
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 | } | |
24871985 CW |
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 | } | |
24871985 CW |
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 | } |