Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
731a0992 | 5 | | CiviCRM version 4.5 | |
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 | * | |
44 | * @param array $params an associative array used in construction | |
45 | * retrieval of the object | |
46 | * | |
47 | */ | |
48 | ||
49 | /** | |
50 | * Create a 'custom field' within a custom field group. | |
51 | * We also empty the static var in the getfields | |
52 | * function after deletion so that the field is available for us (getfields manages date conversion | |
53 | * among other things | |
54 | * | |
55 | * @param $params array Associative array of property name/value pairs to create new custom field. | |
56 | * | |
57 | * @return Newly API success object | |
58 | * | |
59 | * @access public | |
60 | * | |
61 | * @example CustomFieldCreate.php | |
62 | * {@getfields CustomField_create} | |
63 | * {@example CustomFieldCreate.php 0} | |
64 | * | |
65 | */ | |
66 | function civicrm_api3_custom_field_create($params) { | |
67 | ||
6a488035 TO |
68 | // Array created for passing options in params |
69 | if (isset($params['option_values']) && is_array($params['option_values'])) { | |
70 | foreach ($params['option_values'] as $key => $value) { | |
71 | $params['option_label'][$key] = $value['label']; | |
72 | $params['option_value'][$key] = $value['value']; | |
73 | $params['option_status'][$key] = $value['is_active']; | |
74 | $params['option_weight'][$key] = $value['weight']; | |
75 | } | |
76 | } | |
d674c5f8 | 77 | $values = array(); |
6a488035 | 78 | $customField = CRM_Core_BAO_CustomField::create($params); |
6a488035 | 79 | _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]); |
d674c5f8 | 80 | _civicrm_api3_custom_field_flush_static_caches(); |
6a488035 TO |
81 | return civicrm_api3_create_success($values, $params, 'custom_field', $customField); |
82 | } | |
11e09c59 | 83 | |
d674c5f8 E |
84 | /** |
85 | * Flush static caches in functions that might have stored available custom fields | |
86 | */ | |
87 | function _civicrm_api3_custom_field_flush_static_caches(){ | |
88 | civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1)); | |
89 | CRM_Core_BAO_UFField::getAvailableFieldsFlat(TRUE); | |
90 | } | |
11e09c59 | 91 | /** |
6a488035 | 92 | * Adjust Metadata for Create action |
1c88e578 | 93 | * |
6a488035 TO |
94 | * @param array $params array or parameters determined by getfields |
95 | */ | |
96 | function _civicrm_api3_custom_field_create_spec(&$params) { | |
97 | $params['label']['api.required'] = 1; | |
98 | $params['custom_group_id']['api.required'] = 1; | |
d5841dff | 99 | $params['is_active']['api.default'] = 1; |
b958933f | 100 | $params['option_type'] = array( |
101 | 'title' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate', | |
102 | 'api.default' => 1, | |
b29e2e8b | 103 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
b958933f | 104 | ); |
33533dfe | 105 | $params['data_type']['api.default'] = 'String'; |
4b307b2f | 106 | $params['is_active']['api.default'] = 1; |
6a488035 TO |
107 | } |
108 | ||
109 | /** | |
110 | * Use this API to delete an existing custom group field. | |
111 | * | |
112 | * @param $params Array id of the field to be deleted | |
77b97be7 EM |
113 | * |
114 | * @return array | |
6a488035 TO |
115 | * @example CustomFieldDelete.php |
116 | * | |
117 | * {@example CustomFieldDelete.php 0} | |
118 | * {@getfields CustomField_delete} | |
119 | * @access public | |
120 | **/ | |
121 | function civicrm_api3_custom_field_delete($params) { | |
122 | ||
123 | $field = new CRM_Core_BAO_CustomField(); | |
124 | $field->id = $params['id']; | |
125 | $field->find(TRUE); | |
126 | $customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field); | |
127 | civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1)); | |
128 | return $customFieldDelete ? civicrm_api3_create_error('Error while deleting custom field') : civicrm_api3_create_success(); | |
129 | } | |
130 | ||
131 | /** | |
132 | * Use this API to get existing custom fields. | |
133 | * | |
134 | * @param array $params Array to search on | |
77b97be7 EM |
135 | *{* |
136 | * | |
137 | * @return array | |
138 | @getfields CustomField_get} | |
6a488035 TO |
139 | * @access public |
140 | * | |
141 | **/ | |
142 | function civicrm_api3_custom_field_get($params) { | |
143 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
144 | } | |
145 | ||
146 | /* | |
1c88e578 | 147 | * Helper function to validate custom field values |
148 | * | |
6a488035 | 149 | * @params Array $params Custom fields with values |
1c88e578 | 150 | * @params Array $errors Reference fields to be check with |
151 | * @params Boolean $checkForDisallowed Check for disallowed elements | |
6a488035 TO |
152 | * in params |
153 | * @params Boolean $checkForRequired Check for non present required elements | |
154 | * in params | |
155 | * @return Array Validation errors | |
156 | */ | |
157 | ||
11e09c59 | 158 | /** |
6a488035 | 159 | * Helper function to validate custom field value |
1c88e578 | 160 | * |
6a488035 TO |
161 | * @params String $fieldName Custom field name (eg: custom_8 ) |
162 | * @params Mixed $value Field value to be validate | |
163 | * @params Array $fieldDetails Field Details | |
164 | * @params Array $errors Collect validation errors | |
165 | * | |
77b97be7 EM |
166 | * @param $fieldName |
167 | * @param $value | |
168 | * @param $fieldDetails | |
169 | * @param array $errors | |
170 | * | |
6a488035 TO |
171 | * @return Array Validation errors |
172 | * @todo remove this function - not in use but need to review functionality before | |
173 | * removing as it might be useful in wrapper layer | |
174 | */ | |
175 | function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = array( | |
176 | )) { | |
177 | return; | |
178 | //see comment block | |
179 | if (!$value) { | |
180 | return $errors; | |
181 | } | |
182 | ||
183 | $dataType = $fieldDetails['data_type']; | |
184 | $htmlType = $fieldDetails['html_type']; | |
185 | ||
186 | switch ($dataType) { | |
187 | case 'Int': | |
188 | if (!CRM_Utils_Rule::integer($value)) { | |
189 | $errors[$fieldName] = 'Invalid integer value for ' . $fieldName; | |
190 | } | |
191 | break; | |
192 | ||
193 | case 'Float': | |
194 | if (!CRM_Utils_Rule::numeric($value)) { | |
195 | $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName; | |
196 | } | |
197 | break; | |
198 | ||
199 | case 'Money': | |
200 | if (!CRM_Utils_Rule::money($value)) { | |
201 | $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName; | |
202 | } | |
203 | break; | |
204 | ||
205 | case 'Link': | |
206 | if (!CRM_Utils_Rule::url($value)) { | |
207 | $errors[$fieldName] = 'Invalid link for ' . $fieldName; | |
208 | } | |
209 | break; | |
210 | ||
211 | case 'Boolean': | |
212 | if ($value != '1' && $value != '0') { | |
213 | $errors[$fieldName] = 'Invalid boolean (use 1 or 0) value for ' . $fieldName; | |
214 | } | |
215 | break; | |
216 | ||
217 | case 'Country': | |
218 | if (empty($value)) { | |
219 | break; | |
220 | } | |
221 | if ($htmlType != 'Multi-Select Country' && is_array($value)) { | |
222 | $errors[$fieldName] = 'Invalid country for ' . $fieldName; | |
223 | break; | |
224 | } | |
225 | ||
226 | if (!is_array($value)) { | |
227 | $value = array($value); | |
228 | } | |
229 | ||
230 | $query = "SELECT count(*) FROM civicrm_country WHERE id IN (" . implode(',', $value) . ")"; | |
231 | if (CRM_Core_DAO::singleValueQuery($query) < count($value)) { | |
232 | $errors[$fieldName] = 'Invalid country(s) for ' . $fieldName; | |
233 | } | |
234 | break; | |
235 | ||
236 | case 'StateProvince': | |
237 | if (empty($value)) { | |
238 | break; | |
239 | } | |
240 | ||
241 | if ($htmlType != 'Multi-Select State/Province' && is_array($value)) { | |
242 | $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName; | |
243 | break; | |
244 | } | |
245 | ||
246 | if (!is_array($value)) { | |
247 | $value = array($value); | |
248 | } | |
249 | ||
250 | $query = " | |
1c88e578 | 251 | SELECT count(*) |
6a488035 TO |
252 | FROM civicrm_state_province |
253 | WHERE id IN ('" . implode("','", $value) . "')"; | |
254 | if (CRM_Core_DAO::singleValueQuery($query) < count($value)) { | |
255 | $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName; | |
256 | } | |
257 | break; | |
258 | ||
259 | case 'ContactReference': | |
260 | //FIX ME | |
261 | break; | |
262 | } | |
263 | ||
264 | if (in_array($htmlType, array( | |
265 | 'Select', 'Multi-Select', 'CheckBox', 'Radio', 'AdvMulti-Select')) && | |
266 | !isset($errors[$fieldName]) | |
267 | ) { | |
6a488035 TO |
268 | $options = CRM_Core_OptionGroup::valuesByID($fieldDetails['option_group_id']); |
269 | if (!is_array($value)) { | |
270 | $value = array($value); | |
271 | } | |
272 | ||
273 | $invalidOptions = array_diff($value, array_keys($options)); | |
274 | if (!empty($invalidOptions)) { | |
275 | $errors[$fieldName] = "Invalid option(s) for field '{$fieldName}': " . implode(',', $invalidOptions); | |
276 | } | |
277 | } | |
278 | ||
279 | return $errors; | |
280 | } | |
281 |