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