Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
a30c801b TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
c28e1768 | 13 | * This api exposes CiviCRM custom field. |
6a488035 TO |
14 | * |
15 | * @package CiviCRM_APIv3 | |
6a488035 TO |
16 | */ |
17 | ||
6a488035 TO |
18 | /** |
19 | * Create a 'custom field' within a custom field group. | |
2e66abf8 | 20 | * |
6a488035 TO |
21 | * We also empty the static var in the getfields |
22 | * function after deletion so that the field is available for us (getfields manages date conversion | |
23 | * among other things | |
24 | * | |
16b10e64 | 25 | * @param array $params |
2e66abf8 | 26 | * Array per getfields metadata. |
6a488035 | 27 | * |
d60f50a8 CW |
28 | * @return array |
29 | * API success array | |
6a488035 TO |
30 | */ |
31 | function civicrm_api3_custom_field_create($params) { | |
32 | ||
1fd57e91 | 33 | // Legacy handling for old way of naming serialized fields |
a7bcb32d CW |
34 | if (!empty($params['html_type'])) { |
35 | if ($params['html_type'] == 'CheckBox' || strpos($params['html_type'], 'Multi-') === 0) { | |
36 | $params['serialize'] = 1; | |
37 | } | |
38 | $params['html_type'] = str_replace(['Multi-Select', 'Select Country', 'Select State/Province'], 'Select', $params['html_type']); | |
1fd57e91 CW |
39 | } |
40 | ||
2e66abf8 | 41 | // Array created for passing options in params. |
6a488035 | 42 | if (isset($params['option_values']) && is_array($params['option_values'])) { |
58eaa092 | 43 | $weight = 0; |
6a488035 | 44 | foreach ($params['option_values'] as $key => $value) { |
58eaa092 CW |
45 | // Translate simple key/value pairs into full-blown option values |
46 | if (!is_array($value)) { | |
cf8f0fff | 47 | $value = [ |
58eaa092 CW |
48 | 'label' => $value, |
49 | 'value' => $key, | |
50 | 'is_active' => 1, | |
51 | 'weight' => $weight, | |
cf8f0fff | 52 | ]; |
58eaa092 CW |
53 | $key = $weight++; |
54 | } | |
6a488035 TO |
55 | $params['option_label'][$key] = $value['label']; |
56 | $params['option_value'][$key] = $value['value']; | |
57 | $params['option_status'][$key] = $value['is_active']; | |
58 | $params['option_weight'][$key] = $value['weight']; | |
59 | } | |
60 | } | |
cf8f0fff | 61 | $values = []; |
6a488035 | 62 | $customField = CRM_Core_BAO_CustomField::create($params); |
6a488035 | 63 | _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]); |
d674c5f8 | 64 | _civicrm_api3_custom_field_flush_static_caches(); |
244bbdd8 | 65 | return civicrm_api3_create_success($values, $params, 'CustomField', $customField); |
6a488035 | 66 | } |
11e09c59 | 67 | |
d674c5f8 | 68 | /** |
2e66abf8 | 69 | * Flush static caches in functions that might have stored available custom fields. |
d674c5f8 | 70 | */ |
9b873358 | 71 | function _civicrm_api3_custom_field_flush_static_caches() { |
65d27ad8 | 72 | if (isset(\Civi::$statics['CRM_Core_BAO_OptionGroup']['titles_by_name'])) { |
73 | unset(\Civi::$statics['CRM_Core_BAO_OptionGroup']['titles_by_name']); | |
74 | } | |
cf8f0fff | 75 | civicrm_api('CustomField', 'getfields', ['version' => 3, 'cache_clear' => 1]); |
d674c5f8 E |
76 | CRM_Core_BAO_UFField::getAvailableFieldsFlat(TRUE); |
77 | } | |
d60f50a8 | 78 | |
11e09c59 | 79 | /** |
0aa0303c | 80 | * Adjust Metadata for Create action. |
1c88e578 | 81 | * |
cf470720 | 82 | * @param array $params |
b081365f | 83 | * Array of parameters determined by getfields. |
6a488035 TO |
84 | */ |
85 | function _civicrm_api3_custom_field_create_spec(&$params) { | |
86 | $params['label']['api.required'] = 1; | |
87 | $params['custom_group_id']['api.required'] = 1; | |
d5841dff | 88 | $params['is_active']['api.default'] = 1; |
cf8f0fff | 89 | $params['option_values'] = [ |
58eaa092 CW |
90 | 'title' => 'Option Values', |
91 | 'description' => "Pass an array of options (value => label) to create this field's option values", | |
cf8f0fff | 92 | ]; |
58eaa092 | 93 | // TODO: Why expose this to the api at all? |
cf8f0fff | 94 | $params['option_type'] = [ |
b2ed8e73 CW |
95 | 'title' => 'Option Type', |
96 | 'description' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate', | |
b958933f | 97 | 'api.default' => 1, |
b29e2e8b | 98 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
cf8f0fff | 99 | ]; |
33533dfe | 100 | $params['data_type']['api.default'] = 'String'; |
4b307b2f | 101 | $params['is_active']['api.default'] = 1; |
6a488035 TO |
102 | } |
103 | ||
104 | /** | |
c28e1768 | 105 | * Use this API to delete an existing custom field. |
6a488035 | 106 | * |
16b10e64 | 107 | * @param array $params |
cf470720 | 108 | * Array id of the field to be deleted. |
77b97be7 EM |
109 | * |
110 | * @return array | |
c23f45d3 | 111 | */ |
6a488035 | 112 | function civicrm_api3_custom_field_delete($params) { |
6a488035 TO |
113 | $field = new CRM_Core_BAO_CustomField(); |
114 | $field->id = $params['id']; | |
115 | $field->find(TRUE); | |
116 | $customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field); | |
cf8f0fff | 117 | civicrm_api('CustomField', 'getfields', ['version' => 3, 'cache_clear' => 1]); |
6a488035 TO |
118 | return $customFieldDelete ? civicrm_api3_create_error('Error while deleting custom field') : civicrm_api3_create_success(); |
119 | } | |
120 | ||
121 | /** | |
122 | * Use this API to get existing custom fields. | |
123 | * | |
cf470720 TO |
124 | * @param array $params |
125 | * Array to search on. | |
77b97be7 EM |
126 | * |
127 | * @return array | |
c23f45d3 | 128 | */ |
6a488035 | 129 | function civicrm_api3_custom_field_get($params) { |
49df88a8 CW |
130 | // Legacy handling for serialize property |
131 | $handleLegacy = (($params['legacy_html_type'] ?? !isset($params['serialize'])) && CRM_Core_BAO_Domain::isDBVersionAtLeast('5.27.alpha1')); | |
132 | if ($handleLegacy && !empty($params['return'])) { | |
133 | if (!is_array($params['return'])) { | |
134 | $params['return'] = explode(',', str_replace(' ', '', $params['return'])); | |
135 | } | |
136 | if (!in_array('serialize', $params['return'])) { | |
1fd57e91 CW |
137 | $params['return'][] = 'serialize'; |
138 | } | |
a7bcb32d CW |
139 | if (!in_array('data_type', $params['return'])) { |
140 | $params['return'][] = 'data_type'; | |
141 | } | |
49df88a8 | 142 | } |
a7bcb32d CW |
143 | $legacyDataTypes = [ |
144 | 'Select State/Province' => 'StateProvince', | |
145 | 'Select Country' => 'Country', | |
146 | ]; | |
49df88a8 CW |
147 | if ($handleLegacy && !empty($params['html_type'])) { |
148 | $serializedTypes = ['CheckBox', 'Multi-Select', 'Multi-Select Country', 'Multi-Select State/Province']; | |
149 | if (is_string($params['html_type'])) { | |
150 | if (strpos($params['html_type'], 'Multi-Select') === 0) { | |
151 | $params['html_type'] = str_replace('Multi-Select', 'Select', $params['html_type']); | |
152 | $params['serialize'] = 1; | |
153 | } | |
154 | elseif (!in_array($params['html_type'], $serializedTypes)) { | |
155 | $params['serialize'] = 0; | |
156 | } | |
a7bcb32d CW |
157 | if (isset($legacyDataTypes[$params['html_type']])) { |
158 | $params['data_type'] = $legacyDataTypes[$params['html_type']]; | |
159 | unset($params['html_type']); | |
160 | } | |
49df88a8 CW |
161 | } |
162 | elseif (is_array($params['html_type']) && !empty($params['html_type']['IN'])) { | |
163 | $excludeNonSerialized = !array_diff($params['html_type']['IN'], $serializedTypes); | |
164 | $onlyNonSerialized = !array_intersect($params['html_type']['IN'], $serializedTypes); | |
165 | $params['html_type']['IN'] = array_map(function($val) { | |
a7bcb32d | 166 | return str_replace(['Multi-Select', 'Select Country', 'Select State/Province'], 'Select', $val); |
49df88a8 CW |
167 | }, $params['html_type']['IN']); |
168 | if ($excludeNonSerialized) { | |
169 | $params['serialize'] = 1; | |
170 | } | |
171 | if ($onlyNonSerialized) { | |
172 | $params['serialize'] = 0; | |
173 | } | |
1fd57e91 CW |
174 | } |
175 | } | |
176 | ||
177 | $results = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
178 | ||
a7bcb32d CW |
179 | if ($handleLegacy && !empty($results['values']) && is_array($results['values'])) { |
180 | foreach ($results['values'] as $id => &$result) { | |
181 | if (!empty($result['html_type'])) { | |
182 | if (in_array($result['data_type'], $legacyDataTypes)) { | |
183 | $result['html_type'] = array_search($result['data_type'], $legacyDataTypes); | |
184 | } | |
f0725b60 | 185 | if (!empty($result['serialize']) && $result['html_type'] !== 'Autocomplete-Select') { |
a7bcb32d CW |
186 | $result['html_type'] = str_replace('Select', 'Multi-Select', $result['html_type']); |
187 | } | |
1fd57e91 CW |
188 | } |
189 | } | |
190 | } | |
191 | ||
192 | return $results; | |
6a488035 TO |
193 | } |
194 | ||
11e09c59 | 195 | /** |
2e66abf8 EM |
196 | * Helper function to validate custom field value. |
197 | * | |
c490a46a | 198 | * @deprecated |
1c88e578 | 199 | * |
cf470720 TO |
200 | * @param string $fieldName |
201 | * Custom field name (eg: custom_8 ). | |
202 | * @param mixed $value | |
203 | * Field value to be validate. | |
204 | * @param array $fieldDetails | |
205 | * Field Details. | |
206 | * @param array $errors | |
207 | * Collect validation errors. | |
77b97be7 | 208 | * |
795492f3 | 209 | * @return array|NULL |
d60f50a8 | 210 | * Validation errors |
6a488035 TO |
211 | * @todo remove this function - not in use but need to review functionality before |
212 | * removing as it might be useful in wrapper layer | |
213 | */ | |
cf8f0fff | 214 | function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = []) { |
795492f3 | 215 | return NULL; |
35671d00 | 216 | //see comment block |
6a488035 TO |
217 | if (!$value) { |
218 | return $errors; | |
219 | } | |
220 | ||
221 | $dataType = $fieldDetails['data_type']; | |
222 | $htmlType = $fieldDetails['html_type']; | |
223 | ||
224 | switch ($dataType) { | |
225 | case 'Int': | |
226 | if (!CRM_Utils_Rule::integer($value)) { | |
227 | $errors[$fieldName] = 'Invalid integer value for ' . $fieldName; | |
228 | } | |
229 | break; | |
230 | ||
231 | case 'Float': | |
232 | if (!CRM_Utils_Rule::numeric($value)) { | |
233 | $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName; | |
234 | } | |
235 | break; | |
236 | ||
237 | case 'Money': | |
238 | if (!CRM_Utils_Rule::money($value)) { | |
239 | $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName; | |
240 | } | |
241 | break; | |
242 | ||
243 | case 'Link': | |
244 | if (!CRM_Utils_Rule::url($value)) { | |
245 | $errors[$fieldName] = 'Invalid link for ' . $fieldName; | |
246 | } | |
247 | break; | |
248 | ||
249 | case 'Boolean': | |
250 | if ($value != '1' && $value != '0') { | |
251 | $errors[$fieldName] = 'Invalid boolean (use 1 or 0) value for ' . $fieldName; | |
252 | } | |
253 | break; | |
254 | ||
255 | case 'Country': | |
256 | if (empty($value)) { | |
257 | break; | |
258 | } | |
259 | if ($htmlType != 'Multi-Select Country' && is_array($value)) { | |
260 | $errors[$fieldName] = 'Invalid country for ' . $fieldName; | |
261 | break; | |
262 | } | |
263 | ||
264 | if (!is_array($value)) { | |
cf8f0fff | 265 | $value = [$value]; |
6a488035 TO |
266 | } |
267 | ||
268 | $query = "SELECT count(*) FROM civicrm_country WHERE id IN (" . implode(',', $value) . ")"; | |
269 | if (CRM_Core_DAO::singleValueQuery($query) < count($value)) { | |
270 | $errors[$fieldName] = 'Invalid country(s) for ' . $fieldName; | |
271 | } | |
272 | break; | |
273 | ||
274 | case 'StateProvince': | |
275 | if (empty($value)) { | |
276 | break; | |
277 | } | |
278 | ||
279 | if ($htmlType != 'Multi-Select State/Province' && is_array($value)) { | |
280 | $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName; | |
281 | break; | |
282 | } | |
283 | ||
284 | if (!is_array($value)) { | |
cf8f0fff | 285 | $value = [$value]; |
6a488035 TO |
286 | } |
287 | ||
288 | $query = " | |
1c88e578 | 289 | SELECT count(*) |
6a488035 TO |
290 | FROM civicrm_state_province |
291 | WHERE id IN ('" . implode("','", $value) . "')"; | |
292 | if (CRM_Core_DAO::singleValueQuery($query) < count($value)) { | |
293 | $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName; | |
294 | } | |
295 | break; | |
296 | ||
297 | case 'ContactReference': | |
298 | //FIX ME | |
299 | break; | |
300 | } | |
301 | ||
7c31ae57 SL |
302 | if (in_array($htmlType, ['Select', 'Multi-Select', 'CheckBox', 'Radio']) |
303 | && !isset($errors[$fieldName]) | |
6a488035 | 304 | ) { |
6a488035 TO |
305 | $options = CRM_Core_OptionGroup::valuesByID($fieldDetails['option_group_id']); |
306 | if (!is_array($value)) { | |
cf8f0fff | 307 | $value = [$value]; |
6a488035 TO |
308 | } |
309 | ||
310 | $invalidOptions = array_diff($value, array_keys($options)); | |
311 | if (!empty($invalidOptions)) { | |
312 | $errors[$fieldName] = "Invalid option(s) for field '{$fieldName}': " . implode(',', $invalidOptions); | |
313 | } | |
314 | } | |
315 | ||
316 | return $errors; | |
317 | } | |
318 | ||
5322ae12 | 319 | /** |
2e66abf8 EM |
320 | * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom field. |
321 | * | |
d0997921 | 322 | * @param array $params |
2e66abf8 EM |
323 | * Array per getfields metadata. |
324 | * | |
645ee340 | 325 | * @return array |
5322ae12 CW |
326 | */ |
327 | function civicrm_api3_custom_field_setvalue($params) { | |
328 | require_once 'api/v3/Generic/Setvalue.php'; | |
cf8f0fff | 329 | $result = civicrm_api3_generic_setValue(["entity" => 'CustomField', 'params' => $params]); |
5322ae12 CW |
330 | if (empty($result['is_error'])) { |
331 | CRM_Utils_System::flushCache(); | |
332 | } | |
333 | return $result; | |
334 | } |