3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * This api exposes CiviCRM custom field.
15 * @package CiviCRM_APIv3
19 * Create a 'custom field' within a custom field group.
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
25 * @param array $params
26 * Array per getfields metadata.
31 function civicrm_api3_custom_field_create($params) {
33 // Legacy handling for old way of naming serialized fields
34 if (!empty($params['html_type'])) {
35 if ($params['html_type'] == 'CheckBox' ||
strpos($params['html_type'], 'Multi-') === 0) {
36 $params['serialize'] = 1;
38 $params['html_type'] = str_replace(['Multi-Select', 'Select Country', 'Select State/Province'], 'Select', $params['html_type']);
41 // Array created for passing options in params.
42 if (isset($params['option_values']) && is_array($params['option_values'])) {
44 foreach ($params['option_values'] as $key => $value) {
45 // Translate simple key/value pairs into full-blown option values
46 if (!is_array($value)) {
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'];
62 $customField = CRM_Core_BAO_CustomField
::create($params);
63 _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id
]);
64 _civicrm_api3_custom_field_flush_static_caches();
65 return civicrm_api3_create_success($values, $params, 'CustomField', $customField);
69 * Flush static caches in functions that might have stored available custom fields.
71 function _civicrm_api3_custom_field_flush_static_caches() {
72 civicrm_api('CustomField', 'getfields', ['version' => 3, 'cache_clear' => 1]);
73 CRM_Core_BAO_UFField
::getAvailableFieldsFlat(TRUE);
77 * Adjust Metadata for Create action.
79 * @param array $params
80 * Array of parameters determined by getfields.
82 function _civicrm_api3_custom_field_create_spec(&$params) {
83 $params['label']['api.required'] = 1;
84 $params['custom_group_id']['api.required'] = 1;
85 $params['is_active']['api.default'] = 1;
86 $params['option_values'] = [
87 'title' => 'Option Values',
88 'description' => "Pass an array of options (value => label) to create this field's option values",
90 // TODO: Why expose this to the api at all?
91 $params['option_type'] = [
92 'title' => 'Option Type',
93 'description' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate',
95 'type' => CRM_Utils_Type
::T_BOOLEAN
,
97 $params['data_type']['api.default'] = 'String';
98 $params['is_active']['api.default'] = 1;
102 * Use this API to delete an existing custom field.
104 * @param array $params
105 * Array id of the field to be deleted.
109 function civicrm_api3_custom_field_delete($params) {
110 $field = new CRM_Core_BAO_CustomField();
111 $field->id
= $params['id'];
113 $customFieldDelete = CRM_Core_BAO_CustomField
::deleteField($field);
114 civicrm_api('CustomField', 'getfields', ['version' => 3, 'cache_clear' => 1]);
115 return $customFieldDelete ?
civicrm_api3_create_error('Error while deleting custom field') : civicrm_api3_create_success();
119 * Use this API to get existing custom fields.
121 * @param array $params
122 * Array to search on.
126 function civicrm_api3_custom_field_get($params) {
127 // Legacy handling for serialize property
128 $handleLegacy = (($params['legacy_html_type'] ??
!isset($params['serialize'])) && CRM_Core_BAO_Domain
::isDBVersionAtLeast('5.27.alpha1'));
129 if ($handleLegacy && !empty($params['return'])) {
130 if (!is_array($params['return'])) {
131 $params['return'] = explode(',', str_replace(' ', '', $params['return']));
133 if (!in_array('serialize', $params['return'])) {
134 $params['return'][] = 'serialize';
136 if (!in_array('data_type', $params['return'])) {
137 $params['return'][] = 'data_type';
141 'Select State/Province' => 'StateProvince',
142 'Select Country' => 'Country',
144 if ($handleLegacy && !empty($params['html_type'])) {
145 $serializedTypes = ['CheckBox', 'Multi-Select', 'Multi-Select Country', 'Multi-Select State/Province'];
146 if (is_string($params['html_type'])) {
147 if (strpos($params['html_type'], 'Multi-Select') === 0) {
148 $params['html_type'] = str_replace('Multi-Select', 'Select', $params['html_type']);
149 $params['serialize'] = 1;
151 elseif (!in_array($params['html_type'], $serializedTypes)) {
152 $params['serialize'] = 0;
154 if (isset($legacyDataTypes[$params['html_type']])) {
155 $params['data_type'] = $legacyDataTypes[$params['html_type']];
156 unset($params['html_type']);
159 elseif (is_array($params['html_type']) && !empty($params['html_type']['IN'])) {
160 $excludeNonSerialized = !array_diff($params['html_type']['IN'], $serializedTypes);
161 $onlyNonSerialized = !array_intersect($params['html_type']['IN'], $serializedTypes);
162 $params['html_type']['IN'] = array_map(function($val) {
163 return str_replace(['Multi-Select', 'Select Country', 'Select State/Province'], 'Select', $val);
164 }, $params['html_type']['IN']);
165 if ($excludeNonSerialized) {
166 $params['serialize'] = 1;
168 if ($onlyNonSerialized) {
169 $params['serialize'] = 0;
174 $results = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
176 if ($handleLegacy && !empty($results['values']) && is_array($results['values'])) {
177 foreach ($results['values'] as $id => &$result) {
178 if (!empty($result['html_type'])) {
179 if (in_array($result['data_type'], $legacyDataTypes)) {
180 $result['html_type'] = array_search($result['data_type'], $legacyDataTypes);
182 if (!empty($result['serialize']) && $result['html_type'] !== 'Autocomplete-Select') {
183 $result['html_type'] = str_replace('Select', 'Multi-Select', $result['html_type']);
193 * Helper function to validate custom field value.
197 * @param string $fieldName
198 * Custom field name (eg: custom_8 ).
199 * @param mixed $value
200 * Field value to be validate.
201 * @param array $fieldDetails
203 * @param array $errors
204 * Collect validation errors.
208 * @todo remove this function - not in use but need to review functionality before
209 * removing as it might be useful in wrapper layer
211 function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = []) {
218 $dataType = $fieldDetails['data_type'];
219 $htmlType = $fieldDetails['html_type'];
223 if (!CRM_Utils_Rule
::integer($value)) {
224 $errors[$fieldName] = 'Invalid integer value for ' . $fieldName;
229 if (!CRM_Utils_Rule
::numeric($value)) {
230 $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName;
235 if (!CRM_Utils_Rule
::money($value)) {
236 $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName;
241 if (!CRM_Utils_Rule
::url($value)) {
242 $errors[$fieldName] = 'Invalid link for ' . $fieldName;
247 if ($value != '1' && $value != '0') {
248 $errors[$fieldName] = 'Invalid boolean (use 1 or 0) value for ' . $fieldName;
256 if ($htmlType != 'Multi-Select Country' && is_array($value)) {
257 $errors[$fieldName] = 'Invalid country for ' . $fieldName;
261 if (!is_array($value)) {
265 $query = "SELECT count(*) FROM civicrm_country WHERE id IN (" . implode(',', $value) . ")";
266 if (CRM_Core_DAO
::singleValueQuery($query) < count($value)) {
267 $errors[$fieldName] = 'Invalid country(s) for ' . $fieldName;
271 case 'StateProvince':
276 if ($htmlType != 'Multi-Select State/Province' && is_array($value)) {
277 $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName;
281 if (!is_array($value)) {
287 FROM civicrm_state_province
288 WHERE id IN ('" . implode("','", $value) . "')";
289 if (CRM_Core_DAO
::singleValueQuery($query) < count($value)) {
290 $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName;
294 case 'ContactReference':
299 if (in_array($htmlType, ['Select', 'Multi-Select', 'CheckBox', 'Radio'])
300 && !isset($errors[$fieldName])
302 $options = CRM_Core_OptionGroup
::valuesByID($fieldDetails['option_group_id']);
303 if (!is_array($value)) {
307 $invalidOptions = array_diff($value, array_keys($options));
308 if (!empty($invalidOptions)) {
309 $errors[$fieldName] = "Invalid option(s) for field '{$fieldName}': " . implode(',', $invalidOptions);
317 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom field.
319 * @param array $params
320 * Array per getfields metadata.
324 function civicrm_api3_custom_field_setvalue($params) {
325 require_once 'api/v3/Generic/Setvalue.php';
326 $result = civicrm_api3_generic_setValue(["entity" => 'CustomField', 'params' => $params]);
327 if (empty($result['is_error'])) {
328 CRM_Utils_System
::flushCache();