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