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