Skip to content

Commit

Permalink
Merge pull request #28072 from colemanw/advSearchQuickSearchMatch
Browse files Browse the repository at this point in the history
QuickSearch - Fix redirect to adv search with prepopulated criteria
  • Loading branch information
eileenmcnaughton authored Nov 9, 2023
2 parents a239771 + 46527a4 commit 0299459
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 32 deletions.
15 changes: 7 additions & 8 deletions CRM/Admin/Page/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function navMenu() {

$output = [
'menu' => $menu,
'search' => CRM_Utils_Array::makeNonAssociative(self::getSearchOptions()),
'search' => self::getSearchOptions(),
];
// Encourage browsers to cache for a long time - 1 year
$ttl = 60 * 60 * 24 * 364;
Expand Down Expand Up @@ -80,15 +80,14 @@ public static function formatMenuItems(&$menu) {

public static function getSearchOptions() {
$searchOptions = Civi::settings()->get('quicksearch_options');
$labels = CRM_Core_SelectValues::quicksearchOptions();
$allOptions = array_column(CRM_Core_SelectValues::getQuicksearchOptions(), NULL, 'key');
$result = [];
foreach ($searchOptions as $key) {
$label = $labels[$key];
if (strpos($key, 'custom_') === 0) {
$key = 'custom_' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', substr($key, 7), 'id', 'name');
$label = array_slice(explode(': ', $label, 2), -1);
}
$result[$key] = $label;
$result[] = [
'key' => $key,
'value' => $allOptions[$key]['label'],
'adv_search_legacy' => $allOptions[$key]['adv_search_legacy'],
];
}
return $result;
}
Expand Down
91 changes: 70 additions & 21 deletions CRM/Core/SelectValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -1117,31 +1117,66 @@ public static function getDashboardEntriesCount() {
return $optionValues;
}

/**
* Dropdown options for quicksearch in the menu
*
* @return array
* @throws \CRM_Core_Exception
*/
public static function quicksearchOptions() {
public static function getQuicksearchOptions(): array {
$includeEmail = civicrm_api3('setting', 'getvalue', ['name' => 'includeEmailInName', 'group' => 'Search Preferences']);
$options = [
'sort_name' => $includeEmail ? ts('Name/Email') : ts('Name'),
'id' => ts('Contact ID'),
'external_identifier' => ts('External ID'),
'first_name' => ts('First Name'),
'last_name' => ts('Last Name'),
'email_primary.email' => ts('Email'),
'phone_primary.phone_numeric' => ts('Phone'),
'address_primary.street_address' => ts('Street Address'),
'address_primary.city' => ts('City'),
'address_primary.postal_code' => ts('Postal Code'),
'employer_id.sort_name' => ts('Current Employer'),
'job_title' => ts('Job Title'),
[
'key' => 'sort_name',
'label' => $includeEmail ? ts('Name/Email') : ts('Name'),
],
[
'key' => 'id',
'label' => ts('Contact ID'),
],
[
'key' => 'external_identifier',
'label' => ts('External ID'),
],
[
'key' => 'first_name',
'label' => ts('First Name'),
],
[
'key' => 'last_name',
'label' => ts('Last Name'),
],
[
'key' => 'email_primary.email',
'label' => ts('Email'),
'adv_search_legacy' => 'email',
],
[
'key' => 'phone_primary.phone_numeric',
'label' => ts('Phone'),
'adv_search_legacy' => 'phone_numeric',
],
[
'key' => 'address_primary.street_address',
'label' => ts('Street Address'),
'adv_search_legacy' => 'street_address',
],
[
'key' => 'address_primary.city',
'label' => ts('City'),
'adv_search_legacy' => 'city',
],
[
'key' => 'address_primary.postal_code',
'label' => ts('Postal Code'),
'adv_search_legacy' => 'postal_code',
],
[
'key' => 'employer_id.sort_name',
'label' => ts('Current Employer'),
],
[
'key' => 'job_title',
'label' => ts('Job Title'),
],
];
$custom = civicrm_api4('CustomField', 'get', [
'checkPermissions' => FALSE,
'select' => ['name', 'label', 'custom_group_id.name', 'custom_group_id.title', 'option_group_id'],
'select' => ['id', 'name', 'label', 'custom_group_id.name', 'custom_group_id.title', 'option_group_id'],
'where' => [
['custom_group_id.extends', 'IN', array_merge(['Contact'], CRM_Contact_BAO_ContactType::basicTypes())],
['data_type', 'NOT IN', ['ContactReference', 'Date', 'File']],
Expand All @@ -1155,11 +1190,25 @@ public static function quicksearchOptions() {
],
]);
foreach ($custom as $field) {
$options[$field['custom_group_id.name'] . '.' . $field['name'] . ($field['option_group_id'] ? ':label' : '')] = $field['custom_group_id.title'] . ': ' . $field['label'];
$options[] = [
'key' => $field['custom_group_id.name'] . '.' . $field['name'] . ($field['option_group_id'] ? ':label' : ''),
'label' => $field['custom_group_id.title'] . ': ' . $field['label'],
'adv_search_legacy' => 'custom_' . $field['id'],
];
}
return $options;
}

/**
* Dropdown options for quicksearch in the menu
*
* @return array
* @throws \CRM_Core_Exception
*/
public static function quicksearchOptions() {
return array_column(self::getQuicksearchOptions(), 'label', 'key');
}

/**
* Get components (translated for display.
*
Expand Down
3 changes: 2 additions & 1 deletion Civi/Api4/Utils/CoreUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public static function isContact(string $entityName): bool {
*/
public static function getInfoItem(string $entityName, string $keyToReturn) {
$provider = \Civi::service('action_object_provider');
return $provider->getEntities()[$entityName][$keyToReturn] ?? NULL;
$entities = $provider->getEntities();
return $entities[$entityName][$keyToReturn] ?? NULL;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions js/crm.menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@
return false;
}
var $menu = $('#crm-qsearch-input').autocomplete('widget');
// If only one contact was returned, go directly to that contact page
if ($('li.ui-menu-item', $menu).length === 1) {
var cid = $('li.ui-menu-item', $menu).data('ui-autocomplete-item').value;
if (cid > 0) {
Expand All @@ -404,7 +405,8 @@
function setQuickSearchValue() {
var $selection = $('.crm-quickSearchField input:checked'),
label = $selection.parent().text(),
value = $selection.val();
// Set name because the mini-form submits directly to adv search
value = $selection.data('advSearchLegacy') || $selection.val();
$('#crm-qsearch-input').attr({name: value, placeholder: '\uf002 ' + label});
}
$('.crm-quickSearchField').click(function() {
Expand Down Expand Up @@ -464,7 +466,7 @@
'</a>' +
'<ul>' +
'<% _.forEach(items, function(item) { %>' +
'<li><a href="#" class="crm-quickSearchField"><label><input type="radio" value="<%= item.key %>" name="quickSearchField"> <%- item.value %></label></a></li>' +
'<li><a href="#" class="crm-quickSearchField"><label><input type="radio" value="<%= item.key %>" name="quickSearchField" data-adv-search-legacy="<%= item.adv_search_legacy %>"> <%- item.value %></label></a></li>' +
'<% }) %>' +
'</ul>' +
'</li>',
Expand Down

0 comments on commit 0299459

Please sign in to comment.