From 4235341b82ca2826afa96f1aa9b12098159e728f Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 30 Oct 2018 14:52:07 -0400 Subject: [PATCH] Confiurable quicksearch settings --- CRM/Admin/Form/Setting/Search.php | 5 +++ CRM/Admin/Page/AJAX.php | 16 +++++++++- CRM/Core/SelectValues.php | 34 +++++++++++++++++++++ api/v3/Contact.php | 32 ++++++++++++++++--- settings/Search.setting.php | 19 ++++++++++++ templates/CRM/Admin/Form/Setting/Search.tpl | 7 +++++ templates/CRM/common/navigation.js.tpl | 18 +++-------- 7 files changed, 112 insertions(+), 19 deletions(-) diff --git a/CRM/Admin/Form/Setting/Search.php b/CRM/Admin/Form/Setting/Search.php index 0c79505c8d..edc02d635b 100644 --- a/CRM/Admin/Form/Setting/Search.php +++ b/CRM/Admin/Form/Setting/Search.php @@ -50,6 +50,7 @@ class CRM_Admin_Form_Setting_Search extends CRM_Admin_Form_Setting { 'smartGroupCacheTimeout' => CRM_Core_BAO_Setting::SEARCH_PREFERENCES_NAME, 'defaultSearchProfileID' => CRM_Core_BAO_Setting::SEARCH_PREFERENCES_NAME, 'searchPrimaryDetailsOnly' => CRM_Core_BAO_Setting::SEARCH_PREFERENCES_NAME, + 'quicksearch_options' => CRM_Core_BAO_Setting::SEARCH_PREFERENCES_NAME, ); /** @@ -69,6 +70,10 @@ class CRM_Admin_Form_Setting_Search extends CRM_Admin_Form_Setting { // Autocomplete for Contact Reference (custom fields) $element = $this->getElement('contact_reference_options'); $element->_elements[0]->_flagFrozen = TRUE; + + // Freeze first element of quicksearch options + $element = $this->getElement('quicksearch_options'); + $element->_elements[0]->_flagFrozen = TRUE; } /** diff --git a/CRM/Admin/Page/AJAX.php b/CRM/Admin/Page/AJAX.php index 5b1fdf4f68..76808ce37f 100644 --- a/CRM/Admin/Page/AJAX.php +++ b/CRM/Admin/Page/AJAX.php @@ -46,7 +46,7 @@ class CRM_Admin_Page_AJAX { if ($contactID) { CRM_Core_Page_AJAX::setJsHeaders(); $smarty = CRM_Core_Smarty::singleton(); - $smarty->assign('includeEmail', civicrm_api3('setting', 'getvalue', array('name' => 'includeEmailInName', 'group' => 'Search Preferences'))); + $smarty->assign('quicksearchOptions', self::getSearchOptions()); print $smarty->fetchWith('CRM/common/navigation.js.tpl', array( 'navigation' => CRM_Core_BAO_Navigation::createNavigation(), )); @@ -54,6 +54,20 @@ class CRM_Admin_Page_AJAX { CRM_Utils_System::civiExit(); } + public static function getSearchOptions() { + $searchOptions = Civi::settings()->get('quicksearch_options'); + $searchOptions[] = 'sort_name'; + $searchOptions = array_intersect_key(CRM_Core_SelectValues::quicksearchOptions(), array_flip($searchOptions)); + foreach ($searchOptions as $key => $label) { + if (strpos($key, 'custom_') === 0) { + unset($searchOptions[$key]); + $id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', substr($key, 7), 'id', 'name'); + $searchOptions["custom_$id"] = $label; + } + } + return $searchOptions; + } + /** * Process drag/move action for menu tree. */ diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index 44e5bdb854..f5f0de0492 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -1124,4 +1124,38 @@ class CRM_Core_SelectValues { return $optionValues; } + /** + * Dropdown options for quicksearch in the menu + * + * @return array + */ + public static function quicksearchOptions() { + $includeEmail = civicrm_api3('setting', 'getvalue', array('name' => 'includeEmailInName', 'group' => 'Search Preferences')); + $options = [ + 'sort_name' => $includeEmail ? ts('Name/Email') : ts('Name'), + 'contact_id' => ts('Contact ID'), + 'external_identifier' => ts('External ID'), + 'first_name' => ts('First Name'), + 'last_name' => ts('Last Name'), + 'email' => ts('Email'), + 'phone_numeric' => ts('Phone'), + 'street_address' => ts('Street Address'), + 'city' => ts('City'), + 'postal_code' => ts('Postal Code'), + 'job_title' => ts('Job Title'), + ]; + $custom = civicrm_api3('CustomField', 'get', [ + 'return' => ["name", "label", "custom_group_id.title"], + 'custom_group_id.extends' => ['IN' => ["Contact", "Individual", "Organization", "Household"]], + 'custom_group_id.is_active' => 1, + 'is_active' => 1, + 'is_searchable' => 1, + 'options' => ['sort' => ['custom_group_id.weight', 'weight']], + ]); + foreach ($custom['values'] as $field) { + $options['custom_' . $field['name']] = $field['custom_group_id.title'] . ': ' . $field['label']; + } + return $options; + } + } diff --git a/api/v3/Contact.php b/api/v3/Contact.php index f6afa246bf..6c8901b56b 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -758,10 +758,18 @@ function civicrm_api3_contact_getquick($params) { ) ); + $table_names = [ + 'email' => 'eml', + 'phone_numeric' => 'phe', + 'street_address' => 'sts', + 'city' => 'sts', + 'postal_code' => 'sts', + ]; + // get the option values for contact autocomplete $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name'); - $list = array(); + $list = $from = array(); foreach ($acpref as $value) { if ($value && !empty($acOptions[$value])) { $list[$value] = $acOptions[$value]; @@ -774,6 +782,18 @@ function civicrm_api3_contact_getquick($params) { if ($field_name == 'contact_id') { $field_name = 'id'; } + if (isset($table_names[$field_name])) { + $table_name = $table_names[$field_name]; + } + elseif (strpos($field_name, 'custom_') === 0) { + $customInfo = civicrm_api3('CustomField', 'getsingle', [ + 'return' => ["custom_group_id.table_name", "column_name"], + 'id' => substr($field_name, 7), + ]); + $field_name = $customInfo['column_name']; + $table_name = CRM_Utils_String::munge($customInfo['custom_group_id.table_name']); + $from[$field_name] = "LEFT JOIN `$table_name` ON cc.id = `$table_name`.entity_id"; + } // phone_numeric should be phone $searchField = str_replace('_numeric', '', $field_name); if (!in_array($searchField, $list)) { @@ -787,7 +807,6 @@ function civicrm_api3_contact_getquick($params) { $select = $actualSelectElements = array('sort_name'); $where = ''; - $from = array(); foreach ($list as $value) { $suffix = substr($value, 0, 2) . substr($value, -1); switch ($value) { @@ -817,10 +836,13 @@ function civicrm_api3_contact_getquick($params) { break; default: - if ($value != 'id') { + if ($value == 'id') { + $actualSelectElements[] = 'cc.id'; + } + elseif ($value != 'sort_name') { $suffix = 'cc'; - if (!empty($params['field_name']) && $params['field_name'] == 'value') { - $suffix = CRM_Utils_String::munge(CRM_Utils_Array::value('table_name', $params, 'cc')); + if ($field_name == $value) { + $suffix = $table_name; } $actualSelectElements[] = $select[] = $suffix . '.' . $value; } diff --git a/settings/Search.setting.php b/settings/Search.setting.php index 0f805195e1..fb02207eff 100644 --- a/settings/Search.setting.php +++ b/settings/Search.setting.php @@ -211,4 +211,23 @@ return array( 'description' => 'If enabled, only primary details (eg contact\'s primary email, phone, etc) will be included in Basic and Advanced Search results. Disabling this feature will allow users to match contacts using any email, phone etc detail.', 'help_text' => NULL, ), + 'quicksearch_options' => array( + 'group_name' => 'Search Preferences', + 'group' => 'Search Preferences', + 'name' => 'quicksearch_options', + 'type' => 'string', + 'serialize' => CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND, + 'quick_form_type' => 'CheckBoxes', + 'html_type' => 'checkboxes', + 'pseudoconstant' => array( + 'callback' => 'CRM_Core_SelectValues::quicksearchOptions', + ), + 'default' => array('sort_name', 'contact_id', 'external_identifier', 'first_name', 'last_name', 'email', 'phone_numeric', 'street_address', 'city', 'postal_code', 'job_title'), + 'add' => '5.8', + 'title' => ts('Quicksearch options'), + 'is_domain' => '1', + 'is_contact' => 0, + 'description' => ts("Which fields can be searched on in the menubar quicksearch box? Don't see your custom field here? Make sure it is marked as Searchable."), + 'help_text' => NULL, + ), ); diff --git a/templates/CRM/Admin/Form/Setting/Search.tpl b/templates/CRM/Admin/Form/Setting/Search.tpl index 3a704662ad..09e4236450 100644 --- a/templates/CRM/Admin/Form/Setting/Search.tpl +++ b/templates/CRM/Admin/Form/Setting/Search.tpl @@ -68,6 +68,13 @@ {$form.smartGroupCacheTimeout.html}
{ts}The number of minutes to cache smart group contacts. We strongly recommend that this value be greater than zero, since a value of zero means no caching at all. If your contact data changes frequently, you should set this value to at least 5 minutes.{/ts} + + {$form.quicksearch_options.label} + + {$form.quicksearch_options.html} +

{$setting_descriptions.quicksearch_options}

+ + {$form.contact_autocomplete_options.label} {$form.contact_autocomplete_options.html}
diff --git a/templates/CRM/common/navigation.js.tpl b/templates/CRM/common/navigation.js.tpl index 5902068185..5c3d589cc9 100644 --- a/templates/CRM/common/navigation.js.tpl +++ b/templates/CRM/common/navigation.js.tpl @@ -31,22 +31,15 @@ +
{$navigation} @@ -92,8 +85,7 @@ $('#civicrm-menu').ready(function() { option = $('input[name=quickSearchField]:checked'), params = { name: request.term, - field_name: option.val(), - table_name: option.attr("data-tablename") + field_name: option.val() }; CRM.api3('contact', 'getquick', params).done(function(result) { var ret = []; -- 2.25.1