From b4fb2d23f91cd6912de7d1e570ca7f2e1817e041 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 15 Dec 2015 20:55:19 -0500 Subject: [PATCH] CRM-17646 - Consolodate all custom field option lookups --- CRM/Contact/Import/Parser/Contact.php | 2 +- CRM/Core/BAO/CustomOption.php | 26 +++--------- CRM/Core/BAO/CustomQuery.php | 61 ++++++--------------------- 3 files changed, 19 insertions(+), 70 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 53e0fb38f3..a9ea3b964c 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -1999,7 +1999,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { case 'Autocomplete-Select': if ($customFields[$customFieldID]['data_type'] == 'String') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); - foreach ($customOption as $customFldID => $customValue) { + foreach ($customOption as $customValue) { $val = CRM_Utils_Array::value('value', $customValue); $label = CRM_Utils_Array::value('label', $customValue); $label = strtolower($label); diff --git a/CRM/Core/BAO/CustomOption.php b/CRM/Core/BAO/CustomOption.php index 559f9cb638..5b4b9068b8 100644 --- a/CRM/Core/BAO/CustomOption.php +++ b/CRM/Core/BAO/CustomOption.php @@ -79,29 +79,15 @@ class CRM_Core_BAO_CustomOption { return $options; } - $field = CRM_Core_BAO_CustomField::getFieldObject($fieldID); + $optionValues = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $fieldID, array(), $inactiveNeeded ? 'get' : 'create'); - // get the option group id - $optionGroupID = $field->option_group_id; - if (!$optionGroupID) { - return $options; - } - - $optionValues = CRM_Core_BAO_OptionValue::getOptionValuesArray($optionGroupID); - - foreach ($optionValues as $id => $value) { - if (!$inactiveNeeded && empty($value['is_active'])) { - continue; - } - - $options[$id] = array(); - $options[$id]['id'] = $id; - $options[$id]['label'] = $value['label']; - $options[$id]['value'] = $value['value']; + foreach ($optionValues as $value => $label) { + $options[] = array( + 'label' => $label, + 'value' => $value, + ); } - CRM_Utils_Hook::customFieldOptions($fieldID, $options, TRUE); - return $options; } diff --git a/CRM/Core/BAO/CustomQuery.php b/CRM/Core/BAO/CustomQuery.php index 6e7d831fae..b65b1ce527 100644 --- a/CRM/Core/BAO/CustomQuery.php +++ b/CRM/Core/BAO/CustomQuery.php @@ -79,7 +79,8 @@ class CRM_Core_BAO_CustomQuery { public $_qill; /** - * The cache to translate the option values into labels. + * @deprecated + * No longer needed due to CRM-17646 refactoring, but still used in some places * * @var array */ @@ -197,57 +198,19 @@ SELECT f.id, f.label, f.data_type, 'option_group_id' => $dao->option_group_id, ); - // store it in the options cache to make things easier - // during option lookup - $this->_options[$dao->id] = array(); - $this->_options[$dao->id]['attributes'] = array( - 'label' => $dao->label, - 'data_type' => $dao->data_type, - 'html_type' => $dao->html_type, - ); + // Deprecated (and poorly named) cache of field attributes + $this->_options[$dao->id] = array( + 'attributes' => array( + 'label' => $dao->label, + 'data_type' => $dao->data_type, + 'html_type' => $dao->html_type, + ), + ) + CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $dao->id, array(), 'search'); - $optionGroupID = NULL; - $htmlTypes = array('CheckBox', 'Radio', 'Select', 'Multi-Select', 'AdvMulti-Select', 'Autocomplete-Select'); - if (in_array($dao->html_type, $htmlTypes) && $dao->data_type != 'ContactReference') { - if ($dao->option_group_id) { - $optionGroupID = $dao->option_group_id; - } - elseif ($dao->data_type != 'Boolean') { - $errorMessage = ts("The custom field %1 is corrupt. Please delete and re-build the field", - array(1 => $dao->label) - ); - CRM_Core_Error::fatal($errorMessage); - } - } - elseif ($dao->html_type == 'Select Date') { + if ($dao->html_type == 'Select Date') { $this->_options[$dao->id]['attributes']['date_format'] = $dao->date_format; $this->_options[$dao->id]['attributes']['time_format'] = $dao->time_format; } - - // build the cache for custom values with options (label => value) - if ($optionGroupID != NULL) { - $query = " -SELECT label, value - FROM civicrm_option_value - WHERE option_group_id = $optionGroupID -"; - - $option = CRM_Core_DAO::executeQuery($query); - while ($option->fetch()) { - $dataType = $this->_fields[$dao->id]['data_type']; - if ($dataType == 'Int' || $dataType == 'Float') { - $num = round($option->value, 2); - $this->_options[$dao->id]["$num"] = $option->label; - } - else { - $this->_options[$dao->id][$option->value] = $option->label; - } - } - $options = $this->_options[$dao->id]; - //unset attributes to avoid confussion - unset($options['attributes']); - CRM_Utils_Hook::customFieldOptions($dao->id, $options, FALSE); - } } } @@ -409,7 +372,7 @@ SELECT label, value //FIX for custom data query fired against no value(NULL/NOT NULL) $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String'); } - $this->_qill[$grouping][] = "$field[label] $qillOp $qillValue"; + $this->_qill[$grouping][] = $field['label'] . " $qillOp $qillValue"; } break; -- 2.25.1