From ddfc623e911e2d111a998273539f154142baf76f Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 12 Dec 2015 19:05:43 -0500 Subject: [PATCH] CRM-17646 -Toward refactoring out hook_customFieldOptions --- .../Form/Search/Custom/MultipleValues.php | 75 +++---------------- CRM/Core/BAO/CustomField.php | 57 -------------- 2 files changed, 11 insertions(+), 121 deletions(-) diff --git a/CRM/Contact/Form/Search/Custom/MultipleValues.php b/CRM/Contact/Form/Search/Custom/MultipleValues.php index 0fdd01a5aa..cad65fca08 100644 --- a/CRM/Contact/Form/Search/Custom/MultipleValues.php +++ b/CRM/Contact/Form/Search/Custom/MultipleValues.php @@ -37,6 +37,7 @@ class CRM_Contact_Form_Search_Custom_MultipleValues extends CRM_Contact_Form_Sea protected $_options; protected $_aclFrom = NULL; protected $_aclWhere = NULL; + protected $fieldInfo = array(); /** * Class constructor. @@ -68,14 +69,18 @@ class CRM_Contact_Form_Search_Custom_MultipleValues extends CRM_Contact_Form_Sea } } + /** + * Add all the fields for chosen groups + */ public function addColumns() { - // add all the fields for chosen groups - $this->_tables = $this->_options = array(); + $this->_tables = array(); foreach ($this->_groupTree as $groupID => $group) { if (empty($this->_customGroupIDs[$groupID])) { continue; } + $this->fieldInfo += $group['fields']; + // now handle all the fields foreach ($group['fields'] as $fieldID => $field) { $this->_columns[$field['label']] = "custom_{$field['id']}"; @@ -83,12 +88,6 @@ class CRM_Contact_Form_Search_Custom_MultipleValues extends CRM_Contact_Form_Sea $this->_tables[$group['table_name']] = array(); } $this->_tables[$group['table_name']][$field['id']] = $field['column_name']; - - // also build the option array - $this->_options[$field['id']] = array(); - CRM_Core_BAO_CustomField::buildOption($field, - $this->_options[$field['id']] - ); } } } @@ -98,9 +97,6 @@ class CRM_Contact_Form_Search_Custom_MultipleValues extends CRM_Contact_Form_Sea */ public function buildForm(&$form) { - /** - * You can define a custom title for the search form - */ $this->setTitle('Multiple Value Custom Group Search and Export'); $form->add('text', 'sort_name', ts('Contact Name'), TRUE); @@ -292,63 +288,14 @@ contact_a.sort_name as sort_name, * @param $row */ public function alterRow(&$row) { - foreach ($this->_options as $fieldID => $values) { - $customVal = $valueSeparatedArray = array(); - if (in_array($values['attributes']['html_type'], - array('Radio', 'Select', 'Autocomplete-Select') - )) { - if ($values['attributes']['data_type'] == 'ContactReference' && $row["custom_{$fieldID}"]) { - $row["custom_{$fieldID}"] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', (int) $row["custom_{$fieldID}"], 'display_name'); - } - elseif ($row["custom_{$fieldID}"] && - array_key_exists($row["custom_{$fieldID}"], - $values - ) - ) { - $row["custom_{$fieldID}"] = $values[$row["custom_{$fieldID}"]]; - } - } - elseif (in_array($values['attributes']['html_type'], - array('CheckBox', 'Multi-Select', 'AdvMulti-Select') - )) { - $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"])); - foreach ($valueSeparatedArray as $val) { - $customVal[] = $values[$val]; - } - $row["custom_{$fieldID}"] = implode(', ', $customVal); - } - elseif (in_array($values['attributes']['html_type'], - array('Multi-Select State/Province', 'Select State/Province') - )) { - $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"])); - $stateName = CRM_Core_PseudoConstant::stateProvince(); - foreach ($valueSeparatedArray as $val) { - $customVal[] = $stateName[$val]; - } - $row["custom_{$fieldID}"] = implode(', ', $customVal); - } - elseif (in_array($values['attributes']['html_type'], - array('Multi-Select Country', 'Select Country') - )) { - $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"])); - CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', - TRUE, 'name', 'is_active' - ); - foreach ($valueSeparatedArray as $val) { - $customVal[] = $countryNames[$val]; - } - $row["custom_{$fieldID}"] = implode(', ', $customVal); + foreach ($row as $fieldName => &$field) { + if (strpos($fieldName, 'custom_') === 0) { + list(, $id) = explode('_', $fieldName); + $field = CRM_Core_BAO_CustomGroup::formatCustomValues(array('data' => $field), $this->fieldInfo[$id]); } } } - /** - * @param $title - */ - public function setTitle($title) { - CRM_Utils_System::setTitle($title); - } - /** * @param string $tableAlias */ diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index fddbb70188..559b0d2420 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -2234,63 +2234,6 @@ ORDER BY html_type"; return $customData; } - /** - * Build option. - * - * @param array $field - * @param array $options - * - * @throws Exception - */ - public static function buildOption($field, &$options) { - // Fixme - adding anything but options to the $options array is a bad idea - // What if an option had the key 'attributes'? - $options['attributes'] = array( - 'label' => $field['label'], - 'data_type' => $field['data_type'], - 'html_type' => $field['html_type'], - ); - - $optionGroupID = NULL; - if (($field['html_type'] == 'CheckBox' || - $field['html_type'] == 'Radio' || - $field['html_type'] == 'Select' || - $field['html_type'] == 'AdvMulti-Select' || - $field['html_type'] == 'Multi-Select' || - ($field['html_type'] == 'Autocomplete-Select' && $field['data_type'] != 'ContactReference') - ) - ) { - if ($field['option_group_id']) { - $optionGroupID = $field['option_group_id']; - } - elseif ($field['data_type'] != 'Boolean') { - CRM_Core_Error::fatal(); - } - } - - // 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 -"; - - $dao = CRM_Core_DAO::executeQuery($query); - while ($dao->fetch()) { - if ($field['data_type'] == 'Int' || $field['data_type'] == 'Float') { - $num = round($dao->value, 2); - $options["$num"] = $dao->label; - } - else { - $options[$dao->value] = $dao->label; - } - } - - CRM_Utils_Hook::customFieldOptions($field['id'], $options); - } - } - /** * Get custom field ID. * -- 2.25.1