Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
a30c801b TO |
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 | | |
6a488035 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
c28e1768 | 13 | * This api exposes CiviCRM custom field. |
6a488035 TO |
14 | * |
15 | * @package CiviCRM_APIv3 | |
6a488035 TO |
16 | */ |
17 | ||
6a488035 TO |
18 | /** |
19 | * Create a 'custom field' within a custom field group. | |
2e66abf8 | 20 | * |
6a488035 TO |
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 | |
23 | * among other things | |
24 | * | |
16b10e64 | 25 | * @param array $params |
2e66abf8 | 26 | * Array per getfields metadata. |
6a488035 | 27 | * |
d60f50a8 CW |
28 | * @return array |
29 | * API success array | |
6a488035 TO |
30 | */ |
31 | function civicrm_api3_custom_field_create($params) { | |
32 | ||
2e66abf8 | 33 | // Array created for passing options in params. |
6a488035 | 34 | if (isset($params['option_values']) && is_array($params['option_values'])) { |
58eaa092 | 35 | $weight = 0; |
6a488035 | 36 | foreach ($params['option_values'] as $key => $value) { |
58eaa092 CW |
37 | // Translate simple key/value pairs into full-blown option values |
38 | if (!is_array($value)) { | |
cf8f0fff | 39 | $value = [ |
58eaa092 CW |
40 | 'label' => $value, |
41 | 'value' => $key, | |
42 | 'is_active' => 1, | |
43 | 'weight' => $weight, | |
cf8f0fff | 44 | ]; |
58eaa092 CW |
45 | $key = $weight++; |
46 | } | |
6a488035 TO |
47 | $params['option_label'][$key] = $value['label']; |
48 | $params['option_value'][$key] = $value['value']; | |
49 | $params['option_status'][$key] = $value['is_active']; | |
50 | $params['option_weight'][$key] = $value['weight']; | |
51 | } | |
52 | } | |
cf8f0fff | 53 | $values = []; |
6a488035 | 54 | $customField = CRM_Core_BAO_CustomField::create($params); |
6a488035 | 55 | _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]); |
d674c5f8 | 56 | _civicrm_api3_custom_field_flush_static_caches(); |
244bbdd8 | 57 | return civicrm_api3_create_success($values, $params, 'CustomField', $customField); |
6a488035 | 58 | } |
11e09c59 | 59 | |
d674c5f8 | 60 | /** |
2e66abf8 | 61 | * Flush static caches in functions that might have stored available custom fields. |
d674c5f8 | 62 | */ |
9b873358 | 63 | function _civicrm_api3_custom_field_flush_static_caches() { |
cf8f0fff | 64 | civicrm_api('CustomField', 'getfields', ['version' => 3, 'cache_clear' => 1]); |
d674c5f8 E |
65 | CRM_Core_BAO_UFField::getAvailableFieldsFlat(TRUE); |
66 | } | |
d60f50a8 | 67 | |
11e09c59 | 68 | /** |
0aa0303c | 69 | * Adjust Metadata for Create action. |
1c88e578 | 70 | * |
cf470720 | 71 | * @param array $params |
b081365f | 72 | * Array of parameters determined by getfields. |
6a488035 TO |
73 | */ |
74 | function _civicrm_api3_custom_field_create_spec(&$params) { | |
75 | $params['label']['api.required'] = 1; | |
76 | $params['custom_group_id']['api.required'] = 1; | |
d5841dff | 77 | $params['is_active']['api.default'] = 1; |
cf8f0fff | 78 | $params['option_values'] = [ |
58eaa092 CW |
79 | 'title' => 'Option Values', |
80 | 'description' => "Pass an array of options (value => label) to create this field's option values", | |
cf8f0fff | 81 | ]; |
58eaa092 | 82 | // TODO: Why expose this to the api at all? |
cf8f0fff | 83 | $params['option_type'] = [ |
b2ed8e73 CW |
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', | |
b958933f | 86 | 'api.default' => 1, |
b29e2e8b | 87 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
cf8f0fff | 88 | ]; |
33533dfe | 89 | $params['data_type']['api.default'] = 'String'; |
4b307b2f | 90 | $params['is_active']['api.default'] = 1; |
6a488035 TO |
91 | } |
92 | ||
93 | /** | |
c28e1768 | 94 | * Use this API to delete an existing custom field. |
6a488035 | 95 | * |
16b10e64 | 96 | * @param array $params |
cf470720 | 97 | * Array id of the field to be deleted. |
77b97be7 EM |
98 | * |
99 | * @return array | |
c23f45d3 | 100 | */ |
6a488035 | 101 | function civicrm_api3_custom_field_delete($params) { |
6a488035 TO |
102 | $field = new CRM_Core_BAO_CustomField(); |
103 | $field->id = $params['id']; | |
104 | $field->find(TRUE); | |
105 | $customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field); | |
cf8f0fff | 106 | civicrm_api('CustomField', 'getfields', ['version' => 3, 'cache_clear' => 1]); |
6a488035 TO |
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 | * | |
cf470720 TO |
113 | * @param array $params |
114 | * Array to search on. | |
77b97be7 EM |
115 | * |
116 | * @return array | |
c23f45d3 | 117 | */ |
6a488035 TO |
118 | function civicrm_api3_custom_field_get($params) { |
119 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
120 | } | |
121 | ||
11e09c59 | 122 | /** |
2e66abf8 EM |
123 | * Helper function to validate custom field value. |
124 | * | |
c490a46a | 125 | * @deprecated |
1c88e578 | 126 | * |
cf470720 TO |
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. | |
77b97be7 | 135 | * |
795492f3 | 136 | * @return array|NULL |
d60f50a8 | 137 | * Validation errors |
6a488035 TO |
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 | */ | |
cf8f0fff | 141 | function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = []) { |
795492f3 | 142 | return NULL; |
35671d00 | 143 | //see comment block |
6a488035 TO |
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)) { | |
cf8f0fff | 192 | $value = [$value]; |
6a488035 TO |
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)) { | |
cf8f0fff | 212 | $value = [$value]; |
6a488035 TO |
213 | } |
214 | ||
215 | $query = " | |
1c88e578 | 216 | SELECT count(*) |
6a488035 TO |
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 | ||
7c31ae57 SL |
229 | if (in_array($htmlType, ['Select', 'Multi-Select', 'CheckBox', 'Radio']) |
230 | && !isset($errors[$fieldName]) | |
6a488035 | 231 | ) { |
6a488035 TO |
232 | $options = CRM_Core_OptionGroup::valuesByID($fieldDetails['option_group_id']); |
233 | if (!is_array($value)) { | |
cf8f0fff | 234 | $value = [$value]; |
6a488035 TO |
235 | } |
236 | ||
237 | $invalidOptions = array_diff($value, array_keys($options)); | |
238 | if (!empty($invalidOptions)) { | |
239 | $errors[$fieldName] = "Invalid option(s) for field '{$fieldName}': " . implode(',', $invalidOptions); | |
240 | } | |
241 | } | |
242 | ||
243 | return $errors; | |
244 | } | |
245 | ||
5322ae12 | 246 | /** |
2e66abf8 EM |
247 | * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom field. |
248 | * | |
d0997921 | 249 | * @param array $params |
2e66abf8 EM |
250 | * Array per getfields metadata. |
251 | * | |
645ee340 | 252 | * @return array |
5322ae12 CW |
253 | */ |
254 | function civicrm_api3_custom_field_setvalue($params) { | |
255 | require_once 'api/v3/Generic/Setvalue.php'; | |
cf8f0fff | 256 | $result = civicrm_api3_generic_setValue(["entity" => 'CustomField', 'params' => $params]); |
5322ae12 CW |
257 | if (empty($result['is_error'])) { |
258 | CRM_Utils_System::flushCache(); | |
259 | } | |
260 | return $result; | |
261 | } |