Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
731a0992 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | */ |
47 | function civicrm_api3_custom_field_create($params) { | |
48 | ||
2e66abf8 | 49 | // Array created for passing options in params. |
6a488035 TO |
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 | } | |
d674c5f8 | 58 | $values = array(); |
6a488035 | 59 | $customField = CRM_Core_BAO_CustomField::create($params); |
6a488035 | 60 | _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]); |
d674c5f8 | 61 | _civicrm_api3_custom_field_flush_static_caches(); |
244bbdd8 | 62 | return civicrm_api3_create_success($values, $params, 'CustomField', $customField); |
6a488035 | 63 | } |
11e09c59 | 64 | |
d674c5f8 | 65 | /** |
2e66abf8 | 66 | * Flush static caches in functions that might have stored available custom fields. |
d674c5f8 | 67 | */ |
9b873358 | 68 | function _civicrm_api3_custom_field_flush_static_caches() { |
244bbdd8 | 69 | civicrm_api('CustomField', 'getfields', array('version' => 3, 'cache_clear' => 1)); |
d674c5f8 E |
70 | CRM_Core_BAO_UFField::getAvailableFieldsFlat(TRUE); |
71 | } | |
d60f50a8 | 72 | |
11e09c59 | 73 | /** |
0aa0303c | 74 | * Adjust Metadata for Create action. |
1c88e578 | 75 | * |
cf470720 | 76 | * @param array $params |
b081365f | 77 | * Array of parameters determined by getfields. |
6a488035 TO |
78 | */ |
79 | function _civicrm_api3_custom_field_create_spec(&$params) { | |
80 | $params['label']['api.required'] = 1; | |
81 | $params['custom_group_id']['api.required'] = 1; | |
d5841dff | 82 | $params['is_active']['api.default'] = 1; |
b958933f | 83 | $params['option_type'] = array( |
84 | 'title' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate', | |
85 | 'api.default' => 1, | |
b29e2e8b | 86 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
b958933f | 87 | ); |
33533dfe | 88 | $params['data_type']['api.default'] = 'String'; |
4b307b2f | 89 | $params['is_active']['api.default'] = 1; |
6a488035 TO |
90 | } |
91 | ||
92 | /** | |
c28e1768 | 93 | * Use this API to delete an existing custom field. |
6a488035 | 94 | * |
16b10e64 | 95 | * @param array $params |
cf470720 | 96 | * Array id of the field to be deleted. |
77b97be7 EM |
97 | * |
98 | * @return array | |
c23f45d3 | 99 | */ |
6a488035 | 100 | function civicrm_api3_custom_field_delete($params) { |
6a488035 TO |
101 | $field = new CRM_Core_BAO_CustomField(); |
102 | $field->id = $params['id']; | |
103 | $field->find(TRUE); | |
104 | $customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field); | |
244bbdd8 | 105 | civicrm_api('CustomField', 'getfields', array('version' => 3, 'cache_clear' => 1)); |
6a488035 TO |
106 | return $customFieldDelete ? civicrm_api3_create_error('Error while deleting custom field') : civicrm_api3_create_success(); |
107 | } | |
108 | ||
109 | /** | |
110 | * Use this API to get existing custom fields. | |
111 | * | |
cf470720 TO |
112 | * @param array $params |
113 | * Array to search on. | |
77b97be7 EM |
114 | * |
115 | * @return array | |
c23f45d3 | 116 | */ |
6a488035 TO |
117 | function civicrm_api3_custom_field_get($params) { |
118 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
119 | } | |
120 | ||
11e09c59 | 121 | /** |
2e66abf8 EM |
122 | * Helper function to validate custom field value. |
123 | * | |
c490a46a | 124 | * @deprecated |
1c88e578 | 125 | * |
cf470720 TO |
126 | * @param string $fieldName |
127 | * Custom field name (eg: custom_8 ). | |
128 | * @param mixed $value | |
129 | * Field value to be validate. | |
130 | * @param array $fieldDetails | |
131 | * Field Details. | |
132 | * @param array $errors | |
133 | * Collect validation errors. | |
77b97be7 | 134 | * |
795492f3 | 135 | * @return array|NULL |
d60f50a8 | 136 | * Validation errors |
6a488035 TO |
137 | * @todo remove this function - not in use but need to review functionality before |
138 | * removing as it might be useful in wrapper layer | |
139 | */ | |
c490a46a | 140 | function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = array()) { |
795492f3 | 141 | return NULL; |
35671d00 | 142 | //see comment block |
6a488035 TO |
143 | if (!$value) { |
144 | return $errors; | |
145 | } | |
146 | ||
147 | $dataType = $fieldDetails['data_type']; | |
148 | $htmlType = $fieldDetails['html_type']; | |
149 | ||
150 | switch ($dataType) { | |
151 | case 'Int': | |
152 | if (!CRM_Utils_Rule::integer($value)) { | |
153 | $errors[$fieldName] = 'Invalid integer value for ' . $fieldName; | |
154 | } | |
155 | break; | |
156 | ||
157 | case 'Float': | |
158 | if (!CRM_Utils_Rule::numeric($value)) { | |
159 | $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName; | |
160 | } | |
161 | break; | |
162 | ||
163 | case 'Money': | |
164 | if (!CRM_Utils_Rule::money($value)) { | |
165 | $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName; | |
166 | } | |
167 | break; | |
168 | ||
169 | case 'Link': | |
170 | if (!CRM_Utils_Rule::url($value)) { | |
171 | $errors[$fieldName] = 'Invalid link for ' . $fieldName; | |
172 | } | |
173 | break; | |
174 | ||
175 | case 'Boolean': | |
176 | if ($value != '1' && $value != '0') { | |
177 | $errors[$fieldName] = 'Invalid boolean (use 1 or 0) value for ' . $fieldName; | |
178 | } | |
179 | break; | |
180 | ||
181 | case 'Country': | |
182 | if (empty($value)) { | |
183 | break; | |
184 | } | |
185 | if ($htmlType != 'Multi-Select Country' && is_array($value)) { | |
186 | $errors[$fieldName] = 'Invalid country for ' . $fieldName; | |
187 | break; | |
188 | } | |
189 | ||
190 | if (!is_array($value)) { | |
191 | $value = array($value); | |
192 | } | |
193 | ||
194 | $query = "SELECT count(*) FROM civicrm_country WHERE id IN (" . implode(',', $value) . ")"; | |
195 | if (CRM_Core_DAO::singleValueQuery($query) < count($value)) { | |
196 | $errors[$fieldName] = 'Invalid country(s) for ' . $fieldName; | |
197 | } | |
198 | break; | |
199 | ||
200 | case 'StateProvince': | |
201 | if (empty($value)) { | |
202 | break; | |
203 | } | |
204 | ||
205 | if ($htmlType != 'Multi-Select State/Province' && is_array($value)) { | |
206 | $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName; | |
207 | break; | |
208 | } | |
209 | ||
210 | if (!is_array($value)) { | |
211 | $value = array($value); | |
212 | } | |
213 | ||
214 | $query = " | |
1c88e578 | 215 | SELECT count(*) |
6a488035 TO |
216 | FROM civicrm_state_province |
217 | WHERE id IN ('" . implode("','", $value) . "')"; | |
218 | if (CRM_Core_DAO::singleValueQuery($query) < count($value)) { | |
219 | $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName; | |
220 | } | |
221 | break; | |
222 | ||
223 | case 'ContactReference': | |
224 | //FIX ME | |
225 | break; | |
226 | } | |
227 | ||
228 | if (in_array($htmlType, array( | |
229 | 'Select', 'Multi-Select', 'CheckBox', 'Radio', 'AdvMulti-Select')) && | |
230 | !isset($errors[$fieldName]) | |
231 | ) { | |
6a488035 TO |
232 | $options = CRM_Core_OptionGroup::valuesByID($fieldDetails['option_group_id']); |
233 | if (!is_array($value)) { | |
234 | $value = array($value); | |
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'; | |
4846df91 | 256 | $result = civicrm_api3_generic_setValue(array("entity" => 'CustomField', 'params' => $params)); |
5322ae12 CW |
257 | if (empty($result['is_error'])) { |
258 | CRM_Utils_System::flushCache(); | |
259 | } | |
260 | return $result; | |
261 | } |