Merge pull request #5393 from colemanw/plurals
[civicrm-core.git] / api / v3 / CustomField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This api exposes CiviCRM custom field.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34 /**
35 * Create a 'custom field' within a custom field group.
36 *
37 * We also empty the static var in the getfields
38 * function after deletion so that the field is available for us (getfields manages date conversion
39 * among other things
40 *
41 * @param array $params
42 * Array per getfields metadata.
43 *
44 * @return array
45 * API success array
46 */
47 function civicrm_api3_custom_field_create($params) {
48
49 // Array created for passing options in params.
50 if (isset($params['option_values']) && is_array($params['option_values'])) {
51 foreach ($params['option_values'] as $key => $value) {
52 $params['option_label'][$key] = $value['label'];
53 $params['option_value'][$key] = $value['value'];
54 $params['option_status'][$key] = $value['is_active'];
55 $params['option_weight'][$key] = $value['weight'];
56 }
57 }
58 $values = array();
59 $customField = CRM_Core_BAO_CustomField::create($params);
60 _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]);
61 _civicrm_api3_custom_field_flush_static_caches();
62 return civicrm_api3_create_success($values, $params, 'CustomField', $customField);
63 }
64
65 /**
66 * Flush static caches in functions that might have stored available custom fields.
67 */
68 function _civicrm_api3_custom_field_flush_static_caches() {
69 civicrm_api('CustomField', 'getfields', array('version' => 3, 'cache_clear' => 1));
70 CRM_Core_BAO_UFField::getAvailableFieldsFlat(TRUE);
71 }
72
73 /**
74 * Adjust Metadata for Create action.
75 *
76 * @param array $params
77 * Array of parameters determined by getfields.
78 */
79 function _civicrm_api3_custom_field_create_spec(&$params) {
80 $params['label']['api.required'] = 1;
81 $params['custom_group_id']['api.required'] = 1;
82 $params['is_active']['api.default'] = 1;
83 $params['option_type'] = array(
84 'title' => 'Option Type',
85 'description' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate',
86 'api.default' => 1,
87 'type' => CRM_Utils_Type::T_BOOLEAN,
88 );
89 $params['data_type']['api.default'] = 'String';
90 $params['is_active']['api.default'] = 1;
91 }
92
93 /**
94 * Use this API to delete an existing custom field.
95 *
96 * @param array $params
97 * Array id of the field to be deleted.
98 *
99 * @return array
100 */
101 function civicrm_api3_custom_field_delete($params) {
102 $field = new CRM_Core_BAO_CustomField();
103 $field->id = $params['id'];
104 $field->find(TRUE);
105 $customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field);
106 civicrm_api('CustomField', 'getfields', array('version' => 3, 'cache_clear' => 1));
107 return $customFieldDelete ? civicrm_api3_create_error('Error while deleting custom field') : civicrm_api3_create_success();
108 }
109
110 /**
111 * Use this API to get existing custom fields.
112 *
113 * @param array $params
114 * Array to search on.
115 *
116 * @return array
117 */
118 function civicrm_api3_custom_field_get($params) {
119 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
120 }
121
122 /**
123 * Helper function to validate custom field value.
124 *
125 * @deprecated
126 *
127 * @param string $fieldName
128 * Custom field name (eg: custom_8 ).
129 * @param mixed $value
130 * Field value to be validate.
131 * @param array $fieldDetails
132 * Field Details.
133 * @param array $errors
134 * Collect validation errors.
135 *
136 * @return array|NULL
137 * Validation errors
138 * @todo remove this function - not in use but need to review functionality before
139 * removing as it might be useful in wrapper layer
140 */
141 function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = array()) {
142 return NULL;
143 //see comment block
144 if (!$value) {
145 return $errors;
146 }
147
148 $dataType = $fieldDetails['data_type'];
149 $htmlType = $fieldDetails['html_type'];
150
151 switch ($dataType) {
152 case 'Int':
153 if (!CRM_Utils_Rule::integer($value)) {
154 $errors[$fieldName] = 'Invalid integer value for ' . $fieldName;
155 }
156 break;
157
158 case 'Float':
159 if (!CRM_Utils_Rule::numeric($value)) {
160 $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName;
161 }
162 break;
163
164 case 'Money':
165 if (!CRM_Utils_Rule::money($value)) {
166 $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName;
167 }
168 break;
169
170 case 'Link':
171 if (!CRM_Utils_Rule::url($value)) {
172 $errors[$fieldName] = 'Invalid link for ' . $fieldName;
173 }
174 break;
175
176 case 'Boolean':
177 if ($value != '1' && $value != '0') {
178 $errors[$fieldName] = 'Invalid boolean (use 1 or 0) value for ' . $fieldName;
179 }
180 break;
181
182 case 'Country':
183 if (empty($value)) {
184 break;
185 }
186 if ($htmlType != 'Multi-Select Country' && is_array($value)) {
187 $errors[$fieldName] = 'Invalid country for ' . $fieldName;
188 break;
189 }
190
191 if (!is_array($value)) {
192 $value = array($value);
193 }
194
195 $query = "SELECT count(*) FROM civicrm_country WHERE id IN (" . implode(',', $value) . ")";
196 if (CRM_Core_DAO::singleValueQuery($query) < count($value)) {
197 $errors[$fieldName] = 'Invalid country(s) for ' . $fieldName;
198 }
199 break;
200
201 case 'StateProvince':
202 if (empty($value)) {
203 break;
204 }
205
206 if ($htmlType != 'Multi-Select State/Province' && is_array($value)) {
207 $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName;
208 break;
209 }
210
211 if (!is_array($value)) {
212 $value = array($value);
213 }
214
215 $query = "
216 SELECT count(*)
217 FROM civicrm_state_province
218 WHERE id IN ('" . implode("','", $value) . "')";
219 if (CRM_Core_DAO::singleValueQuery($query) < count($value)) {
220 $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName;
221 }
222 break;
223
224 case 'ContactReference':
225 //FIX ME
226 break;
227 }
228
229 if (in_array($htmlType, array(
230 'Select', 'Multi-Select', 'CheckBox', 'Radio', 'AdvMulti-Select')) &&
231 !isset($errors[$fieldName])
232 ) {
233 $options = CRM_Core_OptionGroup::valuesByID($fieldDetails['option_group_id']);
234 if (!is_array($value)) {
235 $value = array($value);
236 }
237
238 $invalidOptions = array_diff($value, array_keys($options));
239 if (!empty($invalidOptions)) {
240 $errors[$fieldName] = "Invalid option(s) for field '{$fieldName}': " . implode(',', $invalidOptions);
241 }
242 }
243
244 return $errors;
245 }
246
247 /**
248 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom field.
249 *
250 * @param array $params
251 * Array per getfields metadata.
252 *
253 * @return array
254 */
255 function civicrm_api3_custom_field_setvalue($params) {
256 require_once 'api/v3/Generic/Setvalue.php';
257 $result = civicrm_api3_generic_setValue(array("entity" => 'CustomField', 'params' => $params));
258 if (empty($result['is_error'])) {
259 CRM_Utils_System::flushCache();
260 }
261 return $result;
262 }