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