Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
39de6fd5 | 5 | | CiviCRM version 4.6 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
731a0992 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * File for the CiviCRM APIv3 custom group functions | |
31 | * | |
32 | * @package CiviCRM_APIv3 | |
33 | * @subpackage API_CustomField | |
34 | * | |
731a0992 | 35 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
36 | * @version $Id: CustomField.php 30879 2010-11-22 15:45:55Z shot $ |
37 | */ | |
38 | ||
6a488035 TO |
39 | /** |
40 | * Most API functions take in associative arrays ( name => value pairs | |
41 | * as parameters. Some of the most commonly used parameters are | |
42 | * described below | |
43 | * | |
cf470720 TO |
44 | * @param array $params |
45 | * An associative array used in construction. | |
6a488035 TO |
46 | * retrieval of the object |
47 | * | |
48 | */ | |
49 | ||
50 | /** | |
51 | * Create a 'custom field' within a custom field group. | |
52 | * We also empty the static var in the getfields | |
53 | * function after deletion so that the field is available for us (getfields manages date conversion | |
54 | * among other things | |
55 | * | |
cf470720 TO |
56 | * @param $params |
57 | * Array Associative array of property name/value pairs to create new custom field. | |
6a488035 TO |
58 | * |
59 | * @return Newly API success object | |
60 | * | |
61 | * @access public | |
62 | * | |
63 | * @example CustomFieldCreate.php | |
64 | * {@getfields CustomField_create} | |
65 | * {@example CustomFieldCreate.php 0} | |
66 | * | |
67 | */ | |
68 | function civicrm_api3_custom_field_create($params) { | |
69 | ||
6a488035 TO |
70 | // Array created for passing options in params |
71 | if (isset($params['option_values']) && is_array($params['option_values'])) { | |
72 | foreach ($params['option_values'] as $key => $value) { | |
73 | $params['option_label'][$key] = $value['label']; | |
74 | $params['option_value'][$key] = $value['value']; | |
75 | $params['option_status'][$key] = $value['is_active']; | |
76 | $params['option_weight'][$key] = $value['weight']; | |
77 | } | |
78 | } | |
d674c5f8 | 79 | $values = array(); |
6a488035 | 80 | $customField = CRM_Core_BAO_CustomField::create($params); |
6a488035 | 81 | _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]); |
d674c5f8 | 82 | _civicrm_api3_custom_field_flush_static_caches(); |
6a488035 TO |
83 | return civicrm_api3_create_success($values, $params, 'custom_field', $customField); |
84 | } | |
11e09c59 | 85 | |
d674c5f8 E |
86 | /** |
87 | * Flush static caches in functions that might have stored available custom fields | |
88 | */ | |
9b873358 | 89 | function _civicrm_api3_custom_field_flush_static_caches() { |
d674c5f8 E |
90 | civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1)); |
91 | CRM_Core_BAO_UFField::getAvailableFieldsFlat(TRUE); | |
92 | } | |
11e09c59 | 93 | /** |
6a488035 | 94 | * Adjust Metadata for Create action |
1c88e578 | 95 | * |
cf470720 TO |
96 | * @param array $params |
97 | * Array or parameters determined by getfields. | |
6a488035 TO |
98 | */ |
99 | function _civicrm_api3_custom_field_create_spec(&$params) { | |
100 | $params['label']['api.required'] = 1; | |
101 | $params['custom_group_id']['api.required'] = 1; | |
d5841dff | 102 | $params['is_active']['api.default'] = 1; |
b958933f | 103 | $params['option_type'] = array( |
104 | 'title' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate', | |
105 | 'api.default' => 1, | |
b29e2e8b | 106 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
b958933f | 107 | ); |
33533dfe | 108 | $params['data_type']['api.default'] = 'String'; |
4b307b2f | 109 | $params['is_active']['api.default'] = 1; |
6a488035 TO |
110 | } |
111 | ||
112 | /** | |
113 | * Use this API to delete an existing custom group field. | |
114 | * | |
cf470720 TO |
115 | * @param $params |
116 | * Array id of the field to be deleted. | |
77b97be7 EM |
117 | * |
118 | * @return array | |
6a488035 TO |
119 | * @example CustomFieldDelete.php |
120 | * | |
121 | * {@example CustomFieldDelete.php 0} | |
122 | * {@getfields CustomField_delete} | |
123 | * @access public | |
124 | **/ | |
125 | function civicrm_api3_custom_field_delete($params) { | |
126 | ||
127 | $field = new CRM_Core_BAO_CustomField(); | |
128 | $field->id = $params['id']; | |
129 | $field->find(TRUE); | |
130 | $customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field); | |
131 | civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1)); | |
132 | return $customFieldDelete ? civicrm_api3_create_error('Error while deleting custom field') : civicrm_api3_create_success(); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Use this API to get existing custom fields. | |
137 | * | |
cf470720 TO |
138 | * @param array $params |
139 | * Array to search on. | |
35671d00 | 140 | * {* |
77b97be7 EM |
141 | * |
142 | * @return array | |
143 | @getfields CustomField_get} | |
6a488035 TO |
144 | * @access public |
145 | * | |
146 | **/ | |
147 | function civicrm_api3_custom_field_get($params) { | |
148 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
149 | } | |
150 | ||
11e09c59 | 151 | /** |
6a488035 | 152 | * Helper function to validate custom field value |
c490a46a | 153 | * @deprecated |
1c88e578 | 154 | * |
cf470720 TO |
155 | * @param string $fieldName |
156 | * Custom field name (eg: custom_8 ). | |
157 | * @param mixed $value | |
158 | * Field value to be validate. | |
159 | * @param array $fieldDetails | |
160 | * Field Details. | |
161 | * @param array $errors | |
162 | * Collect validation errors. | |
77b97be7 | 163 | * |
c490a46a | 164 | * @return array Validation errors |
6a488035 TO |
165 | * @todo remove this function - not in use but need to review functionality before |
166 | * removing as it might be useful in wrapper layer | |
167 | */ | |
c490a46a | 168 | function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = array()) { |
35671d00 TO |
169 | return; |
170 | //see comment block | |
6a488035 TO |
171 | if (!$value) { |
172 | return $errors; | |
173 | } | |
174 | ||
175 | $dataType = $fieldDetails['data_type']; | |
176 | $htmlType = $fieldDetails['html_type']; | |
177 | ||
178 | switch ($dataType) { | |
179 | case 'Int': | |
180 | if (!CRM_Utils_Rule::integer($value)) { | |
181 | $errors[$fieldName] = 'Invalid integer value for ' . $fieldName; | |
182 | } | |
183 | break; | |
184 | ||
185 | case 'Float': | |
186 | if (!CRM_Utils_Rule::numeric($value)) { | |
187 | $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName; | |
188 | } | |
189 | break; | |
190 | ||
191 | case 'Money': | |
192 | if (!CRM_Utils_Rule::money($value)) { | |
193 | $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName; | |
194 | } | |
195 | break; | |
196 | ||
197 | case 'Link': | |
198 | if (!CRM_Utils_Rule::url($value)) { | |
199 | $errors[$fieldName] = 'Invalid link for ' . $fieldName; | |
200 | } | |
201 | break; | |
202 | ||
203 | case 'Boolean': | |
204 | if ($value != '1' && $value != '0') { | |
205 | $errors[$fieldName] = 'Invalid boolean (use 1 or 0) value for ' . $fieldName; | |
206 | } | |
207 | break; | |
208 | ||
209 | case 'Country': | |
210 | if (empty($value)) { | |
211 | break; | |
212 | } | |
213 | if ($htmlType != 'Multi-Select Country' && is_array($value)) { | |
214 | $errors[$fieldName] = 'Invalid country for ' . $fieldName; | |
215 | break; | |
216 | } | |
217 | ||
218 | if (!is_array($value)) { | |
219 | $value = array($value); | |
220 | } | |
221 | ||
222 | $query = "SELECT count(*) FROM civicrm_country WHERE id IN (" . implode(',', $value) . ")"; | |
223 | if (CRM_Core_DAO::singleValueQuery($query) < count($value)) { | |
224 | $errors[$fieldName] = 'Invalid country(s) for ' . $fieldName; | |
225 | } | |
226 | break; | |
227 | ||
228 | case 'StateProvince': | |
229 | if (empty($value)) { | |
230 | break; | |
231 | } | |
232 | ||
233 | if ($htmlType != 'Multi-Select State/Province' && is_array($value)) { | |
234 | $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName; | |
235 | break; | |
236 | } | |
237 | ||
238 | if (!is_array($value)) { | |
239 | $value = array($value); | |
240 | } | |
241 | ||
242 | $query = " | |
1c88e578 | 243 | SELECT count(*) |
6a488035 TO |
244 | FROM civicrm_state_province |
245 | WHERE id IN ('" . implode("','", $value) . "')"; | |
246 | if (CRM_Core_DAO::singleValueQuery($query) < count($value)) { | |
247 | $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName; | |
248 | } | |
249 | break; | |
250 | ||
251 | case 'ContactReference': | |
252 | //FIX ME | |
253 | break; | |
254 | } | |
255 | ||
256 | if (in_array($htmlType, array( | |
257 | 'Select', 'Multi-Select', 'CheckBox', 'Radio', 'AdvMulti-Select')) && | |
258 | !isset($errors[$fieldName]) | |
259 | ) { | |
6a488035 TO |
260 | $options = CRM_Core_OptionGroup::valuesByID($fieldDetails['option_group_id']); |
261 | if (!is_array($value)) { | |
262 | $value = array($value); | |
263 | } | |
264 | ||
265 | $invalidOptions = array_diff($value, array_keys($options)); | |
266 | if (!empty($invalidOptions)) { | |
267 | $errors[$fieldName] = "Invalid option(s) for field '{$fieldName}': " . implode(',', $invalidOptions); | |
268 | } | |
269 | } | |
270 | ||
271 | return $errors; | |
272 | } | |
273 | ||
5322ae12 CW |
274 | /** |
275 | * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom field | |
276 | */ | |
277 | function civicrm_api3_custom_field_setvalue($params) { | |
278 | require_once 'api/v3/Generic/Setvalue.php'; | |
279 | $result = civicrm_api3_generic_setValue(array("entity" => 'custom_field', 'params' => $params)); | |
280 | if (empty($result['is_error'])) { | |
281 | CRM_Utils_System::flushCache(); | |
282 | } | |
283 | return $result; | |
284 | } |