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