Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
12 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
13 | | under the terms of the GNU Affero General Public License | | |
14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
15 | | | | |
16 | | CiviCRM is distributed in the hope that it will be useful, but | | |
17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
19 | | See the GNU Affero General Public License for more details. | | |
20 | | | | |
21 | | You should have received a copy of the GNU Affero General Public | | |
22 | | License and the CiviCRM Licensing Exception along | | |
23 | | with this program; if not, contact CiviCRM LLC | | |
24 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
25 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
27 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | /** | |
31 | * File for the CiviCRM APIv3 custom value functions | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | * @subpackage API_CustomField | |
35 | * | |
36 | * @copyright CiviCRM LLC (c) 2004-2013 | |
37 | * @version $Id: CustomField.php 30879 2010-11-22 15:45:55Z shot $ | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Files required for this package | |
42 | */ | |
43 | ||
44 | ||
45 | /** | |
46 | * Sets custom values for an entity. | |
47 | * | |
48 | * | |
49 | * @param $params 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 | * @return array('values' => TRUE) or array('is_error' => 1, 'error_message' => 'what went wrong') | |
70 | * | |
71 | * @access public | |
72 | * | |
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 | } | |
11e09c59 TO |
116 | |
117 | /** | |
6a488035 | 118 | * Adjust Metadata for Create action |
11e09c59 TO |
119 | * |
120 | * The metadata is used for setting defaults, documentation & validation | |
121 | * @param array $params array or parameters determined by getfields | |
122 | */ | |
6a488035 TO |
123 | function _civicrm_api3_custom_value_create_spec(&$params) { |
124 | $params['entity_id']['api.required'] = 1; | |
125 | } | |
126 | /** | |
127 | * Use this API to get existing custom values for an entity. | |
128 | * | |
129 | * @param $params array specifying the entity_id | |
130 | * Optionally include entity_type param, i.e. 'entity_type' => 'Activity' | |
131 | * If no entity_type is supplied, it will be determined based on the fields you request. | |
132 | * If no entity_type is supplied and no fields are specified, 'Contact' will be assumed. | |
133 | * Optionally include the desired custom data to be fetched (or else all custom data for this entity will be returned) | |
134 | * Example: 'entity_id' => 123, 'return.custom_6' => 1, 'return.custom_33' => 1 | |
135 | * If you do not know the ID, you may use group name : field name, for example 'return.foo_stuff:my_field' => 1 | |
136 | * | |
137 | * @return array. | |
138 | * | |
139 | * @access public | |
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 { | |
180 | return civicrm_api3_create_error($result['error_message']); | |
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]; | |
198 | $info = array_pop(CRM_Core_BAO_CustomField::getNameFromID($fieldNumber)); | |
199 | // id is the index for returned results | |
200 | ||
201 | if (empty($idArray[2])) { | |
202 | $n = 0; | |
203 | $id = $fieldNumber; | |
204 | } | |
205 | else{ | |
206 | $n = $idArray[2]; | |
207 | $id = $fieldNumber . "." . $idArray[2]; | |
208 | } | |
209 | if (CRM_Utils_Array::value('format.field_names', $params)) { | |
210 | $id = $info['field_name']; | |
211 | } | |
212 | else { | |
213 | $id = $fieldNumber; | |
214 | } | |
215 | $values[$id]['entity_id'] = $getParams['entityID']; | |
216 | if (CRM_Utils_Array::value('entityType', $getParams)) { | |
217 | $values[$n]['entity_table'] = $getParams['entityType']; | |
218 | } | |
219 | //set 'latest' -useful for multi fields but set for single for consistency | |
220 | $values[$id]['latest'] = $value; | |
221 | $values[$id]['id'] = $id; | |
222 | $values[$id][$n] = $value; | |
223 | } | |
224 | return civicrm_api3_create_success($values, $params); | |
225 | } | |
226 | } | |
227 | ||
11e09c59 | 228 | /** |
6a488035 | 229 | * Adjust Metadata for Get action |
11e09c59 TO |
230 | * |
231 | * The metadata is used for setting defaults, documentation & validation | |
232 | * @param array $params array or parameters determined by getfields | |
233 | */ | |
6a488035 TO |
234 | function _civicrm_api3_custom_value_get_spec(&$params) { |
235 | $params['entity_id']['api.required'] = 1; | |
236 | } |