9932b0a8f2c5aeaadd17796158196c508221b752
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * File for the CiviCRM APIv3 custom group functions
32 * @package CiviCRM_APIv3
33 * @subpackage API_CustomField
35 * @copyright CiviCRM LLC (c) 2004-2014
36 * @version $Id: CustomField.php 30879 2010-11-22 15:45:55Z shot $
40 * Create a 'custom field' within a custom field group.
41 * We also empty the static var in the getfields
42 * function after deletion so that the field is available for us (getfields manages date conversion
45 * @param array $params
46 * Array Associative array of property name/value pairs to create new custom field.
53 * @example CustomFieldCreate.php
54 * {@getfields CustomField_create}
55 * {@example CustomFieldCreate.php 0}
58 function civicrm_api3_custom_field_create($params) {
60 // Array created for passing options in params
61 if (isset($params['option_values']) && is_array($params['option_values'])) {
62 foreach ($params['option_values'] as $key => $value) {
63 $params['option_label'][$key] = $value['label'];
64 $params['option_value'][$key] = $value['value'];
65 $params['option_status'][$key] = $value['is_active'];
66 $params['option_weight'][$key] = $value['weight'];
70 $customField = CRM_Core_BAO_CustomField
::create($params);
71 _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id
]);
72 _civicrm_api3_custom_field_flush_static_caches();
73 return civicrm_api3_create_success($values, $params, 'custom_field', $customField);
77 * Flush static caches in functions that might have stored available custom fields
79 function _civicrm_api3_custom_field_flush_static_caches() {
80 civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1));
81 CRM_Core_BAO_UFField
::getAvailableFieldsFlat(TRUE);
85 * Adjust Metadata for Create action
87 * @param array $params
88 * Array or parameters determined by getfields.
90 function _civicrm_api3_custom_field_create_spec(&$params) {
91 $params['label']['api.required'] = 1;
92 $params['custom_group_id']['api.required'] = 1;
93 $params['is_active']['api.default'] = 1;
94 $params['option_type'] = array(
95 'title' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate',
97 'type' => CRM_Utils_Type
::T_BOOLEAN
,
99 $params['data_type']['api.default'] = 'String';
100 $params['is_active']['api.default'] = 1;
104 * Use this API to delete an existing custom group field.
106 * @param array $params
107 * Array id of the field to be deleted.
110 * @example CustomFieldDelete.php
112 * {@example CustomFieldDelete.php 0}
113 * {@getfields CustomField_delete}
116 function civicrm_api3_custom_field_delete($params) {
117 $field = new CRM_Core_BAO_CustomField();
118 $field->id
= $params['id'];
120 $customFieldDelete = CRM_Core_BAO_CustomField
::deleteField($field);
121 civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1));
122 return $customFieldDelete ?
civicrm_api3_create_error('Error while deleting custom field') : civicrm_api3_create_success();
126 * Use this API to get existing custom fields.
128 * @param array $params
129 * Array to search on.
132 * @getfields CustomField_get
136 function civicrm_api3_custom_field_get($params) {
137 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
141 * Helper function to validate custom field value
144 * @param string $fieldName
145 * Custom field name (eg: custom_8 ).
146 * @param mixed $value
147 * Field value to be validate.
148 * @param array $fieldDetails
150 * @param array $errors
151 * Collect validation errors.
155 * @todo remove this function - not in use but need to review functionality before
156 * removing as it might be useful in wrapper layer
158 function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = array()) {
165 $dataType = $fieldDetails['data_type'];
166 $htmlType = $fieldDetails['html_type'];
170 if (!CRM_Utils_Rule
::integer($value)) {
171 $errors[$fieldName] = 'Invalid integer value for ' . $fieldName;
176 if (!CRM_Utils_Rule
::numeric($value)) {
177 $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName;
182 if (!CRM_Utils_Rule
::money($value)) {
183 $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName;
188 if (!CRM_Utils_Rule
::url($value)) {
189 $errors[$fieldName] = 'Invalid link for ' . $fieldName;
194 if ($value != '1' && $value != '0') {
195 $errors[$fieldName] = 'Invalid boolean (use 1 or 0) value for ' . $fieldName;
203 if ($htmlType != 'Multi-Select Country' && is_array($value)) {
204 $errors[$fieldName] = 'Invalid country for ' . $fieldName;
208 if (!is_array($value)) {
209 $value = array($value);
212 $query = "SELECT count(*) FROM civicrm_country WHERE id IN (" . implode(',', $value) . ")";
213 if (CRM_Core_DAO
::singleValueQuery($query) < count($value)) {
214 $errors[$fieldName] = 'Invalid country(s) for ' . $fieldName;
218 case 'StateProvince':
223 if ($htmlType != 'Multi-Select State/Province' && is_array($value)) {
224 $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName;
228 if (!is_array($value)) {
229 $value = array($value);
234 FROM civicrm_state_province
235 WHERE id IN ('" . implode("','", $value) . "')";
236 if (CRM_Core_DAO
::singleValueQuery($query) < count($value)) {
237 $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName;
241 case 'ContactReference':
246 if (in_array($htmlType, array(
247 'Select', 'Multi-Select', 'CheckBox', 'Radio', 'AdvMulti-Select')) &&
248 !isset($errors[$fieldName])
250 $options = CRM_Core_OptionGroup
::valuesByID($fieldDetails['option_group_id']);
251 if (!is_array($value)) {
252 $value = array($value);
255 $invalidOptions = array_diff($value, array_keys($options));
256 if (!empty($invalidOptions)) {
257 $errors[$fieldName] = "Invalid option(s) for field '{$fieldName}': " . implode(',', $invalidOptions);
265 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom field
267 function civicrm_api3_custom_field_setvalue($params) {
268 require_once 'api/v3/Generic/Setvalue.php';
269 $result = civicrm_api3_generic_setValue(array("entity" => 'custom_field', 'params' => $params));
270 if (empty($result['is_error'])) {
271 CRM_Utils_System
::flushCache();