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