| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Business objects for managing custom data fields. |
| 36 | */ |
| 37 | class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { |
| 38 | |
| 39 | /** |
| 40 | * Array for valid combinations of data_type & descriptions. |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | public static $_dataType = NULL; |
| 45 | |
| 46 | /** |
| 47 | * Array for valid combinations of data_type & html_type. |
| 48 | * |
| 49 | * @var array |
| 50 | */ |
| 51 | public static $_dataToHtml = NULL; |
| 52 | |
| 53 | /** |
| 54 | * Array to hold (formatted) fields for import |
| 55 | * |
| 56 | * @var array |
| 57 | */ |
| 58 | public static $_importFields = NULL; |
| 59 | |
| 60 | /** |
| 61 | * Build and retrieve the list of data types and descriptions. |
| 62 | * |
| 63 | * @return array |
| 64 | * Data type => Description |
| 65 | */ |
| 66 | public static function &dataType() { |
| 67 | if (!(self::$_dataType)) { |
| 68 | self::$_dataType = array( |
| 69 | 'String' => ts('Alphanumeric'), |
| 70 | 'Int' => ts('Integer'), |
| 71 | 'Float' => ts('Number'), |
| 72 | 'Money' => ts('Money'), |
| 73 | 'Memo' => ts('Note'), |
| 74 | 'Date' => ts('Date'), |
| 75 | 'Boolean' => ts('Yes or No'), |
| 76 | 'StateProvince' => ts('State/Province'), |
| 77 | 'Country' => ts('Country'), |
| 78 | 'File' => ts('File'), |
| 79 | 'Link' => ts('Link'), |
| 80 | 'ContactReference' => ts('Contact Reference'), |
| 81 | ); |
| 82 | } |
| 83 | return self::$_dataType; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get data to html array. |
| 88 | * |
| 89 | * (Does this caching achieve anything?) |
| 90 | * |
| 91 | * @return array |
| 92 | */ |
| 93 | public static function dataToHtml() { |
| 94 | if (!self::$_dataToHtml) { |
| 95 | self::$_dataToHtml = array( |
| 96 | array( |
| 97 | 'Text' => 'Text', |
| 98 | 'Select' => 'Select', |
| 99 | 'Radio' => 'Radio', |
| 100 | 'CheckBox' => 'CheckBox', |
| 101 | 'Multi-Select' => 'Multi-Select', |
| 102 | 'AdvMulti-Select' => 'AdvMulti-Select', |
| 103 | 'Autocomplete-Select' => 'Autocomplete-Select', |
| 104 | ), |
| 105 | array('Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'), |
| 106 | array('Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'), |
| 107 | array('Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'), |
| 108 | array('TextArea' => 'TextArea', 'RichTextEditor' => 'RichTextEditor'), |
| 109 | array('Date' => 'Select Date'), |
| 110 | array('Radio' => 'Radio'), |
| 111 | array('StateProvince' => 'Select State/Province', 'Multi-Select' => 'Multi-Select State/Province'), |
| 112 | array('Country' => 'Select Country', 'Multi-Select' => 'Multi-Select Country'), |
| 113 | array('File' => 'File'), |
| 114 | array('Link' => 'Link'), |
| 115 | array('ContactReference' => 'Autocomplete-Select'), |
| 116 | ); |
| 117 | } |
| 118 | return self::$_dataToHtml; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Takes an associative array and creates a custom field object. |
| 123 | * |
| 124 | * This function is invoked from within the web form layer and also from the api layer |
| 125 | * |
| 126 | * @param array $params |
| 127 | * (reference) an assoc array of name/value pairs. |
| 128 | * |
| 129 | * @return CRM_Core_DAO_CustomField |
| 130 | */ |
| 131 | public static function create(&$params) { |
| 132 | $origParams = array_merge(array(), $params); |
| 133 | |
| 134 | if (!isset($params['id'])) { |
| 135 | if (!isset($params['column_name'])) { |
| 136 | // if add mode & column_name not present, calculate it. |
| 137 | $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32)); |
| 138 | } |
| 139 | if (!isset($params['name'])) { |
| 140 | $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64); |
| 141 | } |
| 142 | } |
| 143 | else { |
| 144 | $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', |
| 145 | $params['id'], |
| 146 | 'column_name' |
| 147 | ); |
| 148 | } |
| 149 | $columnName = $params['column_name']; |
| 150 | |
| 151 | $indexExist = FALSE; |
| 152 | //as during create if field is_searchable we had created index. |
| 153 | if (!empty($params['id'])) { |
| 154 | $indexExist = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'is_searchable'); |
| 155 | } |
| 156 | |
| 157 | switch (CRM_Utils_Array::value('html_type', $params)) { |
| 158 | case 'Select Date': |
| 159 | if (empty($params['date_format'])) { |
| 160 | $config = CRM_Core_Config::singleton(); |
| 161 | $params['date_format'] = $config->dateInputFormat; |
| 162 | } |
| 163 | break; |
| 164 | |
| 165 | case 'CheckBox': |
| 166 | case 'AdvMulti-Select': |
| 167 | case 'Multi-Select': |
| 168 | if (isset($params['default_checkbox_option'])) { |
| 169 | $tempArray = array_keys($params['default_checkbox_option']); |
| 170 | $defaultArray = array(); |
| 171 | foreach ($tempArray as $k => $v) { |
| 172 | if ($params['option_value'][$v]) { |
| 173 | $defaultArray[] = $params['option_value'][$v]; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (!empty($defaultArray)) { |
| 178 | // also add the separator before and after the value per new convention (CRM-1604) |
| 179 | $params['default_value'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultArray) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 180 | } |
| 181 | } |
| 182 | else { |
| 183 | if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']]) |
| 184 | ) { |
| 185 | $params['default_value'] = $params['option_value'][$params['default_option']]; |
| 186 | } |
| 187 | } |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | $transaction = new CRM_Core_Transaction(); |
| 192 | // create any option group & values if required |
| 193 | if ($params['html_type'] != 'Text' && |
| 194 | in_array($params['data_type'], array( |
| 195 | 'String', |
| 196 | 'Int', |
| 197 | 'Float', |
| 198 | 'Money', |
| 199 | )) |
| 200 | ) { |
| 201 | |
| 202 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', |
| 203 | $params['custom_group_id'], |
| 204 | 'table_name' |
| 205 | ); |
| 206 | |
| 207 | //CRM-16659: if option_value then create an option group for this custom field. |
| 208 | if ($params['option_type'] == 1 && (empty($params['option_group_id']) || !empty($params['option_value']))) { |
| 209 | // first create an option group for this custom group |
| 210 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
| 211 | $optionGroup->name = "{$columnName}_" . date('YmdHis'); |
| 212 | $optionGroup->title = $params['label']; |
| 213 | $optionGroup->is_active = 1; |
| 214 | $optionGroup->save(); |
| 215 | $params['option_group_id'] = $optionGroup->id; |
| 216 | if (!empty($params['option_value']) && is_array($params['option_value'])) { |
| 217 | foreach ($params['option_value'] as $k => $v) { |
| 218 | if (strlen(trim($v))) { |
| 219 | $optionValue = new CRM_Core_DAO_OptionValue(); |
| 220 | $optionValue->option_group_id = $optionGroup->id; |
| 221 | $optionValue->label = $params['option_label'][$k]; |
| 222 | $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]); |
| 223 | switch ($params['data_type']) { |
| 224 | case 'Money': |
| 225 | $optionValue->value = CRM_Utils_Rule::cleanMoney($v); |
| 226 | break; |
| 227 | |
| 228 | case 'Int': |
| 229 | $optionValue->value = intval($v); |
| 230 | break; |
| 231 | |
| 232 | case 'Float': |
| 233 | $optionValue->value = floatval($v); |
| 234 | break; |
| 235 | |
| 236 | default: |
| 237 | $optionValue->value = trim($v); |
| 238 | } |
| 239 | |
| 240 | $optionValue->weight = $params['option_weight'][$k]; |
| 241 | $optionValue->is_active = CRM_Utils_Array::value($k, $params['option_status'], FALSE); |
| 242 | $optionValue->save(); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // check for orphan option groups |
| 250 | if (!empty($params['option_group_id'])) { |
| 251 | if (!empty($params['id'])) { |
| 252 | self::fixOptionGroups($params['id'], $params['option_group_id']); |
| 253 | } |
| 254 | |
| 255 | // if we do not have a default value |
| 256 | // retrieve it from one of the other custom fields which use this option group |
| 257 | if (empty($params['default_value'])) { |
| 258 | //don't insert only value separator as default value, CRM-4579 |
| 259 | $defaultValue = self::getOptionGroupDefault($params['option_group_id'], |
| 260 | $params['html_type'] |
| 261 | ); |
| 262 | |
| 263 | if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 264 | $defaultValue |
| 265 | )) |
| 266 | ) { |
| 267 | $params['default_value'] = $defaultValue; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // since we need to save option group id :) |
| 273 | if (!isset($params['attributes']) && strtolower($params['html_type']) == 'textarea') { |
| 274 | $params['attributes'] = 'rows=4, cols=60'; |
| 275 | } |
| 276 | |
| 277 | $customField = new CRM_Core_DAO_CustomField(); |
| 278 | $customField->copyValues($params); |
| 279 | $customField->is_required = CRM_Utils_Array::value('is_required', $params, FALSE); |
| 280 | $customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, FALSE); |
| 281 | $customField->in_selector = CRM_Utils_Array::value('in_selector', $params, FALSE); |
| 282 | $customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, FALSE); |
| 283 | //CRM-15792 - Custom field gets disabled if is_active not set |
| 284 | $customField->is_active = CRM_Utils_Array::value('is_active', $params, TRUE); |
| 285 | $customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE); |
| 286 | $customField->save(); |
| 287 | |
| 288 | // make sure all values are present in the object for further processing |
| 289 | $customField->find(TRUE); |
| 290 | |
| 291 | $triggerRebuild = CRM_Utils_Array::value('triggerRebuild', $params, TRUE); |
| 292 | //create/drop the index when we toggle the is_searchable flag |
| 293 | if (!empty($params['id'])) { |
| 294 | self::createField($customField, 'modify', $indexExist, $triggerRebuild); |
| 295 | } |
| 296 | else { |
| 297 | if (!isset($origParams['column_name'])) { |
| 298 | $columnName .= "_{$customField->id}"; |
| 299 | $params['column_name'] = $columnName; |
| 300 | } |
| 301 | $customField->column_name = $columnName; |
| 302 | $customField->save(); |
| 303 | // make sure all values are present in the object |
| 304 | $customField->find(TRUE); |
| 305 | |
| 306 | $indexExist = FALSE; |
| 307 | self::createField($customField, 'add', $indexExist, $triggerRebuild); |
| 308 | } |
| 309 | |
| 310 | // complete transaction |
| 311 | $transaction->commit(); |
| 312 | |
| 313 | CRM_Utils_System::flushCache(); |
| 314 | |
| 315 | return $customField; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Fetch object based on array of properties. |
| 320 | * |
| 321 | * @param array $params |
| 322 | * (reference ) an assoc array of name/value pairs. |
| 323 | * @param array $defaults |
| 324 | * (reference ) an assoc array to hold the flattened values. |
| 325 | * |
| 326 | * @return CRM_Core_DAO_CustomField |
| 327 | */ |
| 328 | public static function retrieve(&$params, &$defaults) { |
| 329 | return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Update the is_active flag in the db. |
| 334 | * |
| 335 | * @param int $id |
| 336 | * Id of the database record. |
| 337 | * @param bool $is_active |
| 338 | * Value we want to set the is_active field. |
| 339 | * |
| 340 | * @return Object |
| 341 | * DAO object on success, null otherwise |
| 342 | */ |
| 343 | public static function setIsActive($id, $is_active) { |
| 344 | |
| 345 | CRM_Utils_System::flushCache(); |
| 346 | |
| 347 | //enable-disable CustomField |
| 348 | CRM_Core_BAO_UFField::setUFField($id, $is_active); |
| 349 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_CustomField', $id, 'is_active', $is_active); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Get the field title. |
| 354 | * |
| 355 | * @param int $id |
| 356 | * Id of field. |
| 357 | * |
| 358 | * @return string |
| 359 | * name |
| 360 | */ |
| 361 | public static function getTitle($id) { |
| 362 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label'); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Store and return an array of all active custom fields. |
| 367 | * |
| 368 | * @param string $customDataType |
| 369 | * Type of Custom Data; empty is a synonym for "all contact data types". |
| 370 | * @param bool $showAll |
| 371 | * If true returns all fields (includes disabled fields). |
| 372 | * @param bool $inline |
| 373 | * If true returns all inline fields (includes disabled fields). |
| 374 | * @param int $customDataSubType |
| 375 | * Custom Data sub type value. |
| 376 | * @param int $customDataSubName |
| 377 | * Custom Data sub name value. |
| 378 | * @param bool $onlyParent |
| 379 | * Return only top level custom data, for eg, only Participant and ignore subname and subtype. |
| 380 | * @param bool $onlySubType |
| 381 | * Return only custom data for subtype. |
| 382 | * @param bool $checkPermission |
| 383 | * If false, do not include permissioning clause. |
| 384 | * |
| 385 | * @return array |
| 386 | * an array of active custom fields. |
| 387 | */ |
| 388 | public static function &getFields( |
| 389 | $customDataType = 'Individual', |
| 390 | $showAll = FALSE, |
| 391 | $inline = FALSE, |
| 392 | $customDataSubType = NULL, |
| 393 | $customDataSubName = NULL, |
| 394 | $onlyParent = FALSE, |
| 395 | $onlySubType = FALSE, |
| 396 | $checkPermission = TRUE |
| 397 | ) { |
| 398 | if (empty($customDataType)) { |
| 399 | $customDataType = array('Contact', 'Individual', 'Organization', 'Household'); |
| 400 | } |
| 401 | if ($customDataType && !is_array($customDataType)) { |
| 402 | |
| 403 | if (in_array($customDataType, CRM_Contact_BAO_ContactType::subTypes())) { |
| 404 | // This is the case when getFieldsForImport() requires fields |
| 405 | // limited strictly to a subtype. |
| 406 | $customDataSubType = $customDataType; |
| 407 | $customDataType = CRM_Contact_BAO_ContactType::getBasicType($customDataType); |
| 408 | $onlySubType = TRUE; |
| 409 | } |
| 410 | |
| 411 | if (in_array($customDataType, array_keys(CRM_Core_SelectValues::customGroupExtends()))) { |
| 412 | // this makes the method flexible to support retrieving fields |
| 413 | // for multiple extends value. |
| 414 | $customDataType = array($customDataType); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | $customDataSubType = CRM_Utils_Array::explodePadded($customDataSubType); |
| 419 | |
| 420 | if (is_array($customDataType)) { |
| 421 | $cacheKey = implode('_', $customDataType); |
| 422 | } |
| 423 | else { |
| 424 | $cacheKey = $customDataType; |
| 425 | } |
| 426 | |
| 427 | $cacheKey .= !empty($customDataSubType) ? ('_' . implode('_', $customDataSubType)) : '_0'; |
| 428 | $cacheKey .= $customDataSubName ? "{$customDataSubName}_" : '_0'; |
| 429 | $cacheKey .= $showAll ? '_1' : '_0'; |
| 430 | $cacheKey .= $inline ? '_1_' : '_0_'; |
| 431 | $cacheKey .= $onlyParent ? '_1_' : '_0_'; |
| 432 | $cacheKey .= $onlySubType ? '_1_' : '_0_'; |
| 433 | $cacheKey .= $checkPermission ? '_1_' : '_0_'; |
| 434 | |
| 435 | $cgTable = CRM_Core_DAO_CustomGroup::getTableName(); |
| 436 | |
| 437 | // also get the permission stuff here |
| 438 | if ($checkPermission) { |
| 439 | $permissionClause = CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, |
| 440 | "{$cgTable}." |
| 441 | ); |
| 442 | } |
| 443 | else { |
| 444 | $permissionClause = '(1)'; |
| 445 | } |
| 446 | |
| 447 | // lets md5 permission clause and take first 8 characters |
| 448 | $cacheKey .= substr(md5($permissionClause), 0, 8); |
| 449 | |
| 450 | if (strlen($cacheKey) > 40) { |
| 451 | $cacheKey = md5($cacheKey); |
| 452 | } |
| 453 | |
| 454 | if (!self::$_importFields || |
| 455 | CRM_Utils_Array::value($cacheKey, self::$_importFields) === NULL |
| 456 | ) { |
| 457 | if (!self::$_importFields) { |
| 458 | self::$_importFields = array(); |
| 459 | } |
| 460 | |
| 461 | // check if we can retrieve from database cache |
| 462 | $fields = CRM_Core_BAO_Cache::getItem('contact fields', "custom importableFields $cacheKey"); |
| 463 | |
| 464 | if ($fields === NULL) { |
| 465 | $cfTable = self::getTableName(); |
| 466 | |
| 467 | $extends = ''; |
| 468 | if (is_array($customDataType)) { |
| 469 | $value = NULL; |
| 470 | foreach ($customDataType as $dataType) { |
| 471 | if (in_array($dataType, array_keys(CRM_Core_SelectValues::customGroupExtends()))) { |
| 472 | if (in_array($dataType, array('Individual', 'Household', 'Organization'))) { |
| 473 | $val = "'" . CRM_Utils_Type::escape($dataType, 'String') . "', 'Contact' "; |
| 474 | } |
| 475 | else { |
| 476 | $val = "'" . CRM_Utils_Type::escape($dataType, 'String') . "'"; |
| 477 | } |
| 478 | $value = $value ? $value . ", {$val}" : $val; |
| 479 | } |
| 480 | } |
| 481 | if ($value) { |
| 482 | $extends = "AND $cgTable.extends IN ( $value ) "; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (!empty($customDataType) && empty($extends)) { |
| 487 | // $customDataType specified a filter, but there is no corresponding SQL ($extends) |
| 488 | self::$_importFields[$cacheKey] = array(); |
| 489 | return self::$_importFields[$cacheKey]; |
| 490 | } |
| 491 | |
| 492 | if ($onlyParent) { |
| 493 | $extends .= " AND $cgTable.extends_entity_column_value IS NULL AND $cgTable.extends_entity_column_id IS NULL "; |
| 494 | } |
| 495 | |
| 496 | $query = "SELECT $cfTable.id, $cfTable.label, |
| 497 | $cgTable.title, |
| 498 | $cfTable.data_type, |
| 499 | $cfTable.html_type, |
| 500 | $cfTable.default_value, |
| 501 | $cfTable.options_per_line, $cfTable.text_length, |
| 502 | $cfTable.custom_group_id, |
| 503 | $cfTable.is_required, |
| 504 | $cgTable.extends, $cfTable.is_search_range, |
| 505 | $cgTable.extends_entity_column_value, |
| 506 | $cgTable.extends_entity_column_id, |
| 507 | $cfTable.is_view, |
| 508 | $cfTable.option_group_id, |
| 509 | $cfTable.date_format, |
| 510 | $cfTable.time_format, |
| 511 | $cgTable.is_multiple, |
| 512 | og.name as option_group_name |
| 513 | FROM $cfTable |
| 514 | INNER JOIN $cgTable |
| 515 | ON $cfTable.custom_group_id = $cgTable.id |
| 516 | LEFT JOIN civicrm_option_group og |
| 517 | ON $cfTable.option_group_id = og.id |
| 518 | WHERE ( 1 ) "; |
| 519 | |
| 520 | if (!$showAll) { |
| 521 | $query .= " AND $cfTable.is_active = 1 AND $cgTable.is_active = 1 "; |
| 522 | } |
| 523 | |
| 524 | if ($inline) { |
| 525 | $query .= " AND $cgTable.style = 'Inline' "; |
| 526 | } |
| 527 | |
| 528 | //get the custom fields for specific type in |
| 529 | //combination with fields those support any type. |
| 530 | if (!empty($customDataSubType)) { |
| 531 | $subtypeClause = array(); |
| 532 | foreach ($customDataSubType as $subtype) { |
| 533 | $subtype = CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR; |
| 534 | $subtypeClause[] = "$cgTable.extends_entity_column_value LIKE '%{$subtype}%'"; |
| 535 | } |
| 536 | if (!$onlySubType) { |
| 537 | $subtypeClause[] = "$cgTable.extends_entity_column_value IS NULL"; |
| 538 | } |
| 539 | $query .= " AND ( " . implode(' OR ', $subtypeClause) . " )"; |
| 540 | } |
| 541 | |
| 542 | if ($customDataSubName) { |
| 543 | $query .= " AND ( $cgTable.extends_entity_column_id = $customDataSubName ) "; |
| 544 | } |
| 545 | |
| 546 | // also get the permission stuff here |
| 547 | if ($checkPermission) { |
| 548 | $permissionClause = CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, |
| 549 | "{$cgTable}.", TRUE |
| 550 | ); |
| 551 | } |
| 552 | else { |
| 553 | $permissionClause = '(1)'; |
| 554 | } |
| 555 | |
| 556 | $query .= " $extends AND $permissionClause |
| 557 | ORDER BY $cgTable.weight, $cgTable.title, |
| 558 | $cfTable.weight, $cfTable.label"; |
| 559 | |
| 560 | $dao = CRM_Core_DAO::executeQuery($query); |
| 561 | |
| 562 | $fields = array(); |
| 563 | while (($dao->fetch()) != NULL) { |
| 564 | $fields[$dao->id]['label'] = $dao->label; |
| 565 | $fields[$dao->id]['groupTitle'] = $dao->title; |
| 566 | $fields[$dao->id]['data_type'] = $dao->data_type; |
| 567 | $fields[$dao->id]['html_type'] = $dao->html_type; |
| 568 | $fields[$dao->id]['default_value'] = $dao->default_value; |
| 569 | $fields[$dao->id]['text_length'] = $dao->text_length; |
| 570 | $fields[$dao->id]['options_per_line'] = $dao->options_per_line; |
| 571 | $fields[$dao->id]['custom_group_id'] = $dao->custom_group_id; |
| 572 | $fields[$dao->id]['extends'] = $dao->extends; |
| 573 | $fields[$dao->id]['is_search_range'] = $dao->is_search_range; |
| 574 | $fields[$dao->id]['extends_entity_column_value'] = $dao->extends_entity_column_value; |
| 575 | $fields[$dao->id]['extends_entity_column_id'] = $dao->extends_entity_column_id; |
| 576 | $fields[$dao->id]['is_view'] = $dao->is_view; |
| 577 | $fields[$dao->id]['is_multiple'] = $dao->is_multiple; |
| 578 | $fields[$dao->id]['option_group_id'] = $dao->option_group_id; |
| 579 | $fields[$dao->id]['date_format'] = $dao->date_format; |
| 580 | $fields[$dao->id]['time_format'] = $dao->time_format; |
| 581 | $fields[$dao->id]['is_required'] = $dao->is_required; |
| 582 | self::getOptionsForField($fields[$dao->id], $dao->option_group_name); |
| 583 | } |
| 584 | |
| 585 | CRM_Core_BAO_Cache::setItem($fields, |
| 586 | 'contact fields', |
| 587 | "custom importableFields $cacheKey" |
| 588 | ); |
| 589 | } |
| 590 | self::$_importFields[$cacheKey] = $fields; |
| 591 | } |
| 592 | |
| 593 | return self::$_importFields[$cacheKey]; |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Return the field ids and names (with groups) for import purpose. |
| 598 | * |
| 599 | * @param int|string $contactType Contact type |
| 600 | * @param bool $showAll |
| 601 | * If true returns all fields (includes disabled fields). |
| 602 | * @param bool $onlyParent |
| 603 | * Return fields ONLY related to basic types. |
| 604 | * @param bool $search |
| 605 | * When called from search and multiple records need to be returned. |
| 606 | * @param bool $checkPermission |
| 607 | * If false, do not include permissioning clause. |
| 608 | * |
| 609 | * @param bool $withMultiple |
| 610 | * |
| 611 | * @return array |
| 612 | */ |
| 613 | public static function getFieldsForImport( |
| 614 | $contactType = 'Individual', |
| 615 | $showAll = FALSE, |
| 616 | $onlyParent = FALSE, |
| 617 | $search = FALSE, |
| 618 | $checkPermission = TRUE, |
| 619 | $withMultiple = FALSE |
| 620 | ) { |
| 621 | // Note: there are situations when we want getFieldsForImport() return fields related |
| 622 | // ONLY to basic contact types, but NOT subtypes. And thats where $onlyParent is helpful |
| 623 | $fields = &self::getFields($contactType, |
| 624 | $showAll, |
| 625 | FALSE, |
| 626 | NULL, |
| 627 | NULL, |
| 628 | $onlyParent, |
| 629 | FALSE, |
| 630 | $checkPermission |
| 631 | ); |
| 632 | |
| 633 | $importableFields = array(); |
| 634 | foreach ($fields as $id => $values) { |
| 635 | // for now we should not allow multiple fields in profile / export etc, hence unsetting |
| 636 | if (!$search && |
| 637 | (!empty($values['is_multiple']) && !$withMultiple) |
| 638 | ) { |
| 639 | continue; |
| 640 | } |
| 641 | |
| 642 | /* generate the key for the fields array */ |
| 643 | |
| 644 | $key = "custom_$id"; |
| 645 | |
| 646 | $regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array::value(0, $values)); |
| 647 | $importableFields[$key] = array( |
| 648 | 'name' => $key, |
| 649 | 'title' => CRM_Utils_Array::value('label', $values), |
| 650 | 'headerPattern' => '/' . preg_quote($regexp, '/') . '/', |
| 651 | 'import' => 1, |
| 652 | 'custom_field_id' => $id, |
| 653 | 'options_per_line' => CRM_Utils_Array::value('options_per_line', $values), |
| 654 | 'text_length' => CRM_Utils_Array::value('text_length', $values, 255), |
| 655 | 'data_type' => CRM_Utils_Array::value('data_type', $values), |
| 656 | 'html_type' => CRM_Utils_Array::value('html_type', $values), |
| 657 | 'is_search_range' => CRM_Utils_Array::value('is_search_range', $values), |
| 658 | ); |
| 659 | |
| 660 | // CRM-6681, pass date and time format when html_type = Select Date |
| 661 | if (CRM_Utils_Array::value('html_type', $values) == 'Select Date') { |
| 662 | $importableFields[$key]['date_format'] = CRM_Utils_Array::value('date_format', $values); |
| 663 | $importableFields[$key]['time_format'] = CRM_Utils_Array::value('time_format', $values); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | return $importableFields; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Get the field id from an import key. |
| 672 | * |
| 673 | * @param string $key |
| 674 | * The key to parse. |
| 675 | * |
| 676 | * @param bool $all |
| 677 | * |
| 678 | * @return int|null |
| 679 | * The id (if exists) |
| 680 | */ |
| 681 | public static function getKeyID($key, $all = FALSE) { |
| 682 | $match = array(); |
| 683 | if (preg_match('/^custom_(\d+)_?(-?\d+)?$/', $key, $match)) { |
| 684 | if (!$all) { |
| 685 | return $match[1]; |
| 686 | } |
| 687 | else { |
| 688 | return array( |
| 689 | $match[1], |
| 690 | CRM_Utils_Array::value(2, $match), |
| 691 | ); |
| 692 | } |
| 693 | } |
| 694 | return $all ? array(NULL, NULL) : NULL; |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Use the cache to get all values of a specific custom field. |
| 699 | * |
| 700 | * @param int $fieldID |
| 701 | * The custom field ID. |
| 702 | * |
| 703 | * @return CRM_Core_DAO_CustomField |
| 704 | * The field object. |
| 705 | */ |
| 706 | public static function getFieldObject($fieldID) { |
| 707 | $field = new CRM_Core_DAO_CustomField(); |
| 708 | |
| 709 | // check if we can get the field values from the system cache |
| 710 | $cacheKey = "CRM_Core_DAO_CustomField_{$fieldID}"; |
| 711 | $cache = CRM_Utils_Cache::singleton(); |
| 712 | $fieldValues = $cache->get($cacheKey); |
| 713 | if (empty($fieldValues)) { |
| 714 | $field->id = $fieldID; |
| 715 | if (!$field->find(TRUE)) { |
| 716 | CRM_Core_Error::fatal(); |
| 717 | } |
| 718 | |
| 719 | $fieldValues = array(); |
| 720 | CRM_Core_DAO::storeValues($field, $fieldValues); |
| 721 | |
| 722 | $cache->set($cacheKey, $fieldValues); |
| 723 | } |
| 724 | else { |
| 725 | $field->copyValues($fieldValues); |
| 726 | } |
| 727 | |
| 728 | return $field; |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * This function for building custom fields. |
| 733 | * |
| 734 | * @param CRM_Core_Form $qf |
| 735 | * Form object (reference). |
| 736 | * @param string $elementName |
| 737 | * Name of the custom field. |
| 738 | * @param int $fieldId |
| 739 | * @param bool $inactiveNeeded |
| 740 | * -deprecated. |
| 741 | * @param bool $useRequired |
| 742 | * True if required else false. |
| 743 | * @param bool $search |
| 744 | * True if used for search else false. |
| 745 | * @param string $label |
| 746 | * Label for custom field. |
| 747 | */ |
| 748 | public static function addQuickFormElement( |
| 749 | &$qf, |
| 750 | $elementName, |
| 751 | $fieldId, |
| 752 | $inactiveNeeded = FALSE, |
| 753 | $useRequired = TRUE, |
| 754 | $search = FALSE, |
| 755 | $label = NULL |
| 756 | ) { |
| 757 | $field = self::getFieldObject($fieldId); |
| 758 | $widget = $field->html_type; |
| 759 | |
| 760 | // Custom field HTML should indicate group+field name |
| 761 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id); |
| 762 | $dataCrmCustomVal = $groupName . ':' . $field->name; |
| 763 | $dataCrmCustomAttr = 'data-crm-custom="' . $dataCrmCustomVal . '"'; |
| 764 | $field->attributes .= $dataCrmCustomAttr; |
| 765 | |
| 766 | // Fixed for Issue CRM-2183 |
| 767 | if ($widget == 'TextArea' && $search) { |
| 768 | $widget = 'Text'; |
| 769 | } |
| 770 | |
| 771 | $placeholder = $search ? ts('- any -') : ($useRequired ? ts('- select -') : ts('- none -')); |
| 772 | |
| 773 | // FIXME: Why are select state/country separate widget types? |
| 774 | $isSelect = (in_array($widget, array( |
| 775 | 'Select', |
| 776 | 'Multi-Select', |
| 777 | 'Select State/Province', |
| 778 | 'Multi-Select State/Province', |
| 779 | 'Select Country', |
| 780 | 'Multi-Select Country', |
| 781 | 'AdvMulti-Select', |
| 782 | 'CheckBox', |
| 783 | 'Radio', |
| 784 | ))); |
| 785 | |
| 786 | if ($isSelect) { |
| 787 | $options = CRM_Utils_Array::value('values', civicrm_api3('contact', 'getoptions', array( |
| 788 | 'field' => "custom_$fieldId", |
| 789 | 'context' => $search ? 'search' : 'create', |
| 790 | ), array())); |
| 791 | |
| 792 | // Consolidate widget types to simplify the below switch statement |
| 793 | if ($search || ($widget !== 'AdvMulti-Select' && strpos($widget, 'Select') !== FALSE)) { |
| 794 | $widget = 'Select'; |
| 795 | } |
| 796 | $selectAttributes = array( |
| 797 | 'data-crm-custom' => $dataCrmCustomVal, |
| 798 | 'class' => 'crm-select2', |
| 799 | ); |
| 800 | // Search field is always multi-select |
| 801 | if ($search || strpos($field->html_type, 'Multi') !== FALSE) { |
| 802 | $selectAttributes['class'] .= ' huge'; |
| 803 | $selectAttributes['multiple'] = 'multiple'; |
| 804 | $selectAttributes['placeholder'] = $placeholder; |
| 805 | } |
| 806 | // Add data for popup link. Normally this is handled by CRM_Core_Form->addSelect |
| 807 | if ($field->option_group_id && !$search && $widget == 'Select' && CRM_Core_Permission::check('administer CiviCRM')) { |
| 808 | $selectAttributes += array( |
| 809 | 'data-api-entity' => 'contact', |
| 810 | // FIXME: This works because the getoptions api isn't picky about custom fields, but it's WRONG |
| 811 | 'data-api-field' => 'custom_' . $field->id, |
| 812 | 'data-option-edit-path' => 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $field->option_group_id), |
| 813 | ); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | if (!isset($label)) { |
| 818 | $label = $field->label; |
| 819 | } |
| 820 | |
| 821 | // at some point in time we might want to split the below into small functions |
| 822 | |
| 823 | switch ($widget) { |
| 824 | case 'Text': |
| 825 | case 'Link': |
| 826 | if ($field->is_search_range && $search) { |
| 827 | $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $field->attributes); |
| 828 | $qf->add('text', $elementName . '_to', ts('To'), $field->attributes); |
| 829 | } |
| 830 | else { |
| 831 | $element = &$qf->add('text', $elementName, $label, |
| 832 | $field->attributes, |
| 833 | $useRequired && !$search |
| 834 | ); |
| 835 | } |
| 836 | break; |
| 837 | |
| 838 | case 'TextArea': |
| 839 | $attributes = $dataCrmCustomAttr; |
| 840 | if ($field->note_rows) { |
| 841 | $attributes .= 'rows=' . $field->note_rows; |
| 842 | } |
| 843 | else { |
| 844 | $attributes .= 'rows=4'; |
| 845 | } |
| 846 | if ($field->note_columns) { |
| 847 | $attributes .= ' cols=' . $field->note_columns; |
| 848 | } |
| 849 | else { |
| 850 | $attributes .= ' cols=60'; |
| 851 | } |
| 852 | if ($field->text_length) { |
| 853 | $attributes .= ' maxlength=' . $field->text_length; |
| 854 | } |
| 855 | $element = &$qf->add('textarea', |
| 856 | $elementName, |
| 857 | $label, |
| 858 | $attributes, |
| 859 | $useRequired && !$search |
| 860 | ); |
| 861 | break; |
| 862 | |
| 863 | case 'Select Date': |
| 864 | if ($field->is_search_range && $search) { |
| 865 | $qf->addDate($elementName . '_from', $label . ' - ' . ts('From'), FALSE, |
| 866 | array( |
| 867 | 'format' => $field->date_format, |
| 868 | 'timeFormat' => $field->time_format, |
| 869 | 'startOffset' => $field->start_date_years, |
| 870 | 'endOffset' => $field->end_date_years, |
| 871 | 'data-crm-custom' => $dataCrmCustomVal, |
| 872 | ) |
| 873 | ); |
| 874 | |
| 875 | $qf->addDate($elementName . '_to', ts('To'), FALSE, |
| 876 | array( |
| 877 | 'format' => $field->date_format, |
| 878 | 'timeFormat' => $field->time_format, |
| 879 | 'startOffset' => $field->start_date_years, |
| 880 | 'endOffset' => $field->end_date_years, |
| 881 | 'data-crm-custom' => $dataCrmCustomVal, |
| 882 | ) |
| 883 | ); |
| 884 | } |
| 885 | else { |
| 886 | $required = $useRequired && !$search; |
| 887 | |
| 888 | $qf->addDate($elementName, $label, $required, array( |
| 889 | 'format' => $field->date_format, |
| 890 | 'timeFormat' => $field->time_format, |
| 891 | 'startOffset' => $field->start_date_years, |
| 892 | 'endOffset' => $field->end_date_years, |
| 893 | 'data-crm-custom' => $dataCrmCustomVal, |
| 894 | )); |
| 895 | } |
| 896 | break; |
| 897 | |
| 898 | case 'Radio': |
| 899 | $choice = array(); |
| 900 | foreach ($options as $v => $l) { |
| 901 | $choice[] = $qf->createElement('radio', NULL, '', $l, (string) $v, $field->attributes); |
| 902 | } |
| 903 | $group = $qf->addGroup($choice, $elementName, $label); |
| 904 | if ($useRequired && !$search) { |
| 905 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); |
| 906 | } |
| 907 | else { |
| 908 | $group->setAttribute('allowClear', TRUE); |
| 909 | } |
| 910 | break; |
| 911 | |
| 912 | // For all select elements |
| 913 | case 'Select': |
| 914 | if (empty($selectAttributes['multiple'])) { |
| 915 | $options = array('' => $placeholder) + $options; |
| 916 | } |
| 917 | $qf->add('select', $elementName, $label, $options, $useRequired && !$search, $selectAttributes); |
| 918 | |
| 919 | // Add and/or option for fields that store multiple values |
| 920 | if ($search && self::isSerialized($field)) { |
| 921 | |
| 922 | $operators = array( |
| 923 | $qf->createElement('radio', NULL, '', ts('Any'), 'or', array('title' => ts('Results may contain any of the selected options'))), |
| 924 | $qf->createElement('radio', NULL, '', ts('All'), 'and', array('title' => ts('Results must have all of the selected options'))), |
| 925 | ); |
| 926 | $qf->addGroup($operators, $elementName . '_operator'); |
| 927 | $qf->setDefaults(array($elementName . '_operator' => 'or')); |
| 928 | } |
| 929 | break; |
| 930 | |
| 931 | case 'AdvMulti-Select': |
| 932 | $include =& $qf->addElement( |
| 933 | 'advmultiselect', |
| 934 | $elementName, |
| 935 | $label, $options, |
| 936 | array( |
| 937 | 'size' => 5, |
| 938 | 'style' => '', |
| 939 | 'class' => 'advmultiselect', |
| 940 | 'data-crm-custom' => $dataCrmCustomVal, |
| 941 | ) |
| 942 | ); |
| 943 | |
| 944 | $include->setButtonAttributes('add', array('value' => ts('Add >>'))); |
| 945 | $include->setButtonAttributes('remove', array('value' => ts('<< Remove'))); |
| 946 | |
| 947 | if ($useRequired && !$search) { |
| 948 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); |
| 949 | } |
| 950 | break; |
| 951 | |
| 952 | case 'CheckBox': |
| 953 | $check = array(); |
| 954 | foreach ($options as $v => $l) { |
| 955 | $check[] = &$qf->addElement('advcheckbox', $v, NULL, $l, array('data-crm-custom' => $dataCrmCustomVal)); |
| 956 | } |
| 957 | $qf->addGroup($check, $elementName, $label); |
| 958 | if ($useRequired && !$search) { |
| 959 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); |
| 960 | } |
| 961 | break; |
| 962 | |
| 963 | case 'File': |
| 964 | // we should not build upload file in search mode |
| 965 | if ($search) { |
| 966 | return; |
| 967 | } |
| 968 | $qf->add( |
| 969 | strtolower($field->html_type), |
| 970 | $elementName, |
| 971 | $label, |
| 972 | $field->attributes, |
| 973 | $useRequired && !$search |
| 974 | ); |
| 975 | $qf->addUploadElement($elementName); |
| 976 | break; |
| 977 | |
| 978 | case 'RichTextEditor': |
| 979 | $attributes = array( |
| 980 | 'rows' => $field->note_rows, |
| 981 | 'cols' => $field->note_columns, |
| 982 | 'data-crm-custom' => $dataCrmCustomVal, |
| 983 | ); |
| 984 | if ($field->text_length) { |
| 985 | $attributes['maxlength'] = $field->text_length; |
| 986 | } |
| 987 | $qf->add('wysiwyg', $elementName, $label, $attributes, $search); |
| 988 | break; |
| 989 | |
| 990 | case 'Autocomplete-Select': |
| 991 | static $customUrls = array(); |
| 992 | // Fixme: why is this a string in the first place?? |
| 993 | $attributes = array(); |
| 994 | if ($field->attributes) { |
| 995 | foreach (explode(' ', $field->attributes) as $at) { |
| 996 | if (strpos($at, '=')) { |
| 997 | list($k, $v) = explode('=', $at); |
| 998 | $attributes[$k] = trim($v, ' "'); |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | if ($field->data_type == 'ContactReference') { |
| 1003 | $attributes['class'] = (isset($attributes['class']) ? $attributes['class'] . ' ' : '') . 'crm-form-contact-reference huge'; |
| 1004 | $attributes['data-api-entity'] = 'contact'; |
| 1005 | $qf->add('text', $elementName, $label, $attributes, |
| 1006 | $useRequired && !$search |
| 1007 | ); |
| 1008 | |
| 1009 | $urlParams = "context=customfield&id={$field->id}"; |
| 1010 | |
| 1011 | $customUrls[$elementName] = CRM_Utils_System::url('civicrm/ajax/contactref', |
| 1012 | $urlParams, |
| 1013 | FALSE, NULL, FALSE |
| 1014 | ); |
| 1015 | |
| 1016 | } |
| 1017 | else { |
| 1018 | // FIXME: This won't work with customFieldOptions hook |
| 1019 | $attributes += array( |
| 1020 | 'entity' => 'option_value', |
| 1021 | 'placeholder' => $placeholder, |
| 1022 | 'multiple' => $search, |
| 1023 | 'api' => array( |
| 1024 | 'params' => array('option_group_id' => $field->option_group_id), |
| 1025 | ), |
| 1026 | ); |
| 1027 | $qf->addEntityRef($elementName, $label, $attributes, $useRequired && !$search); |
| 1028 | } |
| 1029 | |
| 1030 | $qf->assign('customUrls', $customUrls); |
| 1031 | break; |
| 1032 | } |
| 1033 | |
| 1034 | switch ($field->data_type) { |
| 1035 | case 'Int': |
| 1036 | // integers will have numeric rule applied to them. |
| 1037 | if ($field->is_search_range && $search) { |
| 1038 | $qf->addRule($elementName . '_from', ts('%1 From must be an integer (whole number).', array(1 => $label)), 'integer'); |
| 1039 | $qf->addRule($elementName . '_to', ts('%1 To must be an integer (whole number).', array(1 => $label)), 'integer'); |
| 1040 | } |
| 1041 | elseif ($widget == 'Text') { |
| 1042 | $qf->addRule($elementName, ts('%1 must be an integer (whole number).', array(1 => $label)), 'integer'); |
| 1043 | } |
| 1044 | break; |
| 1045 | |
| 1046 | case 'Float': |
| 1047 | if ($field->is_search_range && $search) { |
| 1048 | $qf->addRule($elementName . '_from', ts('%1 From must be a number (with or without decimal point).', array(1 => $label)), 'numeric'); |
| 1049 | $qf->addRule($elementName . '_to', ts('%1 To must be a number (with or without decimal point).', array(1 => $label)), 'numeric'); |
| 1050 | } |
| 1051 | elseif ($widget == 'Text') { |
| 1052 | $qf->addRule($elementName, ts('%1 must be a number (with or without decimal point).', array(1 => $label)), 'numeric'); |
| 1053 | } |
| 1054 | break; |
| 1055 | |
| 1056 | case 'Money': |
| 1057 | if ($field->is_search_range && $search) { |
| 1058 | $qf->addRule($elementName . '_from', ts('%1 From must in proper money format. (decimal point/comma/space is allowed).', array(1 => $label)), 'money'); |
| 1059 | $qf->addRule($elementName . '_to', ts('%1 To must in proper money format. (decimal point/comma/space is allowed).', array(1 => $label)), 'money'); |
| 1060 | } |
| 1061 | elseif ($widget == 'Text') { |
| 1062 | $qf->addRule($elementName, ts('%1 must be in proper money format. (decimal point/comma/space is allowed).', array(1 => $label)), 'money'); |
| 1063 | } |
| 1064 | break; |
| 1065 | |
| 1066 | case 'Link': |
| 1067 | $element->setAttribute('class', "url"); |
| 1068 | $qf->addRule($elementName, ts('Enter a valid web address beginning with \'http://\' or \'https://\'.'), 'wikiURL'); |
| 1069 | break; |
| 1070 | } |
| 1071 | if ($field->is_view && !$search) { |
| 1072 | $qf->freeze($elementName); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | /** |
| 1077 | * Delete the Custom Field. |
| 1078 | * |
| 1079 | * @param object $field |
| 1080 | * The field object. |
| 1081 | */ |
| 1082 | public static function deleteField($field) { |
| 1083 | CRM_Utils_System::flushCache(); |
| 1084 | |
| 1085 | // first delete the custom option group and values associated with this field |
| 1086 | if ($field->option_group_id) { |
| 1087 | //check if option group is related to any other field, if |
| 1088 | //not delete the option group and related option values |
| 1089 | self::checkOptionGroup($field->option_group_id); |
| 1090 | } |
| 1091 | |
| 1092 | // next drop the column from the custom value table |
| 1093 | self::createField($field, 'delete'); |
| 1094 | |
| 1095 | $field->delete(); |
| 1096 | CRM_Core_BAO_UFField::delUFField($field->id); |
| 1097 | CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_CustomField'); |
| 1098 | } |
| 1099 | |
| 1100 | /** |
| 1101 | * Find the display value for this field. |
| 1102 | * |
| 1103 | * @param mixed $value |
| 1104 | * The custom field value. |
| 1105 | * @param int $id |
| 1106 | * The custom field id. |
| 1107 | * @param array $options |
| 1108 | * The assoc array of option name/value pairs. |
| 1109 | * |
| 1110 | * @param int $contactID |
| 1111 | * @param int $fieldID |
| 1112 | * |
| 1113 | * @return string |
| 1114 | * the display value |
| 1115 | */ |
| 1116 | public static function getDisplayValue($value, $id, &$options, $contactID = NULL, $fieldID = NULL) { |
| 1117 | $option = &$options[$id]; |
| 1118 | $attributes = &$option['attributes']; |
| 1119 | $html_type = $attributes['html_type']; |
| 1120 | $data_type = $attributes['data_type']; |
| 1121 | |
| 1122 | return self::getDisplayValueCommon($value, |
| 1123 | $option, |
| 1124 | $html_type, |
| 1125 | $data_type, |
| 1126 | $contactID, |
| 1127 | $fieldID |
| 1128 | ); |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * Get display value. |
| 1133 | * |
| 1134 | * @param string $value |
| 1135 | * @param array $option |
| 1136 | * @param string $html_type |
| 1137 | * @param string $data_type |
| 1138 | * @param int $contactID |
| 1139 | * @param int $fieldID |
| 1140 | * |
| 1141 | * @return array|mixed|null|string |
| 1142 | */ |
| 1143 | public static function getDisplayValueCommon( |
| 1144 | $value, |
| 1145 | &$option, |
| 1146 | $html_type, |
| 1147 | $data_type, |
| 1148 | $contactID = NULL, |
| 1149 | $fieldID = NULL |
| 1150 | ) { |
| 1151 | $display = $value; |
| 1152 | |
| 1153 | if ($fieldID && |
| 1154 | (($html_type == 'Radio' && $data_type != 'Boolean') || |
| 1155 | ($html_type == 'Autocomplete-Select' && $data_type != 'ContactReference') || |
| 1156 | $html_type == 'Select' || |
| 1157 | $html_type == 'CheckBox' || |
| 1158 | $html_type == 'AdvMulti-Select' || |
| 1159 | $html_type == 'Multi-Select' |
| 1160 | ) |
| 1161 | ) { |
| 1162 | CRM_Utils_Hook::customFieldOptions($fieldID, $option); |
| 1163 | } |
| 1164 | |
| 1165 | switch ($html_type) { |
| 1166 | case 'Radio': |
| 1167 | if ($data_type == 'Boolean') { |
| 1168 | $option = array('No', 'Yes'); |
| 1169 | } |
| 1170 | if (is_array($value)) { |
| 1171 | $display = NULL; |
| 1172 | foreach ($value as $data) { |
| 1173 | $display .= $display ? ', ' . $option[$data] : $option[$data]; |
| 1174 | } |
| 1175 | } |
| 1176 | else { |
| 1177 | $display = CRM_Utils_Array::value($value, $option); |
| 1178 | } |
| 1179 | break; |
| 1180 | |
| 1181 | case 'Autocomplete-Select': |
| 1182 | if ($data_type == 'ContactReference' && |
| 1183 | $value |
| 1184 | ) { |
| 1185 | $display = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'display_name'); |
| 1186 | } |
| 1187 | elseif (is_array($value)) { |
| 1188 | $display = NULL; |
| 1189 | foreach ($value as $data) { |
| 1190 | $display .= $display ? ', ' . $option[$data] : $option[$data]; |
| 1191 | } |
| 1192 | } |
| 1193 | else { |
| 1194 | $display = CRM_Utils_Array::value($value, $option); |
| 1195 | } |
| 1196 | break; |
| 1197 | |
| 1198 | case 'Select': |
| 1199 | if (is_array($value)) { |
| 1200 | $display = NULL; |
| 1201 | foreach ($value as $data) { |
| 1202 | $display .= $display ? ', ' . $option[$data] : $option[$data]; |
| 1203 | } |
| 1204 | } |
| 1205 | else { |
| 1206 | $display = CRM_Utils_Array::value($value, $option); |
| 1207 | } |
| 1208 | break; |
| 1209 | |
| 1210 | case 'CheckBox': |
| 1211 | case 'AdvMulti-Select': |
| 1212 | case 'Multi-Select': |
| 1213 | if (is_array($value)) { |
| 1214 | $checkedData = $value; |
| 1215 | } |
| 1216 | else { |
| 1217 | $checkedData = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 1218 | substr($value, 1, -1) |
| 1219 | ); |
| 1220 | if ($html_type == 'CheckBox') { |
| 1221 | $newData = array(); |
| 1222 | foreach ($checkedData as $v) { |
| 1223 | $v = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, '', $v); |
| 1224 | $newData[] = $v; |
| 1225 | } |
| 1226 | $checkedData = $newData; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | $v = array(); |
| 1231 | foreach ($checkedData as $key => $val) { |
| 1232 | $v[] = CRM_Utils_Array::value($val, $option); |
| 1233 | } |
| 1234 | if (!empty($v)) { |
| 1235 | $display = implode(', ', $v); |
| 1236 | } |
| 1237 | break; |
| 1238 | |
| 1239 | case 'Select Date': |
| 1240 | if (is_array($value)) { |
| 1241 | foreach ($value as $key => $val) { |
| 1242 | $display[$key] = CRM_Utils_Date::customFormat($val); |
| 1243 | } |
| 1244 | } |
| 1245 | else { |
| 1246 | // remove time element display if time is not set |
| 1247 | if (empty($option['attributes']['time_format'])) { |
| 1248 | $value = substr($value, 0, 10); |
| 1249 | } |
| 1250 | $display = CRM_Utils_Date::customFormat($value); |
| 1251 | } |
| 1252 | break; |
| 1253 | |
| 1254 | case 'Select State/Province': |
| 1255 | case 'Multi-Select State/Province': |
| 1256 | $display = NULL; |
| 1257 | $option = CRM_Core_PseudoConstant::stateProvince(FALSE, FALSE); |
| 1258 | foreach ((array) $value as $data) { |
| 1259 | $display .= $display ? ', ' . $option[$data] : $option[$data]; |
| 1260 | } |
| 1261 | break; |
| 1262 | |
| 1263 | case 'Select Country': |
| 1264 | case 'Multi-Select Country': |
| 1265 | $countries = CRM_Core_PseudoConstant::country(FALSE, FALSE); |
| 1266 | $display = NULL; |
| 1267 | foreach ((array) $value as $countryID) { |
| 1268 | $display .= $display ? ', ' . $countries[$countryID] : $countries[$countryID]; |
| 1269 | } |
| 1270 | break; |
| 1271 | |
| 1272 | case 'File': |
| 1273 | // In the context of displaying a profile, show file/image |
| 1274 | if ($contactID && $value) { |
| 1275 | $url = self::getFileURL($contactID, $fieldID, $value); |
| 1276 | if ($url) { |
| 1277 | $display = $url['file_url']; |
| 1278 | } |
| 1279 | } |
| 1280 | // In other contexts show a paperclip icon |
| 1281 | elseif ($value) { |
| 1282 | $icons = CRM_Core_BAO_File::paperIconAttachment('*', $value); |
| 1283 | $display = $icons[$value]; |
| 1284 | } |
| 1285 | break; |
| 1286 | |
| 1287 | case 'TextArea': |
| 1288 | if (empty($value)) { |
| 1289 | $display = ''; |
| 1290 | } |
| 1291 | else { |
| 1292 | $display = is_array($value) ? nl2br(implode(', ', $value)) : nl2br($value); |
| 1293 | } |
| 1294 | break; |
| 1295 | |
| 1296 | case 'Link': |
| 1297 | case 'Text': |
| 1298 | if (empty($value)) { |
| 1299 | $display = ''; |
| 1300 | } |
| 1301 | else { |
| 1302 | $display = is_array($value) ? implode(', ', $value) : $value; |
| 1303 | } |
| 1304 | } |
| 1305 | return $display ? $display : $value; |
| 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * Set default values for custom data used in profile. |
| 1310 | * |
| 1311 | * @param int $customFieldId |
| 1312 | * Custom field id. |
| 1313 | * @param string $elementName |
| 1314 | * Custom field name. |
| 1315 | * @param array $defaults |
| 1316 | * Associated array of fields. |
| 1317 | * @param int $contactId |
| 1318 | * Contact id. |
| 1319 | * @param int $mode |
| 1320 | * Profile mode. |
| 1321 | * @param mixed $value |
| 1322 | * If passed - dont fetch value from db,. |
| 1323 | * just format the given value |
| 1324 | */ |
| 1325 | public static function setProfileDefaults( |
| 1326 | $customFieldId, |
| 1327 | $elementName, |
| 1328 | &$defaults, |
| 1329 | $contactId = NULL, |
| 1330 | $mode = NULL, |
| 1331 | $value = NULL |
| 1332 | ) { |
| 1333 | //get the type of custom field |
| 1334 | $customField = new CRM_Core_BAO_CustomField(); |
| 1335 | $customField->id = $customFieldId; |
| 1336 | $customField->find(TRUE); |
| 1337 | |
| 1338 | if (!$contactId) { |
| 1339 | if ($mode == CRM_Profile_Form::MODE_CREATE) { |
| 1340 | $value = $customField->default_value; |
| 1341 | } |
| 1342 | } |
| 1343 | else { |
| 1344 | if (!isset($value)) { |
| 1345 | $info = self::getTableColumnGroup($customFieldId); |
| 1346 | $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}"; |
| 1347 | $result = CRM_Core_DAO::executeQuery($query); |
| 1348 | if ($result->fetch()) { |
| 1349 | $value = $result->value; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | if ($customField->data_type == 'Country') { |
| 1354 | if (!$value) { |
| 1355 | $config = CRM_Core_Config::singleton(); |
| 1356 | if ($config->defaultContactCountry) { |
| 1357 | $value = $config->defaultContactCountry(); |
| 1358 | } |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | //set defaults if mode is registration |
| 1364 | if (!trim($value) && |
| 1365 | ($value !== 0) && |
| 1366 | (!in_array($mode, array(CRM_Profile_Form::MODE_EDIT, CRM_Profile_Form::MODE_SEARCH))) |
| 1367 | ) { |
| 1368 | $value = $customField->default_value; |
| 1369 | } |
| 1370 | |
| 1371 | if ($customField->data_type == 'Money' && isset($value)) { |
| 1372 | $value = number_format($value, 2); |
| 1373 | } |
| 1374 | switch ($customField->html_type) { |
| 1375 | case 'CheckBox': |
| 1376 | case 'AdvMulti-Select': |
| 1377 | case 'Multi-Select': |
| 1378 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldId, FALSE); |
| 1379 | $defaults[$elementName] = array(); |
| 1380 | $checkedValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 1381 | substr($value, 1, -1) |
| 1382 | ); |
| 1383 | foreach ($customOption as $val) { |
| 1384 | if (in_array($val['value'], $checkedValue)) { |
| 1385 | if ($customField->html_type == 'CheckBox') { |
| 1386 | $defaults[$elementName][$val['value']] = 1; |
| 1387 | } |
| 1388 | elseif ($customField->html_type == 'Multi-Select' || |
| 1389 | $customField->html_type == 'AdvMulti-Select' |
| 1390 | ) { |
| 1391 | $defaults[$elementName][$val['value']] = $val['value']; |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | break; |
| 1396 | |
| 1397 | case 'Select Date': |
| 1398 | if ($value) { |
| 1399 | list($defaults[$elementName], $defaults[$elementName . '_time']) = CRM_Utils_Date::setDateDefaults( |
| 1400 | $value, |
| 1401 | NULL, |
| 1402 | $customField->date_format, |
| 1403 | $customField->time_format |
| 1404 | ); |
| 1405 | } |
| 1406 | break; |
| 1407 | |
| 1408 | case 'Autocomplete-Select': |
| 1409 | if ($customField->data_type == 'ContactReference') { |
| 1410 | if (is_numeric($value)) { |
| 1411 | $defaults[$elementName . '_id'] = $value; |
| 1412 | $defaults[$elementName] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name'); |
| 1413 | } |
| 1414 | } |
| 1415 | else { |
| 1416 | $defaults[$elementName] = $value; |
| 1417 | } |
| 1418 | break; |
| 1419 | |
| 1420 | default: |
| 1421 | $defaults[$elementName] = $value; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /** |
| 1426 | * Get file url. |
| 1427 | * |
| 1428 | * @param int $contactID |
| 1429 | * @param int $cfID |
| 1430 | * @param int $fileID |
| 1431 | * @param bool $absolute |
| 1432 | * |
| 1433 | * @return array |
| 1434 | */ |
| 1435 | public static function getFileURL($contactID, $cfID, $fileID = NULL, $absolute = FALSE, $multiRecordWhereClause = NULL) { |
| 1436 | if ($contactID) { |
| 1437 | if (!$fileID) { |
| 1438 | $params = array('id' => $cfID); |
| 1439 | $defaults = array(); |
| 1440 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults); |
| 1441 | $columnName = $defaults['column_name']; |
| 1442 | |
| 1443 | //table name of custom data |
| 1444 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', |
| 1445 | $defaults['custom_group_id'], |
| 1446 | 'table_name', 'id' |
| 1447 | ); |
| 1448 | |
| 1449 | //query to fetch id from civicrm_file |
| 1450 | if ($multiRecordWhereClause) { |
| 1451 | $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID} AND {$multiRecordWhereClause}"; |
| 1452 | } |
| 1453 | else { |
| 1454 | $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID}"; |
| 1455 | } |
| 1456 | $fileID = CRM_Core_DAO::singleValueQuery($query); |
| 1457 | } |
| 1458 | |
| 1459 | $result = array(); |
| 1460 | if ($fileID) { |
| 1461 | $fileType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File', |
| 1462 | $fileID, |
| 1463 | 'mime_type', |
| 1464 | 'id' |
| 1465 | ); |
| 1466 | $result['file_id'] = $fileID; |
| 1467 | |
| 1468 | if ($fileType == 'image/jpeg' || |
| 1469 | $fileType == 'image/pjpeg' || |
| 1470 | $fileType == 'image/gif' || |
| 1471 | $fileType == 'image/x-png' || |
| 1472 | $fileType == 'image/png' |
| 1473 | ) { |
| 1474 | $entityId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', |
| 1475 | $fileID, |
| 1476 | 'entity_id', |
| 1477 | 'id' |
| 1478 | ); |
| 1479 | list($path) = CRM_Core_BAO_File::path($fileID, $entityId, NULL, NULL); |
| 1480 | list($imageWidth, $imageHeight) = getimagesize($path); |
| 1481 | list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight); |
| 1482 | $url = CRM_Utils_System::url('civicrm/file', |
| 1483 | "reset=1&id=$fileID&eid=$contactID", |
| 1484 | $absolute, NULL, TRUE, TRUE |
| 1485 | ); |
| 1486 | $result['file_url'] = " |
| 1487 | <a href=\"$url\" class='crm-image-popup'> |
| 1488 | <img src=\"$url\" width=$imageThumbWidth height=$imageThumbHeight/> |
| 1489 | </a>"; |
| 1490 | // for non image files |
| 1491 | } |
| 1492 | else { |
| 1493 | $uri = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File', |
| 1494 | $fileID, |
| 1495 | 'uri' |
| 1496 | ); |
| 1497 | $url = CRM_Utils_System::url('civicrm/file', |
| 1498 | "reset=1&id=$fileID&eid=$contactID", |
| 1499 | $absolute, NULL, TRUE, TRUE |
| 1500 | ); |
| 1501 | $result['file_url'] = "<a href=\"$url\">{$uri}</a>"; |
| 1502 | } |
| 1503 | } |
| 1504 | return $result; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * Format custom fields before inserting. |
| 1510 | * |
| 1511 | * @param int $customFieldId |
| 1512 | * Custom field id. |
| 1513 | * @param array $customFormatted |
| 1514 | * Formatted array. |
| 1515 | * @param mixed $value |
| 1516 | * Value of custom field. |
| 1517 | * @param string $customFieldExtend |
| 1518 | * Custom field extends. |
| 1519 | * @param int $customValueId |
| 1520 | * Custom option value id. |
| 1521 | * @param int $entityId |
| 1522 | * Entity id (contribution, membership...). |
| 1523 | * @param bool $inline |
| 1524 | * Consider inline custom groups only. |
| 1525 | * @param bool $checkPermission |
| 1526 | * If false, do not include permissioning clause. |
| 1527 | * @param bool $includeViewOnly |
| 1528 | * If true, fields marked 'View Only' are included. Required for APIv3. |
| 1529 | * |
| 1530 | * @return array|NULL |
| 1531 | * formatted custom field array |
| 1532 | */ |
| 1533 | public static function formatCustomField( |
| 1534 | $customFieldId, &$customFormatted, $value, |
| 1535 | $customFieldExtend, $customValueId = NULL, |
| 1536 | $entityId = NULL, |
| 1537 | $inline = FALSE, |
| 1538 | $checkPermission = TRUE, |
| 1539 | $includeViewOnly = FALSE |
| 1540 | ) { |
| 1541 | //get the custom fields for the entity |
| 1542 | //subtype and basic type |
| 1543 | $customDataSubType = NULL; |
| 1544 | if ($customFieldExtend) { |
| 1545 | // This is the case when getFieldsForImport() requires fields |
| 1546 | // of subtype and its parent.CRM-5143 |
| 1547 | // CRM-16065 - Custom field set data not being saved if contact has more than one contact sub type |
| 1548 | $customDataSubType = array_intersect(CRM_Contact_BAO_ContactType::subTypes(), (array) $customFieldExtend); |
| 1549 | if (!empty($customDataSubType) && is_array($customDataSubType)) { |
| 1550 | $customFieldExtend = CRM_Contact_BAO_ContactType::getBasicType($customDataSubType); |
| 1551 | if (is_array($customFieldExtend)) { |
| 1552 | $customFieldExtend = array_unique(array_values($customFieldExtend)); |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | $customFields = CRM_Core_BAO_CustomField::getFields($customFieldExtend, |
| 1558 | FALSE, |
| 1559 | $inline, |
| 1560 | $customDataSubType, |
| 1561 | NULL, |
| 1562 | FALSE, |
| 1563 | FALSE, |
| 1564 | $checkPermission |
| 1565 | ); |
| 1566 | |
| 1567 | if (!array_key_exists($customFieldId, $customFields)) { |
| 1568 | return NULL; |
| 1569 | } |
| 1570 | |
| 1571 | // return if field is a 'code' field |
| 1572 | if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) { |
| 1573 | return NULL; |
| 1574 | } |
| 1575 | |
| 1576 | list($tableName, $columnName, $groupID) = self::getTableColumnGroup($customFieldId); |
| 1577 | |
| 1578 | if (!$customValueId && |
| 1579 | // we always create new entites for is_multiple unless specified |
| 1580 | !$customFields[$customFieldId]['is_multiple'] && |
| 1581 | $entityId |
| 1582 | ) { |
| 1583 | $query = " |
| 1584 | SELECT id |
| 1585 | FROM $tableName |
| 1586 | WHERE entity_id={$entityId}"; |
| 1587 | |
| 1588 | $customValueId = CRM_Core_DAO::singleValueQuery($query); |
| 1589 | } |
| 1590 | |
| 1591 | //fix checkbox, now check box always submits values |
| 1592 | if ($customFields[$customFieldId]['html_type'] == 'CheckBox') { |
| 1593 | if ($value) { |
| 1594 | // Note that only during merge this is not an array, and you can directly use value |
| 1595 | if (is_array($value)) { |
| 1596 | $selectedValues = array(); |
| 1597 | foreach ($value as $selId => $val) { |
| 1598 | if ($val) { |
| 1599 | $selectedValues[] = $selId; |
| 1600 | } |
| 1601 | } |
| 1602 | if (!empty($selectedValues)) { |
| 1603 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 1604 | $selectedValues |
| 1605 | ) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 1606 | } |
| 1607 | else { |
| 1608 | $value = ''; |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | if ($customFields[$customFieldId]['html_type'] == 'Multi-Select' || |
| 1615 | $customFields[$customFieldId]['html_type'] == 'AdvMulti-Select' |
| 1616 | ) { |
| 1617 | if ($value) { |
| 1618 | // Note that only during merge this is not an array, |
| 1619 | // and you can directly use value, CRM-4385 |
| 1620 | if (is_array($value)) { |
| 1621 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 1622 | array_values($value) |
| 1623 | ) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 1624 | } |
| 1625 | } |
| 1626 | else { |
| 1627 | $value = ''; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | if (($customFields[$customFieldId]['html_type'] == 'Multi-Select' || |
| 1632 | $customFields[$customFieldId]['html_type'] == 'AdvMulti-Select' || |
| 1633 | $customFields[$customFieldId]['html_type'] == 'CheckBox' |
| 1634 | ) && |
| 1635 | $customFields[$customFieldId]['data_type'] == 'String' && |
| 1636 | !empty($customFields[$customFieldId]['text_length']) && |
| 1637 | !empty($value) |
| 1638 | ) { |
| 1639 | // lets make sure that value is less than the length, else we'll |
| 1640 | // be losing some data, CRM-7481 |
| 1641 | if (strlen($value) >= $customFields[$customFieldId]['text_length']) { |
| 1642 | // need to do a few things here |
| 1643 | |
| 1644 | // 1. lets find a new length |
| 1645 | $newLength = $customFields[$customFieldId]['text_length']; |
| 1646 | $minLength = strlen($value); |
| 1647 | while ($newLength < $minLength) { |
| 1648 | $newLength = $newLength * 2; |
| 1649 | } |
| 1650 | |
| 1651 | // set the custom field meta data to have a length larger than value |
| 1652 | // alter the custom value table column to match this length |
| 1653 | CRM_Core_BAO_SchemaHandler::alterFieldLength($customFieldId, $tableName, $columnName, $newLength); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | $date = NULL; |
| 1658 | if ($customFields[$customFieldId]['data_type'] == 'Date') { |
| 1659 | if (!CRM_Utils_System::isNull($value)) { |
| 1660 | $format = $customFields[$customFieldId]['date_format']; |
| 1661 | $date = CRM_Utils_Date::processDate($value, NULL, FALSE, 'YmdHis', $format); |
| 1662 | } |
| 1663 | $value = $date; |
| 1664 | } |
| 1665 | |
| 1666 | if ($customFields[$customFieldId]['data_type'] == 'Float' || |
| 1667 | $customFields[$customFieldId]['data_type'] == 'Money' |
| 1668 | ) { |
| 1669 | if (!$value) { |
| 1670 | $value = 0; |
| 1671 | } |
| 1672 | |
| 1673 | if ($customFields[$customFieldId]['data_type'] == 'Money') { |
| 1674 | $value = CRM_Utils_Rule::cleanMoney($value); |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | if (($customFields[$customFieldId]['data_type'] == 'StateProvince' || |
| 1679 | $customFields[$customFieldId]['data_type'] == 'Country' |
| 1680 | ) && |
| 1681 | empty($value) |
| 1682 | ) { |
| 1683 | // CRM-3415 |
| 1684 | $value = 0; |
| 1685 | } |
| 1686 | |
| 1687 | $fileId = NULL; |
| 1688 | |
| 1689 | if ($customFields[$customFieldId]['data_type'] == 'File') { |
| 1690 | if (empty($value)) { |
| 1691 | return; |
| 1692 | } |
| 1693 | |
| 1694 | $config = CRM_Core_Config::singleton(); |
| 1695 | |
| 1696 | $fName = $value['name']; |
| 1697 | $mimeType = $value['type']; |
| 1698 | |
| 1699 | $filename = pathinfo($fName, PATHINFO_BASENAME); |
| 1700 | |
| 1701 | // rename this file to go into the secure directory |
| 1702 | if (!rename($fName, $config->customFileUploadDir . $filename)) { |
| 1703 | CRM_Core_Error::statusBounce(ts('Could not move custom file to custom upload directory')); |
| 1704 | } |
| 1705 | |
| 1706 | if ($customValueId) { |
| 1707 | $query = " |
| 1708 | SELECT $columnName |
| 1709 | FROM $tableName |
| 1710 | WHERE id = %1"; |
| 1711 | $params = array(1 => array($customValueId, 'Integer')); |
| 1712 | $fileId = CRM_Core_DAO::singleValueQuery($query, $params); |
| 1713 | } |
| 1714 | |
| 1715 | $fileDAO = new CRM_Core_DAO_File(); |
| 1716 | |
| 1717 | if ($fileId) { |
| 1718 | $fileDAO->id = $fileId; |
| 1719 | } |
| 1720 | |
| 1721 | $fileDAO->uri = $filename; |
| 1722 | $fileDAO->mime_type = $mimeType; |
| 1723 | $fileDAO->upload_date = date('Ymdhis'); |
| 1724 | $fileDAO->save(); |
| 1725 | $fileId = $fileDAO->id; |
| 1726 | $value = $filename; |
| 1727 | } |
| 1728 | |
| 1729 | if (!is_array($customFormatted)) { |
| 1730 | $customFormatted = array(); |
| 1731 | } |
| 1732 | |
| 1733 | if (!array_key_exists($customFieldId, $customFormatted)) { |
| 1734 | $customFormatted[$customFieldId] = array(); |
| 1735 | } |
| 1736 | |
| 1737 | $index = -1; |
| 1738 | if ($customValueId) { |
| 1739 | $index = $customValueId; |
| 1740 | } |
| 1741 | |
| 1742 | if (!array_key_exists($index, $customFormatted[$customFieldId])) { |
| 1743 | $customFormatted[$customFieldId][$index] = array(); |
| 1744 | } |
| 1745 | $customFormatted[$customFieldId][$index] = array( |
| 1746 | 'id' => $customValueId > 0 ? $customValueId : NULL, |
| 1747 | 'value' => $value, |
| 1748 | 'type' => $customFields[$customFieldId]['data_type'], |
| 1749 | 'custom_field_id' => $customFieldId, |
| 1750 | 'custom_group_id' => $groupID, |
| 1751 | 'table_name' => $tableName, |
| 1752 | 'column_name' => $columnName, |
| 1753 | 'file_id' => $fileId, |
| 1754 | 'is_multiple' => $customFields[$customFieldId]['is_multiple'], |
| 1755 | ); |
| 1756 | |
| 1757 | //we need to sort so that custom fields are created in the order of entry |
| 1758 | krsort($customFormatted[$customFieldId]); |
| 1759 | return $customFormatted; |
| 1760 | } |
| 1761 | |
| 1762 | /** |
| 1763 | * Get default custom table schema. |
| 1764 | * |
| 1765 | * @param array $params |
| 1766 | * |
| 1767 | * @return array |
| 1768 | */ |
| 1769 | public static function defaultCustomTableSchema($params) { |
| 1770 | // add the id and extends_id |
| 1771 | $table = array( |
| 1772 | 'name' => $params['name'], |
| 1773 | 'is_multiple' => $params['is_multiple'], |
| 1774 | 'attributes' => "ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci", |
| 1775 | 'fields' => array( |
| 1776 | array( |
| 1777 | 'name' => 'id', |
| 1778 | 'type' => 'int unsigned', |
| 1779 | 'primary' => TRUE, |
| 1780 | 'required' => TRUE, |
| 1781 | 'attributes' => 'AUTO_INCREMENT', |
| 1782 | 'comment' => 'Default MySQL primary key', |
| 1783 | ), |
| 1784 | array( |
| 1785 | 'name' => 'entity_id', |
| 1786 | 'type' => 'int unsigned', |
| 1787 | 'required' => TRUE, |
| 1788 | 'comment' => 'Table that this extends', |
| 1789 | 'fk_table_name' => $params['extends_name'], |
| 1790 | 'fk_field_name' => 'id', |
| 1791 | 'fk_attributes' => 'ON DELETE CASCADE', |
| 1792 | ), |
| 1793 | ), |
| 1794 | ); |
| 1795 | |
| 1796 | if (!$params['is_multiple']) { |
| 1797 | $table['indexes'] = array( |
| 1798 | array( |
| 1799 | 'unique' => TRUE, |
| 1800 | 'field_name_1' => 'entity_id', |
| 1801 | ), |
| 1802 | ); |
| 1803 | } |
| 1804 | return $table; |
| 1805 | } |
| 1806 | |
| 1807 | /** |
| 1808 | * Create custom field. |
| 1809 | * |
| 1810 | * @param CRM_Core_DAO_CustomField $field |
| 1811 | * @param string $operation |
| 1812 | * @param bool $indexExist |
| 1813 | * @param bool $triggerRebuild |
| 1814 | */ |
| 1815 | public static function createField($field, $operation, $indexExist = FALSE, $triggerRebuild = TRUE) { |
| 1816 | $tableName = CRM_Core_DAO::getFieldValue( |
| 1817 | 'CRM_Core_DAO_CustomGroup', |
| 1818 | $field->custom_group_id, |
| 1819 | 'table_name' |
| 1820 | ); |
| 1821 | |
| 1822 | $params = array( |
| 1823 | 'table_name' => $tableName, |
| 1824 | 'operation' => $operation, |
| 1825 | 'name' => $field->column_name, |
| 1826 | 'type' => CRM_Core_BAO_CustomValueTable::fieldToSQLType( |
| 1827 | $field->data_type, |
| 1828 | $field->text_length |
| 1829 | ), |
| 1830 | 'required' => $field->is_required, |
| 1831 | 'searchable' => $field->is_searchable, |
| 1832 | ); |
| 1833 | |
| 1834 | if ($operation == 'delete') { |
| 1835 | $fkName = "{$tableName}_{$field->column_name}"; |
| 1836 | if (strlen($fkName) >= 48) { |
| 1837 | $fkName = substr($fkName, 0, 32) . '_' . substr(md5($fkName), 0, 16); |
| 1838 | } |
| 1839 | $params['fkName'] = $fkName; |
| 1840 | } |
| 1841 | if ($field->data_type == 'Country' && $field->html_type == 'Select Country') { |
| 1842 | $params['fk_table_name'] = 'civicrm_country'; |
| 1843 | $params['fk_field_name'] = 'id'; |
| 1844 | $params['fk_attributes'] = 'ON DELETE SET NULL'; |
| 1845 | } |
| 1846 | elseif ($field->data_type == 'Country' && $field->html_type == 'Multi-Select Country') { |
| 1847 | $params['type'] = 'varchar(255)'; |
| 1848 | } |
| 1849 | elseif ($field->data_type == 'StateProvince' && $field->html_type == 'Select State/Province') { |
| 1850 | $params['fk_table_name'] = 'civicrm_state_province'; |
| 1851 | $params['fk_field_name'] = 'id'; |
| 1852 | $params['fk_attributes'] = 'ON DELETE SET NULL'; |
| 1853 | } |
| 1854 | elseif ($field->data_type == 'StateProvince' && $field->html_type == 'Multi-Select State/Province') { |
| 1855 | $params['type'] = 'varchar(255)'; |
| 1856 | } |
| 1857 | elseif ($field->data_type == 'File') { |
| 1858 | $params['fk_table_name'] = 'civicrm_file'; |
| 1859 | $params['fk_field_name'] = 'id'; |
| 1860 | $params['fk_attributes'] = 'ON DELETE SET NULL'; |
| 1861 | } |
| 1862 | elseif ($field->data_type == 'ContactReference') { |
| 1863 | $params['fk_table_name'] = 'civicrm_contact'; |
| 1864 | $params['fk_field_name'] = 'id'; |
| 1865 | $params['fk_attributes'] = 'ON DELETE SET NULL'; |
| 1866 | } |
| 1867 | if (isset($field->default_value)) { |
| 1868 | $params['default'] = "'{$field->default_value}'"; |
| 1869 | } |
| 1870 | |
| 1871 | CRM_Core_BAO_SchemaHandler::alterFieldSQL($params, $indexExist, $triggerRebuild); |
| 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * Determine whether it would be safe to move a field. |
| 1876 | * |
| 1877 | * @param int $fieldID |
| 1878 | * FK to civicrm_custom_field. |
| 1879 | * @param int $newGroupID |
| 1880 | * FK to civicrm_custom_group. |
| 1881 | * |
| 1882 | * @return array |
| 1883 | * array(string) or TRUE |
| 1884 | */ |
| 1885 | public static function _moveFieldValidate($fieldID, $newGroupID) { |
| 1886 | $errors = array(); |
| 1887 | |
| 1888 | $field = new CRM_Core_DAO_CustomField(); |
| 1889 | $field->id = $fieldID; |
| 1890 | if (!$field->find(TRUE)) { |
| 1891 | $errors['fieldID'] = 'Invalid ID for custom field'; |
| 1892 | return $errors; |
| 1893 | } |
| 1894 | |
| 1895 | $oldGroup = new CRM_Core_DAO_CustomGroup(); |
| 1896 | $oldGroup->id = $field->custom_group_id; |
| 1897 | if (!$oldGroup->find(TRUE)) { |
| 1898 | $errors['fieldID'] = 'Invalid ID for old custom group'; |
| 1899 | return $errors; |
| 1900 | } |
| 1901 | |
| 1902 | $newGroup = new CRM_Core_DAO_CustomGroup(); |
| 1903 | $newGroup->id = $newGroupID; |
| 1904 | if (!$newGroup->find(TRUE)) { |
| 1905 | $errors['newGroupID'] = 'Invalid ID for new custom group'; |
| 1906 | return $errors; |
| 1907 | } |
| 1908 | |
| 1909 | $query = " |
| 1910 | SELECT b.id |
| 1911 | FROM civicrm_custom_field a |
| 1912 | INNER JOIN civicrm_custom_field b |
| 1913 | WHERE a.id = %1 |
| 1914 | AND a.label = b.label |
| 1915 | AND b.custom_group_id = %2 |
| 1916 | "; |
| 1917 | $params = array( |
| 1918 | 1 => array($field->id, 'Integer'), |
| 1919 | 2 => array($newGroup->id, 'Integer'), |
| 1920 | ); |
| 1921 | $count = CRM_Core_DAO::singleValueQuery($query, $params); |
| 1922 | if ($count > 0) { |
| 1923 | $errors['newGroupID'] = ts('A field of the same label exists in the destination group'); |
| 1924 | } |
| 1925 | |
| 1926 | $tableName = $oldGroup->table_name; |
| 1927 | $columnName = $field->column_name; |
| 1928 | |
| 1929 | $query = " |
| 1930 | SELECT count(*) |
| 1931 | FROM $tableName |
| 1932 | WHERE $columnName is not null |
| 1933 | "; |
| 1934 | $count = CRM_Core_DAO::singleValueQuery($query, |
| 1935 | CRM_Core_DAO::$_nullArray |
| 1936 | ); |
| 1937 | if ($count > 0) { |
| 1938 | $query = " |
| 1939 | SELECT extends |
| 1940 | FROM civicrm_custom_group |
| 1941 | WHERE id IN ( %1, %2 ) |
| 1942 | "; |
| 1943 | $params = array( |
| 1944 | 1 => array($oldGroup->id, 'Integer'), |
| 1945 | 2 => array($newGroup->id, 'Integer'), |
| 1946 | ); |
| 1947 | |
| 1948 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1949 | $extends = array(); |
| 1950 | while ($dao->fetch()) { |
| 1951 | $extends[] = $dao->extends; |
| 1952 | } |
| 1953 | if ($extends[0] != $extends[1]) { |
| 1954 | $errors['newGroupID'] = ts('The destination group extends a different entity type.'); |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | return empty($errors) ? TRUE : $errors; |
| 1959 | } |
| 1960 | |
| 1961 | /** |
| 1962 | * Move a custom data field from one group (table) to another. |
| 1963 | * |
| 1964 | * @param int $fieldID |
| 1965 | * FK to civicrm_custom_field. |
| 1966 | * @param int $newGroupID |
| 1967 | * FK to civicrm_custom_group. |
| 1968 | */ |
| 1969 | public static function moveField($fieldID, $newGroupID) { |
| 1970 | $validation = self::_moveFieldValidate($fieldID, $newGroupID); |
| 1971 | if (TRUE !== $validation) { |
| 1972 | CRM_Core_Error::fatal(implode(' ', $validation)); |
| 1973 | } |
| 1974 | $field = new CRM_Core_DAO_CustomField(); |
| 1975 | $field->id = $fieldID; |
| 1976 | $field->find(TRUE); |
| 1977 | |
| 1978 | $newGroup = new CRM_Core_DAO_CustomGroup(); |
| 1979 | $newGroup->id = $newGroupID; |
| 1980 | $newGroup->find(TRUE); |
| 1981 | |
| 1982 | $oldGroup = new CRM_Core_DAO_CustomGroup(); |
| 1983 | $oldGroup->id = $field->custom_group_id; |
| 1984 | $oldGroup->find(TRUE); |
| 1985 | |
| 1986 | $add = clone$field; |
| 1987 | $add->custom_group_id = $newGroup->id; |
| 1988 | self::createField($add, 'add'); |
| 1989 | |
| 1990 | $sql = "INSERT INTO {$newGroup->table_name} (entity_id, {$field->column_name}) |
| 1991 | SELECT entity_id, {$field->column_name} FROM {$oldGroup->table_name} |
| 1992 | ON DUPLICATE KEY UPDATE {$field->column_name} = {$oldGroup->table_name}.{$field->column_name} |
| 1993 | "; |
| 1994 | CRM_Core_DAO::executeQuery($sql); |
| 1995 | |
| 1996 | $del = clone$field; |
| 1997 | $del->custom_group_id = $oldGroup->id; |
| 1998 | self::createField($del, 'delete'); |
| 1999 | |
| 2000 | $add->save(); |
| 2001 | |
| 2002 | CRM_Utils_System::flushCache(); |
| 2003 | } |
| 2004 | |
| 2005 | /** |
| 2006 | * Get the database table name and column name for a custom field. |
| 2007 | * |
| 2008 | * @param int $fieldID |
| 2009 | * The fieldID of the custom field. |
| 2010 | * @param bool $force |
| 2011 | * Force the sql to be run again (primarily used for tests). |
| 2012 | * |
| 2013 | * @return array |
| 2014 | * fatal is fieldID does not exists, else array of tableName, columnName |
| 2015 | */ |
| 2016 | public static function getTableColumnGroup($fieldID, $force = FALSE) { |
| 2017 | $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}"; |
| 2018 | $cache = CRM_Utils_Cache::singleton(); |
| 2019 | $fieldValues = $cache->get($cacheKey); |
| 2020 | if (empty($fieldValues) || $force) { |
| 2021 | $query = " |
| 2022 | SELECT cg.table_name, cf.column_name, cg.id |
| 2023 | FROM civicrm_custom_group cg, |
| 2024 | civicrm_custom_field cf |
| 2025 | WHERE cf.custom_group_id = cg.id |
| 2026 | AND cf.id = %1"; |
| 2027 | $params = array(1 => array($fieldID, 'Integer')); |
| 2028 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 2029 | |
| 2030 | if (!$dao->fetch()) { |
| 2031 | CRM_Core_Error::fatal(); |
| 2032 | } |
| 2033 | $dao->free(); |
| 2034 | $fieldValues = array($dao->table_name, $dao->column_name, $dao->id); |
| 2035 | $cache->set($cacheKey, $fieldValues); |
| 2036 | } |
| 2037 | return $fieldValues; |
| 2038 | } |
| 2039 | |
| 2040 | /** |
| 2041 | * Get custom option groups. |
| 2042 | * |
| 2043 | * @param array $includeFieldIds |
| 2044 | * Ids of custom fields for which option groups must be included. |
| 2045 | * |
| 2046 | * Currently this is required in the cases where option groups are to be included |
| 2047 | * for inactive fields : CRM-5369 |
| 2048 | * |
| 2049 | * @return mixed |
| 2050 | */ |
| 2051 | public static function customOptionGroup($includeFieldIds = NULL) { |
| 2052 | static $customOptionGroup = NULL; |
| 2053 | |
| 2054 | $cacheKey = (empty($includeFieldIds)) ? 'onlyActive' : 'force'; |
| 2055 | if ($cacheKey == 'force') { |
| 2056 | $customOptionGroup[$cacheKey] = NULL; |
| 2057 | } |
| 2058 | |
| 2059 | if (empty($customOptionGroup[$cacheKey])) { |
| 2060 | $whereClause = '( g.is_active = 1 AND f.is_active = 1 )'; |
| 2061 | |
| 2062 | //support for single as well as array format. |
| 2063 | if (!empty($includeFieldIds)) { |
| 2064 | if (is_array($includeFieldIds)) { |
| 2065 | $includeFieldIds = implode(',', $includeFieldIds); |
| 2066 | } |
| 2067 | $whereClause .= "OR f.id IN ( $includeFieldIds )"; |
| 2068 | } |
| 2069 | |
| 2070 | $query = " |
| 2071 | SELECT g.id, g.title |
| 2072 | FROM civicrm_option_group g |
| 2073 | INNER JOIN civicrm_custom_field f ON ( g.id = f.option_group_id ) |
| 2074 | WHERE {$whereClause}"; |
| 2075 | |
| 2076 | $dao = CRM_Core_DAO::executeQuery($query); |
| 2077 | while ($dao->fetch()) { |
| 2078 | $customOptionGroup[$cacheKey][$dao->id] = $dao->title; |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | return $customOptionGroup[$cacheKey]; |
| 2083 | } |
| 2084 | |
| 2085 | /** |
| 2086 | * Fix orphan groups. |
| 2087 | * |
| 2088 | * @param int $customFieldId |
| 2089 | * Custom field id. |
| 2090 | * @param int $optionGroupId |
| 2091 | * Option group id. |
| 2092 | */ |
| 2093 | public static function fixOptionGroups($customFieldId, $optionGroupId) { |
| 2094 | // check if option group belongs to any custom Field else delete |
| 2095 | // get the current option group |
| 2096 | $currentOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', |
| 2097 | $customFieldId, |
| 2098 | 'option_group_id' |
| 2099 | ); |
| 2100 | // get the updated option group |
| 2101 | // if both are same return |
| 2102 | if ($currentOptionGroupId == $optionGroupId) { |
| 2103 | return; |
| 2104 | } |
| 2105 | |
| 2106 | // check if option group is related to any other field |
| 2107 | self::checkOptionGroup($currentOptionGroupId); |
| 2108 | } |
| 2109 | |
| 2110 | /** |
| 2111 | * Check if option group is related to more than one custom field. |
| 2112 | * |
| 2113 | * @param int $optionGroupId |
| 2114 | * Option group id. |
| 2115 | */ |
| 2116 | public static function checkOptionGroup($optionGroupId) { |
| 2117 | $query = " |
| 2118 | SELECT count(*) |
| 2119 | FROM civicrm_custom_field |
| 2120 | WHERE option_group_id = {$optionGroupId}"; |
| 2121 | |
| 2122 | $count = CRM_Core_DAO::singleValueQuery($query); |
| 2123 | |
| 2124 | if ($count < 2) { |
| 2125 | //delete the option group |
| 2126 | CRM_Core_BAO_OptionGroup::del($optionGroupId); |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | /** |
| 2131 | * Get option group default. |
| 2132 | * |
| 2133 | * @param int $optionGroupId |
| 2134 | * @param string $htmlType |
| 2135 | * |
| 2136 | * @return null|string |
| 2137 | */ |
| 2138 | public static function getOptionGroupDefault($optionGroupId, $htmlType) { |
| 2139 | $query = " |
| 2140 | SELECT default_value, html_type |
| 2141 | FROM civicrm_custom_field |
| 2142 | WHERE option_group_id = {$optionGroupId} |
| 2143 | AND default_value IS NOT NULL |
| 2144 | ORDER BY html_type"; |
| 2145 | |
| 2146 | $dao = CRM_Core_DAO::executeQuery($query); |
| 2147 | $defaultValue = NULL; |
| 2148 | $defaultHTMLType = NULL; |
| 2149 | while ($dao->fetch()) { |
| 2150 | if ($dao->html_type == $htmlType) { |
| 2151 | return $dao->default_value; |
| 2152 | } |
| 2153 | if ($defaultValue == NULL) { |
| 2154 | $defaultValue = $dao->default_value; |
| 2155 | $defaultHTMLType = $dao->html_type; |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | // some conversions are needed if either the old or new has a html type which has potential |
| 2160 | // multiple default values. |
| 2161 | if (($htmlType == 'CheckBox' || $htmlType == 'Multi-Select') && |
| 2162 | ($defaultHTMLType != 'CheckBox' && $defaultHTMLType != 'Multi-Select') |
| 2163 | ) { |
| 2164 | $defaultValue = CRM_Core_DAO::VALUE_SEPARATOR . $defaultValue . CRM_Core_DAO::VALUE_SEPARATOR; |
| 2165 | } |
| 2166 | elseif (($defaultHTMLType == 'CheckBox' || $defaultHTMLType == 'Multi-Select') && |
| 2167 | ($htmlType != 'CheckBox' && $htmlType != 'Multi-Select') |
| 2168 | ) { |
| 2169 | $defaultValue = substr($defaultValue, 1, -1); |
| 2170 | $values = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 2171 | substr($defaultValue, 1, -1) |
| 2172 | ); |
| 2173 | $defaultValue = $values[0]; |
| 2174 | } |
| 2175 | |
| 2176 | return $defaultValue; |
| 2177 | } |
| 2178 | |
| 2179 | /** |
| 2180 | * Post process function. |
| 2181 | * |
| 2182 | * @param array $params |
| 2183 | * @param int $entityID |
| 2184 | * @param string $customFieldExtends |
| 2185 | * @param bool $inline |
| 2186 | * |
| 2187 | * @return array |
| 2188 | */ |
| 2189 | public static function postProcess( |
| 2190 | &$params, |
| 2191 | $entityID, |
| 2192 | $customFieldExtends, |
| 2193 | $inline = FALSE |
| 2194 | ) { |
| 2195 | $customData = array(); |
| 2196 | |
| 2197 | foreach ($params as $key => $value) { |
| 2198 | if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($key, TRUE)) { |
| 2199 | |
| 2200 | // for autocomplete transfer hidden value instead of label |
| 2201 | if ($params[$key] && isset($params[$key . '_id'])) { |
| 2202 | $value = $params[$key . '_id']; |
| 2203 | } |
| 2204 | |
| 2205 | // we need to append time with date |
| 2206 | if ($params[$key] && isset($params[$key . '_time'])) { |
| 2207 | $value .= ' ' . $params[$key . '_time']; |
| 2208 | } |
| 2209 | |
| 2210 | CRM_Core_BAO_CustomField::formatCustomField($customFieldInfo[0], |
| 2211 | $customData, |
| 2212 | $value, |
| 2213 | $customFieldExtends, |
| 2214 | $customFieldInfo[1], |
| 2215 | $entityID, |
| 2216 | $inline |
| 2217 | ); |
| 2218 | } |
| 2219 | } |
| 2220 | return $customData; |
| 2221 | } |
| 2222 | |
| 2223 | /** |
| 2224 | * Build option. |
| 2225 | * |
| 2226 | * @param array $field |
| 2227 | * @param array $options |
| 2228 | * |
| 2229 | * @throws Exception |
| 2230 | */ |
| 2231 | public static function buildOption($field, &$options) { |
| 2232 | // Fixme - adding anything but options to the $options array is a bad idea |
| 2233 | // What if an option had the key 'attributes'? |
| 2234 | $options['attributes'] = array( |
| 2235 | 'label' => $field['label'], |
| 2236 | 'data_type' => $field['data_type'], |
| 2237 | 'html_type' => $field['html_type'], |
| 2238 | ); |
| 2239 | |
| 2240 | $optionGroupID = NULL; |
| 2241 | if (($field['html_type'] == 'CheckBox' || |
| 2242 | $field['html_type'] == 'Radio' || |
| 2243 | $field['html_type'] == 'Select' || |
| 2244 | $field['html_type'] == 'AdvMulti-Select' || |
| 2245 | $field['html_type'] == 'Multi-Select' || |
| 2246 | ($field['html_type'] == 'Autocomplete-Select' && $field['data_type'] != 'ContactReference') |
| 2247 | ) |
| 2248 | ) { |
| 2249 | if ($field['option_group_id']) { |
| 2250 | $optionGroupID = $field['option_group_id']; |
| 2251 | } |
| 2252 | elseif ($field['data_type'] != 'Boolean') { |
| 2253 | CRM_Core_Error::fatal(); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | // build the cache for custom values with options (label => value) |
| 2258 | if ($optionGroupID != NULL) { |
| 2259 | $query = " |
| 2260 | SELECT label, value |
| 2261 | FROM civicrm_option_value |
| 2262 | WHERE option_group_id = $optionGroupID |
| 2263 | "; |
| 2264 | |
| 2265 | $dao = CRM_Core_DAO::executeQuery($query); |
| 2266 | while ($dao->fetch()) { |
| 2267 | if ($field['data_type'] == 'Int' || $field['data_type'] == 'Float') { |
| 2268 | $num = round($dao->value, 2); |
| 2269 | $options["$num"] = $dao->label; |
| 2270 | } |
| 2271 | else { |
| 2272 | $options[$dao->value] = $dao->label; |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | CRM_Utils_Hook::customFieldOptions($field['id'], $options); |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | /** |
| 2281 | * Get custom field ID. |
| 2282 | * |
| 2283 | * @param string $fieldLabel |
| 2284 | * @param null $groupTitle |
| 2285 | * |
| 2286 | * @return int|null |
| 2287 | */ |
| 2288 | public static function getCustomFieldID($fieldLabel, $groupTitle = NULL) { |
| 2289 | $params = array(1 => array($fieldLabel, 'String')); |
| 2290 | if ($groupTitle) { |
| 2291 | $params[2] = array($groupTitle, 'String'); |
| 2292 | $sql = " |
| 2293 | SELECT f.id |
| 2294 | FROM civicrm_custom_field f |
| 2295 | INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id |
| 2296 | WHERE ( f.label = %1 OR f.name = %1 ) |
| 2297 | AND ( g.title = %2 OR g.name = %2 ) |
| 2298 | "; |
| 2299 | } |
| 2300 | else { |
| 2301 | $sql = " |
| 2302 | SELECT f.id |
| 2303 | FROM civicrm_custom_field f |
| 2304 | WHERE ( f.label = %1 OR f.name = %1 ) |
| 2305 | "; |
| 2306 | } |
| 2307 | |
| 2308 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 2309 | if ($dao->fetch() && |
| 2310 | $dao->N == 1 |
| 2311 | ) { |
| 2312 | return $dao->id; |
| 2313 | } |
| 2314 | else { |
| 2315 | return NULL; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | /** |
| 2320 | * Given ID of a custom field, return its name as well as the name of the custom group it belongs to. |
| 2321 | * |
| 2322 | * @param array $ids |
| 2323 | * |
| 2324 | * @return array |
| 2325 | */ |
| 2326 | public static function getNameFromID($ids) { |
| 2327 | if (is_array($ids)) { |
| 2328 | $ids = implode(',', $ids); |
| 2329 | } |
| 2330 | $sql = " |
| 2331 | SELECT f.id, f.name AS field_name, f.label AS field_label, g.name AS group_name, g.title AS group_title |
| 2332 | FROM civicrm_custom_field f |
| 2333 | INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id |
| 2334 | WHERE f.id IN ($ids)"; |
| 2335 | |
| 2336 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 2337 | $result = array(); |
| 2338 | while ($dao->fetch()) { |
| 2339 | $result[$dao->id] = array( |
| 2340 | 'field_name' => $dao->field_name, |
| 2341 | 'field_label' => $dao->field_label, |
| 2342 | 'group_name' => $dao->group_name, |
| 2343 | 'group_title' => $dao->group_title, |
| 2344 | ); |
| 2345 | } |
| 2346 | return $result; |
| 2347 | } |
| 2348 | |
| 2349 | /** |
| 2350 | * Validate custom data. |
| 2351 | * |
| 2352 | * @param array $params |
| 2353 | * Custom data submitted. |
| 2354 | * ie array( 'custom_1' => 'validate me' ); |
| 2355 | * |
| 2356 | * @return array |
| 2357 | * validation errors. |
| 2358 | */ |
| 2359 | public static function validateCustomData($params) { |
| 2360 | $errors = array(); |
| 2361 | if (!is_array($params) || empty($params)) { |
| 2362 | return $errors; |
| 2363 | } |
| 2364 | |
| 2365 | //pick up profile fields. |
| 2366 | $profileFields = array(); |
| 2367 | $ufGroupId = CRM_Utils_Array::value('ufGroupId', $params); |
| 2368 | if ($ufGroupId) { |
| 2369 | $profileFields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, |
| 2370 | FALSE, |
| 2371 | CRM_Core_Action::VIEW |
| 2372 | ); |
| 2373 | } |
| 2374 | |
| 2375 | //lets start w/ params. |
| 2376 | foreach ($params as $key => $value) { |
| 2377 | $customFieldID = self::getKeyID($key); |
| 2378 | if (!$customFieldID) { |
| 2379 | continue; |
| 2380 | } |
| 2381 | |
| 2382 | //load the structural info for given field. |
| 2383 | $field = new CRM_Core_DAO_CustomField(); |
| 2384 | $field->id = $customFieldID; |
| 2385 | if (!$field->find(TRUE)) { |
| 2386 | continue; |
| 2387 | } |
| 2388 | $dataType = $field->data_type; |
| 2389 | |
| 2390 | $profileField = CRM_Utils_Array::value($key, $profileFields, array()); |
| 2391 | $fieldTitle = CRM_Utils_Array::value('title', $profileField); |
| 2392 | $isRequired = CRM_Utils_Array::value('is_required', $profileField); |
| 2393 | if (!$fieldTitle) { |
| 2394 | $fieldTitle = $field->label; |
| 2395 | } |
| 2396 | |
| 2397 | //no need to validate. |
| 2398 | if (CRM_Utils_System::isNull($value) && !$isRequired) { |
| 2399 | continue; |
| 2400 | } |
| 2401 | |
| 2402 | //lets validate first for required field. |
| 2403 | if ($isRequired && CRM_Utils_System::isNull($value)) { |
| 2404 | $errors[$key] = ts('%1 is a required field.', array(1 => $fieldTitle)); |
| 2405 | continue; |
| 2406 | } |
| 2407 | |
| 2408 | //now time to take care of custom field form rules. |
| 2409 | $ruleName = $errorMsg = NULL; |
| 2410 | switch ($dataType) { |
| 2411 | case 'Int': |
| 2412 | $ruleName = 'integer'; |
| 2413 | $errorMsg = ts('%1 must be an integer (whole number).', |
| 2414 | array(1 => $fieldTitle) |
| 2415 | ); |
| 2416 | break; |
| 2417 | |
| 2418 | case 'Money': |
| 2419 | $ruleName = 'money'; |
| 2420 | $errorMsg = ts('%1 must in proper money format. (decimal point/comma/space is allowed).', |
| 2421 | array(1 => $fieldTitle) |
| 2422 | ); |
| 2423 | break; |
| 2424 | |
| 2425 | case 'Float': |
| 2426 | $ruleName = 'numeric'; |
| 2427 | $errorMsg = ts('%1 must be a number (with or without decimal point).', |
| 2428 | array(1 => $fieldTitle) |
| 2429 | ); |
| 2430 | break; |
| 2431 | |
| 2432 | case 'Link': |
| 2433 | $ruleName = 'wikiURL'; |
| 2434 | $errorMsg = ts('%1 must be valid Website.', |
| 2435 | array(1 => $fieldTitle) |
| 2436 | ); |
| 2437 | break; |
| 2438 | } |
| 2439 | |
| 2440 | if ($ruleName && !CRM_Utils_System::isNull($value)) { |
| 2441 | $valid = FALSE; |
| 2442 | $funName = "CRM_Utils_Rule::{$ruleName}"; |
| 2443 | if (is_callable($funName)) { |
| 2444 | $valid = call_user_func($funName, $value); |
| 2445 | } |
| 2446 | if (!$valid) { |
| 2447 | $errors[$key] = $errorMsg; |
| 2448 | } |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | return $errors; |
| 2453 | } |
| 2454 | |
| 2455 | /** |
| 2456 | * Is this field a multi record field. |
| 2457 | * |
| 2458 | * @param int $customId |
| 2459 | * |
| 2460 | * @return bool |
| 2461 | */ |
| 2462 | public static function isMultiRecordField($customId) { |
| 2463 | $isMultipleWithGid = FALSE; |
| 2464 | if (!is_numeric($customId)) { |
| 2465 | $customId = self::getKeyID($customId); |
| 2466 | } |
| 2467 | if (is_numeric($customId)) { |
| 2468 | $sql = "SELECT cg.id cgId |
| 2469 | FROM civicrm_custom_group cg |
| 2470 | INNER JOIN civicrm_custom_field cf |
| 2471 | ON cg.id = cf.custom_group_id |
| 2472 | WHERE cf.id = %1 AND cg.is_multiple = 1"; |
| 2473 | $params[1] = array($customId, 'Integer'); |
| 2474 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 2475 | if ($dao->fetch()) { |
| 2476 | if ($dao->cgId) { |
| 2477 | $isMultipleWithGid = $dao->cgId; |
| 2478 | } |
| 2479 | } |
| 2480 | } |
| 2481 | |
| 2482 | return $isMultipleWithGid; |
| 2483 | } |
| 2484 | |
| 2485 | /** |
| 2486 | * Does this field store a serialized string? |
| 2487 | * |
| 2488 | * @param array $field |
| 2489 | * |
| 2490 | * @return bool |
| 2491 | */ |
| 2492 | public static function isSerialized($field) { |
| 2493 | // Fields retrieved via api are an array, or from the dao are an object. We'll accept either. |
| 2494 | $field = (array) $field; |
| 2495 | // FIXME: Currently the only way to know if data is serialized is by looking at the html_type. It would be cleaner to decouple this. |
| 2496 | return ($field['html_type'] == 'CheckBox' || strpos($field['html_type'], 'Multi') !== FALSE); |
| 2497 | } |
| 2498 | |
| 2499 | /** |
| 2500 | * Get options for field. |
| 2501 | * |
| 2502 | * @param array $field |
| 2503 | * @param string|null $optionGroupName |
| 2504 | */ |
| 2505 | private static function getOptionsForField(&$field, $optionGroupName) { |
| 2506 | if ($optionGroupName) { |
| 2507 | $field['pseudoconstant'] = array( |
| 2508 | 'optionGroupName' => $optionGroupName, |
| 2509 | 'optionEditPath' => 'civicrm/admin/options/' . $optionGroupName, |
| 2510 | ); |
| 2511 | } |
| 2512 | elseif ($field['data_type'] == 'Boolean') { |
| 2513 | $field['pseudoconstant'] = array( |
| 2514 | 'callback' => 'CRM_Core_SelectValues::boolean', |
| 2515 | ); |
| 2516 | } |
| 2517 | elseif ($field['data_type'] == 'Country') { |
| 2518 | $field['pseudoconstant'] = array( |
| 2519 | 'table' => 'civicrm_country', |
| 2520 | 'keyColumn' => 'id', |
| 2521 | 'labelColumn' => 'name', |
| 2522 | 'nameColumn' => 'iso_code', |
| 2523 | ); |
| 2524 | } |
| 2525 | elseif ($field['data_type'] == 'StateProvince') { |
| 2526 | $field['pseudoconstant'] = array( |
| 2527 | 'table' => 'civicrm_state_province', |
| 2528 | 'keyColumn' => 'id', |
| 2529 | 'labelColumn' => 'name', |
| 2530 | ); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | } |