Commit 4235341b authored by colemanw's avatar colemanw

Confiurable quicksearch settings

parent 899f47ba
......@@ -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;
}
/**
......
......@@ -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.
*/
......
......@@ -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;
}
}
......@@ -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;
}
......
......@@ -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,
),
);
......@@ -68,6 +68,13 @@
<td>{$form.smartGroupCacheTimeout.html}<br />
<span class="description">{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}</span></td>
</tr>
<tr class="crm-search-setting-form-block-quicksearch_options">
<td class="label">{$form.quicksearch_options.label}</td>
<td>
{$form.quicksearch_options.html}
<p class="description">{$setting_descriptions.quicksearch_options}</p>
</td>
</tr>
<tr class="crm-search-setting-form-block-autocompleteContactSearch">
<td class="label">{$form.contact_autocomplete_options.label}</td>
<td>{$form.contact_autocomplete_options.html}<br/>
......
......@@ -31,22 +31,15 @@
<input type="text" class="form-text" id="sort_name_navigation" placeholder="{ts}Contacts{/ts}" name="sort_name" style="width: 6em;" />
<input type="text" id="sort_contact_id" style="display: none" />
<input type="hidden" name="hidden_location" value="1" />
<input type="hidden" name="hidden_custom" value="1" />
<input type="hidden" name="qfKey" value="" />
<div style="height:1px; overflow:hidden;"><input type="submit" value="{ts}Go{/ts}" name="_qf_Advanced_refresh" class="crm-form-submit default" /></div>
</div>
</form>
<ul>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" checked="checked" value="" name="quickSearchField"> {if $includeEmail}{ts}Name/Email{/ts}{else}{ts}Name{/ts}{/if}</label></li>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" value="contact_id" name="quickSearchField"> {ts}Contact ID{/ts}</label></li>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" value="external_identifier" name="quickSearchField"> {ts}External ID{/ts}</label></li>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" value="first_name" name="quickSearchField"> {ts}First Name{/ts}</label></li>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" value="last_name" name="quickSearchField"> {ts}Last Name{/ts}</label></li>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="eml" value="email" name="quickSearchField"> {ts}Email{/ts}</label></li>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="phe" value="phone_numeric" name="quickSearchField"> {ts}Phone{/ts}</label></li>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="sts" value="street_address" name="quickSearchField"> {ts}Street Address{/ts}</label></li>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="sts" value="city" name="quickSearchField"> {ts}City{/ts}</label></li>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="sts" value="postal_code" name="quickSearchField"> {ts}Postal Code{/ts}</label></li>
<li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" value="job_title" name="quickSearchField"> {ts}Job Title{/ts}</label></li>
{foreach from=$quicksearchOptions item="optionLabel" key="optionKey"}
<li><label class="crm-quickSearchField"><input type="radio" {if $optionKey == 'sort_name'}checked="checked"{/if} value="{$optionKey}" name="quickSearchField"> {$optionLabel}</label></li>
{/foreach}
</ul>
</li>
{$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 = [];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment