Merge pull request #14981 from eileenmcnaughton/load_extract
[civicrm-core.git] / api / v3 / CustomField.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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/**
c28e1768 29 * This api exposes CiviCRM custom field.
6a488035
TO
30 *
31 * @package CiviCRM_APIv3
6a488035
TO
32 */
33
6a488035
TO
34/**
35 * Create a 'custom field' within a custom field group.
2e66abf8 36 *
6a488035
TO
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 *
16b10e64 41 * @param array $params
2e66abf8 42 * Array per getfields metadata.
6a488035 43 *
d60f50a8
CW
44 * @return array
45 * API success array
6a488035
TO
46 */
47function civicrm_api3_custom_field_create($params) {
48
2e66abf8 49 // Array created for passing options in params.
6a488035 50 if (isset($params['option_values']) && is_array($params['option_values'])) {
58eaa092 51 $weight = 0;
6a488035 52 foreach ($params['option_values'] as $key => $value) {
58eaa092
CW
53 // Translate simple key/value pairs into full-blown option values
54 if (!is_array($value)) {
cf8f0fff 55 $value = [
58eaa092
CW
56 'label' => $value,
57 'value' => $key,
58 'is_active' => 1,
59 'weight' => $weight,
cf8f0fff 60 ];
58eaa092
CW
61 $key = $weight++;
62 }
6a488035
TO
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'];
67 }
68 }
cf8f0fff 69 $values = [];
6a488035 70 $customField = CRM_Core_BAO_CustomField::create($params);
6a488035 71 _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]);
d674c5f8 72 _civicrm_api3_custom_field_flush_static_caches();
244bbdd8 73 return civicrm_api3_create_success($values, $params, 'CustomField', $customField);
6a488035 74}
11e09c59 75
d674c5f8 76/**
2e66abf8 77 * Flush static caches in functions that might have stored available custom fields.
d674c5f8 78 */
9b873358 79function _civicrm_api3_custom_field_flush_static_caches() {
cf8f0fff 80 civicrm_api('CustomField', 'getfields', ['version' => 3, 'cache_clear' => 1]);
d674c5f8
E
81 CRM_Core_BAO_UFField::getAvailableFieldsFlat(TRUE);
82}
d60f50a8 83
11e09c59 84/**
0aa0303c 85 * Adjust Metadata for Create action.
1c88e578 86 *
cf470720 87 * @param array $params
b081365f 88 * Array of parameters determined by getfields.
6a488035
TO
89 */
90function _civicrm_api3_custom_field_create_spec(&$params) {
91 $params['label']['api.required'] = 1;
92 $params['custom_group_id']['api.required'] = 1;
d5841dff 93 $params['is_active']['api.default'] = 1;
cf8f0fff 94 $params['option_values'] = [
58eaa092
CW
95 'title' => 'Option Values',
96 'description' => "Pass an array of options (value => label) to create this field's option values",
cf8f0fff 97 ];
58eaa092 98 // TODO: Why expose this to the api at all?
cf8f0fff 99 $params['option_type'] = [
b2ed8e73
CW
100 'title' => 'Option Type',
101 'description' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate',
b958933f 102 'api.default' => 1,
b29e2e8b 103 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff 104 ];
33533dfe 105 $params['data_type']['api.default'] = 'String';
4b307b2f 106 $params['is_active']['api.default'] = 1;
6a488035
TO
107}
108
109/**
c28e1768 110 * Use this API to delete an existing custom field.
6a488035 111 *
16b10e64 112 * @param array $params
cf470720 113 * Array id of the field to be deleted.
77b97be7
EM
114 *
115 * @return array
c23f45d3 116 */
6a488035 117function civicrm_api3_custom_field_delete($params) {
6a488035
TO
118 $field = new CRM_Core_BAO_CustomField();
119 $field->id = $params['id'];
120 $field->find(TRUE);
121 $customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field);
cf8f0fff 122 civicrm_api('CustomField', 'getfields', ['version' => 3, 'cache_clear' => 1]);
6a488035
TO
123 return $customFieldDelete ? civicrm_api3_create_error('Error while deleting custom field') : civicrm_api3_create_success();
124}
125
126/**
127 * Use this API to get existing custom fields.
128 *
cf470720
TO
129 * @param array $params
130 * Array to search on.
77b97be7
EM
131 *
132 * @return array
c23f45d3 133 */
6a488035
TO
134function civicrm_api3_custom_field_get($params) {
135 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
136}
137
11e09c59 138/**
2e66abf8
EM
139 * Helper function to validate custom field value.
140 *
c490a46a 141 * @deprecated
1c88e578 142 *
cf470720
TO
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
148 * Field Details.
149 * @param array $errors
150 * Collect validation errors.
77b97be7 151 *
795492f3 152 * @return array|NULL
d60f50a8 153 * Validation errors
6a488035
TO
154 * @todo remove this function - not in use but need to review functionality before
155 * removing as it might be useful in wrapper layer
156 */
cf8f0fff 157function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = []) {
795492f3 158 return NULL;
35671d00 159 //see comment block
6a488035
TO
160 if (!$value) {
161 return $errors;
162 }
163
164 $dataType = $fieldDetails['data_type'];
165 $htmlType = $fieldDetails['html_type'];
166
167 switch ($dataType) {
168 case 'Int':
169 if (!CRM_Utils_Rule::integer($value)) {
170 $errors[$fieldName] = 'Invalid integer value for ' . $fieldName;
171 }
172 break;
173
174 case 'Float':
175 if (!CRM_Utils_Rule::numeric($value)) {
176 $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName;
177 }
178 break;
179
180 case 'Money':
181 if (!CRM_Utils_Rule::money($value)) {
182 $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName;
183 }
184 break;
185
186 case 'Link':
187 if (!CRM_Utils_Rule::url($value)) {
188 $errors[$fieldName] = 'Invalid link for ' . $fieldName;
189 }
190 break;
191
192 case 'Boolean':
193 if ($value != '1' && $value != '0') {
194 $errors[$fieldName] = 'Invalid boolean (use 1 or 0) value for ' . $fieldName;
195 }
196 break;
197
198 case 'Country':
199 if (empty($value)) {
200 break;
201 }
202 if ($htmlType != 'Multi-Select Country' && is_array($value)) {
203 $errors[$fieldName] = 'Invalid country for ' . $fieldName;
204 break;
205 }
206
207 if (!is_array($value)) {
cf8f0fff 208 $value = [$value];
6a488035
TO
209 }
210
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;
214 }
215 break;
216
217 case 'StateProvince':
218 if (empty($value)) {
219 break;
220 }
221
222 if ($htmlType != 'Multi-Select State/Province' && is_array($value)) {
223 $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName;
224 break;
225 }
226
227 if (!is_array($value)) {
cf8f0fff 228 $value = [$value];
6a488035
TO
229 }
230
231 $query = "
1c88e578 232SELECT count(*)
6a488035
TO
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;
237 }
238 break;
239
240 case 'ContactReference':
241 //FIX ME
242 break;
243 }
244
7c31ae57
SL
245 if (in_array($htmlType, ['Select', 'Multi-Select', 'CheckBox', 'Radio'])
246 && !isset($errors[$fieldName])
6a488035 247 ) {
6a488035
TO
248 $options = CRM_Core_OptionGroup::valuesByID($fieldDetails['option_group_id']);
249 if (!is_array($value)) {
cf8f0fff 250 $value = [$value];
6a488035
TO
251 }
252
253 $invalidOptions = array_diff($value, array_keys($options));
254 if (!empty($invalidOptions)) {
255 $errors[$fieldName] = "Invalid option(s) for field '{$fieldName}': " . implode(',', $invalidOptions);
256 }
257 }
258
259 return $errors;
260}
261
5322ae12 262/**
2e66abf8
EM
263 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom field.
264 *
d0997921 265 * @param array $params
2e66abf8
EM
266 * Array per getfields metadata.
267 *
645ee340 268 * @return array
5322ae12
CW
269 */
270function civicrm_api3_custom_field_setvalue($params) {
271 require_once 'api/v3/Generic/Setvalue.php';
cf8f0fff 272 $result = civicrm_api3_generic_setValue(["entity" => 'CustomField', 'params' => $params]);
5322ae12
CW
273 if (empty($result['is_error'])) {
274 CRM_Utils_System::flushCache();
275 }
276 return $result;
277}