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