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