86e507998a7bce01581823c5527e66424abe147e
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * File for the CiviCRM APIv3 custom group functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_CustomField
34 * @copyright CiviCRM LLC (c) 2004-2014
35 * @version $Id: CustomField.php 30879 2010-11-22 15:45:55Z shot $
39 * Create a 'custom field' within a custom field group.
40 * We also empty the static var in the getfields
41 * function after deletion so that the field is available for us (getfields manages date conversion
44 * @param array $params
45 * Array Associative array of property name/value pairs to create new custom field.
52 * @example CustomFieldCreate.php
53 * {@getfields CustomField_create}
54 * {@example CustomFieldCreate.php 0}
57 function civicrm_api3_custom_field_create($params) {
59 // Array created for passing options in params
60 if (isset($params['option_values']) && is_array($params['option_values'])) {
61 foreach ($params['option_values'] as $key => $value) {
62 $params['option_label'][$key] = $value['label'];
63 $params['option_value'][$key] = $value['value'];
64 $params['option_status'][$key] = $value['is_active'];
65 $params['option_weight'][$key] = $value['weight'];
69 $customField = CRM_Core_BAO_CustomField
::create($params);
70 _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id
]);
71 _civicrm_api3_custom_field_flush_static_caches();
72 return civicrm_api3_create_success($values, $params, 'custom_field', $customField);
76 * Flush static caches in functions that might have stored available custom fields
78 function _civicrm_api3_custom_field_flush_static_caches() {
79 civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1));
80 CRM_Core_BAO_UFField
::getAvailableFieldsFlat(TRUE);
84 * Adjust Metadata for Create action
86 * @param array $params
87 * Array or parameters determined by getfields.
89 function _civicrm_api3_custom_field_create_spec(&$params) {
90 $params['label']['api.required'] = 1;
91 $params['custom_group_id']['api.required'] = 1;
92 $params['is_active']['api.default'] = 1;
93 $params['option_type'] = array(
94 'title' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate',
96 'type' => CRM_Utils_Type
::T_BOOLEAN
,
98 $params['data_type']['api.default'] = 'String';
99 $params['is_active']['api.default'] = 1;
103 * Use this API to delete an existing custom group field.
105 * @param array $params
106 * Array id of the field to be deleted.
109 * @example CustomFieldDelete.php
111 * {@example CustomFieldDelete.php 0}
112 * {@getfields CustomField_delete}
115 function civicrm_api3_custom_field_delete($params) {
116 $field = new CRM_Core_BAO_CustomField();
117 $field->id
= $params['id'];
119 $customFieldDelete = CRM_Core_BAO_CustomField
::deleteField($field);
120 civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1));
121 return $customFieldDelete ?
civicrm_api3_create_error('Error while deleting custom field') : civicrm_api3_create_success();
125 * Use this API to get existing custom fields.
127 * @param array $params
128 * Array to search on.
131 * @getfields CustomField_get
135 function civicrm_api3_custom_field_get($params) {
136 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
140 * Helper function to validate custom field value
143 * @param string $fieldName
144 * Custom field name (eg: custom_8 ).
145 * @param mixed $value
146 * Field value to be validate.
147 * @param array $fieldDetails
149 * @param array $errors
150 * Collect validation errors.
154 * @todo remove this function - not in use but need to review functionality before
155 * removing as it might be useful in wrapper layer
157 function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = array()) {
164 $dataType = $fieldDetails['data_type'];
165 $htmlType = $fieldDetails['html_type'];
169 if (!CRM_Utils_Rule
::integer($value)) {
170 $errors[$fieldName] = 'Invalid integer value for ' . $fieldName;
175 if (!CRM_Utils_Rule
::numeric($value)) {
176 $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName;
181 if (!CRM_Utils_Rule
::money($value)) {
182 $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName;
187 if (!CRM_Utils_Rule
::url($value)) {
188 $errors[$fieldName] = 'Invalid link for ' . $fieldName;
193 if ($value != '1' && $value != '0') {
194 $errors[$fieldName] = 'Invalid boolean (use 1 or 0) value for ' . $fieldName;
202 if ($htmlType != 'Multi-Select Country' && is_array($value)) {
203 $errors[$fieldName] = 'Invalid country for ' . $fieldName;
207 if (!is_array($value)) {
208 $value = array($value);
211 $query = "SELECT count(*) FROM civicrm_country WHERE id IN (" . implode(',', $value) . ")";
212 if (CRM_Core_DAO
::singleValueQuery($query) < count($value)) {
213 $errors[$fieldName] = 'Invalid country(s) for ' . $fieldName;
217 case 'StateProvince':
222 if ($htmlType != 'Multi-Select State/Province' && is_array($value)) {
223 $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName;
227 if (!is_array($value)) {
228 $value = array($value);
233 FROM civicrm_state_province
234 WHERE id IN ('" . implode("','", $value) . "')";
235 if (CRM_Core_DAO
::singleValueQuery($query) < count($value)) {
236 $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName;
240 case 'ContactReference':
245 if (in_array($htmlType, array(
246 'Select', 'Multi-Select', 'CheckBox', 'Radio', 'AdvMulti-Select')) &&
247 !isset($errors[$fieldName])
249 $options = CRM_Core_OptionGroup
::valuesByID($fieldDetails['option_group_id']);
250 if (!is_array($value)) {
251 $value = array($value);
254 $invalidOptions = array_diff($value, array_keys($options));
255 if (!empty($invalidOptions)) {
256 $errors[$fieldName] = "Invalid option(s) for field '{$fieldName}': " . implode(',', $invalidOptions);
264 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom field
266 function civicrm_api3_custom_field_setvalue($params) {
267 require_once 'api/v3/Generic/Setvalue.php';
268 $result = civicrm_api3_generic_setValue(array("entity" => 'custom_field', 'params' => $params));
269 if (empty($result['is_error'])) {
270 CRM_Utils_System
::flushCache();