INFRA-132 - Docblock @param and @return tag fixes
[civicrm-core.git] / api / v3 / CustomValue.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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 *
35 * @copyright CiviCRM LLC (c) 2004-2014
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
49 * Expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID.
50 * for example:
51 * // entity ID. You do not need to specify entity type, we figure it out based on the fields you're using
52 * 'entity_id' => 123,
53 * // (omitting :id) inserts or updates a field in a single-valued group
54 * 'custom_6' => 'foo',
55 * // custom_24 is checkbox or multiselect, so pass items as an array
56 * 'custom_24' => array('bar', 'baz'),
57 * // in this case custom_33 is part of a multi-valued group, and we're updating record id 5
58 * 'custom_33:5' => value,
59 * // inserts new record in multi-valued group
60 * 'custom_33:-1' => value,
61 * // inserts another new record in multi-valued group
62 * 'custom_33:-2' => value,
63 * // you can use group_name:field_name instead of ID
64 * 'custom_some_group:my_field => 'myinfo',
65 * // updates record ID 8 in my_other_field in multi-valued some_big_group
66 * 'custom_some_big_group:my_other_field:8 => 'myinfo',
67 *
68 *
69 * @throws Exception
70 * @return array('values' => TRUE) or array('is_error' => 1, 'error_message' => 'what went wrong')
71 *
72 * @access public
73 */
74 function 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 }
116
117 /**
118 * Adjust Metadata for Create action
119 *
120 * The metadata is used for setting defaults, documentation & validation
121 * @param array $params
122 * Array or parameters determined by getfields.
123 */
124 function _civicrm_api3_custom_value_create_spec(&$params) {
125 $params['entity_id']['api.required'] = 1;
126 $params['entity_id']['title'] = 'Entity ID';
127 }
128
129 /**
130 * Use this API to get existing custom values for an entity.
131 *
132 * @param $params
133 * Array specifying the entity_id.
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 *
141 * @throws API_Exception
142 * @return array
143 *
144 *
145 * @access public
146 */
147 function civicrm_api3_custom_value_get($params) {
148
149 $getParams = array(
150 'entityID' => $params['entity_id'],
151 'entityType' => CRM_Utils_Array::value('entity_table', $params, ''),
152 );
153 if (strstr($getParams['entityType'], 'civicrm_')) {
154 $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8));
155 }
156 unset($params['entity_id'], $params['entity_table']);
157 foreach ($params as $id => $param) {
158 if ($param && substr($id, 0, 6) == 'return') {
159 $id = substr($id, 7);
160 list($c, $i) = CRM_Utils_System::explode('_', $id, 2);
161 if ($c == 'custom' && is_numeric($i)) {
162 $names['custom_' . $i] = 'custom_' . $i;
163 $id = $i;
164 }
165 else {
166 // Lookup names if ID was not supplied
167 list($group, $field) = CRM_Utils_System::explode(':', $id, 2);
168 $id = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
169 if (!$id) {
170 continue;
171 }
172 $names['custom_' . $id] = 'custom_' . $i;
173 }
174 $getParams['custom_' . $id] = 1;
175 }
176 }
177
178 $result = CRM_Core_BAO_CustomValueTable::getValues($getParams);
179
180 if ($result['is_error']) {
181 if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") {
182 $values = array();
183 return civicrm_api3_create_success($values, $params);
184 }
185 else {
186 throw new API_Exception($result['error_message']);
187 }
188 }
189 else {
190 $entity_id = $result['entityID'];
191 unset($result['is_error'], $result['entityID']);
192 // Convert multi-value strings to arrays
193 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
194 foreach ($result as $id => $value) {
195 if (strpos($value, $sp) !== FALSE) {
196 $value = explode($sp, trim($value, $sp));
197 }
198
199 $idArray = explode('_', $id);
200 if ($idArray[0] != 'custom') {
201 continue;
202 }
203 $fieldNumber = $idArray[1];
204 $customFieldInfo = CRM_Core_BAO_CustomField::getNameFromID($fieldNumber);
205 $info = array_pop($customFieldInfo);
206 // id is the index for returned results
207
208 if (empty($idArray[2])) {
209 $n = 0;
210 $id = $fieldNumber;
211 }
212 else {
213 $n = $idArray[2];
214 $id = $fieldNumber . "." . $idArray[2];
215 }
216 if (!empty($params['format.field_names'])) {
217 $id = $info['field_name'];
218 }
219 else {
220 $id = $fieldNumber;
221 }
222 $values[$id]['entity_id'] = $getParams['entityID'];
223 if (!empty($getParams['entityType'])) {
224 $values[$n]['entity_table'] = $getParams['entityType'];
225 }
226 //set 'latest' -useful for multi fields but set for single for consistency
227 $values[$id]['latest'] = $value;
228 $values[$id]['id'] = $id;
229 $values[$id][$n] = $value;
230 }
231 return civicrm_api3_create_success($values, $params);
232 }
233 }
234
235 /**
236 * Adjust Metadata for Get action
237 *
238 * The metadata is used for setting defaults, documentation & validation
239 * @param array $params
240 * Array or parameters determined by getfields.
241 */
242 function _civicrm_api3_custom_value_get_spec(&$params) {
243 $params['entity_id']['api.required'] = 1;
244 $params['entity_id']['title'] = 'Entity ID';
245 }