Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 4.3 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | * | |
35 | * @copyright CiviCRM LLC (c) 2004-2013 | |
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 | ||
68 | if (!(CRM_Utils_Array::value('option_type', $params))) { | |
69 | if (CRM_Utils_Array::value('id', $params)) { | |
70 | $params['option_type'] = 2; | |
71 | } | |
72 | else { | |
73 | $params['option_type'] = 1; | |
74 | } | |
75 | } | |
76 | ||
77 | ||
78 | // Array created for passing options in params | |
79 | if (isset($params['option_values']) && is_array($params['option_values'])) { | |
80 | foreach ($params['option_values'] as $key => $value) { | |
81 | $params['option_label'][$key] = $value['label']; | |
82 | $params['option_value'][$key] = $value['value']; | |
83 | $params['option_status'][$key] = $value['is_active']; | |
84 | $params['option_weight'][$key] = $value['weight']; | |
85 | } | |
86 | } | |
87 | $customField = CRM_Core_BAO_CustomField::create($params); | |
88 | civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1)); | |
89 | _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]); | |
90 | return civicrm_api3_create_success($values, $params, 'custom_field', $customField); | |
91 | } | |
11e09c59 TO |
92 | |
93 | /** | |
6a488035 | 94 | * Adjust Metadata for Create action |
1c88e578 | 95 | * |
6a488035 TO |
96 | * @param array $params array or parameters determined by getfields |
97 | */ | |
98 | function _civicrm_api3_custom_field_create_spec(&$params) { | |
99 | $params['label']['api.required'] = 1; | |
100 | $params['custom_group_id']['api.required'] = 1; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Use this API to delete an existing custom group field. | |
105 | * | |
106 | * @param $params Array id of the field to be deleted | |
107 | * @example CustomFieldDelete.php | |
108 | * | |
109 | * {@example CustomFieldDelete.php 0} | |
110 | * {@getfields CustomField_delete} | |
111 | * @access public | |
112 | **/ | |
113 | function civicrm_api3_custom_field_delete($params) { | |
114 | ||
115 | $field = new CRM_Core_BAO_CustomField(); | |
116 | $field->id = $params['id']; | |
117 | $field->find(TRUE); | |
118 | $customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field); | |
119 | civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1)); | |
120 | return $customFieldDelete ? civicrm_api3_create_error('Error while deleting custom field') : civicrm_api3_create_success(); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Use this API to get existing custom fields. | |
125 | * | |
126 | * @param array $params Array to search on | |
127 | *{@getfields CustomField_get} | |
128 | * @access public | |
129 | * | |
130 | **/ | |
131 | function civicrm_api3_custom_field_get($params) { | |
132 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
133 | } | |
134 | ||
135 | /* | |
1c88e578 | 136 | * Helper function to validate custom field values |
137 | * | |
6a488035 | 138 | * @params Array $params Custom fields with values |
1c88e578 | 139 | * @params Array $errors Reference fields to be check with |
140 | * @params Boolean $checkForDisallowed Check for disallowed elements | |
6a488035 TO |
141 | * in params |
142 | * @params Boolean $checkForRequired Check for non present required elements | |
143 | * in params | |
144 | * @return Array Validation errors | |
145 | */ | |
146 | ||
11e09c59 | 147 | /** |
6a488035 | 148 | * Helper function to validate custom field value |
1c88e578 | 149 | * |
6a488035 TO |
150 | * @params String $fieldName Custom field name (eg: custom_8 ) |
151 | * @params Mixed $value Field value to be validate | |
152 | * @params Array $fieldDetails Field Details | |
153 | * @params Array $errors Collect validation errors | |
154 | * | |
155 | * @return Array Validation errors | |
156 | * @todo remove this function - not in use but need to review functionality before | |
157 | * removing as it might be useful in wrapper layer | |
158 | */ | |
159 | function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDetails, &$errors = array( | |
160 | )) { | |
161 | return; | |
162 | //see comment block | |
163 | if (!$value) { | |
164 | return $errors; | |
165 | } | |
166 | ||
167 | $dataType = $fieldDetails['data_type']; | |
168 | $htmlType = $fieldDetails['html_type']; | |
169 | ||
170 | switch ($dataType) { | |
171 | case 'Int': | |
172 | if (!CRM_Utils_Rule::integer($value)) { | |
173 | $errors[$fieldName] = 'Invalid integer value for ' . $fieldName; | |
174 | } | |
175 | break; | |
176 | ||
177 | case 'Float': | |
178 | if (!CRM_Utils_Rule::numeric($value)) { | |
179 | $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName; | |
180 | } | |
181 | break; | |
182 | ||
183 | case 'Money': | |
184 | if (!CRM_Utils_Rule::money($value)) { | |
185 | $errors[$fieldName] = 'Invalid numeric value for ' . $fieldName; | |
186 | } | |
187 | break; | |
188 | ||
189 | case 'Link': | |
190 | if (!CRM_Utils_Rule::url($value)) { | |
191 | $errors[$fieldName] = 'Invalid link for ' . $fieldName; | |
192 | } | |
193 | break; | |
194 | ||
195 | case 'Boolean': | |
196 | if ($value != '1' && $value != '0') { | |
197 | $errors[$fieldName] = 'Invalid boolean (use 1 or 0) value for ' . $fieldName; | |
198 | } | |
199 | break; | |
200 | ||
201 | case 'Country': | |
202 | if (empty($value)) { | |
203 | break; | |
204 | } | |
205 | if ($htmlType != 'Multi-Select Country' && is_array($value)) { | |
206 | $errors[$fieldName] = 'Invalid country for ' . $fieldName; | |
207 | break; | |
208 | } | |
209 | ||
210 | if (!is_array($value)) { | |
211 | $value = array($value); | |
212 | } | |
213 | ||
214 | $query = "SELECT count(*) FROM civicrm_country WHERE id IN (" . implode(',', $value) . ")"; | |
215 | if (CRM_Core_DAO::singleValueQuery($query) < count($value)) { | |
216 | $errors[$fieldName] = 'Invalid country(s) for ' . $fieldName; | |
217 | } | |
218 | break; | |
219 | ||
220 | case 'StateProvince': | |
221 | if (empty($value)) { | |
222 | break; | |
223 | } | |
224 | ||
225 | if ($htmlType != 'Multi-Select State/Province' && is_array($value)) { | |
226 | $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName; | |
227 | break; | |
228 | } | |
229 | ||
230 | if (!is_array($value)) { | |
231 | $value = array($value); | |
232 | } | |
233 | ||
234 | $query = " | |
1c88e578 | 235 | SELECT count(*) |
6a488035 TO |
236 | FROM civicrm_state_province |
237 | WHERE id IN ('" . implode("','", $value) . "')"; | |
238 | if (CRM_Core_DAO::singleValueQuery($query) < count($value)) { | |
239 | $errors[$fieldName] = 'Invalid State/Province for ' . $fieldName; | |
240 | } | |
241 | break; | |
242 | ||
243 | case 'ContactReference': | |
244 | //FIX ME | |
245 | break; | |
246 | } | |
247 | ||
248 | if (in_array($htmlType, array( | |
249 | 'Select', 'Multi-Select', 'CheckBox', 'Radio', 'AdvMulti-Select')) && | |
250 | !isset($errors[$fieldName]) | |
251 | ) { | |
6a488035 TO |
252 | $options = CRM_Core_OptionGroup::valuesByID($fieldDetails['option_group_id']); |
253 | if (!is_array($value)) { | |
254 | $value = array($value); | |
255 | } | |
256 | ||
257 | $invalidOptions = array_diff($value, array_keys($options)); | |
258 | if (!empty($invalidOptions)) { | |
259 | $errors[$fieldName] = "Invalid option(s) for field '{$fieldName}': " . implode(',', $invalidOptions); | |
260 | } | |
261 | } | |
262 | ||
263 | return $errors; | |
264 | } | |
265 |